From bb7d6a3713fe439482ce218f95d037c40032ba9d Mon Sep 17 00:00:00 2001 From: ArrayIterator Date: Sun, 22 Oct 2023 02:43:36 +0700 Subject: [PATCH] add change --- Languages/media-module.pot | 14 ++++----- Servant/DataServe.php | 60 ++++++++++++++++--------------------- Traits/MediaFilterTrait.php | 40 ++++++++++++------------- Traits/MediaPathTrait.php | 32 ++++++++++---------- 4 files changed, 68 insertions(+), 78 deletions(-) diff --git a/Languages/media-module.pot b/Languages/media-module.pot index b070f06..3b37d92 100644 --- a/Languages/media-module.pot +++ b/Languages/media-module.pot @@ -2,7 +2,7 @@ msgid "" msgstr "" "Project-Id-Version: TrayDigita Media Module 1.0.0\n" -"POT-Creation-Date: 2023-10-16 01:39+0700\n" +"POT-Creation-Date: 2023-10-22 02:41+0700\n" "PO-Revision-Date: 2023-09-24 19:00+0700\n" "Last-Translator: ArrayAccess\n" "Language-Team: ArrayAccess\n" @@ -26,35 +26,35 @@ msgstr "" "X-Poedit-SearchPathExcluded-1: *.html\n" "X-Poedit-SearchPathExcluded-2: *.json\n" -#: Media.php:45 +#: Media.php:43 msgctxt "module" msgid "Media Manager" msgstr "" -#: Media.php:54 +#: Media.php:52 msgctxt "module" msgid "" "Module to make application support media & file " "attachments" msgstr "" -#: Uploader/Abstracts/AbstractUploader.php:126 +#: Uploader/Abstracts/AbstractUploader.php:128 msgctxt "module" msgid "File does not have file name" msgstr "" -#: Uploader/Abstracts/AbstractUploader.php:141 +#: Uploader/Abstracts/AbstractUploader.php:143 #, php-format msgctxt "module" msgid "Could not determine file type from mimetype %s" msgstr "" -#: Uploader/Abstracts/AbstractUploader.php:221 +#: Uploader/Abstracts/AbstractUploader.php:259 msgctxt "module" msgid "Could not save uploaded file" msgstr "" -#: Uploader/Abstracts/AbstractUploader.php:233 +#: Uploader/Abstracts/AbstractUploader.php:271 msgctxt "module" msgid "" "Could not save uploaded file & determine target file." diff --git a/Servant/DataServe.php b/Servant/DataServe.php index 74ba5c0..b3891c3 100644 --- a/Servant/DataServe.php +++ b/Servant/DataServe.php @@ -3,26 +3,17 @@ namespace ArrayAccess\TrayDigita\App\Modules\Media\Servant; -// phpcs:disable PSR1.Files.SideEffects use ArrayAccess\TrayDigita\App\Modules\Media\Media; +use ArrayAccess\TrayDigita\Responder\FileResponder; use ArrayAccess\TrayDigita\Util\Filter\DataNormalizer; use ArrayAccess\TrayDigita\Util\Filter\MimeType; use DateTimeImmutable; use DateTimeZone; use function clearstatcache; -use function fclose; -use function feof; use function filemtime; use function filesize; -use function fopen; -use function fread; -use function header; -use function headers_sent; use function is_file; use function is_readable; -use function ob_end_flush; -use function ob_get_level; -use function ob_start; use function str_starts_with; /** @@ -49,10 +40,10 @@ public function getNormalizeFile(string $file) return $this->cachedNormalize[$file]?:null; } $file = DataNormalizer::normalizeDirectorySeparator($file); - $uploadDir = $this->uploader->getDataDirectory(); - if (!str_starts_with($file, $uploadDir)) { + $uploadDirectory = $this->uploader->getDataDirectory(); + if (!str_starts_with($file, $uploadDirectory)) { $file = DataNormalizer::normalizeDirectorySeparator( - $uploadDir . '/' .$file + $uploadDirectory . '/' .$file ); } if (is_file($file) && is_readable($file)) { @@ -103,33 +94,32 @@ public function getLastModified(string $file) : ?DateTimeImmutable ); } + /** + * @param string $file + * @param bool $sendHeaderContentLength + * @param bool $allowRange + * @param bool $sendAsAttachment + * @return bool + */ public function display( string $file, - bool $sendHeaderContentLength = false - ) : int|false { + bool $sendHeaderContentLength = false, + bool $allowRange = false, + bool $sendAsAttachment = false + ) : bool { + $file = $this->getNormalizeFile($file); + if (!$file || !is_file($file)) { + return false; + } $size = $this->size($file); if ($size === false) { return false; } - - $file = $this->uploader->getDataDirectory() . '/'. $file; - $resource = fopen($file, 'rb'); - $level = ob_get_level(); - if ($level > 0) { - ob_end_flush(); - } - if ($sendHeaderContentLength && !headers_sent()) { - header('Content-Length: %d', $size); - } - while (!feof($resource)) { - echo fread($resource, 8192); - } - fclose($resource); - - // re-start buffer - if ($level > ob_get_level()) { - ob_start(); - } - return $size; + $responder = (new FileResponder($file)); + $responder->sendContentLength($sendHeaderContentLength); + $responder->setAllowRange($allowRange); + $responder->sendAsAttachment($sendAsAttachment); + // never + $responder->send(); } } diff --git a/Traits/MediaFilterTrait.php b/Traits/MediaFilterTrait.php index 658b4a7..23f6c18 100644 --- a/Traits/MediaFilterTrait.php +++ b/Traits/MediaFilterTrait.php @@ -89,19 +89,19 @@ public function getAttachmentFileToBasePath( string $path ): ?string { $path = DataNormalizer::normalizeUnixDirectorySeparator($path); - $uploadDir = DataNormalizer::normalizeUnixDirectorySeparator( + $uploadDirectory = DataNormalizer::normalizeUnixDirectorySeparator( $this->getUploadDirectory() ); - $dataDir = DataNormalizer::normalizeUnixDirectorySeparator( + $dataDirectory = DataNormalizer::normalizeUnixDirectorySeparator( $this->getUploadDirectory() ); - $avatarDir = DataNormalizer::normalizeUnixDirectorySeparator( + $avatarDirectory = DataNormalizer::normalizeUnixDirectorySeparator( $this->getUploadDirectory() ); - $currentDirectory = str_starts_with($path, $avatarDir) ? $avatarDir : null; - $currentDirectory ??= str_starts_with($path, $uploadDir) ? $uploadDir : null; - $currentDirectory ??= str_starts_with($path, $dataDir) ? $dataDir : null; + $currentDirectory = str_starts_with($path, $avatarDirectory) ? $avatarDirectory : null; + $currentDirectory ??= str_starts_with($path, $uploadDirectory) ? $uploadDirectory : null; + $currentDirectory ??= str_starts_with($path, $dataDirectory) ? $dataDirectory : null; if (!$currentDirectory) { return null; } @@ -122,44 +122,44 @@ public function getUploadFileToBasePath( string $path ) : ?string { $path = DataNormalizer::normalizeUnixDirectorySeparator($path); - $uploadDir = DataNormalizer::normalizeUnixDirectorySeparator( + $uploadDirectory = DataNormalizer::normalizeUnixDirectorySeparator( $this->getUploadDirectory() ); /** @noinspection DuplicatedCode */ - if (!str_starts_with($path, $uploadDir)) { + if (!str_starts_with($path, $uploadDirectory)) { return null; } - $uploadDir = ltrim(substr($path, strlen($uploadDir)), '/'); - if (str_starts_with($uploadDir, $this->getFrontendPath()) - || str_starts_with($uploadDir, $this->getBackendPath()) + $uploadDirectory = ltrim(substr($path, strlen($uploadDirectory)), '/'); + if (str_starts_with($uploadDirectory, $this->getFrontendPath()) + || str_starts_with($uploadDirectory, $this->getBackendPath()) ) { - $explode = explode('/', $uploadDir, 2); + $explode = explode('/', $uploadDirectory, 2); array_shift($explode); return implode('/', $explode); } - return $uploadDir; + return $uploadDirectory; } public function getDataFileToBasePath( string $path ) : ?string { $path = DataNormalizer::normalizeUnixDirectorySeparator($path); - $dataDir = DataNormalizer::normalizeUnixDirectorySeparator( + $dataDirectory = DataNormalizer::normalizeUnixDirectorySeparator( $this->getDataDirectory() ); /** @noinspection DuplicatedCode */ - if (!str_starts_with($path, $dataDir)) { + if (!str_starts_with($path, $dataDirectory)) { return null; } - $dataDir = ltrim(substr($path, strlen($dataDir)), '/'); - if (str_starts_with($dataDir, $this->getFrontendPath()) - || str_starts_with($dataDir, $this->getBackendPath()) + $dataDirectory = ltrim(substr($path, strlen($dataDirectory)), '/'); + if (str_starts_with($dataDirectory, $this->getFrontendPath()) + || str_starts_with($dataDirectory, $this->getBackendPath()) ) { - $explode = explode('/', $dataDir, 2); + $explode = explode('/', $dataDirectory, 2); array_shift($explode); return implode('/', $explode); } - return $dataDir; + return $dataDirectory; } public function getUploadFileToURI( diff --git a/Traits/MediaPathTrait.php b/Traits/MediaPathTrait.php index fd14a10..10329a2 100644 --- a/Traits/MediaPathTrait.php +++ b/Traits/MediaPathTrait.php @@ -62,34 +62,34 @@ private function doFilterPath(): static $this->registeredPathInit = true; $path = ContainerHelper::service(Config::class, $this->getContainer())->get('path'); $path = $path instanceof Config ? $path : new Config(); - $dataDir = $path->get('data'); - if (!$dataDir || !is_string($dataDir)) { - $dataDir = dirname(TD_APP_DIRECTORY) . '/data'; + $dataDirectory = $path->get('data'); + if (!$dataDirectory || !is_string($dataDirectory)) { + $dataDirectory = dirname(TD_APP_DIRECTORY) . '/data'; } - if (!is_dir($dataDir)) { - mkdir($dataDir, 0755, true); + if (!is_dir($dataDirectory)) { + mkdir($dataDirectory, 0755, true); } - $uploadDir = $path->get('upload'); - $publicDir = $path->get('public'); - if (!$uploadDir || !is_string($uploadDir) || !is_dir($uploadDir)) { - if (!is_string($publicDir) - || !$publicDir - || !realpath($publicDir) + $uploadDirectory = $path->get('upload'); + $publicDirectory = $path->get('public'); + if (!$uploadDirectory || !is_string($uploadDirectory) || !is_dir($uploadDirectory)) { + if (!is_string($publicDirectory) + || !$publicDirectory + || !realpath($publicDirectory) ) { if (!defined('TD_INDEX_FILE')) { throw new RuntimeException( 'Could not determine public directory' ); } - $publicDir = dirname(TD_INDEX_FILE); + $publicDirectory = dirname(TD_INDEX_FILE); } else { - $publicDir = realpath($publicDir); + $publicDirectory = realpath($publicDirectory); } - $uploadDir = $publicDir . '/uploads'; + $uploadDirectory = $publicDirectory . '/uploads'; } - $this->dataDirectory = DataNormalizer::normalizeDirectorySeparator($dataDir); - $this->uploadDirectory = DataNormalizer::normalizeUnixDirectorySeparator($uploadDir); + $this->dataDirectory = DataNormalizer::normalizeDirectorySeparator($dataDirectory); + $this->uploadDirectory = DataNormalizer::normalizeUnixDirectorySeparator($uploadDirectory); $this->publicDirectory = DataNormalizer::normalizeDirectorySeparator(dirname($this->uploadDirectory)); return $this; }