1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\UploadPerPartes\Target\Local\FinalStorage\Storage; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_files\Access\CompositeAdapter; |
7
|
|
|
use kalanis\kw_files\Extended\FindFreeName; |
8
|
|
|
use kalanis\kw_files\FilesException; |
9
|
|
|
use kalanis\kw_paths\ArrayPath; |
10
|
|
|
use kalanis\kw_paths\PathsException; |
11
|
|
|
use kalanis\kw_paths\Stuff; |
12
|
|
|
use kalanis\UploadPerPartes\Interfaces\IFinalStorage; |
13
|
|
|
use kalanis\UploadPerPartes\Interfaces\IUppTranslations; |
14
|
|
|
use kalanis\UploadPerPartes\Traits\TLang; |
15
|
|
|
use kalanis\UploadPerPartes\UploadException; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Files |
20
|
|
|
* @package kalanis\UploadPerPartes\Target\Local\FinalStorage\Storage |
21
|
|
|
* Where to store data on target destination - storage based on kw_files |
22
|
|
|
*/ |
23
|
|
|
class Files implements IFinalStorage |
24
|
|
|
{ |
25
|
|
|
use TLang; |
26
|
|
|
|
27
|
|
|
/** @var string[] */ |
28
|
|
|
protected array $targetDir = []; |
29
|
|
|
protected CompositeAdapter $files; |
30
|
|
|
protected FindFreeName $freeName; |
31
|
|
|
protected ArrayPath $paths; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param CompositeAdapter $files |
35
|
|
|
* @param string[] $targetDir |
36
|
|
|
* @param ArrayPath|null $arrayPaths |
37
|
|
|
* @param IUppTranslations|null $lang |
38
|
|
|
*/ |
39
|
5 |
|
public function __construct(CompositeAdapter $files, array $targetDir = [], ?ArrayPath $arrayPaths = null, IUppTranslations $lang = null) |
40
|
|
|
{ |
41
|
5 |
|
$this->files = $files; |
42
|
5 |
|
$this->targetDir = $targetDir; |
43
|
5 |
|
$this->paths = $arrayPaths ?: new ArrayPath(); |
44
|
5 |
|
$this->freeName = new FindFreeName($files); |
45
|
5 |
|
$this->setUppLang($lang); |
46
|
5 |
|
} |
47
|
|
|
|
48
|
2 |
|
public function exists(string $key): bool |
49
|
|
|
{ |
50
|
|
|
try { |
51
|
2 |
|
return $this->files->exists($this->fullPath($key)); |
52
|
1 |
|
} catch (FilesException | PathsException $ex) { |
53
|
1 |
|
throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
public function store(string $key, $data): bool |
58
|
|
|
{ |
59
|
|
|
try { |
60
|
2 |
|
return $this->files->saveFileStream($this->fullPath($key), $data); |
61
|
1 |
|
} catch (FilesException | PathsException $ex) { |
62
|
1 |
|
throw new UploadException($this->getUppLang()->uppCannotWriteFile($key), $ex->getCode(), $ex); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
public function findName(string $key): string |
67
|
|
|
{ |
68
|
|
|
try { |
69
|
2 |
|
$this->paths->setString($key); |
70
|
2 |
|
$fileName = $this->paths->getFileName(); |
71
|
2 |
|
$directory = $this->paths->getArrayDirectory(); |
72
|
2 |
|
$freeName = $this->freeName->findFreeName( |
73
|
2 |
|
array_merge($this->targetDir, $directory), |
74
|
2 |
|
Stuff::fileBase($fileName), |
75
|
2 |
|
'.' . Stuff::fileExt($fileName) |
76
|
|
|
); |
77
|
1 |
|
return $this->paths->setArray(array_merge( |
78
|
1 |
|
$directory, |
79
|
1 |
|
[$freeName] |
80
|
1 |
|
))->getString(); |
81
|
1 |
|
} catch (FilesException | PathsException $ex) { |
82
|
1 |
|
throw new UploadException($this->getUppLang()->uppCannotWriteFile($key), $ex->getCode(), $ex); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $key |
88
|
|
|
* @throws PathsException |
89
|
|
|
* @return string[] |
90
|
|
|
*/ |
91
|
3 |
|
protected function fullPath(string $key): array |
92
|
|
|
{ |
93
|
3 |
|
return array_merge($this->targetDir, $this->paths->setString($key)->getArray()); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|