From 709aaea6e35c58ee2c868f43709c66971febd1ce Mon Sep 17 00:00:00 2001 From: Pushkraj Jori Date: Sat, 9 Nov 2024 11:01:52 +0530 Subject: [PATCH] Update UploadsController.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit store() Method: Extracts the file’s original name and generates a unique identifier using a shortened md5 hash based on the filename. Constructs a unique filename in the format of original-name-uniquehash.extension. Stores the file with the generated filename and returns the file URL. destroy() Method: Accepts a filename as a payload in the request body. Builds the storage path for the specified filename and attempts to delete it. Returns a 204 No Content response on successful deletion or a 400 response if no filename is provided. --- src/Http/Controllers/UploadsController.php | 31 ++++++++++++---------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/Http/Controllers/UploadsController.php b/src/Http/Controllers/UploadsController.php index f635c1113..a7375b34e 100644 --- a/src/Http/Controllers/UploadsController.php +++ b/src/Http/Controllers/UploadsController.php @@ -5,6 +5,7 @@ use Canvas\Canvas; use Illuminate\Routing\Controller; use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Str; class UploadsController extends Controller { @@ -21,22 +22,21 @@ public function store() return response()->json(null, 400); } - // Only grab the first element because single file uploads - // are not supported at this time + // Only grab the first element because single file uploads are not supported at this time $file = reset($payload); - // Use pathinfo to separate the filename and extension + // Generate a unique identifier based on a hash of the original filename $path_parts = pathinfo($file->getClientOriginalName()); - - $first_name = \Illuminate\Support\Str::kebab($path_parts['filename']); - - // Construct the new filename with time() before the extension - $name = $first_name . '-' . time() . '.' . $path_parts['extension']; + $first_name = Str::kebab($path_parts['filename']); + $unique_id = substr(md5($path_parts['filename']), 0, 8); + $filename = $first_name . '-' . $unique_id . '.' . $path_parts['extension']; - $path = $file->storeAs(Canvas::baseStoragePath(), $name, [ + // Store the file using the generated filename + $path = $file->storeAs(Canvas::baseStoragePath(), $filename, [ 'disk' => config('canvas.storage_disk'), ]); + // Return the URL of the stored file return Storage::disk(config('canvas.storage_disk'))->url($path); } @@ -47,18 +47,21 @@ public function store() */ public function destroy() { - if (empty(request()->getContent())) { + // Validate that a filename is provided + $filename = trim(request()->getContent()); + + if (empty($filename)) { return response()->json(null, 400); } - $file = pathinfo(request()->getContent()); - + // Define the expected storage path $storagePath = Canvas::baseStoragePath(); + $path = "{$storagePath}/{$filename}"; - $path = "{$storagePath}/{$file['basename']}"; - + // Delete the file from storage Storage::disk(config('canvas.storage_disk'))->delete($path); + // Return a 204 No Content response return response()->json([], 204); } }