Completed
Pull Request — master (#7)
by ARCANEDEV
04:58
created

Media::deleteDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php namespace Arcanesoft\Media;
2
3
use Arcanesoft\Media\Contracts\Media as MediaContract;
4
use Arcanesoft\Media\Entities\DirectoryCollection;
5
use Arcanesoft\Media\Entities\FileCollection;
6
use Arcanesoft\Media\Exceptions\DirectoryNotFound;
7
use Arcanesoft\Media\Exceptions\FileNotFoundException;
8
use Carbon\Carbon;
9
use Illuminate\Contracts\Foundation\Application;
10
use Illuminate\Http\UploadedFile;
11
use Illuminate\Support\Str;
12
13
/**
14
 * Class     Media
15
 *
16
 * @package  Arcanesoft\Media
17
 * @author   ARCANEDEV <[email protected]>
18
 */
19
class Media implements MediaContract
20
{
21
    /* -----------------------------------------------------------------
22
     |  Constants
23
     | -----------------------------------------------------------------
24
     */
25
26
    const VERSION = '2.2.2';
27
28
    /* -----------------------------------------------------------------
29
     |  Properties
30
     | -----------------------------------------------------------------
31
     */
32
33
    /**
34
     * The application instance.
35
     *
36
     * @var \Illuminate\Contracts\Foundation\Application
37
     */
38
    protected $app;
39
40
    /* -----------------------------------------------------------------
41
     |  Constructor
42
     | -----------------------------------------------------------------
43
     */
44
45
    /**
46
     * Media constructor.
47
     *
48
     * @param  \Illuminate\Contracts\Foundation\Application  $app
49
     */
50 39
    public function __construct(Application $app)
51
    {
52 39
        $this->app = $app;
53 39
    }
54
55
    /* -----------------------------------------------------------------
56
     |  Getters & Setters
57
     | -----------------------------------------------------------------
58
     */
59
60
    /**
61
     * Get the Filesystem Manager instance.
62
     *
63
     * @return \Illuminate\Contracts\Filesystem\Factory
64
     */
65 30
    public function filesystem()
66
    {
67 30
        return $this->app->make('filesystem');
68
    }
69
70
    /**
71
     * Get the Config Repository.
72
     *
73
     * @return \Illuminate\Contracts\Config\Repository
74
     */
75 36
    protected function config()
76
    {
77 36
        return $this->app->make('config');
78
    }
79
80
    /**
81
     * Get the default disk name.
82
     *
83
     * @return string
84
     */
85 30
    public function getDefaultDiskName()
86
    {
87 30
        return $this->config()->get('arcanesoft.media.filesystem.default');
88
    }
89
90
    /**
91
     * Get excluded directories.
92
     *
93
     * @return array
94
     */
95 24
    public function getExcludedDirectories()
96
    {
97 24
        return Helpers\ExcludePattern::directories(
98 24
            $this->config()->get('arcanesoft.media.directories.excluded', [])
99 8
        );
100
    }
101
102
    /**
103
     * Get excluded files.
104
     *
105
     * @return array
106
     */
107 18
    public function getExcludedFiles()
108
    {
109 18
        return $this->config()->get('arcanesoft.media.files.excluded', []);
110
    }
111
112
    /* -----------------------------------------------------------------
113
     |  Main Methods
114
     | -----------------------------------------------------------------
115
     */
116
117
    /**
118
     * Get a filesystem adapter.
119
     *
120
     * @param  string|null  $driver
121
     *
122
     * @return \Illuminate\Filesystem\FilesystemAdapter|\Illuminate\Contracts\Filesystem\Filesystem
123
     */
124 30
    public function disk($driver = null)
125
    {
126 30
        return $this->filesystem()->disk($driver ?: $this->getDefaultDiskName());
127
    }
128
129
    /**
130
     * Get all the directories & files from a given location.
131
     *
132
     * @param  string  $directory
133
     *
134
     * @return array
135
     */
136 3
    public function all($directory = '/')
137
    {
138
        $directories = $this->directories($directory)->transform(function ($item) {
139 3
            return $item + ['type' => self::MEDIA_TYPE_DIRECTORY];
140 3
        });
141
142
        $files = $this->files($directory)->transform(function (array $item) {
143 3
            return $item + ['type' => self::MEDIA_TYPE_FILE];
144 3
        });
145
146 3
        return array_merge($directories->toArray(), $files->toArray());
147
    }
148
149
    /**
150
     * Get all of the directories within a given directory.
151
     *
152
     * @param  string  $directory
153
     *
154
     * @return \Arcanesoft\Media\Entities\DirectoryCollection
155
     */
156 12
    public function directories($directory)
157
    {
158 12
        $this->checkDirectory($directory);
159
160
        $directories = array_map(function ($dir) use ($directory) {
161
            return [
162 6
                'name' => str_replace("$directory/", '', $dir),
163 6
                'path' => $dir,
164 2
            ];
165 6
        }, $this->disk()->directories($directory));
166
167 6
        return DirectoryCollection::make($directories)
168 6
            ->exclude($this->getExcludedDirectories());
169
    }
170
171
    /**
172
     * Get a collection of all files in a directory.
173
     *
174
     * @param  string  $directory
175
     *
176
     * @return \Arcanesoft\Media\Entities\FileCollection
177
     */
178 15
    public function files($directory)
179
    {
180 15
        $this->checkDirectory($directory);
181
182 15
        $disk = $this->disk();
183
184
        // TODO: Add a feature to exclude unwanted files.
185
        $files = array_map(function ($filePath) use ($disk, $directory) {
186
            return [
187 15
                'name'         => str_replace("$directory/", '', $filePath),
188 15
                'path'         => $filePath,
189 15
                'url'          => $disk->url($filePath),
190 15
                'mimetype'     => $disk->mimeType($filePath),
191 15
                'lastModified' => Carbon::createFromTimestamp($disk->lastModified($filePath))->toDateTimeString(),
192 15
                'visibility'   => $disk->getVisibility($filePath),
193 15
                'size'         => $disk->size($filePath),
194 5
            ];
195 15
        }, $disk->files($directory));
196
197 15
        return FileCollection::make($files)->exclude($this->getExcludedFiles());
198
    }
199
200
    /**
201
     * Get the file details.
202
     *
203
     * @param  string  $path
204
     *
205
     * @return array
206
     */
207 6
    public function file($path)
208
    {
209
        return $this->files(dirname($path))->first(function ($file) use ($path) {
210 6
            return $file['path'] === $path;
211 6
        }, function () use ($path) {
212 3
            throw new FileNotFoundException("File [$path] not found!");
213 6
        });
214
    }
215
216
    /**
217
     * Store an array of files.
218
     *
219
     * @param  string  $directory
220
     * @param  array   $files
221
     */
222 3
    public function storeMany($directory, array $files)
223
    {
224 3
        foreach ($files as $file) {
225 3
            $this->store($directory, $file);
226 1
        }
227 3
    }
228
229
    /**
230
     * Store a file.
231
     *
232
     * @param  string                         $directory
233
     * @param  \Illuminate\Http\UploadedFile  $file
234
     *
235
     * @return string|false
236
     */
237 6
    public function store($directory, UploadedFile $file)
238
    {
239 6
        return $file->store($directory, $this->getDefaultDiskName());
240
    }
241
242
    /**
243
     * Create a directory.
244
     *
245
     * @param  string  $path
246
     *
247
     * @return bool
248
     */
249 6
    public function makeDirectory($path)
250
    {
251 6
        return $this->disk()->makeDirectory($path);
252
    }
253
254
    /**
255
     * Delete a directory.
256
     *
257
     * @param  string  $directory
258
     *
259
     * @return bool
260
     */
261 6
    public function deleteDirectory($directory)
262
    {
263 6
        return $this->disk()->deleteDirectory($directory);
264
    }
265
266
    /**
267
     * Delete a file.
268
     *
269
     * @param  string  $path
270
     *
271
     * @return bool
272
     */
273 3
    public function deleteFile($path)
274
    {
275 3
        return $this->disk()->delete($path);
276
    }
277
278
    /**
279
     * Move a file to a new location.
280
     *
281
     * @param  string  $from
282
     * @param  string  $to
283
     *
284
     * @return bool
285
     */
286 3
    public function move($from, $to)
287
    {
288 3
        return $this->disk()->move($from, $to);
289
    }
290
291
    /* -----------------------------------------------------------------
292
     |  Check Methods
293
     | -----------------------------------------------------------------
294
     */
295
296
    /**
297
     * Determine if a file/directory exists.
298
     *
299
     * @param  string  $path
300
     *
301
     * @return bool
302
     */
303 24
    public function exists($path)
304
    {
305 24
        return $this->disk()->exists($path);
306
    }
307
308
    /**
309
     * Check if the directory is excluded.
310
     *
311
     * @param  string  $directory
312
     *
313
     * @return bool
314
     */
315 21
    public function isExcludedDirectory($directory)
316
    {
317 21
        foreach ($this->getExcludedDirectories() as $pattern) {
318 3
            if (Str::is($pattern, $directory)) return true;
319 6
        }
320
321 18
        return false;
322
    }
323
324
    /* -----------------------------------------------------------------
325
     |  Other Methods
326
     | -----------------------------------------------------------------
327
     */
328
329
    /**
330
     * Check the given directory location.
331
     *
332
     * @param  string  &$directory
333
     *
334
     * @throws \Arcanesoft\Media\Exceptions\DirectoryNotFound
335
     * @throws \Arcanesoft\Media\Exceptions\AccessNotAllowed
336
     */
337 24
    protected function checkDirectory(&$directory)
338
    {
339 24
        $directory = trim($directory, '/');
340
341 24
        $this->checkDirectoryExists($directory);
342 21
        $this->checkDirectoryAccess($directory);
343 18
    }
344
345
    /**
346
     * Check if the directory exists.
347
     *
348
     * @param  string  $directory
349
     *
350
     * @throws \Arcanesoft\Media\Exceptions\DirectoryNotFound
351
     */
352 24
    protected function checkDirectoryExists($directory)
353
    {
354 24
        if ( ! empty($directory) && ! $this->exists($directory)) {
355 3
            throw new DirectoryNotFound("Directory [$directory] not found !", 404);
356
        }
357 21
    }
358
359
    /**
360
     * Check if can access the directory.
361
     *
362
     * @param  string  $directory
363
     *
364
     * @throws \Arcanesoft\Media\Exceptions\AccessNotAllowed
365
     */
366 21
    protected function checkDirectoryAccess($directory)
367
    {
368 21
        if ($this->isExcludedDirectory($directory)) {
369 3
            throw new Exceptions\AccessNotAllowed('Access not allowed.', 405);
370
        }
371 18
    }
372
}
373