Passed
Push — master ( 072a53...0fc8bb )
by Thomas
03:32
created

FilePondField::Field()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
namespace LeKoala\FilePond;
4
5
use Exception;
6
use LogicException;
7
use RuntimeException;
8
use SilverStripe\Forms\Form;
9
use SilverStripe\Assets\File;
10
use SilverStripe\ORM\SS_List;
11
use SilverStripe\Assets\Image;
12
use SilverStripe\Core\Convert;
13
use SilverStripe\ORM\ArrayList;
14
use SilverStripe\ORM\DataObject;
15
use SilverStripe\Control\Director;
16
use SilverStripe\Security\Security;
17
use SilverStripe\View\Requirements;
18
use SilverStripe\Control\HTTPRequest;
19
use SilverStripe\Versioned\Versioned;
20
use SilverStripe\Control\HTTPResponse;
21
use SilverStripe\ORM\DataObjectInterface;
22
use SilverStripe\ORM\ValidationException;
23
use SilverStripe\ORM\FieldType\DBHTMLText;
24
25
/**
26
 * A FilePond field
27
 */
28
class FilePondField extends AbstractUploadField
29
{
30
    const IMAGE_MODE_MIN = "min";
31
    const IMAGE_MODE_MAX = "max";
32
    const IMAGE_MODE_CROP = "crop";
33
    const IMAGE_MODE_RESIZE = "resize";
34
    const IMAGE_MODE_CROP_RESIZE = "crop_resize";
35
    const DEFAULT_POSTER_HEIGHT = 264;
36
    const DEFAULT_POSTER_WIDTH = 352;
37
38
    /**
39
     * @config
40
     * @var array<string>
41
     */
42
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
43
        'upload',
44
        'chunk',
45
        'revert',
46
    ];
47
48
    /**
49
     * @config
50
     * @var boolean
51
     */
52
    private static $enable_requirements = true;
53
54
    /**
55
     * @config
56
     * @var boolean
57
     */
58
    private static $enable_poster = false;
59
60
    /**
61
     * @config
62
     * @var boolean
63
     */
64
    private static $chunk_by_default = false;
65
66
    /**
67
     * @config
68
     * @var boolean
69
     */
70
    private static $enable_default_description = true;
0 ignored issues
show
introduced by
The private property $enable_default_description is not used, and could be removed.
Loading history...
71
72
    /**
73
     * @config
74
     * @var boolean
75
     */
76
    private static $auto_clear_temp_folder = true;
77
78
    /**
79
     * @config
80
     * @var bool
81
     */
82
    private static $auto_clear_threshold = true;
83
84
    /**
85
     * @config
86
     * @var boolean
87
     */
88
    private static $enable_auto_thumbnails = false;
0 ignored issues
show
introduced by
The private property $enable_auto_thumbnails is not used, and could be removed.
Loading history...
89
90
    /**
91
     * @config
92
     * @var int
93
     */
94
    private static $poster_width = 352;
95
96
    /**
97
     * @config
98
     * @var int
99
     */
100
    private static $poster_height = 264;
101
102
    /**
103
     * @var array<string|int,mixed|null>
104
     */
105
    protected $filePondConfig = [];
106
107
    /**
108
     * @var array<string,mixed>|null
109
     */
110
    protected $customServerConfig = null;
111
112
    /**
113
     * @var ?int
114
     */
115
    protected $posterHeight = null;
116
117
    /**
118
     * @var ?int
119
     */
120
    protected $posterWidth = null;
121
122
    /**
123
     * Create a new file field.
124
     *
125
     * @param string $name The internal field name, passed to forms.
126
     * @param string $title The field label.
127
     * @param SS_List $items Items assigned to this field
128
     */
129
    public function __construct($name, $title = null, SS_List $items = null)
130
    {
131
        parent::__construct($name, $title, $items);
132
133
        if (self::config()->chunk_by_default) {
134
            $this->setChunkUploads(true);
135
        }
136
    }
137
138
    /**
139
     * Set a custom config value for this field
140
     *
141
     * @link https://pqina.nl/filepond/docs/patterns/api/filepond-instance/#properties
142
     * @param string $k
143
     * @param string|bool|float|int|array<mixed> $v
144
     * @return $this
145
     */
146
    public function addFilePondConfig($k, $v)
147
    {
148
        $this->filePondConfig[$k] = $v;
149
        return $this;
150
    }
151
152
    /**
153
     * @param string $k
154
     * @param mixed $default
155
     * @return mixed
156
     */
157
    public function getCustomConfigValue($k, $default = null)
158
    {
159
        if (isset($this->filePondConfig[$k])) {
160
            return $this->filePondConfig[$k];
161
        }
162
        return $default;
163
    }
164
165
    /**
166
     * Custom configuration applied to this field
167
     *
168
     * @return array<mixed>
169
     */
170
    public function getCustomFilePondConfig()
171
    {
172
        return $this->filePondConfig;
173
    }
174
175
    /**
176
     * Get the value of chunkUploads
177
     * @return bool
178
     */
179
    public function getChunkUploads()
180
    {
181
        if (!isset($this->filePondConfig['chunkUploads'])) {
182
            return false;
183
        }
184
        return $this->filePondConfig['chunkUploads'];
185
    }
