Skip to content

Commit

Permalink
switched storage from s3 to google drive
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyguy73 committed Dec 10, 2019
1 parent aef9320 commit 99a018e
Show file tree
Hide file tree
Showing 12 changed files with 983 additions and 352 deletions.
11 changes: 7 additions & 4 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public function store(Request $request)
'directions' => 'required',
'image' => 'file|max:40000|mimes:jpeg,gif,png,svg,bmp',
]);
$path = empty(request('image')) ? null : $request->file('image')->store(auth()->id(), 's3');

$path = empty(request('image')) ? null : $request->file('image')->store(null, 'google');

Post::create([
'title' => request('title'),
'ingredients' => request('ingredients'),
Expand Down Expand Up @@ -115,10 +117,11 @@ public function update(Request $request, Post $post)
]);
if (!empty(request('image'))) {
if(!is_null($post->image)) {
$file = $post->image;
Storage::disk('s3')->delete($file);
$file = collect(Storage::disk('google')->getAdapter()->listContents(''))
->where('name', '=', $post->image)->first();
Storage::disk('google')->delete($file['path']);
}
$path = $request->file('image')->store(auth()->id(), 's3');
$path = $request->file('image')->store(null, 'google');
$post->image = $path;
}
$post->title = request('title');
Expand Down
9 changes: 9 additions & 0 deletions app/Providers/GoogleDriveAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace App\Providers;
class GoogleDriveAdapter extends \Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter
{
public function getService()
{
return $this->service;
}
}
36 changes: 36 additions & 0 deletions app/Providers/GoogleDriveServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class GoogleDriveServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
\Storage::extend('google', function($app, $config) {
$client = new \Google_Client();
$client->setClientId($config['clientId']);
$client->setClientSecret($config['clientSecret']);
$client->refreshToken($config['refreshToken']);
$service = new \Google_Service_Drive($client);
$options = [];
if(isset($config['teamDriveId'])) {
$options['teamDriveId'] = $config['teamDriveId'];
}
$adapter = new GoogleDriveAdapter($service, $config['folderId'], $options);
return new \League\Flysystem\Filesystem($adapter);
});
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"laravel/framework": "^5.8.0",
"laravel/tinker": "~1.0",
"league/flysystem-aws-s3-v3": "^1.0",
"nao-pon/flysystem-google-drive": "^1.1",
"uxweb/sweet-alert": "^1.4"
},
"require-dev": {
Expand Down
Loading

0 comments on commit 99a018e

Please sign in to comment.