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