From 93046eea6294554c765715b45e26e88000545b65 Mon Sep 17 00:00:00 2001 From: Patrik Leifert Date: Sat, 8 Jun 2024 21:49:14 +0200 Subject: [PATCH] File manager changes: - Modify storage driver name and location - Add option to select patchline - Add option to mark file for delete - Add option to delete file from the server - Small UI adjustments to new added options --- dist/app/Enums/Launcher/FileAction.php | 9 ++ dist/app/Enums/Launcher/Patchline.php | 20 +++ .../Controllers/Web/GameFileController.php | 49 +++++++- dist/app/Models/GameFile.php | 12 ++ dist/config/filesystems.php | 8 +- ...4_02_24_191110_create_game_files_table.php | 2 +- ...4_04_15_222616_modify_game_files_table.php | 30 +++++ dist/resources/views/file-manager.blade.php | 117 +++++++++++------- dist/routes/admin.php | 6 +- 9 files changed, 194 insertions(+), 59 deletions(-) create mode 100644 dist/app/Enums/Launcher/FileAction.php create mode 100644 dist/app/Enums/Launcher/Patchline.php create mode 100644 dist/database/migrations/2024_04_15_222616_modify_game_files_table.php diff --git a/dist/app/Enums/Launcher/FileAction.php b/dist/app/Enums/Launcher/FileAction.php new file mode 100644 index 0000000..3f873d9 --- /dev/null +++ b/dist/app/Enums/Launcher/FileAction.php @@ -0,0 +1,9 @@ +name === $name) { + return $case; + } + } + return null; + } +} diff --git a/dist/app/Http/Controllers/Web/GameFileController.php b/dist/app/Http/Controllers/Web/GameFileController.php index 693761e..d600a25 100644 --- a/dist/app/Http/Controllers/Web/GameFileController.php +++ b/dist/app/Http/Controllers/Web/GameFileController.php @@ -2,11 +2,13 @@ namespace App\Http\Controllers\Web; +use App\Enums\Launcher\Patchline; use App\Http\Controllers\Controller; use Illuminate\Contracts\View\View; use Illuminate\Http\Request; use App\Models\GameFile; use Illuminate\Auth\Access\AuthorizationException; +use Illuminate\Http\RedirectResponse; use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\Storage; @@ -21,15 +23,15 @@ public function store(Request $request) // Validate the incoming request with appropriate rules for file uploads $request->validate([ 'files.*' => 'required|file', + 'patchline' => 'required|integer', 'gamepath.*' => 'required|string', ]); - //dd($request); - $duplicateFiles = []; $overwrittenFiles = []; $uploadedFiles = []; + // Handle file upload logic if ($request->hasFile('files')) { for($i = 0; $i < count($request->file('files')); $i++) { @@ -38,7 +40,7 @@ public function store(Request $request) $filename = $file->getClientOriginalName(); $filehash = str(hash_file('sha256', $file->getRealPath()))->upper(); - $gameFile = GameFile::where('name', $filename)->first() ?? new GameFile; + $gameFile = GameFile::where('name', $filename)->where('patchline', $request->input('patchline'))->first() ?? new GameFile; if ($gameFile->hash == $filehash) { $duplicateFiles[] = $gameFile->name; @@ -51,10 +53,13 @@ public function store(Request $request) $gameFile->name = $filename; $gameFile->hash = $filehash; + $gameFile->patchline = $request->input('patchline'); - $file->storeAs('', $gameFile->name, ['disk' => 'dg_public']); + $file->storeAs(str($gameFile->patchline->name)->lower(), $gameFile->name, ['disk' => 'patches']); $uploadedFiles[] = $gameFile->name; + $gameFile->game_path = $request->game_path[$i]; + $gameFile->action = $request->file_action[$i]; $gameFile->save(); } } @@ -83,11 +88,43 @@ public function store(Request $request) //return response()->json(['message' => 'Files uploaded successfully']); } - public function index() : View { + public function index(Request $request) : View { if (!auth()->user()->can(\App\Enums\Auth\Permissions::FILE_UPLOAD->value)) { throw new AuthorizationException(); } - return view('file-manager', ['files' => GameFile::latest()->get()]); + $patchline = $request->input('patchline') ?? 0; + + return view('file-manager', ['files' => GameFile::latest()->where('patchline', $patchline)->get()]); + } + + public function update(GameFile $file_manager) : RedirectResponse { + if (!auth()->user()->can(\App\Enums\Auth\Permissions::FILE_UPLOAD->value)) { + throw new AuthorizationException(); + } + + $file_manager->action = (int)!$file_manager->action->value; + + if($file_manager->save()) { + Session::flash('alert-success', 'File ' . $file_manager->name . ' was successfully marked for ' . ($file_manager->action->value ? 'add' : 'delete')); + } else { + Session::flash('alert-error', 'Failed to mark file ' . $file_manager->name . ' for ' . ($file_manager->action->value ? 'add' : 'delete')); + } + + return redirect()->back(); + } + + public function destroy(GameFile $file_manager) : RedirectResponse { + if (!auth()->user()->can(\App\Enums\Auth\Permissions::FILE_UPLOAD->value)) { + throw new AuthorizationException(); + } + + $file_manager->delete(); + + Storage::disk('patches')->delete(str($file_manager->patchline->name)->lower() . '/' . $file_manager->name); + + Session::flash('alert-success', 'File ' . $file_manager->name . ' was successfully deleted'); + + return redirect()->back(); } } diff --git a/dist/app/Models/GameFile.php b/dist/app/Models/GameFile.php index 8c460fd..3bbfbaf 100644 --- a/dist/app/Models/GameFile.php +++ b/dist/app/Models/GameFile.php @@ -2,6 +2,8 @@ namespace App\Models; +use App\Enums\Launcher\FileAction; +use App\Enums\Launcher\Patchline; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -11,4 +13,14 @@ class GameFile extends Model { use HasFactory; + + /** + * The attributes that should be cast. + * + * @var array + */ + protected $casts = [ + 'patchline' => Patchline::class, + 'action' => FileAction::class, + ]; } diff --git a/dist/config/filesystems.php b/dist/config/filesystems.php index 548a264..6837cf0 100644 --- a/dist/config/filesystems.php +++ b/dist/config/filesystems.php @@ -44,15 +44,9 @@ 'throw' => false, ], - 'dg_public' => [ - 'driver' => 'local', - 'root' => public_path().'/game-files', - 'visibility' => 'public' - ], - 'patches' => [ 'driver' => 'local', - 'root' => resource_path('patches'), + 'root' => storage_path('app/patches'), 'throw' => false, ], ], diff --git a/dist/database/migrations/2024_02_24_191110_create_game_files_table.php b/dist/database/migrations/2024_02_24_191110_create_game_files_table.php index cc14bd6..e7d1ca9 100644 --- a/dist/database/migrations/2024_02_24_191110_create_game_files_table.php +++ b/dist/database/migrations/2024_02_24_191110_create_game_files_table.php @@ -17,7 +17,7 @@ public function up(): void $table->string('game_path'); $table->string('hash', 64); // SHA-256 hash is 64 characters long $table->integer('version')->default(1); // Default version is 1 - $table->tinyInteger('action')->default(1); //add = 1, delete = 2 + $table->tinyInteger('action')->default(1); //add = 1, delete = 0 $table->timestamps(); $table->unique(['name', 'hash', 'version']); diff --git a/dist/database/migrations/2024_04_15_222616_modify_game_files_table.php b/dist/database/migrations/2024_04_15_222616_modify_game_files_table.php new file mode 100644 index 0000000..f2ad1ab --- /dev/null +++ b/dist/database/migrations/2024_04_15_222616_modify_game_files_table.php @@ -0,0 +1,30 @@ +integer('patchline')->default(1)->change(); + }); + Schema::table('game_files', function (Blueprint $table) { + $table->renameColumn('patchline', 'version'); + }); + } +}; diff --git a/dist/resources/views/file-manager.blade.php b/dist/resources/views/file-manager.blade.php index 1c6ac87..fe751d0 100644 --- a/dist/resources/views/file-manager.blade.php +++ b/dist/resources/views/file-manager.blade.php @@ -1,17 +1,30 @@ - +

Deathgarden file manager

+ +
+
+ + +
+
+ @if(Session::has('alert-error')) - {!! Session::get('alert-error') !!} + {!! Session::get('alert-error') !!} @endif @if(Session::has('alert-success')) - {!! Session::get('alert-success') !!} + {!! Session::get('alert-success') !!} @endif @if(Session::has('alert-warning')) - {!! Session::get('alert-warning') !!} + {!! Session::get('alert-warning') !!} @endif + +
-
+
Name @@ -22,25 +35,34 @@ @foreach($files as $file) - - + + {{ $file->name }} {{ $file->hash }} - @if(Storage::disk('dg_public')->exists($file->name)) - {{ Storage::disk('dg_public')->size($file->name) / 1000 }} kB + @if(Storage::disk('patches')->exists(str($file->patchline->name)->lower().'/'.$file->name)) + {{ Storage::disk('patches')->size(str($file->patchline->name).'/'.$file->name) / 1000 }} kB @else - Error while fetching file + Error while fetching file @endif {{ $file->updated_at }} - +
+ @method('PUT') + @csrf + +
+
+ @method('DELETE') + @csrf + +
@endforeach @@ -48,14 +70,19 @@
-
-
+
+ + @csrf
-
+
+
@@ -81,19 +108,19 @@ - + \ No newline at end of file diff --git a/dist/routes/admin.php b/dist/routes/admin.php index e8829b0..7e84582 100644 --- a/dist/routes/admin.php +++ b/dist/routes/admin.php @@ -12,8 +12,10 @@ Route::get('dashboard', [DashboardController::class, 'dashboard'])->name('admin.dashboard'); -Route::get('file-manager', [GameFileController::class, 'index'])->name(FileManagerController::class); -Route::post('file-manager', [GameFileController::class, 'store'])->name('file.store'); +//Route::get('file-manager', [GameFileController::class, 'index'])->name(FileManagerController::class); +//Route::get('file-manager', [GameFileController::class, 'getPatchline'])->name('file.patchline'); +//Route::post('file-manager', [GameFileController::class, 'store'])->name('file.store'); +Route::resource('file-manager', GameFileController::class)->except(['edit', 'create', 'show']); Route::get('gamenews', [GameNewsController::class, 'index'])->name(GameNewsController::class); Route::post('gamenews/create', [GameNewsController::class, 'create'])->name('gamenews.create');