186
187
    /**
188
     * Get the value of customServerConfig
189
     * @return array<mixed>
190
     */
191
    public function getCustomServerConfig()
192
    {
193
        return $this->customServerConfig;
194
    }
195
196
    /**
197
     * Set the value of customServerConfig
198
     *
199
     * @param array<mixed> $customServerConfig
200
     * @return $this
201
     */
202
    public function setCustomServerConfig(array $customServerConfig)
203
    {
204
        $this->customServerConfig = $customServerConfig;
205
        return $this;
206
    }
207
208
    /**
209
     * Set the value of chunkUploads
210
     *
211
     * Note: please set max file upload first if you want
212
     * to see the size limit in the description
213
     *
214
     * @param bool $chunkUploads
215
     * @return $this
216
     */
217
    public function setChunkUploads($chunkUploads)
218
    {
219
        $this->addFilePondConfig('chunkUploads', true);
220
        $this->addFilePondConfig('chunkForce', true);
221
        $this->addFilePondConfig('chunkSize', $this->computeMaxChunkSize());
222
        if ($this->isDefaultMaxFileSize()) {
223
            $this->showDescriptionSize = false;
224
        }
225
        return $this;
226
    }
227
228
    /**
229
     * @param array<mixed> $sizes
230
     * @return array<mixed>
231
     */
232
    public function getImageSizeConfigFromArray($sizes)
233
    {
234
        $mode = null;
235
        if (isset($sizes[2])) {
236
            $mode = $sizes[2];
237
        }
238
        return $this->getImageSizeConfig($sizes[0], $sizes[1], $mode);
239
    }
240
241
    /**
242
     * @param int $width
243
     * @param int $height
244
     * @param string $mode min|max|crop|resize|crop_resize
245
     * @return array<mixed>
246
     */
247
    public function getImageSizeConfig($width, $height, $mode = null)
248
    {
249
        if ($mode === null) {
250
            $mode = self::IMAGE_MODE_MIN;
251
        }
252
        $config = [];
253
        switch ($mode) {
254
            case self::IMAGE_MODE_MIN:
255
                $config['imageValidateSizeMinWidth'] = $width;
256
                $config['imageValidateSizeMinHeight'] = $height;
257
                break;
258
            case self::IMAGE_MODE_MAX:
259
                $config['imageValidateSizeMaxWidth'] = $width;
260
                $config['imageValidateSizeMaxHeight'] = $height;
261
                break;
262
            case self::IMAGE_MODE_CROP:
263
                // It crops only to given ratio and tries to keep the largest image
264
                $config['allowImageCrop'] = true;
265
                $config['imageCropAspectRatio'] = "{$width}:{$height}";
266
                break;
267
            case self::IMAGE_MODE_RESIZE:
268
                //  Cover will respect the aspect ratio and will scale to fill the target dimensions
269
                $config['allowImageResize'] = true;
270
                $config['imageResizeTargetWidth'] = $width;
271
                $config['imageResizeTargetHeight'] = $height;
272
273
                // Don't use these settings and keep api simple
274
                // $config['imageResizeMode'] = 'cover';
275
                // $config['imageResizeUpscale'] = true;
276
                break;
277
            case self::IMAGE_MODE_CROP_RESIZE:
278
                $config['allowImageResize'] = true;
279
                $config['imageResizeTargetWidth'] = $width;
280
                $config['imageResizeTargetHeight'] = $height;
281
                $config['allowImageCrop'] = true;
282
                $config['imageCropAspectRatio'] = "{$width}:{$height}";
283
                break;
284
            default:
285
                throw new Exception("Unsupported '$mode' mode");
286
        }
287
        return $config;
288
    }
289
290
    /**
291
     * @link https://pqina.nl/filepond/docs/api/plugins/image-crop/
292
     * @link https://pqina.nl/filepond/docs/api/plugins/image-resize/
293
     * @link https://pqina.nl/filepond/docs/api/plugins/image-validate-size/
294
     * @param int $width
295
     * @param int $height
296
     * @param string $mode min|max|crop|resize|crop_resize
297
     * @return $this
298
     */
299
    public function setImageSize($width, $height, $mode = null)
300
    {
301
        $config = $this->getImageSizeConfig($width, $height, $mode);
302
        foreach ($config as $k => $v) {
303
            $this->addFilePondConfig($k, $v);
304
        }
305
306
        // We need a custom poster size
307
        $this->adjustPosterSize($width, $height);
308
309
        return $this;
310
    }
311
312
    /**
313
     * This is a frontend alternative to setRenamePattern
314
     *
315
     * @link https://pqina.nl/filepond/docs/api/plugins/file-rename/
316
     * @param string $name The name (extension is added automatically)
317
     * @return $this
318
     */
319
    public function setRenameFile($name)
320
    {
321
        $this->addFilePondConfig('fileRenameFunction', $name);
322
        return $this;
323
    }
324
325
    /**
326
     * @param int $width
327
     * @param int $height
328
     * @return void
329
     */
330
    protected function adjustPosterSize($width, $height)
