Skip to content

Commit

Permalink
add change
Browse files Browse the repository at this point in the history
  • Loading branch information
ArrayIterator committed Oct 21, 2023
1 parent 40177a9 commit bb7d6a3
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 78 deletions.
14 changes: 7 additions & 7 deletions Languages/media-module.pot
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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."
Expand Down
60 changes: 25 additions & 35 deletions Servant/DataServe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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)) {
Expand Down Expand Up @@ -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();
}
}
40 changes: 20 additions & 20 deletions Traits/MediaFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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(
Expand Down
32 changes: 16 additions & 16 deletions Traits/MediaPathTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit bb7d6a3

Please sign in to comment.