Skip to content

Commit

Permalink
Added UploadsFiles triat 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 def767a commit dee767e
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 61 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions app/Contracts/Uploader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Contracts;

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

namespace App\Traits;

use Uploader;
use Illuminate\Http\UploadedFile;

trait UploadsFiles
{
/**
* Handle the upload for the uploader, and update it's attributes.
*
* @param Illuminate\Http\UploadedFile
*
* @return bool
*/
public function upload(UploadedFile $file)
{
$upload = Uploader::upload($this->getDirectory(), $file);

foreach ($upload as $attribute => $value) {
$this->attributes[$attribute] = $value;
}

return $this->update();
}
}
16 changes: 14 additions & 2 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
}
}
18 changes: 0 additions & 18 deletions app/Utils/Facades/Uploader.php

This file was deleted.

51 changes: 15 additions & 36 deletions app/Utils/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,6 @@

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 @@ -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'
]);
}
}
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: 3 additions & 1 deletion database/migrations/2014_10_12_000000_create_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit dee767e

Please sign in to comment.