Skip to content

Commit

Permalink
[BUGFIX] Fix missing file and installation instructions (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
mabolek authored Apr 6, 2021
1 parent 853fd9a commit a5ef7b3
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
91 changes: 91 additions & 0 deletions Classes/Controller/SelectorController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace Pixelant\Qbank\Controller;

use Pixelant\Qbank\Service\QbankService;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

/**
* Controller handling Ajax calls relating to the QBank file selector.
*/
class SelectorController
{
/**
* @var QbankService
*/
protected $qbankService;

/**
* SelectorController constructor.
* @param QbankService $qbankService
*/
public function __construct(QbankService $qbankService)
{
$this->qbankService = $qbankService;
}

/**
* Downloads a file to TYPO3's local filesystem (if it doesn't already exist) and returns the file UID.
*
* If the file aleady exists, the UID of the existing file is returned.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
*/
public function downloadFileAction(ServerRequestInterface $request): JsonResponse
{
$mediaId = (int)$request->getParsedBody()['mediaId'];

if ($mediaId === 0) {
return $this->getErrorResponse(
LocalizationUtility::translate('download_file.error.zero_media_identifier', 'qbank')
);
}

$file = $this->qbankService->createLocalMediaCopy($mediaId);

$response = $this->getSuccessResponse(['fileUid' => $file->getUid()]);

return $response;
}

/**
* Returns an error response object with message set.
*
* @param string $message
* @return JsonResponse
*/
protected function getErrorResponse(string $message): JsonResponse
{
return GeneralUtility::makeInstance(JsonResponse::class)
->withStatus(
500,
$message
)
->setPayload([
'success' => false,
'message' => $message,
]);
}

/**
* Returns a success response object with message set.
*
* @param string $message
* @return JsonResponse
*/
protected function getSuccessResponse(array $data): JsonResponse
{
$data['success'] = true;

return GeneralUtility::makeInstance(JsonResponse::class)
->withStatus(200)
->setPayload($data);
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Installation

1. Install the extension using Composer: `composer req pixelant/qbank`
2. Activate the extension in TYPO3 by using the _Admin Tools > Extensions_ module or by running `vendor/bin/typo3 extension:activate qbank` in the command line.
2. Activate the extension in TYPO3 by using the _Admin Tools > Extensions_ module or by running `vendor/bin/typo3 extension:activate qbank; vendor/bin/typo3cms database:updateschema` in the command line.

## Configuration

Expand Down

0 comments on commit a5ef7b3

Please sign in to comment.