Skip to content

Commit

Permalink
add enhancements uploader
Browse files Browse the repository at this point in the history
  • Loading branch information
ArrayIterator committed Oct 19, 2023
1 parent de6a8d5 commit 1b45699
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 43 deletions.
14 changes: 13 additions & 1 deletion Servant/DataServe.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use ArrayAccess\TrayDigita\Util\Filter\MimeType;
use DateTimeImmutable;
use DateTimeZone;
use function clearstatcache;
use function fclose;
use function feof;
use function filemtime;
Expand All @@ -30,6 +31,12 @@
final class DataServe
{
private array $cachedSize = [];

/**
* @var array<string, int>
*/
private array $cachedLastModified = [];

private array $cachedNormalize = [];

public function __construct(public readonly Media $uploader)
Expand Down Expand Up @@ -84,9 +91,14 @@ public function getLastModified(string $file) : ?DateTimeImmutable
if (!$file) {
return null;
}
if (!isset($this->cachedLastModified[$file])) {
// clear stat cache
clearstatcache(true, $file);
$this->cachedLastModified[$file] = filemtime($file);
}
return DateTimeImmutable::createFromFormat(
'c',
gmdate('c', filemtime($file)),
gmdate('c', $this->cachedLastModified[$file]),
new DateTimeZone('UTC')
);
}
Expand Down
59 changes: 22 additions & 37 deletions Uploader/Abstracts/AbstractUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace ArrayAccess\TrayDigita\App\Modules\Media\Uploader\Abstracts;

use ArrayAccess\TrayDigita\App\Modules\Media\Media;
use ArrayAccess\TrayDigita\App\Modules\Media\Uploader\UploadedFileMetadata;
use ArrayAccess\TrayDigita\App\Modules\Users\Entities\Admin;
use ArrayAccess\TrayDigita\App\Modules\Users\Entities\Attachment;
use ArrayAccess\TrayDigita\App\Modules\Users\Entities\User;
use ArrayAccess\TrayDigita\App\Modules\Users\Entities\UserAttachment;
use ArrayAccess\TrayDigita\App\Modules\Media\Media;
use ArrayAccess\TrayDigita\App\Modules\Media\Uploader\UploadedFileMetadata;
use ArrayAccess\TrayDigita\Database\Connection;
use ArrayAccess\TrayDigita\Database\Entities\Abstracts\AbstractAttachment;
use ArrayAccess\TrayDigita\Database\Entities\Abstracts\AbstractUser;
Expand All @@ -25,15 +25,10 @@
use Psr\Http\Message\UploadedFileInterface;
use Throwable;
use function file_exists;
use function file_get_contents;
use function file_put_contents;
use function filesize;
use function function_exists;
use function is_file;
use function is_string;
use function mime_content_type;
use function pathinfo;
use function preg_match;
use function reset;
use function sprintf;
use function unlink;
Expand Down Expand Up @@ -168,16 +163,17 @@ public function uploadAttachment(
$uploadedFile = MimeType::resolveMediaTypeUploadedFiles($uploadedFile);
$originalFileName = $uploadedFile->getClientFilename();
$progress = $this->media->upload($uploadedFile, $request);
$mimeFile = $progress->targetCacheFile
. '.mime_type.'
$metafile = $progress->targetCacheFile
. '.meta.'
. $progress->handler->processor->chunk->partialExtension;
$newMimeType = null;
$metadata = $progress->getMetadata();
$newMimeType = $metadata['mimetype']??null;
$requestTime = $metadata['first_time']??null;
$count = $metadata['count']??null;
$timing = $metadata['timing']??[];
unset($metadata);
if ($progress->handler->processor->isNewRequestId) {
$newMimeType = $uploadedFile->getClientMediaType();
if (!file_exists($mimeFile)) {
// save mime-type
file_put_contents($mimeFile, $newMimeType);
}
$count = 1;
$this->getMedia()->getManager()->attach(
'jsonResponder.format',
function ($e) use ($newMimeType) {
Expand All @@ -186,25 +182,8 @@ function ($e) use ($newMimeType) {
},
priority: PHP_INT_MAX - 5
);
} elseif (is_file($mimeFile)) {
$validMime = false;
// check filesize
if (filesize($mimeFile) <= 128) {
$mime = Consolidation::callbackReduceError(fn() => file_get_contents($mimeFile));
if (is_string($mime)
&& strlen($mime) <= 128
&& preg_match('~^[^/]+/\S+$~', $mime)
&& MimeType::fromMimeType($mime)
) {
$newMimeType = $mime;
$validMime = true;
}
}
if (!$validMime) {
// delete if longer
Consolidation::callbackReduceError(fn() => unlink($mimeFile));
}
}

if ($newMimeType && $newMimeType !== $uploadedFile->getClientMediaType()) {
$uploadedFile = new UploadedFile(
$uploadedFile->getStream(),
Expand All @@ -221,7 +200,10 @@ function ($e) use ($newMimeType) {
$request,
$uploadedFile,
$progress,
false
false,
firstMicrotime: $requestTime,
chunkCount: $count,
timing: $timing
);
}

Expand Down Expand Up @@ -335,14 +317,17 @@ function_exists('mime_content_type')
$progress,
true,
$fullPath,
$attachment
$attachment,
firstMicrotime: $requestTime,
chunkCount: $count,
timing: $timing
);
if ($type === self::TYPE_AVATAR) {
$result = $this->dispatchAvatarUpload($result);
}
} finally {
if (is_file($mimeFile)) {
Consolidation::callbackReduceError(fn() => unlink($mimeFile));
if (is_file($metafile)) {
Consolidation::callbackReduceError(fn() => unlink($metafile));
}
}
return $result;
Expand Down
26 changes: 21 additions & 5 deletions Uploader/UploadedFileMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use function basename;
use function file_exists;
use function filesize;
use function microtime;
use function pathinfo;
use const PATHINFO_FILENAME;

Expand All @@ -28,7 +29,10 @@ public function __construct(
public StartProgress $progress,
public bool $finished,
public ?string $fullPath = null,
public ?AbstractAttachment $attachment = null
public ?AbstractAttachment $attachment = null,
public ?float $firstMicrotime = null,
public ?int $chunkCount = null,
public array $timing = [],
) {
$this->size = $this->fullPath && file_exists($this->fullPath)
? filesize($this->fullPath)
Expand All @@ -37,17 +41,22 @@ public function __construct(

/**
* @param bool $pathOnly
* @param bool $withTiming
* @return array{
* id: ?string,
* uuid: string,
* request_id: string,
* name: string,
* file_name: string,
* mime_type: string,
* saved_name: string,
* saved_name: ?string,
* uri: ?UriInterface|?string,
* size: int
* size: int,
* processed_count: int,
* processed_time: ?float,
* timing: array,
* }
*/
public function toArray(bool $pathOnly = false): array
public function toArray(bool $pathOnly = false, bool $withTiming = true): array
{
$uploadedFile = $this->uploadedFile;
$fileName = $uploadedFile->getClientFilename();
Expand All @@ -66,6 +75,13 @@ public function toArray(bool $pathOnly = false): array
'saved_name' => $this->finished ? basename($this->fullPath) : null,
'uri' => $uri,
'size' => $this->size,
'processed_count' => $this->chunkCount,
'processed_time' => (
$this->firstMicrotime
? round(microtime(true) - $this->firstMicrotime, 10)
: null
),
'timing' => $this->timing
];
}
}

0 comments on commit 1b45699

Please sign in to comment.