1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\UploadPerPartes\Target\Local\FinalStorage\Storage; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_files\FilesException; |
7
|
|
|
use kalanis\kw_files\Traits\TToString; |
8
|
|
|
use kalanis\kw_paths\Stuff; |
9
|
|
|
use kalanis\kw_storage\Interfaces\IStorage; |
10
|
|
|
use kalanis\kw_storage\StorageException; |
11
|
|
|
use kalanis\UploadPerPartes\Interfaces\IFinalStorage; |
12
|
|
|
use kalanis\UploadPerPartes\Interfaces\IUppTranslations; |
13
|
|
|
use kalanis\UploadPerPartes\Traits\TLang; |
14
|
|
|
use kalanis\UploadPerPartes\UploadException; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Storage |
19
|
|
|
* @package kalanis\UploadPerPartes\Target\Local\FinalStorage\Storage |
20
|
|
|
* Where to store data on target destination - storage based on kw_storage |
21
|
|
|
*/ |
22
|
|
|
class Storage implements IFinalStorage |
23
|
|
|
{ |
24
|
|
|
use TLang; |
25
|
|
|
use TToString; |
26
|
|
|
|
27
|
|
|
protected IStorage $storage; |
28
|
|
|
protected string $keyPrefix = ''; |
29
|
|
|
|
30
|
22 |
|
public function __construct(IStorage $storage, string $keyPrefix = '', IUppTranslations $lang = null) |
31
|
|
|
{ |
32
|
22 |
|
$this->storage = $storage; |
33
|
22 |
|
$this->keyPrefix = $keyPrefix; |
34
|
22 |
|
$this->setUppLang($lang); |
35
|
22 |
|
} |
36
|
|
|
|
37
|
7 |
|
public function exists(string $key): bool |
38
|
|
|
{ |
39
|
|
|
try { |
40
|
7 |
|
return $this->storage->exists($this->fullPath($key)); |
41
|
1 |
|
} catch (StorageException $ex) { |
42
|
1 |
|
throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
7 |
|
public function store(string $path, $data): bool |
47
|
|
|
{ |
48
|
|
|
try { |
49
|
7 |
|
return $this->storage->write($this->fullPath($path), $this->toString($path, $data)); |
50
|
1 |
|
} catch (FilesException | StorageException $ex) { |
51
|
1 |
|
throw new UploadException($this->getUppLang()->uppCannotWriteFile($path), $ex->getCode(), $ex); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
6 |
|
public function findName(string $key): string |
56
|
|
|
{ |
57
|
6 |
|
if (!$this->exists($key)) { |
58
|
6 |
|
return $key; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
$name = Stuff::fileBase($key); |
62
|
1 |
|
$suffix = Stuff::fileExt($key); |
63
|
|
|
|
64
|
1 |
|
$i = 0; |
65
|
1 |
|
while ($this->exists($name . $this->getNameSeparator() . strval($i) . $suffix)) { |
66
|
1 |
|
$i++; |
67
|
|
|
} |
68
|
1 |
|
return $name . $this->getNameSeparator() . strval($i) . $suffix; |
69
|
|
|
} |
70
|
|
|
|
71
|
8 |
|
protected function fullPath(string $key): string |
72
|
|
|
{ |
73
|
8 |
|
return $this->keyPrefix . $key; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
protected function getNameSeparator(): string |
77
|
|
|
{ |
78
|
1 |
|
return '__'; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|