331
    {
332
        // If the height is smaller than our default, make smaller
333
        if ($height < self::getDefaultPosterHeight()) {
334
            $this->posterHeight = $height;
335
            $this->posterWidth = $width;
336
        } else {
337
            // Adjust width to keep aspect ratio with our default height
338
            $ratio = $height / self::getDefaultPosterHeight();
339
            //@phpstan-ignore-next-line
340
            $this->posterWidth = round($width / $ratio);
341
        }
342
    }
343
344
    /**
345
     * @return int
346
     */
347
    public function getPosterWidth()
348
    {
349
        if ($this->posterWidth) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->posterWidth of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
350
            return $this->posterWidth;
351
        }
352
        return self::getDefaultPosterWidth();
353
    }
354
355
    /**
356
     * @return int
357
     */
358
    public function getPosterHeight()
359
    {
360
        if ($this->posterHeight) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->posterHeight of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
361
            return $this->posterHeight;
362
        }
363
        return self::getDefaultPosterHeight();
364
    }
365
366
    /**
367
     * Return the config applied for this field
368
     *
369
     * Typically converted to json and set in a data attribute
370
     *
371
     * @return array<string,mixed>
372
     */
373
    public function getFilePondConfig()
374
    {
375
        $this->fixName();
376
        $name = $this->getName();
377
        $multiple = $this->getIsMultiUpload();
378
379
        $i18nConfig = [
380
            'labelIdle' => _t('FilePondField.labelIdle', 'Drag & Drop your files or <span class="filepond--label-action"> Browse </span>'),
381
            'labelFileProcessing' => _t('FilePondField.labelFileProcessing', 'Uploading'),
382
            'labelFileProcessingComplete' => _t('FilePondField.labelFileProcessingComplete', 'Upload complete'),
383
            'labelFileProcessingAborted' => _t('FilePondField.labelFileProcessingAborted', 'Upload cancelled'),
384
            'labelTapToCancel' => _t('FilePondField.labelTapToCancel', 'tap to cancel'),
385
            'labelTapToRetry' => _t('FilePondField.labelTapToCancel', 'tap to retry'),
386
            'labelTapToUndo' => _t('FilePondField.labelTapToCancel', 'tap to undo'),
387
        ];
388
389
        // Base config
390
        $config = [
391
            'name' => $name, // This will also apply to the hidden fields
392
            'allowMultiple' => $multiple,
393
            'maxFiles' => $this->getAllowedMaxFileNumber(),
394
            'server' => $this->getServerOptions(),
395
            'files' => $this->getExistingUploadsData(),
396
        ];
397
        $maxFileSize = $this->getMaxFileSize();
398
        if ($maxFileSize) {
399
            $config['maxFileSize'] = $maxFileSize;
400
        }
401
402
        $acceptedFileTypes = $this->getAcceptedFileTypes();
403
        if (!empty($acceptedFileTypes)) {
404
            $config['acceptedFileTypes'] = array_values($acceptedFileTypes);
405
        }
406
407
        // image poster
408
        // @link https://pqina.nl/filepond/docs/api/plugins/file-poster/#usage
409
        if (self::config()->enable_poster) {
410
            $config['filePosterHeight'] = self::config()->poster_height ?? self::DEFAULT_POSTER_HEIGHT;
411
        }
412
413
        // image validation/crop based on record
414
        /** @var DataObject|null $record */
415
        $record = $this->getForm()->getRecord();
416
        if ($record) {
0 ignored issues
show
introduced by
$record is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
417
            $sizes = $record->config()->image_sizes;
418
            $name = $this->getSafeName();
419
            if ($sizes && isset($sizes[$name])) {
420
                $newConfig = $this->getImageSizeConfigFromArray($sizes[$name]);
421
                $config = array_merge($config, $newConfig);
422
                $this->adjustPosterSize($sizes[$name][0], $sizes[$name][1]);
423
            }
424
        }
425
426
427
        // Any custom setting will override the base ones
428
        $config = array_merge($config, $i18nConfig, $this->filePondConfig);
429
430
        return $config;
431
    }
432
433
    /**
434
     * Compute best size for chunks based on server settings
435
     *
436
     * @return float
437
     */
438
    protected function computeMaxChunkSize()
439
    {
440
        $upload_max_filesize = ini_get('upload_max_filesize');
441
        $post_max_size = ini_get('post_max_size');
442
443
        $upload_max_filesize = $upload_max_filesize ? $upload_max_filesize : "2MB";
444
        $post_max_size = $post_max_size ? $post_max_size : "2MB";
445
446
        $maxUpload = Convert::memstring2bytes($upload_max_filesize);
447
        $maxPost = Convert::memstring2bytes($post_max_size);
448
449
        // ~90%, allow some overhead
450
        return round(min($maxUpload, $maxPost) * 0.9);
451
    }
452
453
    /**
454
     * @param array<mixed>|int|string $value
455
     * @param DataObject|array<string,mixed> $record
456
     * @return $this
457
     */
458
    public function setValue($value, $record = null)
