Completed
Push — master ( 204e09...86e349 )
by Freek
04:41 queued 02:13
created

DefaultFilesystem::renameFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
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 $file, Media $media, bool $conversions = false, string $targetFileName = '')
41
    {
42
        $destination = $this->getMediaDirectory($media, $conversions).
43
            ($targetFileName == '' ? pathinfo($file, PATHINFO_BASENAME) : $targetFileName);
44
45
        if ($media->getDiskDriverName() === 'local') {
46
            $this->filesystem
47
                ->disk($media->disk)
48
                ->put($destination, fopen($file, 'r'));
49
50
            return;
51
        }
52
53
        $this->filesystem
54
            ->disk($media->disk)
55
            ->getDriver()
56
            ->put($destination, fopen($file, 'r'), $this->getRemoteHeadersForFile($file));
57
    }
58
59
    /**
60
     * Add custom remote headers on runtime.
61
     *
62
     * @param array $customRemoteHeaders
63
     */
64
    public function addCustomRemoteHeaders(array $customRemoteHeaders)
65
    {
66
        $this->customRemoteHeaders = $customRemoteHeaders;
67
    }
68
69
    /*
70
     * Get the headers to be used when copying the
71
     * given file to a remote filesytem.
72
     */
73
    public function getRemoteHeadersForFile(string $file) : array
74
    {
75
        $mimeTypeHeader = ['ContentType' => File::getMimeType($file)];
76
77
        $extraHeaders = config('medialibrary.remote.extra_headers');
78
79
        return array_merge($mimeTypeHeader, $extraHeaders, $this->customRemoteHeaders);
80
    }
81
82
    /*
83
     * Copy a file from the medialibrary to the given targetFile.
84
     */
85
    public function copyFromMediaLibrary(Media $media, string $targetFile): string
86
    {
87
        $sourceFile = $this->getMediaDirectory($media).'/'.$media->file_name;
88
89
        touch($targetFile);
90
91
        $stream = $this->filesystem->disk($media->disk)->readStream($sourceFile);
92
        file_put_contents($targetFile, stream_get_contents($stream), FILE_APPEND);
93
        fclose($stream);
94
95
        return $targetFile;
96
    }
97
98
    /*
99
     * Remove all files for the given media.
100
     */
101
    public function removeFiles(Media $media)
102
    {
103
        $mediaDirectory = $this->getMediaDirectory($media);
104
105
        $conversionsDirectory = $this->getConversionDirectory($media);
106
107
        collect([$mediaDirectory, $conversionsDirectory])
108
            ->each(function ($directory) use ($media) {
109
                $this->filesystem->disk($media->disk)->deleteDirectory($directory);
110
            });
111
    }
112
113
    /*
114
     * Rename a file for the given media.
115
     */
116
    public function renameFile(Media $media, string $oldName)
117
    {
118
        $oldFile = $this->getMediaDirectory($media).'/'.$oldName;
119
        $newFile = $this->getMediaDirectory($media).'/'.$media->file_name;
120
121
        $this->filesystem->disk($media->disk)->move($oldFile, $newFile);
122
    }
123
124
    /*
125
     * Return the directory where all files of the given media are stored.
126
     */
127
    public function getMediaDirectory(Media $media, bool $conversion = false) : string
128
    {
129
        $pathGenerator = PathGeneratorFactory::create();
130
131
        $directory = $conversion
132
            ? $pathGenerator->getPathForConversions($media)
133
            : $pathGenerator->getPath($media);
134
135
        if (! in_array($media->getDiskDriverName(), ['s3'], true)) {
136
            $this->filesystem->disk($media->disk)->makeDirectory($directory);
137
        }
138
139
        return $directory;
140
    }
141
142
    /*
143
     * Return the directory where all conversions of the given media are stored.
144
     */
145
    public function getConversionDirectory(Media $media) : string
146
    {
147
        return $this->getMediaDirectory($media, true);
148
    }
149
}
150