Skip to content

Commit

Permalink
Added UploadsFiles trait for Model coupled upload #23
Browse files Browse the repository at this point in the history
  • Loading branch information
Jovert Lota Palonpon committed Mar 30, 2019
1 parent a21d49f commit d78e6f2
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 80 deletions.
6 changes: 0 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ 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
Expand Down
13 changes: 0 additions & 13 deletions app/Contracts/Uploader.php

This file was deleted.

4 changes: 3 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function boot()
*/
public function register()
{
//
app()->bind('uploader', function () {
return new \App\Utils\Uploader;
});
}
}
27 changes: 0 additions & 27 deletions app/Traits/UploadsFiles.php

This file was deleted.

16 changes: 2 additions & 14 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
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, Uploader
class User extends Authenticatable implements JWTSubject
{
use Notifiable, SoftDeletes, HasJWT, UploadsFiles;
use Notifiable, SoftDeletes, HasJWT;

/**
* The attributes that are mass assignable.
Expand All @@ -30,14 +28,4 @@ class User extends Authenticatable implements JWTSubject, Uploader
protected $hidden = [
'password', 'remember_token',
];

/**
* Get the directory for uploads.
*
* @return string
*/
public function getDirectory() : string
{
return 'users/'.$this->getKey();
}
}
18 changes: 18 additions & 0 deletions app/Utils/Facades/Uploader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Utils\Facades;

use Illuminate\Support\Facades\Facade;

class Uploader extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'uploader';
}
}
51 changes: 36 additions & 15 deletions app/Utils/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,30 @@

class Uploader
{
/**
* @var string The storage driver.
*/
protected $disk;

public function __construct()
{
$this->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.
*
Expand All @@ -17,41 +41,38 @@ class Uploader
*
* @return array
*/
public static function upload(string $directory, UploadedFile $file)
public function put(string $directory, UploadedFile $file)
{
$disk = config('filesystems.default');
$filename = str_random(64).'.'.$file->getClientOriginalExtension();
$original_filename = $file->getClientOriginalName();

// Upload the file
$path = Storage::putFileAs($directory, $file, $filename);

$url = Storage::url($path);
$thumbnail_url = null;
$path = Storage::disk($this->disk)->putFileAs(
$directory,
$file,
$filename
);

// 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.{$disk}.root");
$fileSystemRoot = config("filesystems.disks.{$this->disk}.root");
$fullPath = "{$fileSystemRoot}/{$path}";
$fullThumbnailPath =
$thumbnailPath =
"{$fileSystemRoot}/{$thumbnailDirectory}/{$filename}";

Image::make($fullPath)
->fit(240)
->save($fullThumbnailPath, 95);

$thumbnail_url = Storage::url($thumbnailPath);
->save($thumbnailPath, 95);
}

$path = Storage::url($path);

return compact([
'directory', 'filename', 'original_filename', 'url', 'thumbnail_url'
'directory', 'filename', 'original_filename', 'path'
]);
}
}
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions database/migrations/2014_10_12_000000_create_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ 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();
Expand Down

0 comments on commit d78e6f2

Please sign in to comment.