459
    {
460
        // Normalize values to something similar to UploadField usage
461
        if (is_numeric($value)) {
462
            $value = ['Files' => [$value]];
463
        } elseif (is_array($value) && empty($value['Files'])) {
464
            // make sure we don't assign {"name":"","full_path":"","type":"","tmp_name":"","error":4,"size":0}
465
            // if $_FILES is not empty
466
            if (isset($value['tmp_name'])) {
467
                $value = null;
468
            }
469
            $value = ['Files' => $value];
470
        }
471
        if ($record) {
472
            $name = $this->name;
473
            if ($record instanceof DataObject && $record->hasMethod($name)) {
474
                $data = $record->$name();
475
                // Wrap
476
                if ($data instanceof DataObject) {
477
                    $data = new ArrayList([$data]);
478
                }
479
                foreach ($data as $uploadedItem) {
480
                    $this->trackFileID($uploadedItem->ID);
481
                }
482
            }
483
        }
484
        //@phpstan-ignore-next-line
485
        return parent::setValue($value, $record);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type string; however, parameter $value of SilverStripe\Forms\FileUploadReceiver::setValue() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

485
        return parent::setValue(/** @scrutinizer ignore-type */ $value, $record);
Loading history...
486
    }
487
488
    /**
489
     * Get the currently used form.
490
     *
491
     * @return Form|null
492
     */
493
    public function getForm()
494
    {
495
        return $this->form;
496
    }
497
498
    /**
499
     * Configure our endpoint
500
     *
501
     * @link https://pqina.nl/filepond/docs/patterns/api/server/
502
     * @return array<mixed>
503
     */
504
    public function getServerOptions()
505
    {
506
        if (!empty($this->customServerConfig)) {
507
            return $this->customServerConfig;
508
        }
509
        if (!$this->getForm()) {
510
            throw new LogicException(
511
                'Field must be associated with a form to call getServerOptions(). Please use $field->setForm($form);'
512
            );
513
        }
514
        $endpoint = $this->getChunkUploads() ? 'chunk' : 'upload';
515
        $server = [
516
            'process' => $this->getUploadEnabled() ? $this->getLinkParameters($endpoint) : null,
517
            'fetch' => null,
518
            'revert' => $this->getUploadEnabled() ? $this->getLinkParameters('revert') : null,
519
        ];
520
        if ($this->getUploadEnabled() && $this->getChunkUploads()) {
521
            $server['fetch'] =  $this->getLinkParameters($endpoint . "?fetch=");
522
            $server['patch'] =  $this->getLinkParameters($endpoint . "?patch=");
523
        }
524
        return $server;
525
    }
526
527
    /**
528
     * Configure the following parameters:
529
     *
530
     * url : Path to the end point
531
     * method : Request method to use
532
     * withCredentials : Toggles the XMLHttpRequest withCredentials on or off
533
     * headers : An object containing additional headers to send
534
     * timeout : Timeout for this action
535
     * onload : Called when server response is received, useful for getting the unique file id from the server response
536
     * onerror : Called when server error is received, receis the response body, useful to select the relevant error data
537
     *
538
     * @param string $action
539
     * @return array<mixed>
540
     */
541
    protected function getLinkParameters($action)
542
    {
543
        $form = $this->getForm();
544
        $token = $form->getSecurityToken()->getValue();
545
        /** @var DataObject|null $record */
546
        $record = $form->getRecord();
547
548
        $headers = [
549
            'X-SecurityID' => $token
550
        ];
551
        // Allow us to track the record instance
552
        if ($record) {
0 ignored issues
show
introduced by
$record is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
553
            $headers['X-RecordClassName'] = get_class($record);
554
            $headers['X-RecordID'] = $record->ID;
555
        }
556
        return [
557
            'url' => $this->Link($action),
558
            'headers' => $headers,
559
        ];
560
    }
561
562
    /**
563
     * The maximum size of a file, for instance 5MB or 750KB
564
     * Suitable for JS usage
565
     *
566
     * @return string
567
     */
568
    public function getMaxFileSize()
569
    {
570
        $size = $this->getValidator()->getAllowedMaxFileSize();
571
        if (!$size) {
572
            return '';
573
        }
574
575
        // Only supports KB and MB
576
        if ($size < 1024 * 1024) {
577
            $size = round($size / 1024) . ' KB';
578
        } else {
579
            $size = round($size / (1024 * 1024)) . ' MB';
580
        }
581
582
        return str_replace(' ', '', $size);
583
    }
584
585
    /**
586
     * Set initial values to FilePondField
587
     * See: https://pqina.nl/filepond/docs/patterns/api/filepond-object/#setting-initial-files
588
     *
589
     * @return array<mixed>
590
     */
591
    public function getExistingUploadsData()
