Completed
Push — master ( 10badb...88b006 )
by Renato
08:03
created

StorageManager::copy()   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
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace NwLaravel\FileStorage;
4
5
use \Exception;
6
use Symfony\Component\HttpFoundation\File\UploadedFile;
7
use Illuminate\Contracts\Filesystem\Filesystem as Storage;
8
use Intervention\Image\Image;
9
10
/**
11
 * Class StorageManager
12
 */
13
class StorageManager
14
{
15
    /**
16
     * @var Storage
17
     */
18
    protected $storage;
19
20
    /**
21
     * @var ImagineFactory
22
     */
23
    protected $imagineFactory;
24
25
    /**
26
     * Construct
27
     *
28
     * @param Storage        $storage
29
     * @param ImagineFactory $imagineFactory
30
     */
31 27
    public function __construct(Storage $storage, ImagineFactory $imagineFactory = null)
32
    {
33 27
        $this->storage = $storage;
34 27
        $this->imagineFactory = $imagineFactory;
35 27
    }
36
37
    /**
38
     * File Exists
39
     *
40
     * @param string $filename Path File
41
     *
42
     * @return bool
43
     */
44 12
    public function exists($filename)
45
    {
46
        try {
47 12
            return $this->storage->exists($filename);
48
49 1
        } catch (\Exception $e) {
50 1
            return false;
51
        }
52
    }
53
54
    /**
55
     * Get Size
56
     *
57
     * @param string $filename Path File
58
     *
59
     * @return bool
60
     */
61 2
    public function size($filename)
62
    {
63
        try {
64 2
            return intval($this->storage->size($filename));
65
66 1
        } catch (\Exception $e) {
67 1
            return 0;
68
        }
69
    }
70
71
    /**
72
     * Get MimeType File
73
     *
74
     * @param string $filename Path File
75
     *
76
     * @return bool
77
     */
78 12
    public function mimeType($filename)
79
    {
80
        try {
81 12
            return $this->storage->mimeType($filename);
82
83 1
        } catch (\Exception $e) {
84 1
            return null;
85
        }
86
    }
87
88
    /**
89
     * Path is Directory
90
     *
91
     * @param string $path Path Directory
92
     *
93
     * @return bool
94
     */
95 10
    public function isDir($path)
96
    {
97 10
        $mimeType = $this->mimeType($path);
98
99 10
        if ($this->exists($path) && (!$mimeType || $mimeType == 'directory')) {
100 5
            return true;
101
        }
102
103 5
        return false;
104
    }
105
106
    /**
107
     * Is File
108
     *
109
     * @param string $filename Path File
110
     *
111
     * @return bool
112
     */
113 6
    public function isFile($filename)
114
    {
115 6
        return !$this->isDir($filename);
116
    }
117
118
    /**
119
     * Get Meta Data
120
     *
121
     * @param string $filename Path File
122
     *
123
     * @return bool
124
     */
125 2
    public function metaData($filename)
126
    {
127
        try {
128 2
            return $this->storage->getMetadata($filename);
0 ignored issues
show
Bug introduced by
The method getMetadata() does not seem to exist on object<Illuminate\Contra...\Filesystem\Filesystem>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
130 1
        } catch (\Exception $e) {
131 1
            return null;
132
        }
133
    }
134
135
    /**
136
     * Read Content File
137
     *
138
     * @param string $filename Path File
139
     *
140
     * @return bool
141
     */
142 1
    public function readFile($filename)
143
    {
144 1
        return $this->storage->get($filename);
145
    }
146
147
    /**
148
     * Delete File
149
     *
150
     * @param string $filename Path File
151
     *
152
     * @return bool
153
     */
154 2
    public function deleteFile($filename)
155
    {
156 2
        if ($this->isDir($filename)) {
157 1
            return false;
158
        }
159
160 1
        return $this->storage->delete($filename);
161
    }
162
163
    /**
164
     * Delete Folder
165
     *
166
     * @param string $folder
167
     *
168
     * @return bool
169
     */
170 2
    public function deleteFolder($folder)
171
    {
172 2
        if ($this->isFile($folder)) {
173 1
            return false;
174
        }
175
176 1
        return $this->storage->deleteDirectory($folder);
177
    }
178
179
    /**
180
     * Move a file to a new location.
181
     *
182
     * @param  string  $from
183
     * @param  string  $to
184
     * @return bool
185
     */
186 1
    public function move($from, $to)
187
    {
188 1
        return $this->storage->move($from, $to);
189
    }
190
191
    /**
192
     * Copy a file to a new location.
193
     *
194
     * @param  string  $from
195
     * @param  string  $to
196
     * @return bool
197
     */
198 1
    public function copy($from, $to)
199
    {
200 1
        return $this->storage->copy($from, $to);
201
    }
202
203
    /**
204
     * Files in Folder
205
     *
206
     * @param string $path
207
     * @param bool   $recursive
208
     *
209
     * @return array
210
     */
211 2
    public function files($path, $recursive = false)
212
    {
213 2
        if ($this->isFile($path)) {
214 1
            return null;
215
        }
216
217 1
        return $this->storage->files($path, (bool) $recursive);
218
    }
219
220
    /**
221
     * UploadFile
222
     *
223
     * @param UploadedFile $file     Uploaded File
224
     * @param string       $folder   String Folder
225
     * @param string       $name     String Name
226
     * @param bool         $override Boolean Over Ride
227
     *
228
     * @return bool
229
     */
230 2
    public function uploadFile(UploadedFile $file, $folder = null, $name = null, $override = false)
231
    {
232 2
        $data = $this->parseFile($file, $folder, $name, $override);
233
234 2
        $success = (bool) $this->storage->put($data['filename'], file_get_contents($file));
235
236 2
        if ($success) {
237 1
            return $data;
238
        }
239
240 1
        return false;
241
    }
242
243
    /**
244
     * Upload Image
245
     *
246
     * @param UploadedFile $file     Uploaded File
247
     * @param string       $folder   String Folder
248
     * @param string       $name     String Name
249
     * @param array        $options  Array Options
250
     * @param bool         $override Boolean Over Ride
251
     *
252
     * @return bool
253
     */
254 4
    public function uploadImage(
255
        UploadedFile $file,
256
        $folder = null,
257
        $name = null,
258
        array $options = array(),
259
        $override = false
260
    ) {
261 4
        $pathImage = $file->getPathname();
262 4
        $data = $this->parseFile($file, $folder, $name, $override);
263
264 4
        if ($this->imagineFactory) {
265 1
            $width = isset($options['width']) ? intval($options['width']) : 0;
266 1
            $height = isset($options['height']) ? intval($options['height']) : 0;
267 1
            $scale = isset($options['scale']) ? (bool) $options['scale'] : true;
268 1
            $opacity = isset($options['opacity']) ? (float) $options['opacity'] : null;
269 1
            $watermark = isset($options['watermark']) ? $options['watermark'] : null;
270 1
            $quality = isset($options['quality']) ? intval($options['quality']) : 85; // Quality Deufault: 85;
271
            
272 1
            $imagine = $this->imagineFactory->make($pathImage);
273 1
            $imagine->resize($width, $height, !$scale);
274 1
            $imagine->opacity($opacity);
275 1
            $imagine->watermark($watermark);
276
                
277 1
            $image = $imagine->save($pathImage.'.'.$data['extension'], $quality);
278 1
            $data['size'] = $image->filesize();
279 1
            $content = $image->getEncoded();
280 1
        } else {
281 3
            $content = file_get_contents($file);
282
        }
283
284 4
        $success = $this->storage->put($data['filename'], $content);
285
286 4
        if ($success) {
287 3
            return $data;
288
        }
289
290 1
        return false;
291
    }
292
293
    /**
294
     * Parse Filename
295
     *
296
     * @param UploadedFile $file     Uploaded File
297
     * @param string       $name     String Name
298
     * @param string       $folder   String Folder
299
     * @param bool         $override Boolean Over Ride
300
     *
301
     * @return bool|array
302
     */
303 6
    protected function parseFile($file, $folder = null, $name = null, $override = false)
304
    {
305 6
        $folder = trim((string) $folder, '/');
306 6
        $folder = $folder ? "{$folder}/" : "";
307 6
        $this->storage->makeDirectory($folder);
308
309 6
        $name = $name ?: $file->getClientOriginalName();
310 6
        $nameOriginal = str_slug(pathinfo($name, PATHINFO_FILENAME));
311
312 6
        if (empty($nameOriginal)) {
313 1
            $nameOriginal = str_random(10);
314 1
        }
315 6
        $extension = $file->getClientOriginalExtension();
316 6
        $size = $file->getClientSize();
317 6
        $mime = $file->getClientMimeType();
318
319 6
        $sufix = '';
320 6
        $count = 1;
321
        do {
322 6
            $name = "{$nameOriginal}{$sufix}.{$extension}";
323 6
            $filename = "{$folder}{$name}";
324 6
            $sufix = "({$count})";
325 6
            $count++;
326
327 6
        } while (!$override && $this->storage->exists($filename));
328
329 6
        return compact('filename', 'name', 'extension', 'size', 'mime');
330
    }
331
}
332