From 50af3bbe1ce1925e5c2263b5e59f7c30bdefd3b0 Mon Sep 17 00:00:00 2001 From: Axel Date: Wed, 17 May 2023 11:29:30 +0200 Subject: [PATCH] Fixes files deletion and listing of existing bundles --- app/Helpers/Upload.php | 17 ++- app/Http/Controllers/UploadController.php | 28 ++--- resources/views/homepage.blade.php | 22 ++-- resources/views/upload.blade.php | 123 ++++++++++++---------- 4 files changed, 92 insertions(+), 98 deletions(-) diff --git a/app/Helpers/Upload.php b/app/Helpers/Upload.php index bf4799b..313f1ce 100644 --- a/app/Helpers/Upload.php +++ b/app/Helpers/Upload.php @@ -24,7 +24,8 @@ class Upload { return []; } - public static function setMetadata(string $bundleId, array $metadata = []) { + public static function setMetadata(String $bundleId, Array $metadata = []) { + $origin = self::getMetadata($bundleId); $updated = array_merge($origin, $metadata); @@ -35,7 +36,7 @@ class Upload { return $updated; } - public static function addFileMetaData(string $bundleId, array $file) { + public static function addFileMetaData(String $bundleId, Array $file) { $metadata = self::getMetadata($bundleId); if (empty($metadata)) { @@ -45,15 +46,12 @@ class Upload { } array_push($metadata['files'], $file); - - if (! Storage::disk('uploads')->put($bundleId.'/bundle.json', json_encode($metadata))) { - throw new Exception('Cannot store metadata'); - } + self::setMetadata($bundleId, $metadata); return $metadata; } - public static function deleteFile(string $bundleId, string $uuid) { + public static function deleteFile(String $bundleId, String $uuid) { $metadata = self::getMetadata($bundleId); if (! empty($metadata['files'])) { @@ -65,10 +63,8 @@ class Upload { unset($metadata['files'][$key]); } } - } - if (! Storage::disk('uploads')->put($bundleId.'/bundle.json', json_encode($metadata))) { - throw new Exception('Cannot store metadata'); + self::setMetadata($bundleId, $metadata); } return $metadata; @@ -112,7 +108,6 @@ class Upload { return $min; } - public static function canUpload($current_ip) { // Getting the IP limit configuration diff --git a/app/Http/Controllers/UploadController.php b/app/Http/Controllers/UploadController.php index 761bd2e..01e627e 100644 --- a/app/Http/Controllers/UploadController.php +++ b/app/Http/Controllers/UploadController.php @@ -29,8 +29,6 @@ class UploadController extends Controller // The upload form public function storeBundle(Request $request, String $bundleId) { - $original = Upload::getMetadata($bundleId); - $metadata = [ 'expiry' => $request->expiry ?? null, 'password' => $request->password ?? null, @@ -49,15 +47,10 @@ class UploadController extends Controller public function uploadFile(Request $request, String $bundleId) { - // Getting metadata - $metadata = Upload::getMetadata($bundleId); - - // Validating file - abort_if(! $request->hasFile('file'), 422); - abort_if(! $request->file('file')->isValid(), 422); - - $this->validate($request, [ - 'file' => 'required|file|max:'.(Upload::fileMaxSize() / 1000) + // Validating form data + $request->validate([ + 'uuid' => 'required|uuid', + 'file' => 'required|file|max:'.(Upload::fileMaxSize() / 1000) ]); // Generating the file name @@ -72,7 +65,7 @@ class UploadController extends Controller // Generating file metadata $file = [ - 'uuid' => Str::uuid(), + 'uuid' => $request->uuid, 'original' => $original, 'filesize' => Storage::disk('uploads')->size($fullpath), 'fullpath' => $fullpath, @@ -84,7 +77,8 @@ class UploadController extends Controller $metadata = Upload::addFileMetaData($bundleId, $file); return response()->json([ - 'result' => true + 'result' => true, + 'file' => $file ]); } catch (Exception $e) { @@ -99,12 +93,12 @@ class UploadController extends Controller public function deleteFile(Request $request, String $bundleId) { - $metadata = Upload::getMetadata($bundleId); - - abort_if(empty($request->file), 422); + $request->validate([ + 'uuid' => 'required|uuid' + ]); try { - $metadata = Upload::deleteFile($bundleId, $request->file); + $metadata = Upload::deleteFile($bundleId, $request->uuid); return response()->json($metadata); } catch (Exception $e) { diff --git a/resources/views/homepage.blade.php b/resources/views/homepage.blade.php index 090baaf..926cf23 100644 --- a/resources/views/homepage.blade.php +++ b/resources/views/homepage.blade.php @@ -6,6 +6,8 @@ document.addEventListener('alpine:init', () => { Alpine.data('bundle', () => ({ bundles: null, + active: null, + expired: null, currentBundle: null, init: function() { @@ -15,25 +17,21 @@ this.bundles = JSON.parse(bundles) if (this.bundles != null && Object.keys(this.bundles).length > 0) { - this.bundles.active = [] - this.bundles.expired = [] + this.active = [] + this.expired = [] this.bundles.forEach( (bundle) => { if (bundle.title == null || bundle.title == '') { bundle.title = 'untitled' } - //bundle.title += ' - '+Object.keys(bundle.files).length+' {{ __('app.files') }} - {{ __('app.created-at') }} '+moment.unix(bundle.created_at).fromNow() - if (bundle.expires_at != null && moment.unix(bundle.expires_at).isBefore(moment())) { - this.bundles.expired.push(bundle) + this.expired.push(bundle) } else { - this.bundles.active.push(bundle) + this.active.push(bundle) } }) - - console.log(this.bundles) } // If bundle is empty, initializing it @@ -126,17 +124,17 @@ > -