-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Pharaonic/feature/structure-enhancement
Version 4.0
- Loading branch information
Showing
23 changed files
with
717 additions
and
660 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/vendor | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
return [ | ||
/** | ||
* Storage Disk (local, public, s3, ...) | ||
*/ | ||
'disk' => env('FILESYSTEM_DISK', 'local'), | ||
|
||
/** | ||
* Uploading Path | ||
*/ | ||
'path' => '/', | ||
|
||
/** | ||
* Expiry time of temporary-url in minutes (S3) | ||
*/ | ||
'expire' => 30, | ||
|
||
/** | ||
* Visibility of the uploaded files (S3) | ||
*/ | ||
'visibility' => 'private', | ||
|
||
/** | ||
* Include the file extension in the URL (local) | ||
*/ | ||
'extensional' => true, | ||
|
||
/** | ||
* Route Contoller Class | ||
*/ | ||
'controller' => \Pharaonic\Laravel\Uploader\Http\Controllers\UploadController::class, | ||
|
||
/** | ||
* Route URI (/file/{hash}) | ||
*/ | ||
'uri' => '/file', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Route; | ||
use Pharaonic\Laravel\Uploader\Facades\Uploader; | ||
|
||
Route::middleware('web') | ||
->name('uploader.file') | ||
->get( | ||
Uploader::route()->uri(), | ||
Uploader::route()->action() | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
|
||
namespace Pharaonic\Laravel\Uploader\Actions; | ||
|
||
use Illuminate\Http\UploadedFile; | ||
use Illuminate\Support\Facades\File; | ||
use Illuminate\Support\Str; | ||
use Intervention\Image\Facades\Image; | ||
use Pharaonic\Laravel\Uploader\Facades\Uploader; | ||
use Pharaonic\Laravel\Uploader\Models\Upload; | ||
|
||
class CreateFile | ||
{ | ||
/** | ||
* Handle the action. | ||
* | ||
* @param UploadedFile $file | ||
* @param array|null $options | ||
* @return Upload | ||
*/ | ||
public function handle(UploadedFile $file, array $options = []) | ||
{ | ||
$hash = $this->generateHash($file); | ||
$extension = $file->extension(); | ||
extract($this->options($options)); | ||
|
||
$file->storeAs($path, $hash . '.' . $extension, [ | ||
'disk' => $disk, | ||
'visibility' => $visibility, | ||
]); | ||
|
||
// Thumbnail Generating | ||
if (str_starts_with($file->getMimeType(), 'image/') && isset($options['thumbnail'])) { | ||
$thumbnail = $this->uploadThumbnail($file, $options); | ||
} | ||
|
||
return Upload::create([ | ||
'thumbnail_id' => isset($thumbnail) ? $thumbnail->id : null, | ||
'hash' => $hash, | ||
'disk' => $disk, | ||
'visibility' => $visibility, | ||
'path' => $path . $hash . '.' . $extension, | ||
'extension' => $extension, | ||
'name' => $file->getClientOriginalName(), | ||
'mime' => $file->getMimeType(), | ||
'size' => $file->getSize(), | ||
]); | ||
} | ||
|
||
/** | ||
* Generate a new Hash. | ||
* | ||
* @param UploadedFile $file | ||
* @return string | ||
*/ | ||
protected function generateHash(UploadedFile $file) | ||
{ | ||
$hash = Str::random(27) . pathinfo($file->hashName(), PATHINFO_FILENAME); | ||
|
||
if (Upload::whereHash($hash)->exists()) | ||
return $this->generateHash($file); | ||
|
||
return $hash; | ||
} | ||
|
||
/** | ||
* Get the options list. | ||
* | ||
* @param array $options | ||
* @return array | ||
*/ | ||
protected function options(array $options) | ||
{ | ||
$originalOptions = Uploader::options(); | ||
|
||
$path = trim($originalOptions['path'], '/') . '/'; | ||
$filePath = trim($options['path'] ?? '', '/'); | ||
|
||
if (!empty($filePath)) { | ||
$path .= $filePath . '/'; | ||
} | ||
|
||
return [ | ||
'disk' => $disk = ($options['disk'] ?? $originalOptions['disk']), | ||
'visibility' => config('filesystems.disks.' . $disk . '.driver') != 'local' ? ($options['visibility'] ?? $originalOptions['visibility']) : null, | ||
'path' => str_replace( | ||
'/', | ||
DIRECTORY_SEPARATOR, | ||
$path . date('Y-m-d', time()) . '/' | ||
), | ||
]; | ||
} | ||
|
||
/** | ||
* Upload Thumbnail. | ||
* | ||
* @param UploadedFile $file | ||
* @param array $options | ||
* @return Upload | ||
*/ | ||
protected function uploadThumbnail(UploadedFile $file, array $options = []) | ||
{ | ||
$ratio = $options['thumbnail']['ratio'] ?? false; | ||
$width = $options['thumbnail']['width'] ?? null; | ||
$height = $options['thumbnail']['height'] ?? null; | ||
unset($options['thumbnail']); | ||
|
||
if (!$width && !$height) { | ||
throw new \Exception('You have to set width or height thumbnail\'s option.'); | ||
} | ||
|
||
$thumbnail = Image::make($file); | ||
|
||
// Ratio or Fixed | ||
if ($ratio) { | ||
$thumbnail->resize( | ||
$width ?? null, | ||
$width > 0 ? null : $height, | ||
fn($constraint) => $constraint->aspectRatio() | ||
); | ||
} else { | ||
$thumbnail->resize($width, $height); | ||
} | ||
|
||
// Create Fake file | ||
$name = Str::random(37) . '.' . $file->extension(); | ||
|
||
File::ensureDirectoryExists(storage_path('app/public/pharaonic-thumbs')); | ||
$thumbnail->save(storage_path('app/public/pharaonic-thumbs/' . $name), 100); | ||
|
||
$file = $this->handle( | ||
new UploadedFile( | ||
storage_path('app/public/pharaonic-thumbs/' . $name), | ||
$file->getClientOriginalName() . '-thumbnail', | ||
$thumbnail->mime() | ||
), | ||
$options | ||
); | ||
|
||
// Delete Fake File | ||
File::delete(storage_path('app/public/pharaonic-thumbs/' . $name)); | ||
|
||
return $file; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Pharaonic\Laravel\Uploader\Actions; | ||
|
||
use Illuminate\Support\Facades\Storage; | ||
use Pharaonic\Laravel\Uploader\Models\Upload; | ||
|
||
class DeleteFile | ||
{ | ||
/** | ||
* Delete file with the thumbnail. | ||
* | ||
* @param Upload $file | ||
* @return void | ||
*/ | ||
public function handle(Upload $file) | ||
{ | ||
if ($file->thumbnail_id) { | ||
$file->thumbnail->delete(); | ||
} | ||
|
||
$fs = Storage::disk($file->disk); | ||
|
||
if (!$fs->exists($file->path)) { | ||
return; | ||
} | ||
|
||
return $fs->delete($file->path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Pharaonic\Laravel\Uploader\Classes; | ||
|
||
/** | ||
* @method array action() | ||
* @method string uri() | ||
*/ | ||
class Router | ||
{ | ||
/** | ||
* The options list. | ||
* | ||
* @var array | ||
*/ | ||
protected array $options; | ||
|
||
/** | ||
* Create a new Router instance. | ||
* | ||
* @param array $options | ||
*/ | ||
public function __construct(array $options) | ||
{ | ||
$this->options = $options; | ||
} | ||
|
||
/** | ||
* Get route action. | ||
* | ||
* @return array | ||
*/ | ||
public function action() | ||
{ | ||
return $this->options['controller']; | ||
} | ||
|
||
/** | ||
* Get route URI. | ||
* | ||
* @return string | ||
*/ | ||
public function uri() | ||
{ | ||
return $this->options['uri'] . '/{hash}'; | ||
} | ||
} |
Oops, something went wrong.