Completed
Push — master ( 66842d...b9e2d3 )
by Freek
9s
created

Filesystem::removeFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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