1
|
|
|
<?php |
2
|
|
|
namespace AppBundle\Sync\Storage; |
3
|
|
|
|
4
|
|
|
use AppBundle\Exception\LocalStorageException; |
5
|
|
|
use DateTime; |
6
|
|
|
use FilesystemIterator; |
7
|
|
|
use AppBundle\Sync\Entity\File; |
8
|
|
|
use AppBundle\Sync\Entity\FileCollection; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Local app storage |
12
|
|
|
* |
13
|
|
|
* @author Sergey Sadovoi <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Local implements StorageInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* {@inheritdoc} |
19
|
|
|
*/ |
20
|
5 |
|
public function put($sourcePath, $destPath) |
21
|
|
|
{ |
22
|
5 |
|
if (!is_file($sourcePath)) { |
23
|
1 |
|
throw new LocalStorageException( |
24
|
1 |
|
sprintf('File %s not found', $sourcePath), |
25
|
|
|
LocalStorageException::FILE_NOT_FOUND |
26
|
1 |
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
4 |
|
$this->ensureDirectory(dirname($destPath)); |
30
|
|
|
|
31
|
3 |
|
$result = @copy($sourcePath, $destPath); |
32
|
3 |
|
if (!$result) { |
33
|
1 |
|
throw new LocalStorageException( |
34
|
1 |
|
sprintf('Copy failed: %s', $sourcePath), |
35
|
|
|
LocalStorageException::OPERATION_FAIL |
36
|
1 |
|
); |
37
|
|
|
} |
38
|
2 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
4 |
|
public function delete($path) |
44
|
|
|
{ |
45
|
4 |
|
if (!is_file($path)) { |
46
|
1 |
|
throw new LocalStorageException( |
47
|
1 |
|
sprintf('File %s not found', $path), |
48
|
|
|
LocalStorageException::FILE_NOT_FOUND |
49
|
1 |
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
$result = unlink($path); |
53
|
3 |
|
if (!$result) { |
54
|
1 |
|
throw new LocalStorageException( |
55
|
1 |
|
sprintf('Delete failed: %s', $path), |
56
|
|
|
LocalStorageException::OPERATION_FAIL |
57
|
1 |
|
); |
58
|
|
|
} |
59
|
2 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
3 |
|
public function listContents($directory = '') |
65
|
|
|
{ |
66
|
3 |
|
$flags = FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS; |
67
|
|
|
|
68
|
|
|
try { |
69
|
3 |
|
$dirIterator = new \RecursiveDirectoryIterator($directory, $flags); |
70
|
3 |
|
} catch (\UnexpectedValueException $e) { |
71
|
1 |
|
throw new LocalStorageException( |
72
|
1 |
|
sprintf('Directory missing: %s', $directory), |
73
|
|
|
LocalStorageException::FILE_NOT_FOUND |
74
|
1 |
|
); |
75
|
|
|
} |
76
|
2 |
|
$fileIterator = new \RecursiveIteratorIterator($dirIterator); |
77
|
|
|
|
78
|
2 |
|
$fc = new FileCollection(); |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var \SplFileInfo $rawFile |
82
|
|
|
*/ |
83
|
2 |
|
foreach ($fileIterator as $rawFile) { |
84
|
2 |
|
$modified = new DateTime('@' . $rawFile->getMTime()); |
85
|
|
|
|
86
|
2 |
|
$file = new File(); |
87
|
2 |
|
$file->setUid($rawFile->getBasename()); |
88
|
2 |
|
$file->setPath($rawFile->getPathname()); |
89
|
2 |
|
$file->setSize($rawFile->getSize()); |
90
|
2 |
|
$file->setModified($modified); |
91
|
|
|
|
92
|
2 |
|
$fc->addFile($file); |
93
|
2 |
|
} |
94
|
|
|
|
95
|
2 |
|
return $fc; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Ensure the directory exists. |
100
|
|
|
* |
101
|
|
|
* @param string $dir Directory path |
102
|
|
|
* |
103
|
|
|
* @return string Real path to dir |
104
|
|
|
*/ |
105
|
4 |
|
protected function ensureDirectory($dir) |
106
|
|
|
{ |
107
|
4 |
|
if (!is_dir($dir)) { |
108
|
3 |
|
$result = mkdir($dir, 0755, true); |
109
|
3 |
|
if (!$result) { |
110
|
1 |
|
throw new LocalStorageException( |
111
|
1 |
|
sprintf('Can\'t create directory %s', $dir), |
112
|
|
|
LocalStorageException::OPERATION_FAIL |
113
|
1 |
|
); |
114
|
|
|
} |
115
|
2 |
|
} |
116
|
|
|
|
117
|
3 |
|
return realpath($dir); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|