From 0f98e729ceaa7c2442fb186372560b65e951ed82 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Mon, 2 Apr 2018 17:34:58 -0500 Subject: [PATCH] Save file asset --- app/Http/Controllers/Admin/FileController.php | 20 ++++------ app/Services/FileService.php | 40 ++++++++++++++----- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/app/Http/Controllers/Admin/FileController.php b/app/Http/Controllers/Admin/FileController.php index 4fc88548..f4d4ff37 100644 --- a/app/Http/Controllers/Admin/FileController.php +++ b/app/Http/Controllers/Admin/FileController.php @@ -29,6 +29,7 @@ class FileController extends Controller * Store a newly file * @param Request $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector + * @throws \Hashids\HashidsException */ public function store(Request $request) { @@ -49,19 +50,14 @@ class FileController extends Controller Log::info('Uploading files', $attrs); + $file = $request->file('file'); - $file_path = $this->fileSvc->saveFile($file, 'files'); - - $asset = new File(); - $asset->name = $attrs['filename']; - $asset->description = $attrs['file_description']; - $asset->disk = config('filesystems.public_files'); - $asset->path = $file_path; - $asset->public = false; // need to be logged in to see. default (for now) - $asset->ref_model = $attrs['ref_model']; - $asset->ref_model_id = $attrs['ref_model_id']; - - $asset->save(); + $this->fileSvc->saveFile($file, 'files', [ + 'name' => $attrs['filename'], + 'description' => $attrs['file_descriptoin'], + 'ref_model' => $attrs['ref_model'], + 'ref_model_id' => $attrs['ref_model_id'], + ]); Flash::success('Files uploaded successfully.'); return redirect()->back(); diff --git a/app/Services/FileService.php b/app/Services/FileService.php index eaf71a30..02f7322d 100644 --- a/app/Services/FileService.php +++ b/app/Services/FileService.php @@ -3,6 +3,7 @@ namespace App\Services; use App\Interfaces\Service; +use App\Models\File; /** * Class FileService @@ -11,20 +12,41 @@ use App\Interfaces\Service; class FileService extends Service { /** - * Save a file to disk and return the path + * Save a file to disk and return a File asset * @param \Illuminate\Http\UploadedFile $file * @param string $folder - * @param string|null $disk - * @return string + * @param array $attrs + * @return File + * @throws \Hashids\HashidsException */ - public function saveFile($file, $folder, $disk = null): string + public function saveFile($file, $folder, array $attrs) { - if (!$disk) { - $disk = config('filesystems.public_files'); - } + $attrs = array_merge([ + 'name' => '', + 'description' => '', + 'public' => false, + 'ref_model' => '', + 'ref_model_id' => '', + 'disk' => config('filesystems.public_files'), + ], $attrs); + $id = File::createNewHashId(); $path_info = pathinfo($file->getClientOriginalName()); - $filename = str_slug($path_info['filename']).'.'.$path_info['extension']; - return $file->storeAs($folder, $filename, $disk); + + # Create the file, add the ID to the front of the file to account + # for any duplicate filenames, but still can be found in an `ls` + + $filename = $id . '_' + . str_slug(trim($path_info['filename'])) + . '.' . $path_info['extension']; + + $file_path = $file->storeAs($folder, $filename, $attrs['disk']); + + $asset = new File($attrs); + $asset->id = $id; + $asset->path = $file_path; + $asset->save(); + + return $asset; } }