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
|
|
|
fclose($file); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Add custom remote headers on runtime. |
66
|
|
|
* |
67
|
|
|
* @param array $customRemoteHeaders |
68
|
|
|
*/ |
69
|
|
|
public function addCustomRemoteHeaders(array $customRemoteHeaders) |
70
|
|
|
{ |
71
|
|
|
$this->customRemoteHeaders = $customRemoteHeaders; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/* |
75
|
|
|
* Get the headers to be used when copying the |
76
|
|
|
* given file to a remote filesytem. |
77
|
|
|
*/ |
78
|
|
|
public function getRemoteHeadersForFile(string $file) : array |
79
|
|
|
{ |
80
|
|
|
$mimeTypeHeader = ['ContentType' => File::getMimeType($file)]; |
81
|
|
|
|
82
|
|
|
$extraHeaders = config('medialibrary.remote.extra_headers'); |
83
|
|
|
|
84
|
|
|
return array_merge($mimeTypeHeader, $extraHeaders, $this->customRemoteHeaders); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/* |
88
|
|
|
* Copy a file from the medialibrary to the given targetFile. |
89
|
|
|
*/ |
90
|
|
|
public function copyFromMediaLibrary(Media $media, string $targetFile): string |
91
|
|
|
{ |
92
|
|
|
$sourceFile = $this->getMediaDirectory($media).'/'.$media->file_name; |
93
|
|
|
|
94
|
|
|
touch($targetFile); |
95
|
|
|
|
96
|
|
|
$stream = $this->filesystem->disk($media->disk)->readStream($sourceFile); |
97
|
|
|
file_put_contents($targetFile, stream_get_contents($stream), FILE_APPEND); |
98
|
|
|
fclose($stream); |
99
|
|
|
|
100
|
|
|
return $targetFile; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/* |
104
|
|
|
* Remove all files for the given media. |
105
|
|
|
*/ |
106
|
|
|
public function removeFiles(Media $media) |
107
|
|
|
{ |
108
|
|
|
$mediaDirectory = $this->getMediaDirectory($media); |
109
|
|
|
|
110
|
|
|
$conversionsDirectory = $this->getConversionDirectory($media); |
111
|
|
|
|
112
|
|
|
collect([$mediaDirectory, $conversionsDirectory]) |
113
|
|
|
->each(function ($directory) use ($media) { |
114
|
|
|
$this->filesystem->disk($media->disk)->deleteDirectory($directory); |
115
|
|
|
}); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/* |
119
|
|
|
* Rename a file for the given media. |
120
|
|
|
*/ |
121
|
|
|
public function renameFile(Media $media, string $oldName) |
122
|
|
|
{ |
123
|
|
|
$oldFile = $this->getMediaDirectory($media).'/'.$oldName; |
124
|
|
|
$newFile = $this->getMediaDirectory($media).'/'.$media->file_name; |
125
|
|
|
|
126
|
|
|
$this->filesystem->disk($media->disk)->move($oldFile, $newFile); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/* |
130
|
|
|
* Return the directory where all files of the given media are stored. |
131
|
|
|
*/ |
132
|
|
|
public function getMediaDirectory(Media $media, bool $conversion = false) : string |
133
|
|
|
{ |
134
|
|
|
$pathGenerator = PathGeneratorFactory::create(); |
135
|
|
|
|
136
|
|
|
$directory = $conversion |
137
|
|
|
? $pathGenerator->getPathForConversions($media) |
138
|
|
|
: $pathGenerator->getPath($media); |
139
|
|
|
|
140
|
|
|
if (! in_array($media->getDiskDriverName(), ['s3'], true)) { |
141
|
|
|
$this->filesystem->disk($media->disk)->makeDirectory($directory); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $directory; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/* |
148
|
|
|
* Return the directory where all conversions of the given media are stored. |
149
|
|
|
*/ |
150
|
|
|
public function getConversionDirectory(Media $media) : string |
151
|
|
|
{ |
152
|
|
|
return $this->getMediaDirectory($media, true); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
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: