-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathMedia.php
More file actions
308 lines (279 loc) · 9.5 KB
/
Media.php
File metadata and controls
308 lines (279 loc) · 9.5 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
namespace ProcessMaker\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Collection;
use Illuminate\Validation\ValidationException;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Traits\Exportable;
use Spatie\MediaLibrary\MediaCollections\Models\Media as MediaLibraryModel;
/**
* Represents media files stored in the database
*
* @property int 'id',
* @property int 'model_id',
* @property string 'model_type',
* @property string 'collection_name',
* @property string 'name',
* @property string 'file_name',
* @property string 'mime_type',
* @property string 'disk',
* @property string 'size',
* @property string 'manipulations',
* @property string 'custom_properties',
* @property string 'responsive_images',
* @property string 'order_column',
* @property \Carbon\Carbon $updated_at
* @property \Carbon\Carbon $created_at
*
* @OA\Schema(
* schema="mediaEditable",
* @OA\Property(property="id", type="integer", format="id"),
* @OA\Property(property="model_id", type="integer", format="id"),
* @OA\Property(property="model_type", type="string", format="id"),
* @OA\Property(property="collection_name", type="string"),
* @OA\Property(property="name", type="string"),
* @OA\Property(property="file_name", type="string"),
* @OA\Property(property="mime_type", type="string"),
* @OA\Property(property="disk", type="string"),
* @OA\Property(property="size", type="integer"),
* @OA\Property(property="manipulations", type="object"),
* @OA\Property(property="custom_properties", type="object"),
* @OA\Property(property="responsive_images", type="object"),
* @OA\Property(property="order_column", type="integer"),
* ),
* @OA\Schema(
* schema="media",
* allOf={@OA\Schema(ref="#/components/schemas/mediaEditable")},
* @OA\Property(property="created_at", type="string", format="date-time"),
* @OA\Property(property="updated_at", type="string", format="date-time"),
* ),
* @OA\Schema(
* schema="mediaExported",
* @OA\Property(property="url", type="string"),
* )
*/
class Media extends MediaLibraryModel
{
use Exportable;
use HasFactory;
protected $connection = 'processmaker';
protected $table = 'media';
const COLLECTION_SLIDESHOW = 'images_slideshow';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'model_id',
'model_type',
'collection_name',
'name',
'file_name',
'mime_type',
'disk',
'size',
'manipulations',
'custom_properties',
'responsive_images',
'order_column',
];
/**
* Validation rules
*
* @param $existing
*
* @return array
*/
public static function rules($existing = null)
{
return [
'model_id' => 'required',
'model_type' => 'required',
'collection_name' => 'required',
'name' => 'required,alpha_spaces',
'file_name' => 'required',
'mime_type' => 'required',
'disk' => 'required',
'size' => 'required',
'manipulations' => 'required',
'custom_properties' => 'required',
'responsive_images' => 'required',
'order_column' => 'required',
];
}
/**
* The binary UUID attributes that should be converted to text.
*
* @var array
*/
protected $ids = [
'model_id',
];
public function determineCollectionName(Process $process, $name)
{
// In order to differentiate them from other process-related images ['launchpad', 'slideshow']
if ($name === 'launchpad') {
return $process->uuid . '_' . 'images_carousel';
} elseif ($name === 'slideshow') {
return self::COLLECTION_SLIDESHOW;
}
return null;
}
/**
* Override the default boot method to allow access to lifecycle hooks
*
* @return null
*/
public static function boot()
{
parent::boot();
self::creating(function ($media) {
$user = pmUser();
if (!$media->hasCustomProperty('createdBy')) {
$media->setCustomProperty('createdBy', $user ? $user->id : null);
}
$media->setCustomProperty('updatedBy', $user ? $user->id : null);
});
self::saving(function ($media) {
if ($media->model instanceof ProcessRequest) {
if (empty($media->getCustomProperty('data_name'))) {
throw ValidationException::withMessages(['data_name' => 'data_name is required']);
}
}
$media->setCustomProperty('updatedBy', pmUser() ? pmUser()->id : null);
});
}
public function isPublicFile()
{
if ($this->model instanceof ProcessRequest) {
if ($this->model->process && $this->model->process->package_key == 'package-files/public-files') {
return true;
}
}
return false;
}
public function getManagerNameAttribute()
{
if (isset($this->custom_properties['data_name'])) {
return last(explode('/', $this->custom_properties['data_name']));
} else {
return $this->name;
}
}
public function getManagerUrlAttribute()
{
if ($this->isPublicFile()) {
$route = route('file-manager.index');
return $route . '#/public/' . $this->custom_properties['data_name'];
} else {
return route('requests.show.files.viewer', [
'request' => $this->model,
'filePath' => $this->custom_properties['data_name'],
]);
}
}
/**
* Save the media related to the Process
*
* @param Process $process
* @param array $properties
* @param string $key
* @param string $mediaType
*
* @return void
*/
public function saveProcessMedia(Process $process, $properties, $key = 'uuid', $mediaType = 'launchpad')
{
// Validate if the image smaller than 2MB
$maxFileSize = 2 * 1024 * 1024;
$imageData = base64_decode($properties['url']);
if (strlen($imageData) > $maxFileSize) {
return;
}
// Validate four images
$mediaCount = $process->getMedia()->count();
if ($mediaCount > 4) {
return;
}
// Get collection name to save
$collectionName = self::determineCollectionName($process, $mediaType);
// Check if exist
$exist = $process->media()->where($key, $properties[$key])->exists();
if (!$exist) {
// Define the custom properties
$customProperties = [
'media_type' => $mediaType,
'type' => $properties['type'],
];
if ($mediaType === 'slideshow') {
$customProperties['node_id'] = $properties['node_id'] ?? '';
$customProperties['alternative'] = $properties['alternative'] ?? '';
}
// Store the images related move to MEDIA
$media = $process->addMediaFromBase64($properties['url'])
->withCustomProperties($customProperties);
// Only if the file_name exist this will save
if (isset($properties['file_name']) && !empty($properties['file_name'])) {
$media->usingFileName($properties['file_name']);
}
// Add the media
$media->toMediaCollection($collectionName);
}
}
/**
* getFilesRequest
*
* @param ProcessRequest $request
* @return Collection | Media
*/
public static function getFilesRequest(ProcessRequest $request, $id = null)
{
$requestTokenIds = [$request->id];
if ($request->collaboration && $request->collaboration->requests()) {
// Get all processes and subprocesses request token id's ..
$requestTokenIds = $request->collaboration->requests->pluck('id')->toArray();
}
// Return a single file when $id is set
if ($id) {
try {
$mediaById = self::findOrFail($id);
} catch (\Exception) {
abort(400, __('Something went wrong and the file cannot be previewed or downloaded.'));
}
if (!in_array($mediaById->process_request_id, $requestTokenIds)) {
abort(404, __('File is not part of this request'));
}
return $mediaById;
}
// Get all files for process and all subprocesses ..
return self::where('model_type', ProcessRequest::class)
->whereIn('model_id', $requestTokenIds)->get();
}
public function getProcessRequestIdAttribute()
{
if ($this->model_type == TaskDraft::class) {
return $this->model->processRequestToken->processRequest->id;
} elseif ($this->model_type == ProcessRequest::class) {
return $this->model->id;
}
return null;
}
public function toArray()
{
$array = parent::toArray();
$array['process_request_id'] = $this->process_request_id;
return $array;
}
/**
* Check if the S3 is ready to use
*/
public static function s3IsReady()
{
return config('filesystems.disks.s3.key')
&& config('filesystems.disks.s3.secret')
&& config('filesystems.disks.s3.region')
&& config('filesystems.disks.s3.bucket');
}
}