Skip to content

Commit

Permalink
Update UploadsController.php
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Pushkraj19 authored Nov 9, 2024
1 parent 07a81b7 commit 709aaea
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/Http/Controllers/UploadsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Canvas\Canvas;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class UploadsController extends Controller
{
Expand All @@ -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);
}

Expand All @@ -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);
}
}

0 comments on commit 709aaea

Please sign in to comment.