--wip-- [skip ci]

This commit is contained in:
Philipp Lang 2023-03-12 21:01:08 +01:00
parent f269864194
commit f5f183c2c4
3 changed files with 81 additions and 0 deletions

57
src/OrderController.php Normal file
View File

@ -0,0 +1,57 @@
<?php
namespace Zoomyboy\MedialibraryHelper;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
class OrderController
{
use AuthorizesRequests;
public function __invoke(Request $request, $parentModel, int $parentId, string $collectionName)
{
Media::setNewOrder($request->order);
$model = $model::find($parentId);
dd($model);
$model = $this->validateModel($request);
$collection = $model->getMediaCollection($request->input('collection'));
$isSingle = 1 === $collection->collectionSizeLimit;
$this->authorize('storeMedia', [$model, $collection->name]);
$request->validate($isSingle ? [
'payload' => 'array',
'payload.*' => '',
'payload.name' => 'required|string|max:255',
'payload.content' => 'required|string',
] : [
'payload' => 'required|array|min:1',
'payload.*' => 'array',
'payload.*.name' => 'string',
'payload.*.content' => 'string',
]);
$content = $isSingle ? [$request->input('payload')] : $request->input('payload');
$medias = collect($content)->map(function ($c) use ($collection, $model) {
$pathinfo = pathinfo($c['name']);
$basename = $collection->runCallback('forceFileName', $model, $pathinfo['filename']);
$path = $basename.'.'.$pathinfo['extension'];
$adder = $model
->addMediaFromBase64($c['content'])
->usingName($basename)
->usingFileName($path)
->withCustomProperties($collection->runCallback('withDefaultProperties', $path));
return tap(
$collection->runCallback('storing', $adder, $path)->toMediaCollection($collection->name),
fn ($media) => $collection->runCallback('stored', $media)
);
});
return $isSingle ? MediaData::from($medias->first()) : MediaData::collection($medias);
}
}

View File

@ -19,6 +19,7 @@ class ServiceProvider extends BaseServiceProvider
$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_name}', [MediaController::class, 'index'])->name('media.index');
$router->post('mediaupload/{parent_model}/{parent_id}/{collection_name}', OrderController::class)->name('media.order');
$router->patch('mediaupload/{media}', [MediaController::class, 'update'])->name('media.update');
});

View File

@ -0,0 +1,23 @@
<?php
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
test('it can reorder media', function () {
$this->auth()->registerModel();
$post = $this->newPost();
$post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
$post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
$post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
$order = $post->getMedia('images')->pluck('id');
$order->prepend($order->pop());
$response = $this->postJson("/mediaupload/post/{$post->id}/images", [
'order' => $order,
]);
$response->assertStatus(200);
$response->assertJson('0.id', $order->get(0));
$response->assertJson('1.id', $order->get(1));
$response->assertJson('2.id', $order->get(2));
$this->assertEquals($order, $post->fresh()->getMedia('images')->pluck('id'));
});