Compare commits

...

3 Commits

Author SHA1 Message Date
philipp lang 424ca30932 Fix response codes 2024-11-13 13:50:41 +01:00
philipp lang 9870ac8bab Fix response code of multiple upload 2024-11-13 13:28:03 +01:00
philipp lang e9975e9c5a Fix media data 2024-11-13 13:19:00 +01:00
4 changed files with 11 additions and 8 deletions

View File

@ -29,9 +29,9 @@ class DeferredMediaData extends Data
]); ]);
} }
public static function collectionFromPaths(Collection $paths, MediaCollection $collection): DataCollection public static function collectionFromPaths(Collection $paths, MediaCollection $collection): Collection
{ {
return static::collection($paths->map(fn ($path) => static::fromPath($path, $collection))); return static::collect($paths->map(fn ($path) => static::fromPath($path, $collection)));
} }
public function with(): array public function with(): array

View File

@ -5,6 +5,8 @@ namespace Zoomyboy\MedialibraryHelper;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Collection;
use Spatie\Image\Image; use Spatie\Image\Image;
use Spatie\LaravelData\DataCollection; use Spatie\LaravelData\DataCollection;
use Spatie\MediaLibrary\MediaCollections\Exceptions\InvalidBase64Data; use Spatie\MediaLibrary\MediaCollections\Exceptions\InvalidBase64Data;
@ -66,7 +68,7 @@ class MediaController
$collection->runCallback('after', $model->fresh()); $collection->runCallback('after', $model->fresh());
return $isSingle ? MediaData::from($medias->first()) : MediaData::collection($medias); return $isSingle ? MediaData::from($medias->first()) : response(MediaData::collect($medias), 201);
} }
protected function storeDeferred(Request $request) protected function storeDeferred(Request $request)
@ -98,7 +100,7 @@ class MediaController
}); });
$this->authorize('storeMedia', [$modelName, null, $collection->name]); $this->authorize('storeMedia', [$modelName, null, $collection->name]);
return $isSingle ? DeferredMediaData::fromPath($tempPaths->first(), $collection) : DeferredMediaData::collectionFromPaths($tempPaths, $collection); return $isSingle ? DeferredMediaData::fromPath($tempPaths->first(), $collection) : response(DeferredMediaData::collectionFromPaths($tempPaths, $collection), 201);
} }
public function update(Request $request, Media $media): MediaData public function update(Request $request, Media $media): MediaData
@ -116,7 +118,7 @@ class MediaController
return MediaData::from($media); return MediaData::from($media);
} }
public function index(Request $request, $parentModel, int $parentId, string $collectionName): MediaData|DataCollection public function index(Request $request, $parentModel, int $parentId, string $collectionName): MediaData|JsonResponse
{ {
$model = app('media-library-helpers')->get($parentModel); $model = app('media-library-helpers')->get($parentModel);
$model = $model::find($parentId); $model = $model::find($parentId);
@ -132,7 +134,7 @@ class MediaController
return $isSingle return $isSingle
? MediaData::from($model->getFirstMedia($collectionName)) ? MediaData::from($model->getFirstMedia($collectionName))
: MediaData::collection($model->getMedia($collectionName)); : response()->json(MediaData::collect($model->getMedia($collectionName))->toArray());
} }
public function destroy(Media $media, Request $request): JsonResponse public function destroy(Media $media, Request $request): JsonResponse

View File

@ -16,7 +16,7 @@ class OrderController
$mediaCount = collect($request->order)->map(function ($media) { $mediaCount = collect($request->order)->map(function ($media) {
$media = Media::findOrFail($media); $media = Media::findOrFail($media);
return $media->model_id.'_'.$media->model_type; return $media->model_id . '_' . $media->model_type;
})->unique()->count(); })->unique()->count();
if (1 !== $mediaCount) { if (1 !== $mediaCount) {
@ -31,6 +31,6 @@ class OrderController
$model->getMediaCollection($collectionName)->runCallback('after', $model->fresh()); $model->getMediaCollection($collectionName)->runCallback('after', $model->fresh());
return MediaData::collection($model->getMedia($collectionName)); return MediaData::collect($model->getMedia($collectionName))->toArray();
} }
} }

View File

@ -7,6 +7,7 @@ use Spatie\MediaLibrary\MediaCollections\Models\Media;
test('it gets all medias', function () { test('it gets all medias', function () {
$this->auth()->registerModel(); $this->auth()->registerModel();
$this->withoutExceptionHandling();
$post = $this->newPost(); $post = $this->newPost();
$firstMedia = $post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('images'); $firstMedia = $post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('images');
$secondMedia = $post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('images'); $secondMedia = $post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('images');