|
1
|
|
|
<?php |
|
2
|
|
|
namespace CbCaio\ImgAttacher\Managers; |
|
3
|
|
|
|
|
4
|
|
|
use CbCaio\ImgAttacher\Contracts\AttacherImageContract; |
|
5
|
|
|
use CbCaio\ImgAttacher\Contracts\FileManagerContract; |
|
6
|
|
|
use GrahamCampbell\Flysystem\FlysystemManager; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Intervention\Image\Image; |
|
9
|
|
|
|
|
10
|
|
|
class FileManager implements FileManagerContract |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var \GrahamCampbell\Flysystem\FlysystemManager |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $flysystem; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(FlysystemManager $flysystemManager) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->flysystem = $flysystemManager; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param Image $image |
|
24
|
|
|
* @param string $path |
|
25
|
|
|
* @return bool |
|
26
|
|
|
*/ |
|
27
|
|
|
public function save(Image $image, $path) |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->flysystem->put($path, $image->encode()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param Collection $images |
|
34
|
|
|
* @param AttacherImageContract $attacherImage |
|
35
|
|
|
* @return bool |
|
36
|
|
|
*/ |
|
37
|
|
|
public function saveMany(Collection $images, AttacherImageContract $attacherImage) |
|
38
|
|
|
{ |
|
39
|
|
|
foreach ($images as $style_name => $image) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->save($image, $attacherImage->getPath($style_name)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return TRUE; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param Image $image |
|
49
|
|
|
* @param string $path |
|
50
|
|
|
* @param string $oldPath |
|
51
|
|
|
* @return bool |
|
52
|
|
|
*/ |
|
53
|
|
|
public function update(Image $image, $path, $oldPath) |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->delete($oldPath) ? $this->flysystem->put($path, $image->encode()) : FALSE; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param Collection $images |
|
60
|
|
|
* @param AttacherImageContract $attacherImage |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
|
|
public function updateMany(Collection $images, AttacherImageContract $attacherImage) |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->delete($attacherImage->getPreviousPath()) |
|
66
|
|
|
? $this->saveMany($images, $attacherImage) |
|
67
|
|
|
: FALSE; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $path |
|
72
|
|
|
* @return bool |
|
73
|
|
|
*/ |
|
74
|
|
|
public function delete($path) |
|
75
|
|
|
{ |
|
76
|
|
|
$folders_list = $this->flysystem->listContents($path, FALSE); |
|
77
|
|
|
|
|
78
|
|
|
if (empty($folders_list)) |
|
79
|
|
|
{ |
|
80
|
|
|
return FALSE; |
|
81
|
|
|
} else |
|
82
|
|
|
{ |
|
83
|
|
|
foreach ($this->flysystem->listContents($path, FALSE) as $folder) |
|
84
|
|
|
{ |
|
85
|
|
|
if ($folder['type'] == 'dir') |
|
86
|
|
|
{ |
|
87
|
|
|
$this->flysystem->deleteDir($folder['path']); |
|
88
|
|
|
} else |
|
89
|
|
|
{ |
|
90
|
|
|
$this->flysystem->delete($folder['path']); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return TRUE; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param string $path |
|
100
|
|
|
* @return bool |
|
101
|
|
|
*/ |
|
102
|
|
|
public function pathExists($path) |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->flysystem->has($path); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|