Skip to content

Commit

Permalink
add disk option
Browse files Browse the repository at this point in the history
  • Loading branch information
MoamenEltouny committed Jan 1, 2023
1 parent 5648862 commit ce09631
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
];
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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);

}

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -180,15 +181,25 @@ 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
*
* @return string
*/
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ce09631

Please sign in to comment.