fix video uploads

This commit is contained in:
2023-08-28 14:00:07 +10:00
parent 33f658e088
commit 3e7f34d5c4
5 changed files with 57 additions and 35 deletions

View File

@@ -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' => [

View File

@@ -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
}

View File

@@ -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

View File

@@ -192,7 +192,8 @@
class="mt-4 text-center">
<p class="text-xs text-black mb-4">
Showing {{ mediaItems.length }} of
{{ totalItems }} media item{{
{{ totalItems }}
media item{{
totalItems == 1 ? "" : "s"
}}
</p>
@@ -242,17 +243,17 @@
<div v-if="lastSelected != null">
<div
class="flex text-xs border-b border-gray-3 pb-4">
<img
:src="
mediaGetThumbnail(
<div
class="w-100 h-100 max-h-20 max-w-20 mr-2 bg-contain bg-no-repeat bg-center"
:style="{
backgroundImage: `url('${mediaGetThumbnail(
lastSelected,
null,
itemRequiresRefresh(
lastSelected.id,
),
)
"
class="max-h-20 max-w-20 mr-2" />
)}')`,
}"></div>
<div class="flex flex-col w-100">
<p class="m-0 text-bold">
{{ 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 {

View File

@@ -71,11 +71,21 @@
<template #item-size="item">
{{ bytesReadable(item.size) }}
</template>
<template #item-title="item"
>{{ item.title }}<br /><span class="small"
>({{ item.name }})</span
></template
>
<template #item-title="item">
<div class="flex gap-2">
<div
class="w-100 h-100 max-h-15 max-w-20 mr-2 bg-contain bg-no-repeat bg-center"
:style="{
backgroundImage: `url('${mediaGetThumbnail(
item,
)}')`,
}"></div>
<div class="flex flex-col flex-justify-center">
<span>{{ item.title }}</span>
<span class="small">({{ item.name }})</span>
</div>
</div>
</template>
<template #item-actions="item">
<button
type="button"
@@ -164,6 +174,7 @@ import { updateRouterParams } from "../../helpers/url";
import { userHasPermission } from "../../helpers/utils";
import SMPageStatus from "../../components/SMPageStatus.vue";
import SMCheckbox from "../../components/SMCheckbox.vue";
import { mediaGetThumbnail } from "../../helpers/media";
const route = useRoute();
const router = useRouter();