|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\AssetLibrary\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Http\File; |
|
7
|
|
|
use Illuminate\Http\UploadedFile; |
|
8
|
|
|
use Spatie\MediaLibrary\HasMedia\HasMediaTrait; |
|
9
|
|
|
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMediaConversions; |
|
10
|
|
|
use Spatie\MediaLibrary\Media; |
|
11
|
|
|
use Thinktomorrow\Locale\Locale; |
|
12
|
|
|
|
|
13
|
|
|
class AssetUploader extends Model |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Uploads the file/files or asset by creating the |
|
17
|
|
|
* asset that is needed to upload the files too. |
|
18
|
|
|
* |
|
19
|
|
|
* @param $files |
|
20
|
|
|
* @param bool $keepOriginal |
|
21
|
|
|
* @return \Illuminate\Support\Collection|null|Asset |
|
|
|
|
|
|
22
|
|
|
*/ |
|
23
|
|
|
public static function upload($files, $keepOriginal = false) |
|
24
|
|
|
{ |
|
25
|
|
|
$list = collect([]); |
|
26
|
|
|
|
|
27
|
|
|
if ($files instanceof Asset) { |
|
28
|
|
|
return $files; |
|
29
|
|
|
} elseif (is_array($files)) { |
|
30
|
|
|
collect($files)->each(function ($file) use ($list) { |
|
31
|
|
|
if ($file instanceof Asset) { |
|
32
|
|
|
$list->push($file); |
|
33
|
|
|
} else { |
|
34
|
|
|
$self = new Asset(); |
|
35
|
|
|
$self->save(); |
|
36
|
|
|
$list->push(self::uploadToAsset($file, $self)); |
|
37
|
|
|
} |
|
38
|
|
|
}); |
|
39
|
|
|
|
|
40
|
|
|
return $list; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$self = new Asset(); |
|
44
|
|
|
$self->save(); |
|
45
|
|
|
|
|
46
|
|
|
return self::uploadToAsset($files, $self, $keepOriginal); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Uploads the given file to this instance of asset |
|
51
|
|
|
* and sets the dimensions as a custom property. |
|
52
|
|
|
* |
|
53
|
|
|
* @param $files |
|
54
|
|
|
* @param bool $keepOriginal |
|
55
|
|
|
* @return $this|null |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function uploadToAsset($files, $asset, $keepOriginal = false) |
|
58
|
|
|
{ |
|
59
|
|
|
if (! ($files instanceof File) && ! ($files instanceof UploadedFile)) { |
|
60
|
|
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$customProps = []; |
|
64
|
|
|
if (self::isImage($files)) { |
|
65
|
|
|
$customProps['dimensions'] = getimagesize($files)[0].' x '.getimagesize($files)[1]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$fileAdd = $asset->addMedia($files)->withCustomProperties($customProps); |
|
69
|
|
|
if ($keepOriginal) { |
|
70
|
|
|
$fileAdd = $fileAdd->preservingOriginal(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$fileAdd->toMediaCollection(); |
|
74
|
|
|
|
|
75
|
|
|
return $asset->load('media'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private static function isImage($file) |
|
79
|
|
|
{ |
|
80
|
|
|
return str_before($file->getMimetype(), '/') === 'image'; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.