/
Storage.php
297 lines (257 loc) · 8.3 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
namespace vojtabiberle\MediaStorage;
use Nette\Database\Table\ActiveRow;
use vojtabiberle\MediaStorage\Bridges\FilesystemStorage;
use vojtabiberle\MediaStorage\Bridges\IDatabaseStorage;
use vojtabiberle\MediaStorage\Bridges\IFilesystemStorage;
use vojtabiberle\MediaStorage\Bridges\IFileUpload;
use vojtabiberle\MediaStorage\Exceptions\FileNotFoundException;
use vojtabiberle\MediaStorage\Exceptions\StorageException;
use vojtabiberle\MediaStorage\Files\IFile;
use vojtabiberle\MediaStorage\Images\IImage;
use vojtabiberle\MediaStorage\Images\Image;
class Storage implements IStorage
{
/** @var IDatabaseStorage */
private $databaseStorage;
/** @var IFilesystemStorage */
private $filesystemStorage;
private static $mediaStoragePath;
private static $fileClass;
private static $imageClass;
/**
* @var IFileFilter $currentFilter
*/
private $currentFilter = null;
public function __construct(
IDatabaseStorage $databaseStorage,
IFilesystemStorage $filesystemStorage,
$mediaStoragePath,
$fileClass = 'vojtabiberle\\MediaStorage\\Files\\File',
$imageClass = 'vojtabiberle\\MediaStorage\\Images\\Image'
)
{
$this->databaseStorage = $databaseStorage;
$this->filesystemStorage = $filesystemStorage;
self::$mediaStoragePath = $mediaStoragePath;
if (!file_exists(self::$mediaStoragePath)) {
FilesystemStorage::mkdir(self::$mediaStoragePath);
}
self::$fileClass = $fileClass;
self::$imageClass = $imageClass;
}
/**
* @return IFile
*/
public static function fileObjectBuilder()
{
/** @var IFile $file */
$file = new self::$fileClass;
$file->setStoragePath(self::$mediaStoragePath);
return $file;
}
/**
* @return IImage
*/
public static function imageObjectBuilder()
{
/** @var IImage $image */
$image = new self::$imageClass;
$image->setStoragePath(self::$mediaStoragePath);
return $image;
}
/**
* Save file informations into database and file on disk
*
* @param IFile $file
* @return IFile
*/
public function save(IFile $file)
{
$file->setStoragePath(self::$mediaStoragePath);
if(is_null($file->getUID())) {
$file->generateUID();
}
$newPath = self::$mediaStoragePath . DIRECTORY_SEPARATOR . $file->getUID();
$file->save($newPath);
$this->databaseStorage->store($file);
return $file;
}
public function delete(IFile $file, $webRoot)
{
$files = $this->databaseStorage->deleteFiles(FileFilter::create()->findByName($file->getName()));
foreach($files as $file) {
$this->deleteById($file->id, $webRoot);
}
return true; //?
}
public function deleteById($id, $webRoot)
{
$filter = FileFilter::create()->getbyId($id);
/** @var ActiveRow $file */
$file = $this->databaseStorage->find($filter);
if (!$file) {
throw new FileNotFoundException('File not found.');
}
$this->databaseStorage->deleteById($file->id);
FilesystemStorage::deleteFile(self::$mediaStoragePath . DIRECTORY_SEPARATOR . $file->id);
/** TODO: Nepředávat webRoot path k media z Manageru do Storage */
FilesystemStorage::deleteFiles(FilesystemStorage::findFiles($webRoot.'/media/', $file->name));
return true; //?
}
public function deleteUsageByNamespace($ns)
{
return $this->databaseStorage->deleteUsageByNamespace($ns);
}
/**
* Save uploaded file into database and on disk
*
* @param IFileUpload $uploadedFile
* @return mixed
*/
public function saveUploaded(IFileUpload $uploadedFile)
{
if ($uploadedFile->isImage()) {
$file = self::imageObjectBuilder();
} else {
$file = self::fileObjectBuilder();
}
/** @var IFile $file */
$file->fillFileUpload($uploadedFile);
$this->databaseStorage->store($file);
return $file;
}
/**
* Find files
*
* @param IFileFilter $filter
* @return array[] of IFile or IImage
*/
public function find(IFileFilter $filter)
{
$this->currentFilter = $filter;
$data = $this->databaseStorage->find($filter);
if (!$data) {
throw new FileNotFoundException('File not found.');
}
return $this->hydrateRowContainer($data);
}
/**
* Try to find file in database based on name and namespace
*
* @param $name
* @param null $namespace
* @return IFile|IImage
* @throws StorageException
*/
public function get($name, $namespace = null)
{
/** @var ActiveRow $data */
$data = $this->databaseStorage->get($name, $namespace);
if (!$data) {
throw new FileNotFoundException('File not found.');
}
return $this->hydrateRow($data);
}
public function getNoimage($name)
{
$name = str_replace('noimage-', '', $name);
$path = self::$mediaStoragePath . DIRECTORY_SEPARATOR . 'noimage';
if (file_exists($path . DIRECTORY_SEPARATOR . $name)) {
return Image::fromFile($path . DIRECTORY_SEPARATOR . $name);
}
throw new FileNotFoundException('File not found!');
}
/**
* @param IFile $file
* @param $path
* @return IFile
*/
public function publishFile(IFile $file, $path)
{
$onlyPath = dirname($path);
if (!file_exists($onlyPath)) {
FilesystemStorage::mkdir($onlyPath, 0777, true);
}
$file->save($path);
return $file;
}
/**
* @param string $namespace
* @param array|\Traversable $used
* @param array|\Traversable $removed
* @return mixed
*/
public function saveUsages($namespace, $used, $removed)
{
if (
is_string($namespace) &&
( is_array($used) || ($used instanceof \Traversable || is_null($used)) ) &&
( is_array($removed) || ($removed instanceof \Traversable) || is_null($removed))
) {
$return = [];
if (!is_null($used)) {
foreach ($used as $usage) {
$return['used'][] = $this->databaseStorage->saveUsage([
'namespace' => $namespace,
'media_id' => $usage,
]);
}
}
if (!is_null($removed)) {
foreach ($removed as $remove) {
$return['removed'][] = $this->databaseStorage->removeUsage([
'namespace' => $namespace,
'media_id' => $remove,
]);
}
}
return $return;
} else {
throw new \InvalidArgumentException('Arguments must by "(string) $namespace", "(array) $used" and "(array) $removed"".');
}
}
/**
* @param string $namepsace
* @param string|bool $mediaId if string set primary for concrete media_id, if false, unset primary for whole namespace
* @return mixed
*/
public function setPrimary($namespace, $mediaId)
{
return $this->databaseStorage->setPrimary($namespace, $mediaId);
}
/**
* @param $row
* @return mixed
*/
private function hydrateRow($row)
{
if (isset($row['is_image'])) {
if ($row['is_image'] == 1) {
$file = self::imageObjectBuilder();
} else {
$file = self::fileObjectBuilder();
}
$file->fillData($row);
return $file;
}
throw new StorageException('Can\'not create file or image object from data.');
}
private function hydrateRowContainer($rowContainer)
{
if (is_array($this->currentFilter->getPairs())) {
return $rowContainer;
}
if ($this->currentFilter->isFind()) {
$rows = [];
foreach ($rowContainer as $row) {
$rows[] = $this->hydrateRow($row);
}
return $rows;
}
if ($this->currentFilter->isGet()) {
return $this->hydrateRow($rowContainer);
}
throw new StorageException('Can\'not determine hydratation mode.');
}
}