1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Model; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
use Jitamin\Foundation\Database\Model; |
16
|
|
|
use Jitamin\Foundation\ObjectStorage\ObjectStorageException; |
17
|
|
|
use Jitamin\Foundation\Thumbnail; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Base File Model. |
21
|
|
|
*/ |
22
|
|
|
abstract class FileModel extends Model |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Get the table. |
26
|
|
|
* |
27
|
|
|
* @abstract |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
abstract protected function getTable(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Define the foreign key. |
35
|
|
|
* |
36
|
|
|
* @abstract |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
abstract protected function getForeignKey(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the path prefix. |
44
|
|
|
* |
45
|
|
|
* @abstract |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
abstract protected function getPathPrefix(); |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Fire file creation event. |
53
|
|
|
* |
54
|
|
|
* @abstract |
55
|
|
|
* |
56
|
|
|
* @param int $file_id |
57
|
|
|
*/ |
58
|
|
|
abstract protected function fireCreationEvent($file_id); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get PicoDb query to get all files. |
62
|
|
|
* |
63
|
|
|
* @return \PicoDb\Table |
64
|
|
|
*/ |
65
|
|
|
protected function getQuery() |
66
|
|
|
{ |
67
|
|
|
return $this->db |
|
|
|
|
68
|
|
|
->table($this->getTable()) |
69
|
|
|
->columns( |
70
|
|
|
$this->getTable().'.id', |
71
|
|
|
$this->getTable().'.name', |
72
|
|
|
$this->getTable().'.path', |
73
|
|
|
$this->getTable().'.is_image', |
74
|
|
|
$this->getTable().'.'.$this->getForeignKey(), |
75
|
|
|
$this->getTable().'.date', |
76
|
|
|
$this->getTable().'.user_id', |
77
|
|
|
$this->getTable().'.size', |
78
|
|
|
UserModel::TABLE.'.username', |
79
|
|
|
UserModel::TABLE.'.name as user_name' |
80
|
|
|
) |
81
|
|
|
->join(UserModel::TABLE, 'id', 'user_id'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get a file by the id. |
86
|
|
|
* |
87
|
|
|
* @param int $file_id File id |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function getById($file_id) |
92
|
|
|
{ |
93
|
|
|
return $this->db->table($this->getTable())->eq('id', $file_id)->findOne(); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get all files. |
98
|
|
|
* |
99
|
|
|
* @param int $id |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
public function getAll($id) |
104
|
|
|
{ |
105
|
|
|
return $this->getQuery()->desc($this->getTable().'.id')->eq($this->getForeignKey(), $id)->findAll(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get all images. |
110
|
|
|
* |
111
|
|
|
* @param int $id |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
public function getAllImages($id) |
116
|
|
|
{ |
117
|
|
|
return $this->getQuery()->desc($this->getTable().'.id')->eq($this->getForeignKey(), $id)->eq('is_image', 1)->findAll(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get all files without images. |
122
|
|
|
* |
123
|
|
|
* @param int $id |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
public function getAllDocuments($id) |
128
|
|
|
{ |
129
|
|
|
return $this->getQuery()->desc($this->getTable().'.id')->eq($this->getForeignKey(), $id)->eq('is_image', 0)->findAll(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Create a file entry in the database. |
134
|
|
|
* |
135
|
|
|
* @param int $foreign_key_id Foreign key |
136
|
|
|
* @param string $name Filename |
137
|
|
|
* @param string $path Path on the disk |
138
|
|
|
* @param int $size File size |
139
|
|
|
* |
140
|
|
|
* @return bool|int |
141
|
|
|
*/ |
142
|
|
|
public function create($foreign_key_id, $name, $path, $size) |
143
|
|
|
{ |
144
|
|
|
$values = [ |
145
|
|
|
$this->getForeignKey() => $foreign_key_id, |
146
|
|
|
'name' => substr($name, 0, 255), |
147
|
|
|
'path' => $path, |
148
|
|
|
'is_image' => $this->isImage($name) ? 1 : 0, |
149
|
|
|
'size' => $size, |
150
|
|
|
'user_id' => $this->userSession->getId() ?: 0, |
|
|
|
|
151
|
|
|
'date' => time(), |
152
|
|
|
]; |
153
|
|
|
|
154
|
|
|
$result = $this->db->table($this->getTable())->insert($values); |
|
|
|
|
155
|
|
|
|
156
|
|
|
if ($result) { |
157
|
|
|
$file_id = (int) $this->db->getLastId(); |
|
|
|
|
158
|
|
|
$this->fireCreationEvent($file_id); |
159
|
|
|
|
160
|
|
|
return $file_id; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return false; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Remove all files. |
168
|
|
|
* |
169
|
|
|
* @param int $id |
170
|
|
|
* |
171
|
|
|
* @return bool |
172
|
|
|
*/ |
173
|
|
|
public function removeAll($id) |
174
|
|
|
{ |
175
|
|
|
$file_ids = $this->db->table($this->getTable())->eq($this->getForeignKey(), $id)->asc('id')->findAllByColumn('id'); |
|
|
|
|
176
|
|
|
$results = []; |
177
|
|
|
|
178
|
|
|
foreach ($file_ids as $file_id) { |
179
|
|
|
$results[] = $this->remove($file_id); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return !in_array(false, $results, true); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Remove a file. |
187
|
|
|
* |
188
|
|
|
* @param int $file_id File id |
189
|
|
|
* |
190
|
|
|
* @return bool |
191
|
|
|
*/ |
192
|
|
|
public function remove($file_id) |
193
|
|
|
{ |
194
|
|
|
try { |
195
|
|
|
$file = $this->getById($file_id); |
196
|
|
|
$this->objectStorage->remove($file['path']); |
|
|
|
|
197
|
|
|
|
198
|
|
|
if ($file['is_image'] == 1) { |
199
|
|
|
$this->objectStorage->remove($this->getThumbnailPath($file['path'])); |
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $this->db->table($this->getTable())->eq('id', $file['id'])->remove(); |
|
|
|
|
203
|
|
|
} catch (ObjectStorageException $e) { |
204
|
|
|
$this->logger->error($e->getMessage()); |
|
|
|
|
205
|
|
|
|
206
|
|
|
return false; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Check if a filename is an image (file types that can be shown as thumbnail). |
212
|
|
|
* |
213
|
|
|
* @param string $filename Filename |
214
|
|
|
* |
215
|
|
|
* @return bool |
216
|
|
|
*/ |
217
|
|
|
public function isImage($filename) |
218
|
|
|
{ |
219
|
|
|
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); |
220
|
|
|
|
221
|
|
|
switch ($extension) { |
222
|
|
|
case 'jpeg': |
223
|
|
|
case 'jpg': |
224
|
|
|
case 'png': |
225
|
|
|
case 'gif': |
226
|
|
|
return true; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return false; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Generate the path for a thumbnails. |
234
|
|
|
* |
235
|
|
|
* @param string $key Storage key |
236
|
|
|
* |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
|
|
public function getThumbnailPath($key) |
240
|
|
|
{ |
241
|
|
|
return 'thumbnails'.DIRECTORY_SEPARATOR.$key; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Generate the path for a new filename. |
246
|
|
|
* |
247
|
|
|
* @param int $id Foreign key |
248
|
|
|
* @param string $filename Filename |
249
|
|
|
* |
250
|
|
|
* @return string |
251
|
|
|
*/ |
252
|
|
|
public function generatePath($id, $filename) |
253
|
|
|
{ |
254
|
|
|
return $this->getPathPrefix().DIRECTORY_SEPARATOR.$id.DIRECTORY_SEPARATOR.hash('sha1', $filename.time()); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Upload multiple files. |
259
|
|
|
* |
260
|
|
|
* @param int $id |
261
|
|
|
* @param array $files |
262
|
|
|
* |
263
|
|
|
* @return bool |
264
|
|
|
*/ |
265
|
|
|
public function uploadFiles($id, array $files) |
266
|
|
|
{ |
267
|
|
|
try { |
268
|
|
|
if (empty($files)) { |
269
|
|
|
return false; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
foreach (array_keys($files['error']) as $key) { |
273
|
|
|
$file = [ |
274
|
|
|
'name' => $files['name'][$key], |
275
|
|
|
'tmp_name' => $files['tmp_name'][$key], |
276
|
|
|
'size' => $files['size'][$key], |
277
|
|
|
'error' => $files['error'][$key], |
278
|
|
|
]; |
279
|
|
|
|
280
|
|
|
$this->uploadFile($id, $file); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
return true; |
284
|
|
|
} catch (Exception $e) { |
285
|
|
|
$this->logger->error($e->getMessage()); |
|
|
|
|
286
|
|
|
|
287
|
|
|
return false; |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Upload a file. |
293
|
|
|
* |
294
|
|
|
* @param int $id |
295
|
|
|
* @param array $file |
296
|
|
|
* |
297
|
|
|
* @throws Exception |
298
|
|
|
*/ |
299
|
|
|
public function uploadFile($id, array $file) |
300
|
|
|
{ |
301
|
|
|
if ($file['error'] == UPLOAD_ERR_OK && $file['size'] > 0) { |
302
|
|
|
$destination_filename = $this->generatePath($id, $file['name']); |
303
|
|
|
|
304
|
|
|
if ($this->isImage($file['name'])) { |
305
|
|
|
$this->generateThumbnailFromFile($file['tmp_name'], $destination_filename); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
$this->objectStorage->moveUploadedFile($file['tmp_name'], $destination_filename); |
|
|
|
|
309
|
|
|
$this->create($id, $file['name'], $destination_filename, $file['size']); |
310
|
|
|
} else { |
311
|
|
|
throw new Exception('File not uploaded: '.var_export($file['error'], true)); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Handle file upload (base64 encoded content). |
317
|
|
|
* |
318
|
|
|
* @param int $id |
319
|
|
|
* @param string $original_filename |
320
|
|
|
* @param string $blob |
321
|
|
|
* |
322
|
|
|
* @return bool|int |
323
|
|
|
*/ |
324
|
|
|
public function uploadContent($id, $original_filename, $blob) |
325
|
|
|
{ |
326
|
|
|
try { |
327
|
|
|
$data = base64_decode($blob); |
328
|
|
|
|
329
|
|
|
if (empty($data)) { |
330
|
|
|
return false; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
$destination_filename = $this->generatePath($id, $original_filename); |
334
|
|
|
$this->objectStorage->put($destination_filename, $data); |
|
|
|
|
335
|
|
|
|
336
|
|
|
if ($this->isImage($original_filename)) { |
337
|
|
|
$this->generateThumbnailFromData($destination_filename, $data); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
return $this->create( |
341
|
|
|
$id, |
342
|
|
|
$original_filename, |
343
|
|
|
$destination_filename, |
344
|
|
|
strlen($data) |
345
|
|
|
); |
346
|
|
|
} catch (ObjectStorageException $e) { |
347
|
|
|
$this->logger->error($e->getMessage()); |
|
|
|
|
348
|
|
|
|
349
|
|
|
return false; |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Generate thumbnail from a blob. |
355
|
|
|
* |
356
|
|
|
* @param string $destination_filename |
357
|
|
|
* @param string $data |
358
|
|
|
*/ |
359
|
|
|
public function generateThumbnailFromData($destination_filename, &$data) |
360
|
|
|
{ |
361
|
|
|
$blob = Thumbnail::createFromString($data) |
362
|
|
|
->resize() |
363
|
|
|
->toString(); |
364
|
|
|
|
365
|
|
|
$this->objectStorage->put($this->getThumbnailPath($destination_filename), $blob); |
|
|
|
|
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Generate thumbnail from a local file. |
370
|
|
|
* |
371
|
|
|
* @param string $uploaded_filename |
372
|
|
|
* @param string $destination_filename |
373
|
|
|
*/ |
374
|
|
|
public function generateThumbnailFromFile($uploaded_filename, $destination_filename) |
375
|
|
|
{ |
376
|
|
|
$blob = Thumbnail::createFromFile($uploaded_filename) |
377
|
|
|
->resize() |
378
|
|
|
->toString(); |
379
|
|
|
|
380
|
|
|
$this->objectStorage->put($this->getThumbnailPath($destination_filename), $blob); |
|
|
|
|
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.