592
    {
593
        // Both Value() & dataValue() seem to return an array eg: ['Files' => [258, 259, 257]]
594
        $fileIDarray = $this->Value() ?: ['Files' => []];
595
        if (!isset($fileIDarray['Files']) || !count($fileIDarray['Files'])) {
596
            return [];
597
        }
598
599
        $existingUploads = [];
600
        foreach ($fileIDarray['Files'] as $fileID) {
601
            /** @var File|null $file */
602
            $file = File::get()->byID($fileID);
603
            if (!$file) {
604
                continue;
605
            }
606
            $existingUpload = [
607
                // the server file reference
608
                'source' => (int) $fileID,
609
                // set type to local to indicate an already uploaded file
610
                'options' => [
611
                    'type' => 'local',
612
                    // file information
613
                    'file' => [
614
                        'name' => $file->Name,
615
                        'size' => (int) $file->getAbsoluteSize(),
616
                        'type' => $file->getMimeType(),
617
                    ],
618
                ],
619
                'metadata' => []
620
            ];
621
622
            // Show poster
623
            // @link https://pqina.nl/filepond/docs/api/plugins/file-poster/#usage
624
            if (self::config()->enable_poster && $file instanceof Image && $file->ID) {
625
                // Size matches the one from asset admin or from or set size
626
                $w = self::getDefaultPosterWidth();
627
                if ($this->posterWidth) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->posterWidth of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
628
                    $w = $this->posterWidth;
629
                }
630
                $h = self::getDefaultPosterHeight();
631
                if ($this->posterHeight) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->posterHeight of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
632
                    $h = $this->posterHeight;
633
                }
634
                /** @var Image|null $resizedImage */
635
                $resizedImage = $file->Fill($w, $h);
636
                if ($resizedImage) {
637
                    $poster = $resizedImage->getAbsoluteURL();
638
                    $existingUpload['options']['metadata']['poster'] = $poster;
639
                }
640
            }
641
            $existingUploads[] = $existingUpload;
642
        }
643
        return $existingUploads;
644
    }
645
646
    /**
647
     * @return int
648
     */
649
    public static function getDefaultPosterWidth()
650
    {
651
        return self::config()->poster_width ?? self::DEFAULT_POSTER_WIDTH;
652
    }
653
654
    /**
655
     * @return int
656
     */
657
    public static function getDefaultPosterHeight()
658
    {
659
        return self::config()->poster_height ?? self::DEFAULT_POSTER_HEIGHT;
660
    }
661
662
    /**
663
     * @return void
664
     */
665
    public static function Requirements()
666
    {
667
        // It includes css styles already
668
        Requirements::javascript('lekoala/silverstripe-filepond: javascript/filepond-input.min.js');
669
    }
670
671
    public function getAttributes()
672
    {
673
        $attrs = parent::getAttributes();
674
675
        $this->fixName();
676
        $attrs['name'] = $this->getName();
677
678
        return $attrs;
679
    }
680
681
    /**
682
     * Make sure the name is correct
683
     * @return void
684
     */
685
    protected function fixName()
686
    {
687
        $name = $this->getName();
688
        $multiple = $this->getIsMultiUpload();
689
690
        // Multi uploads need []
691
        if ($multiple && strpos($name, '[]') === false) {
692
            $name .= '[]';
693
            $this->setName($name);
694
        }
695
    }
696
697
    /**
698
     * @param array<string,mixed> $properties
699
     * @return DBHTMLText
700
     */
701
    public function FieldHolder($properties = array())
702
    {
703
        if (self::config()->enable_requirements) {
704
            self::Requirements();
705
        }
706
        return parent::FieldHolder($properties);
707
    }
708
709
    /**
710
     * @param array<mixed> $properties
711
     * @return DBHTMLText|string
712
     */
713
    public function Field($properties = array())
714
    {
715
        $html = parent::Field($properties);
716
717
        $config = $this->getFilePondConfig();
718
719
        // Simply wrap with custom element and set config
720
        $html = "<filepond-input data-config='" . json_encode($config) . "'>" . $html . '</filepond-input>';
721
722
        return $html;
723
    }
724
725
    /**
726
     * Check the incoming request
727
     *
728
     * @param HTTPRequest $request
729
     * @return array<mixed>
730
     */
731
    public function prepareUpload(HTTPRequest $request)
732
    {
733
        $name = $this->getName();
734
        $tmpFile = $request->postVar($name);
735
        if (!$tmpFile) {
736
            throw new RuntimeException("No file");
737
        }
738
        $tmpFile = $this->normalizeTempFile($tmpFile);
739
740
        // Update $tmpFile with a better name
741
        if ($this->renamePattern) {
742
            $tmpFile['name'] = $this->changeFilenameWithPattern(
743
                $tmpFile['name'],
744
                $this->renamePattern
745
            );
746
        }
747
748
        return $tmpFile;
749
    }
750
751
    /**
752
     * @param HTTPRequest $request
753
     * @return void
754
     */
755
    protected function securityChecks(HTTPRequest $request)
756
    {
757
        if ($this->isDisabled() || $this->isReadonly()) {
758
            throw new RuntimeException("Field is disabled or readonly");
759
        }
760
761
        // CSRF check
762
        $token = $this->getForm()->getSecurityToken();
763
        if (!$token->checkRequest($request)) {
764
            throw new RuntimeException("Invalid token");
765
        }
766
    }
767
768
    /**
769
     * @param File $file
770
     * @param HTTPRequest $request
771
     * @return void
772
     */
773
    protected function setFileDetails(File $file, HTTPRequest $request)
