From 3e7f34d5c46e1c4a204a338367c945eee6b25608 Mon Sep 17 00:00:00 2001 From: James Collins Date: Mon, 28 Aug 2023 14:00:07 +1000 Subject: [PATCH] fix video uploads --- app/Http/Controllers/Api/MediaController.php | 2 +- app/Jobs/MediaJob.php | 12 +++---- app/Models/Media.php | 36 +++++++++++-------- .../js/components/dialogs/SMDialogMedia.vue | 21 ++++++----- resources/js/views/dashboard/MediaList.vue | 21 ++++++++--- 5 files changed, 57 insertions(+), 35 deletions(-) diff --git a/app/Http/Controllers/Api/MediaController.php b/app/Http/Controllers/Api/MediaController.php index 1aab7a3..1d39573 100644 --- a/app/Http/Controllers/Api/MediaController.php +++ b/app/Http/Controllers/Api/MediaController.php @@ -105,7 +105,7 @@ class MediaController extends ApiController $mediaItem = $request->user()->media()->create($request->except(['file','transform'])); - $temporaryFilePath = generateTempFilePath(); + $temporaryFilePath = generateTempFilePath(pathinfo($mediaItem->name, PATHINFO_EXTENSION)); copy($file->path(), $temporaryFilePath); $transformData = ['file' => [ diff --git a/app/Jobs/MediaJob.php b/app/Jobs/MediaJob.php index 1aaa71d..968a65b 100644 --- a/app/Jobs/MediaJob.php +++ b/app/Jobs/MediaJob.php @@ -94,7 +94,7 @@ class MediaJob implements ShouldQueue $jpgFileName = pathinfo($filePath, PATHINFO_FILENAME) . '.jpg'; $jpgFilePath = $uploadedFileDirectory . '/' . $jpgFileName; if (file_exists($jpgFilePath) === true) { - $this->media->error("File already exists in storage"); + $this->media->error("File already exists on server"); return; } @@ -110,9 +110,9 @@ class MediaJob implements ShouldQueue // Check if file already exists if (Storage::disk($this->media->storage)->exists($this->media->name) === true) { if (array_key_exists('replace', $uploadData) === false || isTrue($uploadData['replace']) === false) { - $this->media->error("File already exists in storage"); - $errorStr = "cannot upload file {$this->media->storage} " . // phpcs:ignore - "/ {$this->media->name} as it already exists"; + $this->media->error("File already exists on server"); + $errorStr = "cannot upload file " . $this->media->storage . " " . // phpcs:ignore + "/ " . $this->media->name . " as it already exists"; Log::info($errorStr); throw new \Exception($errorStr); } @@ -282,10 +282,10 @@ class MediaJob implements ShouldQueue $this->media->deleteStagingFile(); if (strpos($this->media->status, 'Error') !== 0) { - $this->media->error('Failed to process'); + $this->media->error('Failed to process the file'); } - Log::error($e->getMessage()); + Log::error($e->getMessage() . "\n" . $e->getFile() . " - " . $e->getLine() . "\n" . $e->getTraceAsString()); $this->fail($e); }//end try } diff --git a/app/Models/Media.php b/app/Models/Media.php index 271afe0..47c0aaf 100644 --- a/app/Models/Media.php +++ b/app/Models/Media.php @@ -8,6 +8,8 @@ use App\Jobs\MoveMediaJob; use App\Jobs\StoreUploadedFileJob; use App\Traits\Uuids; use Exception; +use FFMpeg\Coordinate\TimeCode; +use FFMpeg\FFMpeg; use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\InvalidCastException; @@ -694,7 +696,7 @@ class Media extends Model public function createStagingFile(): bool { if ($this->stagingFilePath === "") { - $readStream = Storage::disk($this->storageDisk)->readStream($this->name); + $readStream = Storage::disk($this->storage)->readStream($this->name); $filePath = generateTempFilePath(pathinfo($this->name, PATHINFO_EXTENSION)); $writeStream = fopen($filePath, 'w'); @@ -788,8 +790,8 @@ class Media extends Model // delete existing thumbnail if (strlen($this->thumbnail) !== 0) { $path = substr($this->thumbnail, strlen($this->getUrlPath())); - if (strlen($path) > 0 && Storage::disk($this->storageDisk)->exists($path) === true) { - Storage::disk($this->storageDisk)->delete($path); + if (strlen($path) > 0 && Storage::disk($this->storage)->exists($path) === true) { + Storage::disk($this->storage)->delete($path); } } @@ -800,8 +802,6 @@ class Media extends Model $newFilename = pathinfo($this->name, PATHINFO_FILENAME) . "-thumb.webp"; $success = false; - $ffmpegPath = env('FFMPEG_PATH', '/usr/bin/ffmpeg'); - if (strpos($this->mime_type, 'image/') === 0) { $image = Image::make($filePath); $image->resize($thumbnailWidth, $thumbnailHeight, function ($constraint) { @@ -852,18 +852,24 @@ class Media extends Model $image->encode('webp', 75)->save($tempImagePath); $success = true; - } elseif (file_exists($ffmpegPath) === true && strpos($this->mime_type, 'video/') === 0) { + } elseif (strpos($this->mime_type, 'video/') === 0) { $tempImagePath .= '.webp'; - $command = "$ffmpegPath -i $filePath -ss 00:00:05 -vframes 1 " . // phpcs:ignore - "-s {$thumbnailWidth}x{$thumbnailHeight} -c:v webp {$tempImagePath}"; - exec($command); + + try { + $ffmpeg = FFMpeg::create(); + $video = $ffmpeg->open($filePath); + $frame = $video->frame(TimeCode::fromSeconds(5)); + $frame->save($tempImagePath); + } catch(\Exception $e) { + Log::error($e); + } $success = true; }//end if if ($success === true && file_exists($tempImagePath) === true) { /** @var Illuminate\Filesystem\FilesystemAdapter */ - $fileSystem = Storage::disk($this->storageDisk); + $fileSystem = Storage::disk($this->storage); $fileSystem->putFileAs('/', new SplFileInfo($tempImagePath), $newFilename); unlink($tempImagePath); @@ -893,8 +899,8 @@ class Media extends Model if (strlen($this->thumbnail) > 0) { $path = substr($this->thumbnail, strlen($this->getUrlPath())); - if (strlen($path) > 0 && Storage::disk($this->storageDisk)->exists($path) === true) { - Storage::disk($this->storageDisk)->delete($path); + if (strlen($path) > 0 && Storage::disk($this->storage)->exists($path) === true) { + Storage::disk($this->storage)->delete($path); $this->thumbnail = ''; // Clear the thumbnail property } } @@ -917,8 +923,8 @@ class Media extends Model // delete existing variants if (is_array($this->variants) === true) { foreach ($this->variants as $variantName => $variantFile) { - if (Storage::disk($this->storageDisk)->exists($variantFile) === true) { - Storage::disk($this->storageDisk)->delete($variantFile); + if (Storage::disk($this->storage)->exists($variantFile) === true) { + Storage::disk($this->storage)->delete($variantFile); } } } @@ -973,7 +979,7 @@ class Media extends Model $tempImagePath = tempnam(sys_get_temp_dir(), 'optimize'); $image->encode('webp', 75)->save($tempImagePath); /** @var Illuminate\Filesystem\FilesystemAdapter */ - $fileSystem = Storage::disk($this->storageDisk); + $fileSystem = Storage::disk($this->storage); $fileSystem->putFileAs('/', new SplFileInfo($tempImagePath), $newFilename); unlink($tempImagePath); }//end if diff --git a/resources/js/components/dialogs/SMDialogMedia.vue b/resources/js/components/dialogs/SMDialogMedia.vue index c52bc30..84cc00c 100644 --- a/resources/js/components/dialogs/SMDialogMedia.vue +++ b/resources/js/components/dialogs/SMDialogMedia.vue @@ -192,7 +192,8 @@ class="mt-4 text-center">

Showing {{ mediaItems.length }} of - {{ totalItems }} media item{{ + {{ totalItems }} + media item{{ totalItems == 1 ? "" : "s" }}

@@ -242,17 +243,17 @@
- + )}')`, + }">

{{ lastSelected.title }} @@ -910,6 +911,7 @@ const startFilesUpload = async () => { return true; }); + totalItems.value++; updateFiles(); currentUploadFileNum.value++; } @@ -963,11 +965,14 @@ const updateFiles = async () => { (mediaItem) => mediaItem.id !== updateData.medium.id, ); + lastSelected.value = null; + totalItems.value--; useToastStore().addToast({ title: "Upload failed", type: "danger", - content: `${item.name} failed to be processed by the server.`, + content: updateData.medium.status, + // content: `${item.name} failed to be processed by the server.`, }); } } else { diff --git a/resources/js/views/dashboard/MediaList.vue b/resources/js/views/dashboard/MediaList.vue index ed9a9fd..e1fcabd 100644 --- a/resources/js/views/dashboard/MediaList.vue +++ b/resources/js/views/dashboard/MediaList.vue @@ -71,11 +71,21 @@ - +