From ddd18c7ea6f872540c293f40d99c987842ac0f6f Mon Sep 17 00:00:00 2001 From: cuneytsenturk Date: Tue, 2 Jan 2018 16:22:30 +0300 Subject: [PATCH] Invoice path --- app/Http/Controllers/Common/Uploads.php | 34 +++++++++++-------- app/Http/Controllers/Incomes/Invoices.php | 2 +- app/Http/Controllers/Items/Items.php | 4 +-- app/Models/Common/Media.php | 2 +- app/Traits/Uploads.php | 6 ++-- .../views/incomes/invoices/show.blade.php | 2 +- routes/web.php | 4 +-- 7 files changed, 29 insertions(+), 25 deletions(-) diff --git a/app/Http/Controllers/Common/Uploads.php b/app/Http/Controllers/Common/Uploads.php index 0b3a975c2..26b39e473 100644 --- a/app/Http/Controllers/Common/Uploads.php +++ b/app/Http/Controllers/Common/Uploads.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Common; use App\Http\Controllers\Controller; use App\Models\Common\Media; use Storage; +use File; class Uploads extends Controller { @@ -15,10 +16,12 @@ class Uploads extends Controller * @param $file * @return boolean|Response */ - public function get($folder, $file) + public function get($id) { + $media = Media::find($id); + // Get file path - if (!$path = $this->getPath($folder, $file)) { + if (!$path = $this->getPath($media)) { return false; } @@ -32,10 +35,12 @@ class Uploads extends Controller * @param $file * @return boolean|Response */ - public function download($folder, $file) + public function download($id) { + $media = Media::find($id); + // Get file path - if (!$path = $this->getPath($folder, $file)) { + if (!$path = $this->getPath($media)) { return false; } @@ -49,21 +54,23 @@ class Uploads extends Controller * @param $file * @return callable */ - public function destroy($folder, $id) + public function destroy($id) { $media = Media::find($id); // Get file path - /*if (!$path = $this->getPath($folder, $id)) { - $message = trans('messages.warning.deleted', ['name' => $file, 'text' => $file]); + if (!$path = $this->getPath($media)) { + $message = trans('messages.warning.deleted', ['name' => $media->basename, 'text' => $media->basename]); flash($message)->warning(); return back(); - }*/ + } $media->delete(); //will not delete files + File::delete($path); + return back(); } @@ -74,14 +81,13 @@ class Uploads extends Controller * @param $file * @return boolean|string */ - protected function getPath($folder, $file) + protected function getPath($media) { - // Add company id - if ($folder != 'users') { - $folder = session('company_id') . '/' . $folder; - } + $path = $media->basename; - $path = $folder . '/' . $file; + if (!empty($media->directory)) { + $path = $media->directory . '/' . $media->basename; + } if (!Storage::exists($path)) { return false; diff --git a/app/Http/Controllers/Incomes/Invoices.php b/app/Http/Controllers/Incomes/Invoices.php index 40215ef9e..870c7625f 100644 --- a/app/Http/Controllers/Incomes/Invoices.php +++ b/app/Http/Controllers/Incomes/Invoices.php @@ -416,7 +416,7 @@ class Invoices extends Controller if ($request->file('attachment')) { $media = $this->getMedia($request->file('attachment'), 'invoices'); - $invoice->syncMedia($media, 'attachment'); + $invoice->attachMedia($media, 'attachment'); } // Delete previous invoice totals diff --git a/app/Http/Controllers/Items/Items.php b/app/Http/Controllers/Items/Items.php index 075d2a6c6..c9d7b3ad0 100644 --- a/app/Http/Controllers/Items/Items.php +++ b/app/Http/Controllers/Items/Items.php @@ -56,7 +56,7 @@ class Items extends Controller $item = Item::create($request->input()); // Upload picture - if ($media) { + if ($request->file('picture')) { $media = $this->getMedia($request->file('picture'), 'items'); $item->attachMedia($media, 'picture'); @@ -141,7 +141,7 @@ class Items extends Controller $item->update($request->input()); // Upload picture - if ($media) { + if ($request->file('picture')) { $media = $this->getMedia($request->file('picture'), 'items'); $item->syncMedia($media, 'picture'); diff --git a/app/Models/Common/Media.php b/app/Models/Common/Media.php index 744d67644..517b12394 100644 --- a/app/Models/Common/Media.php +++ b/app/Models/Common/Media.php @@ -11,4 +11,4 @@ class Media extends PMedia protected $dates = ['deleted_at']; -} \ No newline at end of file +} diff --git a/app/Traits/Uploads.php b/app/Traits/Uploads.php index 07c5074ec..a4364158b 100644 --- a/app/Traits/Uploads.php +++ b/app/Traits/Uploads.php @@ -36,10 +36,8 @@ trait Uploads $company_id = session('company_id'); } - $path = config('filesystems.disks.uploads.root') . '/' . $company_id . '/' . $folder; + $path = $company_id . '/' . $folder; - config(['filesystems.disks.uploads.root' => $path]); - - return MediaUploader::fromSource($file)->upload(); + return MediaUploader::fromSource($file)->toDirectory($path)->upload(); } } diff --git a/resources/views/incomes/invoices/show.blade.php b/resources/views/incomes/invoices/show.blade.php index 18109d0bb..e0d324c4a 100644 --- a/resources/views/incomes/invoices/show.blade.php +++ b/resources/views/incomes/invoices/show.blade.php @@ -189,7 +189,7 @@ {!! Form::open([ 'id' => 'attachment-' . $invoice->attachment->id, 'method' => 'DELETE', - 'url' => [url('uploads/invoices/' . $invoice->attachment->id)], + 'url' => [url('uploads/' . $invoice->attachment->id)], 'style' => 'display:inline' ]) !!} {{ Form::hidden('id', $invoice->id) }} diff --git a/routes/web.php b/routes/web.php index ecd605e03..c8802ee7c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -9,8 +9,8 @@ Route::group(['middleware' => 'language'], function () { Route::group(['middleware' => 'auth'], function () { Route::group(['prefix' => 'uploads'], function () { - Route::get('{folder}/{file}', 'Common\Uploads@get'); - Route::get('{folder}/{file}/download', 'Common\Uploads@download'); + Route::get('{id}', 'Common\Uploads@get'); + Route::get('{id}/download', 'Common\Uploads@download'); Route::delete('{id}', 'Common\Uploads@destroy'); });