diff --git a/app/Http/Controllers/MediaController.php b/app/Http/Controllers/MediaController.php index 5003a76..dc59ba0 100644 --- a/app/Http/Controllers/MediaController.php +++ b/app/Http/Controllers/MediaController.php @@ -11,29 +11,6 @@ use Illuminate\Support\Facades\Validator; class MediaController extends Controller { - /** - * The disk to store public media. - * - * @var string - */ - private static $publicStorageDisk = 'public'; - - /** - * The disk to store temporary media. - * - * @var string - */ - private static $tempStorageDisk = 'temp'; - - /** - * Media preprocessors. - * - * @var array - */ - private static $preProcessors = [ - \App\MediaServices\Converters\HEICToJPEG::class, - ]; - /** * Display a listing of the resource. */ @@ -316,6 +293,18 @@ class MediaController extends Controller // $mediaData['hash'] = $hash; // } + if($request->get('password_clear') === 'on') { + $mediaData['password'] = null; + } else { + $password = $request->get('password'); + + if($password !== null && $password !== '') { + $mediaData['password'] = password_hash($request->get('password'), PASSWORD_DEFAULT); + } else { + unset($mediaData['password']); + } + } + $media->update($mediaData); // if($file) { @@ -421,6 +410,26 @@ class MediaController extends Controller abort(404, 'File not found'); } + if($media->password !== null) { + if(!$request->has('password')) { + return view('media-password'); + } else { + $password = $request->get('password'); + + if($password === '' || $password === null) { + return view('media-password', [ + 'error' => 'Password is required', + ]); + } + + if(!password_verify(base64_decode($password), $media->password)) { + return view('media-password', [ + 'error' => 'Password is incorrect', + ]); + } + } + } + $variant = ''; $download = false; $variants = array_keys($media->getVariantTypes()); diff --git a/app/Models/Media.php b/app/Models/Media.php index e007a46..ece2c7d 100644 --- a/app/Models/Media.php +++ b/app/Models/Media.php @@ -23,7 +23,8 @@ class Media extends Model 'mime_type', 'size', 'user_id', - 'hash' + 'hash', + 'password', ]; /** @@ -53,7 +54,8 @@ class Media extends Model * @var array */ protected $casts = [ - 'variants' => 'array' + 'variants' => 'array', + 'password' => 'hashed' ]; /** diff --git a/database/migrations/2024_04_25_152341_add_password_to_media.php b/database/migrations/2024_04_25_152341_add_password_to_media.php new file mode 100644 index 0000000..d01a1cf --- /dev/null +++ b/database/migrations/2024_04_25_152341_add_password_to_media.php @@ -0,0 +1,28 @@ +string('password')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('media', function (Blueprint $table) { + $table->dropColumn('password'); + }); + } +}; diff --git a/resources/views/admin/media/edit.blade.php b/resources/views/admin/media/edit.blade.php index d4a42e5..c7b7b73 100644 --- a/resources/views/admin/media/edit.blade.php +++ b/resources/views/admin/media/edit.blade.php @@ -1,3 +1,10 @@ +@php +$password = ''; +if(isset($medium) && ($medium->password !== null && $medium->password !== '')) { + $password = 'yes'; +} +@endphp + {{ isset($medium) ? 'Edit' : 'Create' }} Media @@ -16,6 +23,10 @@ @endisset +
+ +
+
diff --git a/resources/views/components/ui/button.blade.php b/resources/views/components/ui/button.blade.php index 88a947b..efd11be 100644 --- a/resources/views/components/ui/button.blade.php +++ b/resources/views/components/ui/button.blade.php @@ -5,6 +5,7 @@ 'outline' => 'hover:bg-gray-500 focus-visible:outline-primary-color text-gray-800 border border-gray-400 bg-white hover:text-white', 'primary' => 'hover:bg-primary-color-dark focus-visible:outline-primary-color bg-primary-color text-white', 'primary-outline' => 'hover:bg-primary-color-dark focus-visible:outline-primary-color text-primary-color border border-primary-color bg-white hover:text-white', + 'primary-outline-sm' => '!font-normal !text-xs !px-4 !py-1 hover:bg-primary-color-dark focus-visible:outline-primary-color text-primary-color border border-primary-color bg-white hover:text-white', 'danger' => 'hover:bg-danger-color-dark focus-visible:outline-danger-color bg-danger-color text-white', 'success' => 'hover:bg-success-color-dark focus-visible:outline-success-color bg-success-color text-white' ][$color]; diff --git a/resources/views/components/ui/checkbox.blade.php b/resources/views/components/ui/checkbox.blade.php index 3972a54..8d8f68e 100644 --- a/resources/views/components/ui/checkbox.blade.php +++ b/resources/views/components/ui/checkbox.blade.php @@ -1,4 +1,13 @@ -@props(['name', 'label', 'checked' => false]) +@props(['name', 'label', 'checked' => false, 'small' => false]) + +@php + $checkBoxClasses = ''; + $labelClasses = ''; + if($small) { + $checkBoxClasses = 'h-6 w-6 rounded-md text-xs'; + $labelClasses = 'pl-1 text-xs'; + } +@endphp -
+
- - + +
diff --git a/resources/views/components/ui/password.blade.php b/resources/views/components/ui/password.blade.php new file mode 100644 index 0000000..d4729c2 --- /dev/null +++ b/resources/views/components/ui/password.blade.php @@ -0,0 +1,37 @@ +@props(['name', 'label' => '', 'value' => '', 'error' => null]) + +@php + if($error === null) { + $error = $errors->first($name); + } + + $hasError = $error !== ''; + $classes = 'disabled:bg-gray-100 bg-white block px-2.5 pb-2.5 w-full text-sm text-gray-900 rounded-lg border appearance-nonefocus:outline-none focus:ring-0 focus:border-blue-600 ' . ($hasError ? 'border-red-600 ring-red-600 focus:border-red-600 focus:ring-red-600' : 'border-gray-300 focus:border-indigo-300 focus:ring-indigo-300'); + + $label = ($value === '' ? '' : 'Change ') . $label; +@endphp + +
+
+ +
+ + @if($value !== '') + + @endif +
+
+ @if(isset($info) && $info !== '') +
{{ $info }}
+ @endif + @if ($hasError) +
{{ $error }}
+ @endif +
diff --git a/resources/views/media-password.blade.php b/resources/views/media-password.blade.php new file mode 100644 index 0000000..082b901 --- /dev/null +++ b/resources/views/media-password.blade.php @@ -0,0 +1,55 @@ + + @props(['formmethod' => 'POST', 'formaction' => '']) +
+
+

Password Required

+
+ A password is required to access this file. +
+
+ +
+ Download +
+ +
+
+

Requesting file

+
+ The file has been requested from the server... +
+
+ Please wait +
+
+ Try again + Home +
+
+
+
+ +