774
    {
775
        // Mark as temporary until properly associated with a record
776
        // Files will be unmarked later on by saveInto method
777
        $file->IsTemporary = true; //@phpstan-ignore-line
778
779
        // We can also track the record
780
        $RecordID = $request->getHeader('X-RecordID');
781
        $RecordClassName = $request->getHeader('X-RecordClassName');
782
        if (!$file->ObjectID) { //@phpstan-ignore-line
783
            $file->ObjectID = $RecordID;
784
        }
785
        if (!$file->ObjectClass) { //@phpstan-ignore-line
786
            $file->ObjectClass = $RecordClassName;
787
        }
788
789
        if ($file->isChanged()) {
790
            // If possible, prevent creating a version for no reason
791
            // @link https://docs.silverstripe.org/en/4/developer_guides/model/versioning/#writing-changes-to-a-versioned-dataobject
792
            if ($file->hasExtension(Versioned::class)) {
793
                $file->writeWithoutVersion();
794
            } else {
795
                $file->write();
796
            }
797
        }
798
    }
799
800
    /**
801
     * Creates a single file based on a form-urlencoded upload.
802
     *
803
     * 1 client uploads file my-file.jpg as multipart/form-data using a POST request
804
     * 2 server saves file to unique location tmp/12345/my-file.jpg
805
     * 3 server returns unique location id 12345 in text/plain response
806
     * 4 client stores unique id 12345 in a hidden input field
807
     * 5 client submits the FilePond parent form containing the hidden input field with the unique id
808
     * 6 server uses the unique id to move tmp/12345/my-file.jpg to its final location and remove the tmp/12345 folder
809
     *
810
     * Along with the file object, FilePond also sends the file metadata to the server, both these objects are given the same name.
811
     *
812
     * @param HTTPRequest $request
813
     * @return HTTPResponse
814
     */
815
    public function upload(HTTPRequest $request)
816
    {
817
        try {
818
            $this->securityChecks($request);
819
            $tmpFile = $this->prepareUpload($request);
820
        } catch (Exception $ex) {
821
            return $this->httpError(400, $ex->getMessage());
822
        }
823
824
        $file = $this->saveTemporaryFile($tmpFile, $error);
825
826
        // Handle upload errors
827
        if ($error) {
828
            $this->getUpload()->clearErrors();
829
            $jsonError = json_encode($error);
830
            $jsonError = $jsonError ? $jsonError : json_last_error_msg();
831
            return $this->httpError(400, $jsonError);
832
        }
833
834
        // File can be an AssetContainer and not a DataObject
835
        if ($file instanceof DataObject) {
836
            $this->setFileDetails($file, $request); //@phpstan-ignore-line
837
        }
838
839
        $this->getUpload()->clearErrors();
840
        $fileId = $file->ID; //@phpstan-ignore-line
0 ignored issues
show
Bug introduced by
Accessing ID on the interface SilverStripe\Assets\Storage\AssetContainer suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
841
        $this->trackFileID($fileId);
842
843
        if (self::config()->auto_clear_temp_folder) {
844
            // Set a limit of 100 because otherwise it would be really slow
845
            self::clearTemporaryUploads(true, 100);
846
        }
847
848
        // server returns unique location id 12345 in text/plain response
849
        $response = new HTTPResponse($fileId);
850
        $response->addHeader('Content-Type', 'text/plain');
851
        return $response;
852
    }
853
854
    /**
855
     * @link https://pqina.nl/filepond/docs/api/server/#process-chunks
856
     * @param HTTPRequest $request
857
     * @return HTTPResponse
858
     */
859
    public function chunk(HTTPRequest $request)
860
    {
861
        try {
862
            $this->securityChecks($request);
863
        } catch (Exception $ex) {
864
            return $this->httpError(400, $ex->getMessage());
865
        }
866
867
        $method = $request->httpMethod();
868
869
        // The random token is returned as a query string
870
        $id = $request->getVar('patch');
871
872
        // FilePond will send a POST request (without file) to start a chunked transfer,
873
        // expecting to receive a unique transfer id in the response body, it'll add the Upload-Length header to this request.
874
        if ($method == "POST") {
875
            // Initial post payload doesn't contain name
876
            // It would be better to return some kind of random token instead
877
            // But FilePond stores the id upon the first request :-(
878
            $file = new File();
879
            $this->setFileDetails($file, $request);
880
            $fileId = $file->ID;
881
            $this->trackFileID($fileId);
882
            $response = new HTTPResponse((string)$fileId, 200);
883
            $response->addHeader('Content-Type', 'text/plain');
884
            return $response;
885
        }
886
887
        // location of patch files
888
        $filePath = TEMP_PATH . "/filepond-" . $id;
889
890
        // FilePond will send a HEAD request to determine which chunks have already been uploaded,
891
        // expecting the file offset of the next expected chunk in the Upload-Offset response header.
892
        if ($method == "HEAD") {
893
            $nextOffset = 0;
894
            while (is_file($filePath . '.patch.' . $nextOffset)) {
895
                $nextOffset++;
896
            }
897
898
            $response = new HTTPResponse((string)$nextOffset, 200);
899
            $response->addHeader('Content-Type', 'text/plain');
900
            $response->addHeader('Upload-Offset', (string)$nextOffset);
901
            return $response;
902
        }
903
904
        // FilePond will send a PATCH request to push a chunk to the server.
905
        // Each of these requests is accompanied by a Content-Type, Upload-Offset, Upload-Name, and Upload-Length header.
906
        if ($method != "PATCH") {
907
            return $this->httpError(400, "Invalid method");
908
        }
909
910
        // The name of the file being transferred
911
        $uploadName = $request->getHeader('Upload-Name');
912
        // The offset of the chunk being transferred (starts with 0)
913
        $offset = $request->getHeader('Upload-Offset');
914
        // The total size of the file being transferred (in bytes)
915
        $length = (int) $request->getHeader('Upload-Length');
916
917
        // should be numeric values, else exit
918
        if (!is_numeric($offset) || !is_numeric($length)) {
919
            return $this->httpError(400, "Invalid offset or length");
920
        }
921
922
        // write patch file for this request
923
        file_put_contents($filePath . '.patch.' . $offset, $request->getBody());
924
925
        // calculate total size of patches
926
        $size = 0;
927
        $patch = glob($filePath . '.patch.*');
928
        if ($patch) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $patch of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
929
            foreach ($patch as $filename) {
930
                $size += filesize($filename);
931
            }
932
        }
