|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\UploadPerPartes\Target\Local\FinalStorage\Storage; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_paths\Stuff; |
|
7
|
|
|
use kalanis\UploadPerPartes\Interfaces\IFinalStorage; |
|
8
|
|
|
use kalanis\UploadPerPartes\Interfaces\IUppTranslations; |
|
9
|
|
|
use kalanis\UploadPerPartes\Traits\TLang; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class Files |
|
14
|
|
|
* @package kalanis\UploadPerPartes\Target\Local\FinalStorage\Storage |
|
15
|
|
|
* Where to store data on target destination - storage is local volume |
|
16
|
|
|
*/ |
|
17
|
|
|
class Volume implements IFinalStorage |
|
18
|
|
|
{ |
|
19
|
|
|
use TLang; |
|
20
|
|
|
|
|
21
|
|
|
protected string $pathPrefix = ''; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(string $pathPrefix = '', IUppTranslations $lang = null) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->pathPrefix = $pathPrefix; |
|
26
|
|
|
$this->setUppLang($lang); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function exists(string $key): bool |
|
30
|
|
|
{ |
|
31
|
|
|
return @file_exists($this->fullPath($key)); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function store(string $key, $data): bool |
|
35
|
|
|
{ |
|
36
|
|
|
@rewind($data); |
|
|
|
|
|
|
37
|
|
|
return false !== @file_put_contents($this->fullPath($key), $data); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function findName(string $key): string |
|
41
|
|
|
{ |
|
42
|
|
|
if (!$this->exists($key)) { |
|
43
|
|
|
return $key; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$name = Stuff::fileBase($key); |
|
47
|
|
|
$suffix = Stuff::fileExt($key); |
|
48
|
|
|
|
|
49
|
|
|
$i = 0; |
|
50
|
|
|
while ($this->exists($name . $this->getNameSeparator() . strval($i) . '.' . $suffix)) { |
|
51
|
|
|
$i++; |
|
52
|
|
|
} |
|
53
|
|
|
return $name . $this->getNameSeparator() . strval($i) . '.' . $suffix; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function fullPath(string $key): string |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->pathPrefix . $key; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function getNameSeparator(): string |
|
62
|
|
|
{ |
|
63
|
|
|
return '__'; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: