From ce096312d41c9fbc7603b886a63929c282fb6cac Mon Sep 17 00:00:00 2001 From: Moamen Eltouny Date: Sun, 1 Jan 2023 13:06:48 +0200 Subject: [PATCH] add disk option --- src/Upload.php | 29 +++++++++++++------ ...2021_02_01_000001_create_uploads_table.php | 1 + 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/Upload.php b/src/Upload.php index 4baee61..e4d4d5e 100644 --- a/src/Upload.php +++ b/src/Upload.php @@ -25,7 +25,7 @@ class Upload extends Model */ protected $fillable = [ 'uploader_id', - 'hash', 'name', 'path', 'size', 'extension', 'mime', + 'hash', 'name', 'path', 'size', 'extension', 'mime', 'disk', 'visitable', 'visits', 'private', 'thumbnail_id' ]; @@ -56,15 +56,16 @@ public static function upload(UploadedFile $file, array $options = []) $originalOptions = array_merge(config('Pharaonic.uploader.options', []), $options); $options = (object) $originalOptions; - $name = $file->getClientOriginalName(); - $hash = $file->hashName(); + $disk = $options['disk'] ?? config('Pharaonic.uploader.disk', 'public'); + $name = $file->getClientOriginalName(); + $hash = $file->hashName(); if (strpos($hash, '.') !== false) { $hash = explode('.', $hash); unset($hash[count($hash) - 1]); $hash = implode('', $hash); } - $hash = uniqid(config('Pharaonic.uploader.options.prefix', ''), true) . Str::random(7) . '-' . $hash; + $hash = uniqid(config('Pharaonic.uploader.options.prefix', ''), true) . Str::random(7) . '-' . $hash; if (strpos($name, '.') !== false) { $ext = explode('.', $name); @@ -84,7 +85,7 @@ public static function upload(UploadedFile $file, array $options = []) // Save File $plusName = 'raggi.' . Str::random(20); - $file->storeAs($path, $hash . $plusName, config('Pharaonic.uploader.disk', 'public')); + $file->storeAs($path, $hash . $plusName, $disk); // Thumbnail Generating $thumbnail = null; @@ -119,7 +120,7 @@ public static function upload(UploadedFile $file, array $options = []) $thumbnail = upload($thumbnail, $originalOptions)->id; // Delete Fake File - Storage::disk(config('Pharaonic.uploader.disk', 'public'))->delete('pharaonic-thumbs/' . $thumb_name); + Storage::disk($disk)->delete('pharaonic-thumbs/' . $thumb_name); } @@ -167,7 +168,7 @@ public static function upload(UploadedFile $file, array $options = []) */ public function deleteFile() { - return Storage::disk(config('Pharaonic.uploader.disk', 'public'))->delete($this->path); + return Storage::disk($this->getDisk())->delete($this->path); } /** @@ -180,6 +181,16 @@ public function readableSize(bool $decimal = true) return ReadableSize($this->size, $decimal); } + /** + * Get uploading disk + * + * @return string + */ + protected function getDisk() + { + return $this->disk ?? config('Pharaonic.uploader.disk', 'public'); + } + /** * Getting URL * @@ -187,8 +198,8 @@ public function readableSize(bool $decimal = true) */ public function getUrlAttribute() { - if (!in_array(config('Pharaonic.uploader.disk'), ['local', 'public'])) { - $driver = Storage::disk(config('Pharaonic.uploader.disk')); + if (!in_array($this->getDisk(), ['local', 'public'])) { + $driver = Storage::disk($this->getDisk()); return $driver->providesTemporaryUrls() ? $driver->temporaryUrl($this->path, now()->addMinutes(config('Pharaonic.uploader.expire', 5))) : $driver->url($this->path); } else { return route('uploaded', $this->hash); diff --git a/src/database/migrations/2021_02_01_000001_create_uploads_table.php b/src/database/migrations/2021_02_01_000001_create_uploads_table.php index 4fdcd4f..f5e8c1c 100644 --- a/src/database/migrations/2021_02_01_000001_create_uploads_table.php +++ b/src/database/migrations/2021_02_01_000001_create_uploads_table.php @@ -22,6 +22,7 @@ public function up() $table->bigInteger('size'); $table->string('extension', 20); $table->string('mime', 50); + $table->string('disk')->nullable(); $table->boolean('visitable')->default(false); $table->unsignedBigInteger('visits')->default(0);