Skip to content

Commit

Permalink
wip #23
Browse files Browse the repository at this point in the history
  • Loading branch information
Jovert Lota Palonpon committed Mar 29, 2019
1 parent a24e8b9 commit def767a
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

FILESYSTEM_DRIVER=local

TELESCOPE_ENABLED=true

JWT_SECRET=noclue
Expand Down
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;
});
}
}
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';
}
}
78 changes: 78 additions & 0 deletions app/Utils/Uploader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace App\Utils;

use Image;
use Storage;
use Illuminate\Support\Str;
use Illuminate\Http\UploadedFile;

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.
*
* @param string $directory
* @param Illuminate\Http\UploadedFile $file
*
* @return array
*/
public function put(string $directory, UploadedFile $file)
{
$filename = str_random(64).'.'.$file->getClientOriginalExtension();
$original_filename = $file->getClientOriginalName();

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

if (Str::startsWith($file->getClientMimeType(), 'image')) {
$thumbnailDirectory = "{$directory}/thumbnails";

if (! Storage::exists($thumbnailDirectory)) {
Storage::makeDirectory($thumbnailDirectory);
}

$fileSystemRoot = config("filesystems.disks.{$this->disk}.root");
$fullPath = "{$fileSystemRoot}/{$path}";
$thumbnailPath =
"{$fileSystemRoot}/{$thumbnailDirectory}/{$filename}";

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

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

return compact([
'directory', 'filename', 'original_filename', 'path'
]);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"beyondcode/laravel-self-diagnosis": "^1.1",
"fideloper/proxy": "^4.0",
"fzaninotto/faker": "^1.4",
"intervention/image": "^2.4",
"laravel/framework": "5.8.*",
"laravel/telescope": "^2.0",
"laravel/tinker": "^1.0",
Expand Down
229 changes: 228 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit def767a

Please sign in to comment.