From 6ce5e91624927160a865e67ec6febf6d6b78612a Mon Sep 17 00:00:00 2001 From: ro Date: Mon, 11 Mar 2024 16:32:38 -0600 Subject: [PATCH] file upload service added ported over the new file uploader from the old build and made it a service make for from some additonal file processing i.e. image optimization or video converstion before it is saved to the system --- .../API/FileUploadAPIController.php | 22 ++++++ app/Providers/FipamoServiceProvider.php | 5 ++ app/Services/FileUploadService.php | 77 +++++++++++++++++++ .../assets/scripts/dash/app/ui/FileManager.js | 2 +- routes/api.php | 2 + 5 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 app/Http/Controllers/API/FileUploadAPIController.php create mode 100644 app/Services/FileUploadService.php diff --git a/app/Http/Controllers/API/FileUploadAPIController.php b/app/Http/Controllers/API/FileUploadAPIController.php new file mode 100644 index 0000000..536313f --- /dev/null +++ b/app/Http/Controllers/API/FileUploadAPIController.php @@ -0,0 +1,22 @@ +upload = $fileUploadService; + } + + public function upload(Request $request, $type = null) + { + return $this->upload->handleFile($request, $type); + } +} diff --git a/app/Providers/FipamoServiceProvider.php b/app/Providers/FipamoServiceProvider.php index da23647..7579e41 100644 --- a/app/Providers/FipamoServiceProvider.php +++ b/app/Providers/FipamoServiceProvider.php @@ -11,6 +11,7 @@ use App\Services\ContentService; use App\Services\PaginateService; use App\Services\ThemeService; use App\Services\DocService; +use App\Services\FileUploadService; class FipamoServiceProvider extends ServiceProvider { @@ -47,6 +48,10 @@ class FipamoServiceProvider extends ServiceProvider $this->app->bind(DocService::class, function ($app) { return new DocService(); }); + + $this->app->bind(FileUploadService::class, function ($app) { + return new FileUploadService(); + }); } /** diff --git a/app/Services/FileUploadService.php b/app/Services/FileUploadService.php new file mode 100644 index 0000000..a98d6fd --- /dev/null +++ b/app/Services/FileUploadService.php @@ -0,0 +1,77 @@ +getUploadedFiles(); //grab uploaded files + //$options = $request->getParsedBody(); + //$file = $upload['upload_files'][0]; + //front end sends one by one for progress tracking, so grab first + $file = $request->file('upload_files'); + $type = $file[0]->extension(); + $public_path = '../public'; + $filesPath = ''; + $path = date('Y') . '/' . date('m'); + $response = []; + switch ($type) { + case 'jpeg': + case 'jpg': + case 'png': + case 'gif': + if (isset($options["source"])) { + if ($options["source"] == "avatar-upload") { + $filesPath = '/assets/images/user/' . $path . '/'; + //Member::updateData('avi', $filesPath . $file->getClientFileName()); + } else { + $filesPath = '/assets/images/user/' . $path . '/'; + // Settings::updateGlobalData('background', $filesPath . '/' . $file->getClientFileName()); + } + } else { + $filesPath = '/assets/images/blog/' . $path . '/'; + } + + break; + case 'video/mp4': + $filesPath = '/assets/video/blog/' . $path . '/'; + break; + case 'audio/mpeg': + $filesPath = '/assets/sound/blog/' . $path . '/'; + break; + case 'application/pdf': + case 'text/plain': + case 'text/rtf': + $filesPath = '/assets/docs/blog/' . $path . '/'; + break; + } + + //FileUploader::uploadFile('../public' . $filesPath, $file); + $response = []; + try { + // if does not exist, so lets create it. + //var_dump($filesPath); + if (!is_dir($public_path . $filesPath)) { + mkdir($public_path . $filesPath, 0755, true); + } + $filename = urlencode($file[0]->getClientOriginalName()); + $file[0]->move($public_path . $filesPath, $filename); + } catch (RuntimeException $e) { + echo 'ERROR ' . $e->getMessage(); + } + + $response = [ + 'message' => "File Uploaded. Great!", + "filePath" => $filesPath . urlencode($file[0]->getClientOriginalName()), + "fileName" => urlencode($file[0]->getClientOriginalName()), + 'type' => $type, + ]; + + return $response; + } +} diff --git a/public/assets/scripts/dash/app/ui/FileManager.js b/public/assets/scripts/dash/app/ui/FileManager.js index 08628bd..411b113 100644 --- a/public/assets/scripts/dash/app/ui/FileManager.js +++ b/public/assets/scripts/dash/app/ui/FileManager.js @@ -117,7 +117,7 @@ export default class FileManager { self.mm .filesUpload(theFile.type, upload, progress) .then(result => { - item.setAttribute('data-source', result.filePath); + item.setAttribute('data-id', result.filePath); item.style.background = 'url(' + f.target.result + diff --git a/routes/api.php b/routes/api.php index 4232598..0ec8da1 100644 --- a/routes/api.php +++ b/routes/api.php @@ -3,6 +3,7 @@ use Illuminate\Support\Facades\Route; use App\Http\Controllers\API\AuthAPIController; use App\Http\Controllers\API\PageAPIController; +use App\Http\Controllers\API\FileUploadAPIController; /* |-------------------------------------------------------------------------- @@ -18,3 +19,4 @@ use App\Http\Controllers\API\PageAPIController; //check if session is active Route::get("/v1/status", [AuthAPIController::class, 'status']); Route::put("/v1/page/write", [PageAPIController::class, 'write']); +Route::post("/v1/files", [FileUploadAPIController::class, 'upload']);