-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLocalFileStorage.php
More file actions
518 lines (459 loc) · 19 KB
/
LocalFileStorage.php
File metadata and controls
518 lines (459 loc) · 19 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
<?php
namespace App\Helpers\FileStorage;
use App\Helpers\TmpFilesHelper;
use Nette\Utils\Arrays;
use Nette\SmartObject;
use ZipArchive;
use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
/**
* File storage that stores immutable files organized in sub-directories.
* The storage also allows direct ZIP archive dereference (path/to/file.zip#zip).
* The ZIP archive access allows only one-level of dereference (i.e., not accessing archives within archives).
*
* Furthermore, there are no explicit methods for managing directories. Directories (as well as archives) are
* automatically created as needed (e.g., when file is being stored) and removed when empty (removed are only
* directories, not archives).
*
* The local archive must be entirely on one partition, so the files may be moved by rename function.
*/
class LocalFileStorage implements IFileStorage
{
use SmartObject;
/**
* @var TmpFilesHelper
*/
protected $tmpFilesHelper;
protected $rootDirectory;
public function getRootDirectory(): ?string
{
return $this->rootDirectory;
}
/**
* Constructor
* @param array $params initial configuration
*/
public function __construct(TmpFilesHelper $tmpFilesHelper, array $params = [])
{
$this->tmpFilesHelper = $tmpFilesHelper;
$this->rootDirectory = Arrays::get($params, "root", null);
if (!$this->rootDirectory || !is_dir($this->rootDirectory)) {
throw new FileStorageException(
"Specified file storage root must be an existing directory.",
$this->rootDirectory
);
}
}
/**
* Verify and normalize relative storage path. The path must not contain '..' as a substring.
* @param string $path to normalize
* @return string normalized path
* @throws FileStorageException if the path is invalid
*/
private static function normalizePath(string $path): string
{
$path = preg_replace('@/[.]/@', '/', $path);
$path = preg_replace('@[/\\\\]+@', '/', $path);
$path = preg_replace('@(^[.]/)|(/[.]?$)@', '', $path);
if (str_starts_with($path, '../') || str_contains($path, '/../') || str_ends_with($path, '/..')) {
throw new FileStorageException("Substring '..' must not be present in any path.", $path);
}
return $path;
}
/**
* Decode a storage path.
* @param string $path path is also normalized (that is why it is passed by reference)
* @param bool|null $exists If true/false, (non)existence checks are peformed, null = no checks.
* Checks are performed on real path only (not inside ZIP archives).
* @param bool $mkdir whether to make sure all underlying sub-directories exist (mkdir if necessary)
* @return array a tuple containing real path (first) and ZIP file entry or null (second)
*/
private function decodePath(string &$path, bool $exists = null, $mkdir = false): array
{
$tokens = explode('#', $path, 2);
array_push($tokens, null); // make sure second item always exists
[$realPath, $zipEntry] = $tokens;
$realPath = $this->rootDirectory . '/' . self::normalizePath($realPath);
if (is_dir($realPath)) {
throw new FileStorageException("Given path refers to a directory.", $path);
}
if ($exists === true) {
// checking the real path exists (not the zip entry)
if (!file_exists($realPath) || !is_file($realPath)) {
throw new FileStorageException("File not found within the storage.", $path);
}
}
if ($exists === false && !$zipEntry) {
// check the file does not exist (skipped for zip entries, since we are not openning the ZIP archive here)
if (file_exists($realPath)) {
throw new FileStorageException("File already exists.", $path);
}
}
if ($mkdir) {
$dir = dirname($realPath);
if (!@mkdir($dir, 0775, true) && !is_dir($dir)) {
throw new FileStorageException("Unable to create a directory path.", $dir);
}
}
return [$realPath, $zipEntry];
}
/**
* Verify that given directory is empty and remove it.
* Works recursively up to storage root.
* @param string $path relative (storage) path to the directory.
*/
private function removeEmptyDirectory(string $path): void
{
if (!$path || $path === '.') {
return;
}
$realPath = $this->rootDirectory . '/' . $path;
if (is_dir($realPath)) {
$dh = opendir($realPath);
if (!$dh) {
return; // something is very odd, this should have worked
}
// check emptiness
while (($file = readdir($dh)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
closedir($dh);
return; // we found at least one valid entry -> cannot delete
}
closedir($dh);
// It is empty, let's proceed!
if (@rmdir($realPath) && str_contains($path, '/')) {
$this->removeEmptyDirectory(dirname($path));
}
}
}
/*
* IFileStorage
*/
public function fetch(string $path): ?IImmutableFile
{
[$realPath, $zipEntry] = $this->decodePath($path);
if (!file_exists($realPath) || !is_file($realPath)) {
return null;
}
if ($zipEntry) {
$zip = new ZipArchive();
if ($zip->open($realPath) !== true || !$zip->statName($zipEntry)) {
$zip->close();
return null;
}
return new ArchivedImmutableFile($realPath, $zipEntry, $path, $this->tmpFilesHelper);
}
return new LocalImmutableFile($realPath, $path);
}
public function fetchOrThrow(string $path): IImmutableFile
{
$file = $this->fetch($path);
if (!$file) {
throw new FileStorageException("File not found within the storage.", $path);
}
return $file;
}
public function storeFile(string $localPath, string $storagePath, bool $move = true, bool $overwrite = false): void
{
// make sure input file exists and is readable
if (!file_exists($localPath) || !is_file($localPath)) {
throw new FileStorageException("Given local file not found.", $localPath);
}
if (!is_readable($localPath)) {
throw new FileStorageException("Given file is not accessible for reading.", $localPath);
}
[$realPath, $zipEntry] = $this->decodePath(
$storagePath,
$overwrite ? null : false, // no overwrite -> not exist check
true // construct directory path
);
if (!$zipEntry) {
// saving an actual local file
if (file_exists($realPath) && $overwrite) {
@unlink($realPath);
}
if (!$move || !@rename($localPath, $realPath)) { // rename fails -> fallback to copying
if (!@copy($localPath, $realPath)) {
throw new FileStorageException(
"Unable to add given file into file storage as '$storagePath'.",
$localPath
);
}
if ($move) {
@unlink($localPath); // copy used to simulated move -> delete original
}
}
} else {
// saving a ZIP entry
$zip = new ZipFileStorage($this->tmpFilesHelper, $realPath, null, false);
$zip->storeFile($localPath, $zipEntry, $move, $overwrite);
$zip->close();
}
}
public function storeContents($contents, string $storagePath, bool $overwrite = false): void
{
[$realPath, $zipEntry] = $this->decodePath(
$storagePath,
$overwrite ? null : false, // no overwrite -> not exist check
true // construct directory path
);
if (!$zipEntry) {
if (file_put_contents($realPath, $contents) === false) {
throw new FileStorageException(
"Unable to save data as file in the storage.",
$storagePath
);
}
} else {
// saving a ZIP entry
$zip = new ZipFileStorage($this->tmpFilesHelper, $realPath, null, false);
$zip->storeContents($contents, $zipEntry, $overwrite);
$zip->close();
}
}
public function storeStream($stream, string $storagePath, bool $overwrite = false): void
{
[$realPath, $zipEntry] = $this->decodePath(
$storagePath,
$overwrite ? null : false, // no overwrite -> not exist check
true // construct directory path
);
if (!$zipEntry) {
$fp = @fopen($realPath, "wb");
if (!$fp) {
throw new FileStorageException(
"Unable to open target file in the storage for writing.",
$storagePath
);
}
if (stream_copy_to_stream($stream, $fp) === false || !@fclose($fp)) {
throw new FileStorageException(
"Copying stream data into target file failed.",
$storagePath
);
}
} else {
// saving a ZIP entry
$zip = new ZipFileStorage($this->tmpFilesHelper, $realPath, null, false);
$zip->storeStream($stream, $zipEntry, $overwrite);
$zip->close();
}
}
public function copy(string $src, string $dst, bool $overwrite = false): void
{
[$srcReal, $srcZip] = $this->decodePath($src, true); // true = check exists
[$dstReal, $dstZip] = $this->decodePath(
$dst,
$overwrite ? null : false, // no overwrite -> not exist check
true // construct directory path
);
if ($srcReal === $dstReal && $srcZip === $dstZip) {
throw new FileStorageException("Unable to copy file to itself.", $src);
}
if ($srcReal === $dstReal && ($srcZip || $dstZip) && (!$srcZip || !$dstZip)) {
throw new FileStorageException(
"Unable to manipulate with ZIP archive and its contents in one copy procedure.",
$src
);
}
if ($srcZip && $dstZip && $srcReal === $dstReal) {
// copy witin one archive -> use ZipFileStorage implementation
$zip = new ZipFileStorage($this->tmpFilesHelper, $srcReal, null, false);
$zip->copy($srcZip, $dstZip, $overwrite);
$zip->close();
} elseif ($srcZip || $dstZip) {
// source or destination (or both) are ZIP archives -> fetch & store
$srcFile = $this->fetch($src);
if ($srcFile === null) {
throw new FileStorageException("File not found within the storage.", $src);
}
if ($srcFile->getSize() < 4096 * 1024) {
// copy via memory
$contents = $srcFile->getContents();
$this->storeContents($contents, $dst, $overwrite);
} else {
// fallback to tmp file
$tmpFile = $this->tmpFilesHelper->createTmpFile("rexlfs");
try {
$srcFile->saveAs($tmpFile);
$this->storeFile($tmpFile, $dst, $overwrite);
} finally {
@unlink($tmpFile);
}
}
} else {
// copy regular file within the storage (using internal copy() function)
if (file_exists($dstReal) && $overwrite) { // file exists and not overwrite is handled in decodePath
if (!@unlink($dstReal)) {
throw new FileStorageException("Unable to overwrite target file.", $dst);
}
}
if (!@copy($srcReal, $dstReal)) {
throw new FileStorageException("Copying failed.", $src);
}
}
}
public function move(string $src, string $dst, bool $overwrite = false): void
{
[$srcReal, $srcZip] = $this->decodePath($src, true); // true = check exists
[$dstReal, $dstZip] = $this->decodePath(
$dst,
$overwrite ? null : false, // no overwrite -> not exist check
true // construct directory path
);
if ($srcReal === $dstReal && $srcZip === $dstZip) {
return; // nothing to move
}
if ($srcReal === $dstReal && ($srcZip || $dstZip) && (!$srcZip || !$dstZip)) {
throw new FileStorageException(
"Unable to manipulate with ZIP archive and its contents in one move procedure.",
$src
);
}
if ($srcZip && $dstZip && $srcReal === $dstReal) {
// move witin one archive
$zip = new ZipFileStorage($this->tmpFilesHelper, $srcReal, null, false);
$zip->move($srcZip, $dstZip, $overwrite);
$zip->close();
} elseif ($srcZip || $dstZip) {
// move with some archive involvment, but not simply in one archive -> fallback to copy
$this->copy($src, $dst, $overwrite);
if (!$this->delete($src)) {
throw new FileStorageException("Unable to remove source file after copy-moving procedure.", $src);
}
} else {
// move regular file within the storage
if (file_exists($dstReal) && $overwrite) { // file exists and not overwrite is handled in decodePath
if (!@unlink($dstReal)) {
throw new FileStorageException("Unable to overwrite target file.", $dst);
}
}
if (!@rename($srcReal, $dstReal)) {
throw new FileStorageException("Moving failed.", $src);
}
}
}
public function extract(string $storagePath, string $localPath, bool $overwrite = false): void
{
if (!$overwrite && file_exists($localPath)) {
throw new FileStorageException("Target file exists.", $localPath);
}
[$srcReal, $srcZip] = $this->decodePath($storagePath, true); // true = check exists
if ($srcZip) {
$zip = new ZipFileStorage($this->tmpFilesHelper, $srcReal, null, false);
$zip->extract($srcZip, $localPath, $overwrite);
$zip->close();
} else {
if (!@rename($srcReal, $localPath) && !@copy($srcReal, $localPath)) {
throw new FileStorageException("Extraction failed, unable to move nor copy the file.", $storagePath);
}
if (file_exists($srcReal) && !@unlink($srcReal)) {
throw new FileStorageException("Unable to delete file in the storage.", $storagePath);
}
if (str_contains($storagePath, '/')) {
// removing unnecessary empty directories
$this->removeEmptyDirectory(dirname($storagePath));
}
}
}
public function delete(string $path): bool
{
[$realPath, $zipEntry] = $this->decodePath($path);
if (!$zipEntry) {
// removing actual file
if (!file_exists($realPath)) {
return false;
}
if (!@unlink($realPath)) {
throw new FileStorageException("Unable to delete file in the storage.", $path);
}
if (str_contains($path, '/')) {
// removing unnecessary empty directories
$this->removeEmptyDirectory(dirname($path));
}
return true;
} else {
// removing a ZIP entry
$zip = new ZipFileStorage($this->tmpFilesHelper, $realPath, null, false);
$res = $zip->delete($zipEntry);
$zip->close();
return $res;
}
}
public function deleteOldFiles(string $glob, int $threshold): int
{
$glob = $this->rootDirectory . '/' . $glob;
$rootDirLen = strlen($this->rootDirectory ?? '');
$deleted = 0;
$affectedDirectories = [];
foreach (glob($glob) as $file) {
if (!is_file($file)) {
continue;
}
$ts = @filemtime($file);
if ($ts !== false && $ts < $threshold) {
// file is too old, remove it!
if (substr($file, 0, $rootDirLen) === $this->rootDirectory) {
$dir = dirname(substr($file, $rootDirLen + 1));
$affectedDirectories[$dir] = true; // save the directory for the final cleanup
}
if (!@unlink($file)) {
throw new FileStorageException("Unable to delete file in the storage.", $file);
}
++$deleted;
}
}
// try to remove all affected directories (if they are empty)
foreach (array_keys($affectedDirectories) as $dir) {
$this->removeEmptyDirectory($dir);
}
return $deleted;
}
public function deleteByFilter(string $prefix, callable $filter): int
{
$root = $this->rootDirectory;
// small optimization - if dir is recognized, let's scan only that subdir to save time
$slashPos = strrpos($prefix, '/');
if ($slashPos) {
$root .= '/' . substr($prefix, 0, $slashPos);
}
if (!is_dir($root) || !is_writeable($root)) {
return 0; // nothinth to do
}
$rootDirLen = strlen($this->rootDirectory ?? '') + 1; // plus 1 for '/';
$dirIterator = new RecursiveDirectoryIterator(
$root,
FilesystemIterator::CURRENT_AS_PATHNAME | FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS
);
$recursiveIterator = new RecursiveIteratorIterator($dirIterator);
// iterate over files and filter them
$toDelete = []; // we store the paths as a list first to avoid any iterator confusions
foreach ($recursiveIterator as $realPath) {
if (!str_starts_with($realPath, $root)) {
throw new FileStorageException("Iterator returned a file outside the root directory.", $realPath);
}
$path = substr($realPath, $rootDirLen); // get the suffix without the root directory
if (!$prefix || str_starts_with($path, $prefix)) {
$file = new LocalImmutableFile($realPath, $path);
if (!$filter($file)) {
$toDelete[] = $path;
}
}
}
// go through the list and try to delete the files
$actuallyDeleted = 0;
foreach ($toDelete as $path) {
if ($this->delete($path)) {
++$actuallyDeleted;
}
}
return $actuallyDeleted;
}
public function flush(): void
{
// local fs does not require flush, all changes are made immediately
}
}