From 07591ea52b2ff20e781d130a0a7cfd8fbe244335 Mon Sep 17 00:00:00 2001 From: Philipp Lang Date: Mon, 6 Mar 2023 14:00:22 +0100 Subject: [PATCH] Add controller --- src/MediaController.php | 80 +++++++++++++++++++++++++++++++++++++++++ src/ServiceProvider.php | 30 ++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 src/MediaController.php create mode 100644 src/ServiceProvider.php diff --git a/src/MediaController.php b/src/MediaController.php new file mode 100644 index 0000000..1d5e512 --- /dev/null +++ b/src/MediaController.php @@ -0,0 +1,80 @@ +validate([ + 'name' => 'nullable|string', + 'content' => 'required|string', + 'model' => ['required', 'string', Rule::in(app('media-library-helpers')->keys())], + ]); + + $model = $this->validateModel($request); + $this->authorize('storeMedia', $model); + + $collection = $model->getMediaCollection($request->input('collection')); + $isSingle = 1 === $collection->collectionSizeLimit; + + if ($collection->forceFileRenamer) { + $fileRenamer = $collection->forceFileRenamer; + $path = $fileRenamer($model); + } else { + $path = $request->input('name', Str::random(32)); + } + + $content = $isSingle ? [$request->input('content')] : $request->input('content'); + + $medias = collect([]); + foreach ($content as $c) { + Storage::disk('public')->put($path, base64_decode($c)); + + $medias->push($model->addMedia(Storage::disk('public')->path($path))->toMediaCollection($collection->name)); + } + + return response()->json($isSingle ? $medias->first()->toArray() : $medias->map(fn ($media) => $media->toArray())); + } + + public function index(Request $request, $parentModel, int $parentId, string $collection): JsonResponse + { + $model = app('media-library-helpers')->get($parentModel); + $model = $model::findOrFail($parentId); + $isSingle = $model->getMediaCollection($collection)->collectionSizeLimit; + + return response()->json([ + 'data' => $isSingle ? $model->getFirstMedia($collection) : $model->getMedia($collection)->map(fn ($c) => $c->toArray()), + ]); + } + + public function destroy(Media $media, Request $request): JsonResponse + { + $this->authorize('destroyMedia', $media->model); + $media->delete(); + + return response()->json([]); + } + + private function validateModel(Request $request): HasMedia + { + $model = app('media-library-helpers')->get($request->input('model')); + + $request->validate([ + 'collection' => ['required', 'string', Rule::in((new $model())->getRegisteredMediaCollections()->map(fn ($collection) => $collection->name)->toArray())], + ]); + + return $model::findOrFail($request->input('id')); + } +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php new file mode 100644 index 0000000..577b6a6 --- /dev/null +++ b/src/ServiceProvider.php @@ -0,0 +1,30 @@ +bind('media-library-helpers', fn () => collect([])); + } + + public function boot(): void + { + app(Router::class)->group(['middleware' => ['web', 'auth:web']], function ($router) { + $router->post('mediaupload', [MediaController::class, 'store'])->name('media.store'); + $router->delete('mediaupload/{media}', [MediaController::class, 'destroy'])->name('media.destroy'); + $router->get('mediaupload/{parent_model}/{parent_id}/{collection}', [MediaController::class, 'index'])->name('media.index'); + }); + + MediaCollection::macro('forceFileName', function ($callback) { + $this->forceFileRenamer = $callback; + + return $this; + }); + } +}