933
934
        // check if we are above our size limit
935
        $maxAllowedSize = $this->getValidator()->getAllowedMaxFileSize();
936
        if ($maxAllowedSize && $size > $maxAllowedSize) {
937
            return $this->httpError(400, "File must not be larger than " . $this->getMaxFileSize());
938
        }
939
940
        // if total size equals length of file we have gathered all patch files
941
        if ($size >= $length) {
942
            // create output file
943
            $outputFile = fopen($filePath, 'wb');
944
            if ($patch && $outputFile) {
0 ignored issues
show
introduced by
$outputFile is of type resource, thus it always evaluated to false.
Loading history...
Bug Best Practice introduced by
The expression $patch of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
945
                // write patches to file
946
                foreach ($patch as $filename) {
947
                    // get offset from filename
948
                    list($dir, $offset) = explode('.patch.', $filename, 2);
949
                    // read patch and close
950
                    $patchFile = fopen($filename, 'rb');
951
                    $patchFileSize = filesize($filename);
952
                    if ($patchFile && $patchFileSize) {
953
                        $patchContent = fread($patchFile, $patchFileSize);
954
                        if ($patchContent) {
955
                            fclose($patchFile);
956
957
                            // apply patch
958
                            fseek($outputFile, (int) $offset);
959
                            fwrite($outputFile, $patchContent);
960
                        }
961
                    }
962
                }
963
                // remove patches
964
                foreach ($patch as $filename) {
965
                    unlink($filename);
966
                }
967
                // done with file
968
                fclose($outputFile);
969
            }
970
971
            // Finalize real filename
972
973
            // We need to class this as it mutates the state and set the record if any
974
            $relationClass = $this->getRelationAutosetClass(File::class);
0 ignored issues
show
Unused Code introduced by
The assignment to $relationClass is dead and can be removed.
Loading history...
975
            $realFilename = $this->getFolderName() . "/" . $uploadName;
976
            if ($this->renamePattern) {
977
                $realFilename = $this->changeFilenameWithPattern(
978
                    $realFilename,
979
                    $this->renamePattern
980
                );
981
            }
982
983
            // write output file to asset store
984
            $file = $this->getFileByID($id);
985
            if (!$file) {
986
                return $this->httpError(400, "File $id not found");
987
            }
988
            $file->setFromLocalFile($filePath);
989
            $file->setFilename($realFilename);
990
            $file->Title = $uploadName;
991
            // Set proper class
992
            $relationClass = File::get_class_for_file_extension(
993
                File::get_file_extension($realFilename)
994
            );
995
            $file->setClassName($relationClass);
996
            $file->write();
997
            // Reload file instance to get the right class
998
            // it is not cached so we should get a fresh record
999
            $file = $this->getFileByID($id);
1000
            // since we don't go through our upload object, call extension manually
1001
            $file->extend('onAfterUpload');
1002
1003
            // Cleanup temp files
1004
            $patch = glob($filePath . '.patch.*');
1005
            if ($patch) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $patch of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1006
                foreach ($patch as $filename) {
1007
                    unlink($filename);
1008
                }
1009
            }
1010
        }
1011
        $response = new HTTPResponse('', 204);
1012
        return $response;
1013
    }
1014
1015
    /**
1016
     * @link https://pqina.nl/filepond/docs/api/server/#revert
1017
     * @param HTTPRequest $request
1018
     * @return HTTPResponse
1019
     */
1020
    public function revert(HTTPRequest $request)
1021
    {
1022
        try {
1023
            $this->securityChecks($request);
1024
        } catch (Exception $ex) {
1025
            return $this->httpError(400, $ex->getMessage());
1026
        }
1027
1028
        $method = $request->httpMethod();
1029
1030
        if ($method != "DELETE") {
1031
            return $this->httpError(400, "Invalid method");
1032
        }
1033
1034
        $fileID = (int) $request->getBody();
1035
        if (!in_array($fileID, $this->getTrackedIDs())) {
1036
            return $this->httpError(400, "Invalid ID");
1037
        }
1038
        $file = File::get()->byID($fileID);
1039
        if (!$file->IsTemporary) {
1040
            return $this->httpError(400, "Invalid file");
1041
        }
1042
        if (!$file->canDelete()) {
1043
            return $this->httpError(400, "Cannot delete file");
1044
        }
1045
        $file->delete();
1046
        $response = new HTTPResponse('', 200);
1047
        return $response;
1048
    }
