-
Notifications
You must be signed in to change notification settings - Fork 101
/
MediaUploader.php
1020 lines (887 loc) · 28.9 KB
/
MediaUploader.php
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
namespace Plank\Mediable;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Filesystem\FilesystemManager;
use League\Flysystem\UnableToRetrieveMetadata;
use Plank\Mediable\Exceptions\MediaUpload\ConfigurationException;
use Plank\Mediable\Exceptions\MediaUpload\FileExistsException;
use Plank\Mediable\Exceptions\MediaUpload\FileNotFoundException;
use Plank\Mediable\Exceptions\MediaUpload\FileNotSupportedException;
use Plank\Mediable\Exceptions\MediaUpload\FileSizeException;
use Plank\Mediable\Exceptions\MediaUpload\ForbiddenException;
use Plank\Mediable\Helpers\File;
use Plank\Mediable\SourceAdapters\RawContentAdapter;
use Plank\Mediable\SourceAdapters\SourceAdapterFactory;
/**
* Media Uploader.
*
* Validates files, uploads them to disk and generates Media
*/
class MediaUploader
{
const ON_DUPLICATE_UPDATE = 'update';
const ON_DUPLICATE_INCREMENT = 'increment';
const ON_DUPLICATE_ERROR = 'error';
const ON_DUPLICATE_REPLACE = 'replace';
const ON_DUPLICATE_REPLACE_WITH_VARIANTS = 'replace_with_variants';
/**
* @var FileSystemManager
*/
private $filesystem;
/**
* @var SourceAdapterFactory
*/
private $factory;
/**
* Mediable configurations.
* @var array
*/
private $config;
/**
* Source adapter.
* @var \Plank\Mediable\SourceAdapters\SourceAdapterInterface
*/
private $source;
/**
* Name of the filesystem disk.
* @var string
*/
private $disk;
/**
* Path relative to the filesystem disk root.
* @var string
*/
private $directory = '';
/**
* Name of the new file.
* @var string|null
*/
private $filename = null;
/**
* If true the contents hash of the source will be used as the filename.
* @var bool
*/
private $hashFilename = false;
/**
* Visibility for the new file
* @var string
*/
private $visibility = Filesystem::VISIBILITY_PUBLIC;
/**
* Callable allowing to alter the model before save.
* @var callable
*/
private $before_save;
/**
* Additional options to pass to the filesystem while uploading
* @var array
*/
private $options = [];
/**
* Constructor.
* @param FilesystemManager $filesystem
* @param SourceAdapterFactory $factory
* @param array|null $config
*/
public function __construct(FileSystemManager $filesystem, SourceAdapterFactory $factory, array $config = null)
{
$this->filesystem = $filesystem;
$this->factory = $factory;
$this->config = $config ?: config('mediable', []);
}
/**
* Set the source for the file.
*
* @param mixed $source
*
* @return $this
* @throws ConfigurationException
*/
public function fromSource($source): self
{
$this->source = $this->factory->create($source);
return $this;
}
/**
* Set the source for the string data.
* @param string $source
* @return $this
*/
public function fromString(string $source): self
{
$this->source = new RawContentAdapter($source);
return $this;
}
/**
* Set the filesystem disk and relative directory where the file will be saved.
*
* @param string $disk
* @param string $directory
*
* @return $this
* @throws ConfigurationException
* @throws ForbiddenException
*/
public function toDestination(string $disk, string $directory): self
{
return $this->toDisk($disk)->toDirectory($directory);
}
/**
* Set the filesystem disk on which the file will be saved.
*
* @param string $disk
*
* @return $this
* @throws ConfigurationException
* @throws ForbiddenException
*/
public function toDisk(string $disk): self
{
$this->disk = $this->verifyDisk($disk);
return $this;
}
/**
* Set the directory relative to the filesystem disk at which the file will be saved.
* @param string $directory
* @return $this
*/
public function toDirectory(string $directory): self
{
$this->directory = File::sanitizePath($directory);
return $this;
}
/**
* Specify the filename to copy to the file to.
* @param string $filename
* @return $this
*/
public function useFilename(string $filename): self
{
$this->filename = File::sanitizeFilename($filename);
$this->hashFilename = false;
return $this;
}
/**
* Indicates to the uploader to generate a filename using the file's MD5 hash.
* @return $this
*/
public function useHashForFilename(): self
{
$this->hashFilename = true;
$this->filename = null;
return $this;
}
/**
* Restore the default behaviour of using the source file's filename.
* @return $this
*/
public function useOriginalFilename(): self
{
$this->filename = null;
$this->hashFilename = false;
return $this;
}
/**
* Change the class to use for generated Media.
* @param string $class
* @return $this
* @throws ConfigurationException if $class does not extend Plank\Mediable\Media
*/
public function setModelClass(string $class): self
{
if (!is_subclass_of($class, Media::class)) {
throw ConfigurationException::cannotSetModel($class);
}
$this->config['model'] = $class;
return $this;
}
/**
* Change the maximum allowed file size.
* @param int $size
* @return $this
*/
public function setMaximumSize(int $size): self
{
$this->config['max_size'] = (int)$size;
return $this;
}
/**
* Change the behaviour for when a file already exists at the destination.
* @param string $behavior
* @return $this
*/
public function setOnDuplicateBehavior(string $behavior): self
{
$this->config['on_duplicate'] = (string)$behavior;
return $this;
}
/**
* Get current behavior when duplicate file is uploaded.
*
* @return string
*/
public function getOnDuplicateBehavior(): string
{
return $this->config['on_duplicate'];
}
/**
* Throw an exception when file already exists at the destination.
*
* @return $this
*/
public function onDuplicateError(): self
{
return $this->setOnDuplicateBehavior(self::ON_DUPLICATE_ERROR);
}
/**
* Append incremented counter to file name when file already exists at destination.
*
* @return $this
*/
public function onDuplicateIncrement(): self
{
return $this->setOnDuplicateBehavior(self::ON_DUPLICATE_INCREMENT);
}
/**
* Overwrite existing Media when file already exists at destination.
*
* This will delete the old media record and create a new one, detaching any existing associations.
*
* @return $this
*/
public function onDuplicateReplace(): self
{
return $this->setOnDuplicateBehavior(self::ON_DUPLICATE_REPLACE);
}
/**
* Overwrite existing Media when file already exists at destination and delete any variants of the original record.
*
* This will delete the old media record and create a new one, detaching any existing associations.
*
* This will also delete any existing
*
* @return $this
*/
public function onDuplicateReplaceWithVariants(): self
{
return $this->setOnDuplicateBehavior(self::ON_DUPLICATE_REPLACE_WITH_VARIANTS);
}
/**
* Overwrite existing files and update the existing media record.
*
* This will retain any existing associations.
*
* @return $this
*/
public function onDuplicateUpdate(): self
{
return $this->setOnDuplicateBehavior(self::ON_DUPLICATE_UPDATE);
}
/**
* Change whether both the MIME type and extensions must match the same aggregate type.
* @param bool $strict
* @return $this
*/
public function setStrictTypeChecking(bool $strict): self
{
$this->config['strict_type_checking'] = $strict;
return $this;
}
/**
* Change whether files not matching any aggregate types are allowed.
* @param bool $allow
* @return $this
*/
public function setAllowUnrecognizedTypes(bool $allow): self
{
$this->config['allow_unrecognized_types'] = $allow;
return $this;
}
/**
* Add or update the definition of a aggregate type.
* @param string $type the name of the type
* @param string[] $mimeTypes list of MIME types recognized
* @param string[] $extensions list of file extensions recognized
* @return $this
*/
public function setTypeDefinition(string $type, array $mimeTypes, array $extensions): self
{
$this->config['aggregate_types'][$type] = [
'mime_types' => array_map('strtolower', $mimeTypes),
'extensions' => array_map('strtolower', $extensions),
];
return $this;
}
/**
* Set a list of MIME types that the source file must be restricted to.
* @param string[] $allowedMimes
* @return $this
*/
public function setAllowedMimeTypes(array $allowedMimes): self
{
$this->config['allowed_mime_types'] = array_map('strtolower', $allowedMimes);
return $this;
}
/**
* Set a list of file extensions that the source file must be restricted to.
* @param string[] $allowedExtensions
* @return $this
*/
public function setAllowedExtensions(array $allowedExtensions): self
{
$this->config['allowed_extensions'] = array_map('strtolower', $allowedExtensions);
return $this;
}
/**
* Set a list of aggregate types that the source file must be restricted to.
* @param string[] $allowedTypes
* @return $this
*/
public function setAllowedAggregateTypes(array $allowedTypes): self
{
$this->config['allowed_aggregate_types'] = $allowedTypes;
return $this;
}
/**
* Make the resulting file public (default behaviour)
* @return $this
*/
public function makePublic(): self
{
$this->visibility = Filesystem::VISIBILITY_PUBLIC;
return $this;
}
/**
* Make the resulting file private
* @return $this
*/
public function makePrivate(): self
{
$this->visibility = Filesystem::VISIBILITY_PRIVATE;
return $this;
}
/**
* Additional options to pass to the filesystem when uploading
* @param array $options
* @return $this
*/
public function withOptions(array $options): self
{
$this->options = $options;
return $this;
}
/**
* Determine the aggregate type of the file based on the MIME type and the extension.
* @param string $mimeType
* @param string $extension
* @return string
* @throws FileNotSupportedException If the file type is not recognized
* @throws FileNotSupportedException If the file type is restricted
* @throws FileNotSupportedException If the aggregate type is restricted
*/
public function inferAggregateType(string $mimeType, string $extension): string
{
$mimeType = strtolower($mimeType);
$extension = strtolower($extension);
$allowedTypes = $this->config['allowed_aggregate_types'] ?? [];
$typesForMime = $this->possibleAggregateTypesForMimeType($mimeType);
$typesForExtension = $this->possibleAggregateTypesForExtension($extension);
if (count($allowedTypes)) {
$intersection = array_intersect($typesForMime, $typesForExtension, $allowedTypes);
} else {
$intersection = array_intersect($typesForMime, $typesForExtension);
}
if (count($intersection)) {
$type = $intersection[0];
} elseif (empty($typesForMime) && empty($typesForExtension)) {
if (!$this->config['allow_unrecognized_types'] ?? false) {
throw FileNotSupportedException::unrecognizedFileType($mimeType, $extension);
}
$type = Media::TYPE_OTHER;
} else {
if ($this->config['strict_type_checking'] ?? false) {
throw FileNotSupportedException::strictTypeMismatch($mimeType, $extension);
}
$merged = array_merge($typesForMime, $typesForExtension);
$type = reset($merged);
}
if (count($allowedTypes) && !in_array($type, $allowedTypes)) {
throw FileNotSupportedException::aggregateTypeRestricted($type, $allowedTypes);
}
return $type;
}
/**
* Determine the aggregate type of the file based on the MIME type.
* @param string $mime
* @return string[]
*/
public function possibleAggregateTypesForMimeType(string $mime): array
{
$types = [];
foreach ($this->config['aggregate_types'] ?? [] as $type => $attributes) {
if (in_array($mime, $attributes['mime_types'])) {
$types[] = $type;
}
}
return $types;
}
/**
* Determine the aggregate type of the file based on the extension.
* @param string $extension
* @return string[]
*/
public function possibleAggregateTypesForExtension(string $extension): array
{
$types = [];
foreach ($this->config['aggregate_types'] ?? [] as $type => $attributes) {
if (in_array($extension, $attributes['extensions'])) {
$types[] = $type;
}
}
return $types;
}
/**
* Process the file upload.
*
* Validates the source, then stores the file onto the disk and creates and stores a new Media instance.
*
* @return Media
* @throws ConfigurationException
* @throws FileExistsException
* @throws FileNotFoundException
* @throws FileNotSupportedException
* @throws FileSizeException
*/
public function upload(): Media
{
$this->verifyFile();
$model = $this->populateModel($this->makeModel());
if (is_callable($this->before_save)) {
call_user_func($this->before_save, $model, $this->source);
}
$this->verifyDestination($model);
$this->writeToDisk($model);
$model->save();
return $model;
}
/**
* Process the file upload, overwriting an existing media's file
*
* Uploader will automatically place the file on the same disk as the original media.
*
* @param Media $media
* @return Media
*
* @throws ConfigurationException
* @throws FileNotFoundException
* @throws FileNotSupportedException
* @throws FileSizeException
* @throws ForbiddenException
* @throws FileExistsException
*/
public function replace(Media $media): Media
{
if (!$this->disk) {
$this->toDisk($media->disk);
}
if (!$this->directory) {
$this->toDirectory($media->directory);
}
if (!$this->filename) {
$this->useFilename($media->filename);
}
// Remember original file location.
// We will only delete it if validation passes
$disk = $media->disk;
$path = $media->getDiskPath();
$model = $this->populateModel($media);
if (is_callable($this->before_save)) {
call_user_func($this->before_save, $model, $this->source);
}
$this->verifyDestination($model);
// Delete original file, if necessary
$this->filesystem->disk($disk)->delete($path);
$this->writeToDisk($model);
$model->save();
return $model;
}
/**
* Validate input and convert to Media attributes
* @param Media $model
* @return Media
*
* @throws ConfigurationException
* @throws FileNotFoundException
* @throws FileNotSupportedException
* @throws FileSizeException
*/
private function populateModel(Media $model): Media
{
$model->size = $this->verifyFileSize($this->source->size());
$model->mime_type = $this->verifyMimeType($this->source->mimeType());
$model->extension = $this->verifyExtension($this->source->extension());
$model->aggregate_type = $this->inferAggregateType($model->mime_type, $model->extension);
$model->disk = $this->disk ?: $this->config['default_disk'];
$model->directory = $this->directory;
$model->filename = $this->generateFilename();
return $model;
}
/**
* Set the before save callback
* @param callable $callable
* @return $this
*/
public function beforeSave(callable $callable): self
{
$this->before_save = $callable;
return $this;
}
/**
* Create a `Media` record for a file already on a disk.
*
* @param string $disk
* @param string $path Path to file, relative to disk root
*
* @return Media
* @throws ConfigurationException
* @throws FileNotFoundException
* @throws FileNotSupportedException
* @throws FileSizeException
* @throws ForbiddenException
*/
public function importPath(string $disk, string $path): Media
{
$directory = File::cleanDirname($path);
$filename = pathinfo($path, PATHINFO_FILENAME);
$extension = pathinfo($path, PATHINFO_EXTENSION);
return $this->import($disk, $directory, $filename, $extension);
}
/**
* Create a `Media` record for a file already on a disk.
*
* @param string $disk
* @param string $directory
* @param string $filename
* @param string $extension
*
* @return Media
* @throws ConfigurationException
* @throws FileNotFoundException If the file does not exist
* @throws FileNotSupportedException
* @throws FileSizeException
* @throws ForbiddenException
*/
public function import(string $disk, string $directory, string $filename, string $extension): Media
{
$disk = $this->verifyDisk($disk);
$storage = $this->filesystem->disk($disk);
$model = $this->makeModel();
$model->disk = $disk;
$model->directory = $directory;
$model->filename = $filename;
$model->extension = $this->verifyExtension($extension, false);
if (!$storage->has($model->getDiskPath())) {
throw FileNotFoundException::fileNotFound($model->getDiskPath());
}
$model->mime_type = $this->verifyMimeType(
$this->inferMimeType($storage, $model->getDiskPath())
);
$model->aggregate_type = $this->inferAggregateType($model->mime_type, $model->extension);
$model->size = $this->verifyFileSize($storage->size($model->getDiskPath()));
$storage->setVisibility($model->getDiskPath(), $this->visibility);
if (is_callable($this->before_save)) {
call_user_func($this->before_save, $model, $this->source);
}
$model->save();
return $model;
}
/**
* Reanalyze a media record's file and adjust the aggregate type and size, if necessary.
*
* @param Media $media
*
* @return bool Whether the model was modified
* @throws FileNotSupportedException
* @throws FileSizeException
*/
public function update(Media $media): bool
{
$storage = $this->filesystem->disk($media->disk);
$media->size = $this->verifyFileSize($storage->size($media->getDiskPath()));
$media->mime_type = $this->verifyMimeType(
$this->inferMimeType($storage, $media->getDiskPath())
);
$media->aggregate_type = $this->inferAggregateType($media->mime_type, $media->extension);
if ($dirty = $media->isDirty()) {
$media->save();
}
return $dirty;
}
/**
* Verify if file is valid
* @throws ConfigurationException If no source is provided
* @throws FileNotFoundException If the source is invalid
* @throws FileSizeException If the file is too large
* @throws FileNotSupportedException If the mime type is not allowed
* @throws FileNotSupportedException If the file extension is not allowed
* @return void
*/
public function verifyFile(): void
{
$this->verifySource();
$this->verifyFileSize($this->source->size());
$this->verifyMimeType($this->source->mimeType());
$this->verifyExtension($this->source->extension());
}
/**
* Generate an instance of the `Media` class.
* @return Media
*/
private function makeModel(): Media
{
$class = $this->config['model'] ?? Media::class;
return new $class;
}
/**
* Ensure that the provided filesystem disk name exists and is allowed.
* @param string $disk
* @return string
* @throws ConfigurationException If the disk does not exist
* @throws ForbiddenException If the disk is not included in the `allowed_disks` config.
*/
private function verifyDisk(string $disk): string
{
if (!array_key_exists($disk, config('filesystems.disks', []))) {
throw ConfigurationException::diskNotFound($disk);
}
if (!in_array($disk, $this->config['allowed_disks'] ?? [])) {
throw ForbiddenException::diskNotAllowed($disk);
}
return $disk;
}
/**
* Ensure that a valid source has been provided.
* @return void
* @throws ConfigurationException If no source is provided
* @throws FileNotFoundException If the source is invalid
*/
private function verifySource(): void
{
if (empty($this->source)) {
throw ConfigurationException::noSourceProvided();
}
if (!$this->source->valid()) {
throw FileNotFoundException::fileNotFound($this->source->path());
}
}
private function inferMimeType(Filesystem $filesystem, string $path): string
{
try {
$mime = $filesystem->mimeType($path);
} catch (UnableToRetrieveMetadata $e) {
// previous versions of flysystem would default to octet-stream when
// the file was unrecognized. Maintain the behaviour for now
return 'application/octet-stream';
}
return $mime ?: 'application/octet-stream';
}
/**
* Ensure that the file's mime type is allowed.
* @param string $mimeType
* @return string
* @throws FileNotSupportedException If the mime type is not allowed
*/
private function verifyMimeType(string $mimeType): string
{
$mimeType = strtolower($mimeType);
$allowed = $this->config['allowed_mime_types'] ?? [];
if (!empty($allowed) && !in_array($mimeType, $allowed)) {
throw FileNotSupportedException::mimeRestricted($mimeType, $allowed);
}
return $mimeType;
}
/**
* Ensure that the file's extension is allowed.
* @param string $extension
* @return string
* @throws FileNotSupportedException If the file extension is not allowed
*/
private function verifyExtension(string $extension, bool $toLower = true): string
{
$extensionLower = strtolower($extension);
$allowed = $this->config['allowed_extensions'] ?? [];
if (!empty($allowed) && !in_array($extensionLower, $allowed)) {
throw FileNotSupportedException::extensionRestricted($extensionLower, $allowed);
}
return $toLower ? $extensionLower : $extension;
}
/**
* Verify that the file being uploaded is not larger than the maximum.
* @param int $size
* @return int
* @throws FileSizeException If the file is too large
*/
private function verifyFileSize(int $size): int
{
$max = $this->config['max_size'] ?? 0;
if ($max > 0 && $size > $max) {
throw FileSizeException::fileIsTooBig($size, $max);
}
return $size;
}
/**
* Verify that the intended destination is available and handle any duplications.
* @param Media $model
* @return void
*
* @throws FileExistsException
*/
private function verifyDestination(Media $model): void
{
$storage = $this->filesystem->disk($model->disk);
if ($storage->has($model->getDiskPath())) {
$this->handleDuplicate($model);
}
}
/**
* Decide what to do about duplicated files.
*
* @param Media $model
* @return Media
* @throws FileExistsException If directory is not writable or file already exists at the destination and on_duplicate is set to 'error'
*/
private function handleDuplicate(Media $model): Media
{
switch ($this->config['on_duplicate'] ?? MediaUploader::ON_DUPLICATE_INCREMENT) {
case static::ON_DUPLICATE_ERROR:
throw FileExistsException::fileExists($model->getDiskPath());
case static::ON_DUPLICATE_REPLACE:
$this->deleteExistingMedia($model);
break;
case static::ON_DUPLICATE_REPLACE_WITH_VARIANTS:
$this->deleteExistingMedia($model, true);
break;
case static::ON_DUPLICATE_UPDATE:
$original = $model->newQuery()
->where('disk', $model->disk)
->where('directory', $model->directory)
->where('filename', $model->filename)
->where('extension', $model->extension)
->first();
if ($original) {
$model->{$model->getKeyName()} = $original->getKey();
$model->exists = true;
}
break;
case static::ON_DUPLICATE_INCREMENT:
default:
$model->filename = $this->generateUniqueFilename($model);
}
return $model;
}
/**
* Delete the media that previously existed at a destination.
* @param Media $model
* @return void
*/
private function deleteExistingMedia(Media $model, bool $withVariants = false): void
{
$original = $model->newQuery()
->where('disk', $model->disk)
->where('directory', $model->directory)
->where('filename', $model->filename)
->where('extension', $model->extension)
->first();
if ($original) {
$models = $withVariants ? $original->getAllVariantsAndSelf() : collect([$original]);
$models->each(
function (Media $variant) {
$variant->delete();
$this->deleteExistingFile($variant);
}
);
}
}
/**
* Delete the file on disk.
* @param Media $model
* @return void
*/
private function deleteExistingFile(Media $model): void
{
$this->filesystem->disk($model->disk)->delete($model->getDiskPath());
}
/**
* Increment model's filename until one is found that doesn't already exist.
* @param Media $model
* @return string
*/
private function generateUniqueFilename(Media $model): string
{
$storage = $this->filesystem->disk($model->disk);
$counter = 0;
do {
$filename = "{$model->filename}";
if ($counter > 0) {
$filename .= '-' . $counter;
}
$path = "{$model->directory}/{$filename}.{$model->extension}";
++$counter;
} while ($storage->has($path));
return $filename;
}
/**
* Generate the model's filename.
* @return string
*/
private function generateFilename(): string
{
if ($this->filename) {
return $this->filename;
}
if ($this->hashFilename) {
return $this->generateHash();
}
return File::sanitizeFileName($this->source->filename());
}
/**
* Calculate hash of source contents.
* @return string
*/
private function generateHash(): string
{
$ctx = hash_init('md5');
// We don't need to read the file contents if the source has a path
if ($this->source->path()) {
hash_update_file($ctx, $this->source->path());
} else {
hash_update($ctx, $this->source->contents());
}
return hash_final($ctx);
}
private function writeToDisk(Media $model): void
{
$stream = $this->source->getStreamResource();
if (!is_resource($stream)) {
$stream = $this->source->contents();
};
$this->filesystem->disk($model->disk)