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