From ba1f7e7e2cb98f2384f12d8d73425a0d45f18702 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sun, 26 Jul 2020 22:05:52 -0600 Subject: [PATCH] Add MediaBlocklist feature --- .../Admin/AdminMediaController.php | 14 +- .../Controllers/MediaBlocklistController.php | 50 +++++++ app/MediaBlocklist.php | 10 ++ ...5_230100_create_media_blocklists_table.php | 37 +++++ resources/views/admin/media/home.blade.php | 132 ++++++++++++------ resources/views/admin/media/show.blade.php | 5 +- routes/web.php | 2 + 7 files changed, 206 insertions(+), 44 deletions(-) create mode 100644 app/Http/Controllers/MediaBlocklistController.php create mode 100644 app/MediaBlocklist.php create mode 100644 database/migrations/2020_07_25_230100_create_media_blocklists_table.php diff --git a/app/Http/Controllers/Admin/AdminMediaController.php b/app/Http/Controllers/Admin/AdminMediaController.php index 736922813..eb4bd93a8 100644 --- a/app/Http/Controllers/Admin/AdminMediaController.php +++ b/app/Http/Controllers/Admin/AdminMediaController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin; use DB, Cache; use App\{ Media, + MediaBlocklist, Profile, Status }; @@ -21,8 +22,8 @@ trait AdminMediaController 'nullable', 'string', 'min:1', - 'max:4', - Rule::in(['grid','list']) + 'max:13', + Rule::in(['grid','list', 'banned', 'addbanned']) ], 'search' => 'nullable|string|min:1|max:20' ]); @@ -34,9 +35,14 @@ trait AdminMediaController ->whereIn('profile_id', $profiles) ->orWhere('mime', $request->input('search')) ->paginate(12); - } else { - $media = Media::whereHas('status')->with('status')->orderby('id', 'desc')->paginate(12); + return view('admin.media.home', compact('media')); } + + if($request->input('layout') == 'banned') { + $media = MediaBlocklist::latest()->paginate(12); + return view('admin.media.home', compact('media')); + } + $media = Media::whereHas('status')->with('status')->orderby('id', 'desc')->paginate(12); return view('admin.media.home', compact('media')); } diff --git a/app/Http/Controllers/MediaBlocklistController.php b/app/Http/Controllers/MediaBlocklistController.php new file mode 100644 index 000000000..bf862be0b --- /dev/null +++ b/app/Http/Controllers/MediaBlocklistController.php @@ -0,0 +1,50 @@ +middleware('auth'); + $this->middleware('admin'); + } + + public function add(Request $request) + { + $this->validate($request, [ + 'hash' => 'required|string|size:64', + 'name' => 'nullable|string', + 'description' => 'nullable|string|max:500', + ]); + + $hash = $request->input('hash'); + abort_if(preg_match("/^([a-f0-9]{64})$/", $hash) !== 1, 400); + + $name = $request->input('name'); + $description = $request->input('description'); + + $mb = new MediaBlocklist; + $mb->sha256 = $hash; + $mb->name = $name; + $mb->description = $description; + $mb->save(); + + return redirect('/i/admin/media?layout=banned'); + } + + public function delete(Request $request) + { + $this->validate($request, [ + 'id' => 'required|integer' + ]); + + $media = MediaBlocklist::findOrFail($request->input('id')); + $media->delete(); + + return redirect('/i/admin/media?layout=banned'); + } +} diff --git a/app/MediaBlocklist.php b/app/MediaBlocklist.php new file mode 100644 index 000000000..221ff6e2c --- /dev/null +++ b/app/MediaBlocklist.php @@ -0,0 +1,10 @@ +id(); + $table->string('sha256')->nullable()->unique()->index(); + $table->string('sha512')->nullable()->unique()->index(); + $table->string('name')->nullable(); + $table->text('description')->nullable(); + $table->boolean('active')->default(true)->index(); + $table->json('metadata')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('media_blocklists'); + } +} diff --git a/resources/views/admin/media/home.blade.php b/resources/views/admin/media/home.blade.php index 4344afb73..a26132941 100644 --- a/resources/views/admin/media/home.blade.php +++ b/resources/views/admin/media/home.blade.php @@ -4,11 +4,11 @@

Media

- - + + All - - + + Banned