From dee767ea143672b3b0c5610d319836a84898e452 Mon Sep 17 00:00:00 2001 From: Jovert Lota Palonpon Date: Sat, 30 Mar 2019 12:41:36 +0800 Subject: [PATCH] Added UploadsFiles triat for Model coupled upload #23 --- .env.example | 6 +++ app/Contracts/Uploader.php | 13 +++++ app/Providers/AppServiceProvider.php | 4 +- app/Traits/UploadsFiles.php | 27 ++++++++++ app/User.php | 16 +++++- app/Utils/Facades/Uploader.php | 18 ------- app/Utils/Uploader.php | 51 ++++++------------- config/app.php | 2 +- .../2014_10_12_000000_create_users_table.php | 4 +- 9 files changed, 80 insertions(+), 61 deletions(-) create mode 100644 app/Contracts/Uploader.php create mode 100644 app/Traits/UploadsFiles.php delete mode 100644 app/Utils/Facades/Uploader.php diff --git a/.env.example b/.env.example index 8ecde12..86e9a08 100755 --- a/.env.example +++ b/.env.example @@ -42,6 +42,12 @@ MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" FILESYSTEM_DRIVER=local +AWS_ACCESS_KEY_ID=null +AWS_SECRET_ACCESS_KEY=null +AWS_DEFAULT_REGION=null +AWS_BUCKET=null +AWS_URL=null + TELESCOPE_ENABLED=true JWT_SECRET=noclue diff --git a/app/Contracts/Uploader.php b/app/Contracts/Uploader.php new file mode 100644 index 0000000..4038ab1 --- /dev/null +++ b/app/Contracts/Uploader.php @@ -0,0 +1,13 @@ +bind('uploader', function () { - return new \App\Utils\Uploader; - }); + // } } diff --git a/app/Traits/UploadsFiles.php b/app/Traits/UploadsFiles.php new file mode 100644 index 0000000..a180be0 --- /dev/null +++ b/app/Traits/UploadsFiles.php @@ -0,0 +1,27 @@ +getDirectory(), $file); + + foreach ($upload as $attribute => $value) { + $this->attributes[$attribute] = $value; + } + + return $this->update(); + } +} diff --git a/app/User.php b/app/User.php index e429100..941c6f6 100755 --- a/app/User.php +++ b/app/User.php @@ -3,15 +3,17 @@ namespace App; use App\Traits\HasJWT; +use App\Contracts\Uploader; +use App\Traits\UploadsFiles; use Tymon\JWTAuth\Contracts\JWTSubject; use Illuminate\Notifications\Notifiable; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; -class User extends Authenticatable implements JWTSubject +class User extends Authenticatable implements JWTSubject, Uploader { - use Notifiable, SoftDeletes, HasJWT; + use Notifiable, SoftDeletes, HasJWT, UploadsFiles; /** * The attributes that are mass assignable. @@ -28,4 +30,14 @@ class User extends Authenticatable implements JWTSubject protected $hidden = [ 'password', 'remember_token', ]; + + /** + * Get the directory for uploads. + * + * @return string + */ + public function getDirectory() : string + { + return 'users/'.$this->getKey(); + } } diff --git a/app/Utils/Facades/Uploader.php b/app/Utils/Facades/Uploader.php deleted file mode 100644 index 9b807a2..0000000 --- a/app/Utils/Facades/Uploader.php +++ /dev/null @@ -1,18 +0,0 @@ -disk = config('filesystems.default'); - } - - /** - * Specify which storage driver will be used. - * - * @param string $name - * - * @return App\Utils\Uploader - */ - public function disk(string $name = 'public') - { - $this->disk = $name; - - return $this; - } - /** * Put the file into the storage. * @@ -41,38 +17,41 @@ public function disk(string $name = 'public') * * @return array */ - public function put(string $directory, UploadedFile $file) + public static function upload(string $directory, UploadedFile $file) { + $disk = config('filesystems.default'); $filename = str_random(64).'.'.$file->getClientOriginalExtension(); $original_filename = $file->getClientOriginalName(); - $path = Storage::disk($this->disk)->putFileAs( - $directory, - $file, - $filename - ); + // Upload the file + $path = Storage::putFileAs($directory, $file, $filename); + + $url = Storage::url($path); + $thumbnail_url = null; + // Do image manipulations if (Str::startsWith($file->getClientMimeType(), 'image')) { $thumbnailDirectory = "{$directory}/thumbnails"; + $thumbnailPath = "{$thumbnailDirectory}/{$filename}"; if (! Storage::exists($thumbnailDirectory)) { Storage::makeDirectory($thumbnailDirectory); } - $fileSystemRoot = config("filesystems.disks.{$this->disk}.root"); + $fileSystemRoot = config("filesystems.disks.{$disk}.root"); $fullPath = "{$fileSystemRoot}/{$path}"; - $thumbnailPath = + $fullThumbnailPath = "{$fileSystemRoot}/{$thumbnailDirectory}/{$filename}"; Image::make($fullPath) ->fit(240) - ->save($thumbnailPath, 95); - } + ->save($fullThumbnailPath, 95); - $path = Storage::url($path); + $thumbnail_url = Storage::url($thumbnailPath); + } return compact([ - 'directory', 'filename', 'original_filename', 'path' + 'directory', 'filename', 'original_filename', 'url', 'thumbnail_url' ]); } } diff --git a/config/app.php b/config/app.php index 9a3a818..921f1fb 100755 --- a/config/app.php +++ b/config/app.php @@ -198,8 +198,8 @@ */ 'aliases' => [ + 'Uploader' => App\Utils\Uploader::class, 'Image' => Intervention\Image\Facades\Image::class, - 'Uploader' => App\Utils\Facades\Uploader::class, 'App' => Illuminate\Support\Facades\App::class, 'Artisan' => Illuminate\Support\Facades\Artisan::class, diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index 7d350ae..fca47b1 100755 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -32,9 +32,11 @@ public function up() $table->date('birthdate', 'Y-m-d')->nullable(); $table->text('address')->nullable(); - $table->text('path')->nullable(); $table->string('directory')->nullable(); $table->string('filename')->nullable(); + $table->string('original_filename')->nullable(); + $table->text('url')->nullable(); + $table->text('thumbnail_url')->nullable(); $table->string('created_by')->nullable(); $table->string('updated_by')->nullable();