forked from aemaddin/nova-medialibrary-field
-
Notifications
You must be signed in to change notification settings - Fork 4
/
AttachController.php
76 lines (59 loc) · 2.52 KB
/
AttachController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php declare(strict_types=1);
namespace DmitryBubyakin\NovaMedialibraryField\Http\Controllers;
use DmitryBubyakin\NovaMedialibraryField\Http\Requests\MedialibraryRequest;
use DmitryBubyakin\NovaMedialibraryField\TransientModel;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\UploadedFile;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\MediaCollections\Filesystem;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\Support\TemporaryDirectory;
class AttachController
{
public function __invoke(MedialibraryRequest $request): JsonResponse
{
[$model, $uuid] = $request->resourceExists()
? [$request->findModelOrFail(), '']
: [TransientModel::make(), $request->fieldUuid()];
$field = $request->medialibraryField();
if ($request->has('media')) {
$this->replaceFile($request);
}
$request->validate(['file' => $field->getAttachRules($request)]);
$this->rememberTargetModel($request, $model, $field->collectionName);
call_user_func_array($field->attachCallback, [
$model,
$request->file,
$field->collectionName,
$field->diskName,
$uuid,
]);
return response()->json([], 201);
}
private function replaceFile(MedialibraryRequest $request): void
{
$media = config('media-library.media_model')::findOrFail($request->media);
$directory = TemporaryDirectory::create();
$temporaryFile = $directory->path('/').DIRECTORY_SEPARATOR.$media->file_name;
app(Filesystem::class)->copyFromMediaLibrary($media, $temporaryFile);
$request->merge([
'file' => new UploadedFile($temporaryFile, $media->file_name, $media->mime_type, null, true),
]);
}
private function rememberTargetModel(MedialibraryRequest $request, HasMedia $model, string $collectionName): void
{
if (! $model instanceof TransientModel) {
return;
}
config('media-library.media_model')::creating(function (Media $media) use ($request, $model, $collectionName): void {
if ($model instanceof TransientModel && $media->collection_name === $request->fieldUuid()) {
TransientModel::setCustomPropertyValue(
$media,
$request->resource()::$model,
$collectionName
);
$model->registerAllMediaConversions($media);
}
});
}
}