/
Storage.php
258 lines (237 loc) · 6.65 KB
/
Storage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
namespace trntv\filekit;
use League\Flysystem\FilesystemInterface;
use trntv\filekit\events\StorageEvent;
use trntv\filekit\filesystem\FilesystemBuilderInterface;
use yii\base\BootstrapInterface;
use yii\base\Component;
use yii\base\InvalidConfigException;
/**
* Class Storage
* @package trntv\filekit
* @author Eugene Terentev <eugene@terentev.net>
*/
class Storage extends Component
{
/**
* Event triggered after delete
*/
const EVENT_BEFORE_DELETE = 'beforeDelete';
/**
* Event triggered after save
*/
const EVENT_BEFORE_SAVE = 'beforeSave';
/**
* Event triggered after delete
*/
const EVENT_AFTER_DELETE = 'afterDelete';
/**
* Event triggered after save
*/
const EVENT_AFTER_SAVE = 'afterSave';
/**
* @var
*/
public $baseUrl;
/**
* @var
*/
public $filesystemComponent;
/**
* @var
*/
protected $filesystem;
/**
* Max files in directory
* "-1" = unlimited
* @var int
*/
public $maxDirFiles = 65535; // Default: Fat32 limit
/**
* @var int
*/
private $dirindex = 1;
/**
* @throws InvalidConfigException
*/
public function init()
{
if ($this->baseUrl !== null) {
$this->baseUrl = \Yii::getAlias($this->baseUrl);
}
if ($this->filesystemComponent !== null) {
$this->filesystem = \Yii::$app->get($this->filesystemComponent);
} else {
$this->filesystem = \Yii::createObject($this->filesystem);
if ($this->filesystem instanceof FilesystemBuilderInterface) {
$this->filesystem = $this->filesystem->build();
}
}
}
/**
* @return FilesystemInterface
* @throws InvalidConfigException
*/
public function getFilesystem()
{
return $this->filesystem;
}
/**
* @param $filesystem
*/
public function setFilesystem($filesystem)
{
$this->filesystem = $filesystem;
}
/**
* @param $file string|\yii\web\UploadedFile
* @param bool $preserveFileName
* @param bool $overwrite
* @return bool|string
*/
public function save($file, $preserveFileName = false, $overwrite = false)
{
$fileObj = File::create($file);
$dirIndex = $this->getDirIndex();
if ($preserveFileName === false) {
do {
$filename = implode('.', [
\Yii::$app->security->generateRandomString(),
$fileObj->getExtension()
]);
$path = implode('/', [$dirIndex, $filename]);
} while ($this->getFilesystem()->has($path));
} else {
$filename = $fileObj->getPathInfo('filename');
$path = implode('/', [$dirIndex, $filename]);
}
$this->beforeSave($fileObj->getPath(), $this->getFilesystem());
$stream = fopen($fileObj->getPath(), 'r+');
if ($overwrite) {
$success = $this->getFilesystem()->putStream($path, $stream);
} else {
$success = $this->getFilesystem()->writeStream($path, $stream);
}
fclose($stream);
if ($success) {
$this->afterSave($path, $this->getFilesystem());
return $path;
}
return false;
}
/**
* @param $files array|\yii\web\UploadedFile[]
* @param bool $preserveFileName
* @param bool $overwrite
* @return array
*/
public function saveAll($files, $preserveFileName = false, $overwrite = false)
{
$paths = [];
foreach ($files as $file) {
$paths[] = $this->save($file, $preserveFileName, $overwrite);
}
return $paths;
}
/**
* @param $path
* @return bool
*/
public function delete($path)
{
if ($this->getFilesystem()->has($path)) {
$this->beforeDelete($path, $this->getFilesystem());
if ($this->getFilesystem()->delete($path)) {
$this->afterDelete($path, $this->getFilesystem());
return true;
};
}
return false;
}
/**
* @param $files
*/
public function deleteAll($files)
{
foreach ($files as $file) {
$this->delete($file);
}
}
/**
* @return false|int|string
*/
protected function getDirIndex()
{
if (!$this->getFilesystem()->has('.dirindex')) {
$this->getFilesystem()->write('.dirindex', $this->dirindex);
} else {
$this->dirindex = $this->getFilesystem()->read('.dirindex');
if ($this->maxDirFiles !== -1) {
$filesCount = count($this->getFilesystem()->listContents($this->dirindex));
if ($filesCount > $this->maxDirFiles) {
$this->dirindex++;
$this->getFilesystem()->put('.dirindex', $this->dirindex);
}
}
}
return $this->dirindex;
}
/**
* @param $path
* @throws InvalidConfigException
*/
public function beforeSave($path)
{
/* @var \trntv\filekit\events\StorageEvent $event */
$event = \Yii::createObject([
'class' => StorageEvent::className(),
'path' => $path
]);
$this->trigger(self::EVENT_BEFORE_SAVE, $event);
}
/**
* @param $path
* @param $filesystem
* @throws InvalidConfigException
*/
public function afterSave($path, $filesystem)
{
/* @var \trntv\filekit\events\StorageEvent $event */
$event = \Yii::createObject([
'class' => StorageEvent::className(),
'path' => $path,
'filesystem' => $filesystem
]);
$this->trigger(self::EVENT_AFTER_SAVE, $event);
}
/**
* @param $path
* @param $filesystem
* @throws InvalidConfigException
*/
public function beforeDelete($path, $filesystem)
{
/* @var \trntv\filekit\events\StorageEvent $event */
$event = \Yii::createObject([
'class' => StorageEvent::className(),
'path' => $path,
'filesystem' => $filesystem
]);
$this->trigger(self::EVENT_BEFORE_DELETE, $event);
}
/**
* @param $path
* @param $filesystem
* @throws InvalidConfigException
*/
public function afterDelete($path, $filesystem)
{
/* @var \trntv\filekit\events\StorageEvent $event */
$event = \Yii::createObject([
'class' => StorageEvent::className(),
'path' => $path,
'filesystem' => $filesystem
]);
$this->trigger(self::EVENT_AFTER_DELETE, $event);
}
}