1049
1050
    /**
1051
     * Clear temp folder that should not contain any file other than temporary
1052
     *
1053
     * @param boolean $doDelete Set to true to actually delete the files, otherwise it's just a dry-run
1054
     * @param int $limit
1055
     * @return File[] List of files removed
1056
     */
1057
    public static function clearTemporaryUploads($doDelete = false, $limit = 0)
1058
    {
1059
        $tempFiles = File::get()->filter('IsTemporary', true);
1060
        if ($limit) {
1061
            $tempFiles = $tempFiles->limit($limit);
1062
        }
1063
1064
        $threshold = self::config()->auto_clear_threshold;
1065
1066
        // Set a default threshold if none set
1067
        if (!$threshold) {
1068
            if (Director::isDev()) {
1069
                $threshold = '-10 minutes';
1070
            } else {
1071
                $threshold = '-1 day';
1072
            }
1073
        }
1074
        if (is_int($threshold)) {
1075
            $thresholdTime = time() - $threshold;
1076
        } else {
1077
            $thresholdTime = strtotime($threshold);
1078
        }
1079
1080
        // Update query to avoid fetching unecessary records
1081
        $tempFiles = $tempFiles->filter("Created:LessThan", date('Y-m-d H:i:s', $thresholdTime));
1082
1083
        $filesDeleted = [];
1084
        foreach ($tempFiles as $tempFile) {
1085
            $createdTime = strtotime($tempFile->Created);
1086
            if ($createdTime < $thresholdTime) {
1087
                $filesDeleted[] = $tempFile;
1088
                if ($doDelete) {
1089
                    if ($tempFile->hasExtension(Versioned::class)) {
1090
                        $tempFile->deleteFromStage(Versioned::LIVE);
1091
                        $tempFile->deleteFromStage(Versioned::DRAFT);
1092
                    } else {
1093
                        $tempFile->delete();
1094
                    }
1095
                }
1096
            }
1097
        }
1098
        return $filesDeleted;
1099
    }
1100
1101
    /**
1102
     * Allows tracking uploaded ids to prevent unauthorized attachements
1103
     *
1104
     * @param int|string $fileId
1105
     * @return void
1106
     */
1107
    public function trackFileID($fileId)
1108
    {
1109
        $fileId = is_string($fileId) ? intval($fileId) : $fileId;
1110
        $session = $this->getRequest()->getSession();
1111
        $uploadedIDs = $this->getTrackedIDs();
1112
        if (!in_array($fileId, $uploadedIDs)) {
1113
            $uploadedIDs[] = $fileId;
1114
        }
1115
        $session->set('FilePond', $uploadedIDs);
1116
    }
1117
1118
    /**
1119
     * Get all authorized tracked ids
1120
     * @return array<mixed>
1121
     */
1122
    public function getTrackedIDs()
1123
    {
1124
        $session = $this->getRequest()->getSession();
1125
        $uploadedIDs = $session->get('FilePond');
1126
        if ($uploadedIDs) {
1127
            return $uploadedIDs;
1128
        }
1129
        return [];
1130
    }
1131
1132
    public function saveInto(DataObjectInterface $record)
1133
    {
1134
        // Note that the list of IDs is based on the value sent by the user
1135
        // It can be spoofed because checks are minimal (by default, canView = true and only check if isInDB)
1136
        $IDs = $this->getItemIDs();
1137
1138
        $Member = Security::getCurrentUser();
1139
1140
        // Ensure the files saved into the DataObject have been tracked (either because already on the DataObject or uploaded by the user)
1141
        $trackedIDs = $this->getTrackedIDs();
1142
        foreach ($IDs as $ID) {
1143
            if (!in_array($ID, $trackedIDs)) {
1144
                throw new ValidationException("Invalid file ID : $ID");
1145
            }
1146
        }
1147
1148
        // Move files out of temporary folder
1149
        foreach ($IDs as $ID) {
1150
            $file = $this->getFileByID($ID);
1151
            //@phpstan-ignore-next-line
1152
            if ($file && $file->IsTemporary) {
1153
                // The record does not have an ID which is a bad idea to attach the file to it
1154
                if (!$record->ID) {
1155
                    $record->write();
1156
                }
1157
                // Check if the member is owner
1158
                if ($Member && $Member->ID != $file->OwnerID) {
1159
                    throw new ValidationException("Failed to authenticate owner");
1160
                }
1161
                $file->IsTemporary = false;
1162
                $file->ObjectID = $record->ID; //@phpstan-ignore-line
1163
                $file->ObjectClass = get_class($record); //@phpstan-ignore-line
1164
                $file->write();
1165
            } else {
1166
                // File was uploaded earlier, no need to do anything
1167
            }
1168
        }
1169
1170
        // Proceed
1171
        parent::saveInto($record);
1172
1173
        return $this;
1174
    }
1175
1176
    /**
1177
     * @return string
1178
     */
1179
    public function Type()
1180
    {
1181
        return 'filepond';
1182
    }
1183
}
1184