-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathMedia.php
More file actions
205 lines (188 loc) · 6.06 KB
/
Media.php
File metadata and controls
205 lines (188 loc) · 6.06 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
<?php
namespace ProcessMaker\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Validation\ValidationException;
use ProcessMaker\Models\ProcessRequest;
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 HasFactory;
protected $connection = 'processmaker';
protected $table = 'media';
/**
* 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',
];
/**
* 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'],
]);
}
}
/**
* getFilesRequest
*
* @param ProcessRequest $request
* @return Media files
*/
public static function getFilesRequest(ProcessRequest $request)
{
$requestTokenIds = [$request->id];
if ($request->collaboration && $request->collaboration->requests()) {
// Get all processes and subprocesses request token id's ..
$requestTokenIds = $request->collaboration->requests->pluck('id');
}
// Get all files for process and all subprocesses ..
return self::whereIn('model_id', $requestTokenIds)->get();
}
/**
* 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');
}
}