1
|
|
|
<?php namespace Modules\Media\Http\Controllers\Api; |
2
|
|
|
|
3
|
|
|
use Illuminate\Http\Request; |
4
|
|
|
use Illuminate\Routing\Controller; |
5
|
|
|
use Illuminate\Support\Facades\DB; |
6
|
|
|
use Illuminate\Support\Facades\Response; |
7
|
|
|
use Modules\Media\Events\FileWasLinked; |
8
|
|
|
use Modules\Media\Events\FileWasUnlinked; |
9
|
|
|
use Modules\Media\Events\FileWasUploaded; |
10
|
|
|
use Modules\Media\Http\Requests\UploadMediaRequest; |
11
|
|
|
use Modules\Media\Image\Imagy; |
12
|
|
|
use Modules\Media\Repositories\FileRepository; |
13
|
|
|
use Modules\Media\Services\FileService; |
14
|
|
|
|
15
|
|
|
class MediaController extends Controller |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var FileService |
19
|
|
|
*/ |
20
|
|
|
private $fileService; |
21
|
|
|
/** |
22
|
|
|
* @var FileRepository |
23
|
|
|
*/ |
24
|
|
|
private $file; |
25
|
|
|
/** |
26
|
|
|
* @var Imagy |
27
|
|
|
*/ |
28
|
|
|
private $imagy; |
29
|
|
|
|
30
|
|
|
public function __construct(FileService $fileService, FileRepository $file, Imagy $imagy) |
31
|
|
|
{ |
32
|
|
|
$this->fileService = $fileService; |
33
|
|
|
$this->file = $file; |
34
|
|
|
$this->imagy = $imagy; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function all() |
38
|
|
|
{ |
39
|
|
|
$files = $this->file->all(); |
40
|
|
|
|
41
|
|
|
return [ |
42
|
|
|
'count' => $files->count(), |
43
|
|
|
'data' => $files, |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Store a newly created resource in storage. |
49
|
|
|
* @param UploadMediaRequest $request |
50
|
|
|
* @return Response |
51
|
|
|
*/ |
52
|
|
|
public function store(UploadMediaRequest $request) |
53
|
|
|
{ |
54
|
|
|
$savedFile = $this->fileService->store($request->file('file')); |
|
|
|
|
55
|
|
|
|
56
|
|
|
if (is_string($savedFile)) { |
57
|
|
|
return Response::json(['error' => $savedFile], 409); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
event(new FileWasUploaded($savedFile)); |
61
|
|
|
|
62
|
|
|
return Response::json($savedFile->toArray()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Link the given entity with a media file |
67
|
|
|
* @param Request $request |
68
|
|
|
*/ |
69
|
|
|
public function linkMedia(Request $request) |
70
|
|
|
{ |
71
|
|
|
$mediaId = $request->get('mediaId'); |
72
|
|
|
$entityClass = $request->get('entityClass'); |
73
|
|
|
$entityId = $request->get('entityId'); |
74
|
|
|
|
75
|
|
|
$entity = $entityClass::find($entityId); |
76
|
|
|
$zone = $request->get('zone'); |
77
|
|
|
$entity->files()->attach($mediaId, ['imageable_type' => $entityClass, 'zone' => $zone]); |
78
|
|
|
$imageable = DB::table('media__imageables')->whereFileId($mediaId)->whereZone($zone)->whereImageableType($entityClass)->first(); |
79
|
|
|
$file = $this->file->find($imageable->file_id); |
80
|
|
|
|
81
|
|
|
$thumbnailPath = $this->imagy->getThumbnail($file->path, 'mediumThumb'); |
82
|
|
|
|
83
|
|
|
event(new FileWasLinked($file, $entity)); |
84
|
|
|
|
85
|
|
|
return Response::json([ |
86
|
|
|
'error' => false, |
87
|
|
|
'message' => 'The link has been added.', |
88
|
|
|
'result' => ['path' => $thumbnailPath, 'imageableId' => $imageable->id] |
89
|
|
|
]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Remove the record in the media__imageables table for the given id |
94
|
|
|
* @param Request $request |
95
|
|
|
*/ |
96
|
|
|
public function unlinkMedia(Request $request) |
97
|
|
|
{ |
98
|
|
|
$imageableId = $request->get('imageableId'); |
99
|
|
|
$deleted = DB::table('media__imageables')->whereId($imageableId)->delete(); |
100
|
|
|
if (! $deleted) { |
101
|
|
|
return Response::json(['error' => true, 'message' => 'The file was not found.']); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
event(new FileWasUnlinked($imageableId)); |
105
|
|
|
|
106
|
|
|
return Response::json(['error' => false, 'message' => 'The link has been removed.']); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.