diff --git a/app/modules/web/Controllers/AccountFile/ViewController.php b/app/modules/web/Controllers/AccountFile/ViewController.php index a0b95b0e..4b561efb 100644 --- a/app/modules/web/Controllers/AccountFile/ViewController.php +++ b/app/modules/web/Controllers/AccountFile/ViewController.php @@ -33,6 +33,9 @@ use SP\Domain\Http\Dtos\JsonMessage; use SP\Infrastructure\File\FileSystem; use SP\Modules\Web\Controllers\Traits\JsonTrait; +use function SP\__u; +use function SP\processException; + /** * Class ViewController * @@ -47,23 +50,22 @@ final class ViewController extends AccountFileBase /** * View action * - * @param int $id + * @param int $id * * @return bool * @throws JsonException + * @throws SPException */ public function viewAction(int $id): bool { try { - if (null === ($fileData = $this->accountFileService->getById($id))) { - throw new SPException(__u('File does not exist'), SPException::INFO); - } + $fileDto = $this->accountFileService->getById($id); $this->view->addTemplate('file', 'itemshow'); - if (FileSystem::isImage($fileData)) { - $this->view->assign('data', chunk_split(base64_encode($fileData->getContent()))); - $this->view->assign('fileData', $fileData); + if (FileSystem::isImage($fileDto->type)) { + $this->view->assign('data', chunk_split(base64_encode($fileDto->content))); + $this->view->assign('fileData', $fileDto); $this->view->assign('isImage', 1); $this->eventDispatcher->notify( @@ -72,18 +74,18 @@ final class ViewController extends AccountFileBase $this, EventMessage::factory() ->addDescription(__u('File viewed')) - ->addDetail(__u('File'), $fileData->getName()) + ->addDetail(__u('File'), $fileDto->name) ) ); return $this->returnJsonResponseData(['html' => $this->render()]); } - $type = strtolower($fileData->getType()); + $type = strtolower($fileDto->type); if (in_array($type, self::MIME_VIEW)) { $this->view->assign('mime', $type); - $this->view->assign('data', htmlentities($fileData->getContent())); + $this->view->assign('data', htmlentities($fileDto->content)); $this->eventDispatcher->notify( 'show.accountFile', @@ -91,7 +93,7 @@ final class ViewController extends AccountFileBase $this, EventMessage::factory() ->addDescription(__u('File viewed')) - ->addDetail(__u('File'), $fileData->getName()) + ->addDetail(__u('File'), $fileDto->name) ) ); diff --git a/app/modules/web/themes/material-blue/views/itemshow/file.inc b/app/modules/web/themes/material-blue/views/itemshow/file.inc index 9d98b5ac..2bbc8fe3 100644 --- a/app/modules/web/themes/material-blue/views/itemshow/file.inc +++ b/app/modules/web/themes/material-blue/views/itemshow/file.inc @@ -23,30 +23,29 @@ */ /** - * @var File $fileData + * @var FileDto $fileDto * @var ThemeIconsInterface $icons * @var ConfigDataInterface $configData * @var callable $_getvar * @var TemplateInterface $this */ -use SP\Domain\Account\Models\File; +use SP\Domain\Account\Dtos\FileDto; use SP\Domain\Config\Ports\ConfigDataInterface; use SP\Domain\Core\UI\ThemeIconsInterface; use SP\Mvc\View\TemplateInterface; -$fileData = $_getvar('fileData'); +$fileDto = $_getvar('fileData'); ?> - + <?php
+    echo $fileDto->name; ?>
getName(), ENT_QUOTES); ?>
-name, ENT_QUOTES); ?>
diff --git a/lib/SP/Domain/Account/Ports/AccountFileService.php b/lib/SP/Domain/Account/Ports/AccountFileService.php index c8fb2d09..3577a91a 100644 --- a/lib/SP/Domain/Account/Ports/AccountFileService.php +++ b/lib/SP/Domain/Account/Ports/AccountFileService.php @@ -50,7 +50,7 @@ interface AccountFileService * @throws ConstraintException * @throws QueryException */ - public function create(FileModel $itemData): int; + public function create(FileModel $file): int; /** * Returns the item for given id diff --git a/lib/SP/Domain/Account/Services/AccountFile.php b/lib/SP/Domain/Account/Services/AccountFile.php index c6380ca7..b83a81b7 100644 --- a/lib/SP/Domain/Account/Services/AccountFile.php +++ b/lib/SP/Domain/Account/Services/AccountFile.php @@ -62,22 +62,22 @@ final class AccountFile extends Service implements AccountFileService /** * Creates an item * - * @param FileModel $itemData + * @param FileModel $file * * @return int * @throws ConstraintException * @throws InvalidImageException * @throws QueryException */ - public function create(FileModel $itemData): int + public function create(FileModel $file): int { - if (FileSystem::isImage($itemData)) { - $itemData->setThumb($this->imageUtil->createThumbnail($itemData->getContent())); + if (FileSystem::isImage($file->getType())) { + $file->setThumb($this->imageUtil->createThumbnail($file->getContent())); } else { - $itemData->setThumb('no_thumb'); + $file->setThumb('no_thumb'); } - return $this->accountFileRepository->create($itemData); + return $this->accountFileRepository->create($file); } /** diff --git a/lib/SP/Infrastructure/File/FileSystem.php b/lib/SP/Infrastructure/File/FileSystem.php index fde0bb25..2f8b6529 100644 --- a/lib/SP/Infrastructure/File/FileSystem.php +++ b/lib/SP/Infrastructure/File/FileSystem.php @@ -29,7 +29,6 @@ namespace SP\Infrastructure\File; use FilesystemIterator; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; -use SP\Domain\Account\Models\File; use SP\Domain\Core\Exceptions\FileNotFoundException; use SP\Domain\Core\Exceptions\InvalidClassException; @@ -79,9 +78,9 @@ class FileSystem return rmdir($dir); } - public static function isImage(File $fileData): bool + public static function isImage(string $fileType): bool { - return in_array(strtolower($fileData->getType()), self::IMAGE_MIME, true); + return in_array(strtolower($fileType), self::IMAGE_MIME, true); } /** diff --git a/tests/SP/Modules/Web/Controllers/AccountFile/AccountFileTest.php b/tests/SP/Modules/Web/Controllers/AccountFile/AccountFileTest.php index 1784926f..c036af92 100644 --- a/tests/SP/Modules/Web/Controllers/AccountFile/AccountFileTest.php +++ b/tests/SP/Modules/Web/Controllers/AccountFile/AccountFileTest.php @@ -275,6 +275,43 @@ class AccountFileTest extends IntegrationTestCase ); } + /** + * @return void + * @throws ContainerExceptionInterface + * @throws Exception + * @throws FileException + * @throws InvalidClassException + * @throws NotFoundExceptionInterface + */ + #[Test] + public function view() + { + $fileDataGenerator = FileDataGenerator::factory(); + + $this->addDatabaseMapperResolver( + File::class, + new QueryResult([File::buildFromSimpleModel($fileDataGenerator->buildFileData())]) + ); + + $definitions = $this->getModuleDefinitions(); + $definitions[OutputHandlerInterface::class] = $this->setupOutputHandler(function (string $output): void { + $crawler = new Crawler($output); + $filter = $crawler->filterXPath('//img|//div[@class="title"]')->count(); + + assert(!empty($output)); + assert($filter === 2); + + $this->assertTrue(true); + }); + + $container = $this->buildContainer( + $definitions, + $this->buildRequest('get', 'index.php', ['r' => 'accountFile/view/100']) + ); + + $this->runApp($container); + } + protected function getConfigData(): ConfigDataInterface|Stub { $configData = parent::getConfigData();