|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibrary\Filesystem; |
|
4
|
|
|
|
|
5
|
|
|
use Spatie\MediaLibrary\Media; |
|
6
|
|
|
use Spatie\MediaLibrary\Helpers\File; |
|
7
|
|
|
use Spatie\MediaLibrary\FileManipulator; |
|
8
|
|
|
use Illuminate\Contracts\Filesystem\Factory; |
|
9
|
|
|
use Spatie\MediaLibrary\Events\MediaHasBeenAdded; |
|
10
|
|
|
use Spatie\MediaLibrary\PathGenerator\PathGeneratorFactory; |
|
11
|
|
|
|
|
12
|
|
|
class DefaultFilesystem implements Filesystem |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var \Illuminate\Contracts\Filesystem\Factory */ |
|
15
|
|
|
protected $filesystem; |
|
16
|
|
|
|
|
17
|
|
|
/** @var array */ |
|
18
|
|
|
protected $customRemoteHeaders = []; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(Factory $filesystems) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->filesystem = $filesystems; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/* |
|
26
|
|
|
* Add a file to the mediaLibrary for the given media. |
|
27
|
|
|
*/ |
|
28
|
|
|
public function add(string $file, Media $media, string $targetFileName = '') |
|
29
|
|
|
{ |
|
30
|
|
|
$this->copyToMediaLibrary($file, $media, false, $targetFileName); |
|
31
|
|
|
|
|
32
|
|
|
event(new MediaHasBeenAdded($media)); |
|
33
|
|
|
|
|
34
|
|
|
app(FileManipulator::class)->createDerivedFiles($media); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/* |
|
38
|
|
|
* Copy a file to the medialibrary for the given $media. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function copyToMediaLibrary(string $pathToFile, Media $media, bool $conversions = false, string $targetFileName = '') |
|
41
|
|
|
{ |
|
42
|
|
|
$destination = $this->getMediaDirectory($media, $conversions). |
|
43
|
|
|
($targetFileName == '' ? pathinfo($pathToFile, PATHINFO_BASENAME) : $targetFileName); |
|
44
|
|
|
|
|
45
|
|
|
$file = fopen($pathToFile, 'r'); |
|
46
|
|
|
|
|
47
|
|
|
if ($media->getDiskDriverName() === 'local') { |
|
48
|
|
|
$this->filesystem |
|
49
|
|
|
->disk($media->disk) |
|
50
|
|
|
->put($destination, $file); |
|
51
|
|
|
|
|
52
|
|
|
fclose($file); |
|
53
|
|
|
|
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->filesystem |
|
58
|
|
|
->disk($media->disk) |
|
59
|
|
|
->put($destination, $file, $this->getRemoteHeadersForFile($pathToFile)); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
if (is_resource($file)) { |
|
62
|
|
|
fclose($file); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Add custom remote headers on runtime. |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $customRemoteHeaders |
|
70
|
|
|
*/ |
|
71
|
|
|
public function addCustomRemoteHeaders(array $customRemoteHeaders) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->customRemoteHeaders = $customRemoteHeaders; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/* |
|
77
|
|
|
* Get the headers to be used when copying the |
|
78
|
|
|
* given file to a remote filesytem. |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getRemoteHeadersForFile(string $file) : array |
|
81
|
|
|
{ |
|
82
|
|
|
$mimeTypeHeader = ['ContentType' => File::getMimeType($file)]; |
|
83
|
|
|
|
|
84
|
|
|
$extraHeaders = config('medialibrary.remote.extra_headers'); |
|
85
|
|
|
|
|
86
|
|
|
return array_merge($mimeTypeHeader, $extraHeaders, $this->customRemoteHeaders); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/* |
|
90
|
|
|
* Copy a file from the medialibrary to the given targetFile. |
|
91
|
|
|
*/ |
|
92
|
|
|
public function copyFromMediaLibrary(Media $media, string $targetFile): string |
|
93
|
|
|
{ |
|
94
|
|
|
$sourceFile = $this->getMediaDirectory($media).'/'.$media->file_name; |
|
95
|
|
|
|
|
96
|
|
|
touch($targetFile); |
|
97
|
|
|
|
|
98
|
|
|
$stream = $this->filesystem->disk($media->disk)->readStream($sourceFile); |
|
99
|
|
|
file_put_contents($targetFile, stream_get_contents($stream), FILE_APPEND); |
|
100
|
|
|
fclose($stream); |
|
101
|
|
|
|
|
102
|
|
|
return $targetFile; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/* |
|
106
|
|
|
* Remove all files for the given media. |
|
107
|
|
|
*/ |
|
108
|
|
|
public function removeFiles(Media $media) |
|
109
|
|
|
{ |
|
110
|
|
|
$mediaDirectory = $this->getMediaDirectory($media); |
|
111
|
|
|
|
|
112
|
|
|
$conversionsDirectory = $this->getConversionDirectory($media); |
|
113
|
|
|
|
|
114
|
|
|
collect([$mediaDirectory, $conversionsDirectory]) |
|
115
|
|
|
->each(function ($directory) use ($media) { |
|
116
|
|
|
$this->filesystem->disk($media->disk)->deleteDirectory($directory); |
|
117
|
|
|
}); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/* |
|
121
|
|
|
* Rename a file for the given media. |
|
122
|
|
|
*/ |
|
123
|
|
|
public function renameFile(Media $media, string $oldName) |
|
124
|
|
|
{ |
|
125
|
|
|
$oldFile = $this->getMediaDirectory($media).'/'.$oldName; |
|
126
|
|
|
$newFile = $this->getMediaDirectory($media).'/'.$media->file_name; |
|
127
|
|
|
|
|
128
|
|
|
$this->filesystem->disk($media->disk)->move($oldFile, $newFile); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/* |
|
132
|
|
|
* Return the directory where all files of the given media are stored. |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getMediaDirectory(Media $media, bool $conversion = false) : string |
|
135
|
|
|
{ |
|
136
|
|
|
$pathGenerator = PathGeneratorFactory::create(); |
|
137
|
|
|
|
|
138
|
|
|
$directory = $conversion |
|
139
|
|
|
? $pathGenerator->getPathForConversions($media) |
|
140
|
|
|
: $pathGenerator->getPath($media); |
|
141
|
|
|
|
|
142
|
|
|
if (! in_array($media->getDiskDriverName(), ['s3'], true)) { |
|
143
|
|
|
$this->filesystem->disk($media->disk)->makeDirectory($directory); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $directory; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/* |
|
150
|
|
|
* Return the directory where all conversions of the given media are stored. |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getConversionDirectory(Media $media) : string |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->getMediaDirectory($media, true); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: