Add controller

This commit is contained in:
Philipp Lang 2023-03-06 14:00:22 +01:00
parent 4e80d6cd19
commit 07591ea52b
2 changed files with 110 additions and 0 deletions

80
src/MediaController.php Normal file
View File

@ -0,0 +1,80 @@
<?php
namespace Zoomyboy\MedialibraryHelper;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
class MediaController
{
use AuthorizesRequests;
public function store(Request $request): JsonResponse
{
$request->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'));
}
}

30
src/ServiceProvider.php Normal file
View File

@ -0,0 +1,30 @@
<?php
namespace Zoomyboy\MedialibraryHelper;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use Spatie\MediaLibrary\MediaCollections\MediaCollection;
class ServiceProvider extends BaseServiceProvider
{
public function register(): void
{
app()->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;
});
}
}