1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\UploadPerPartes\Target\Local\TemporaryStorage\Storage; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_files\FilesException; |
7
|
|
|
use kalanis\kw_files\Traits\TToStream; |
8
|
|
|
use kalanis\kw_storage\Interfaces\IStorage; |
9
|
|
|
use kalanis\kw_storage\StorageException; |
10
|
|
|
use kalanis\UploadPerPartes\Interfaces\ITemporaryStorage; |
11
|
|
|
use kalanis\UploadPerPartes\Interfaces\IUppTranslations; |
12
|
|
|
use kalanis\UploadPerPartes\Traits\TLang; |
13
|
|
|
use kalanis\UploadPerPartes\UploadException; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Storage |
18
|
|
|
* @package kalanis\UploadPerPartes\Target\Local\TemporaryStorage\Storage |
19
|
|
|
* Storing driving file data in simple storage represented by kw_storage package |
20
|
|
|
*/ |
21
|
|
|
class Storage implements ITemporaryStorage |
22
|
|
|
{ |
23
|
|
|
use TLang; |
24
|
|
|
use TToStream; |
25
|
|
|
|
26
|
|
|
protected IStorage $storage; |
27
|
|
|
protected string $keyPrefix = ''; |
28
|
|
|
|
29
|
|
|
public function __construct(IStorage $storage, string $keyPrefix = '', IUppTranslations $lang = null) |
30
|
|
|
{ |
31
|
|
|
$this->storage = $storage; |
32
|
|
|
$this->keyPrefix = $keyPrefix; |
33
|
|
|
$this->setUppLang($lang); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function exists(string $path): bool |
37
|
|
|
{ |
38
|
|
|
try { |
39
|
|
|
return $this->storage->exists($this->fullPath($path)); |
40
|
|
|
} catch (StorageException $ex) { |
41
|
|
|
throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function readData(string $path, ?int $fromByte, ?int $length): string |
46
|
|
|
{ |
47
|
|
|
try { |
48
|
|
|
$fullPath = $this->fullPath($path); |
49
|
|
|
$current = $this->storage->exists($fullPath) ? $this->storage->read($fullPath) : ''; |
50
|
|
|
return substr($current, intval($fromByte), $length); |
51
|
|
|
} catch (StorageException $ex) { |
52
|
|
|
throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function truncate(string $path, int $fromByte): bool |
57
|
|
|
{ |
58
|
|
|
try { |
59
|
|
|
$fullPath = $this->fullPath($path); |
60
|
|
|
$current = $this->storage->exists($fullPath) ? $this->storage->read($fullPath) : ''; |
61
|
|
|
return $this->storage->write($fullPath, substr($current, 0, $fromByte)); |
62
|
|
|
} catch (StorageException $ex) { |
63
|
|
|
throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function append(string $path, string $content): bool |
68
|
|
|
{ |
69
|
|
|
try { |
70
|
|
|
$fullPath = $this->fullPath($path); |
71
|
|
|
$current = $this->storage->exists($fullPath) ? $this->storage->read($fullPath) : ''; |
72
|
|
|
return $this->storage->write($fullPath, $current . $content); |
73
|
|
|
} catch (StorageException $ex) { |
74
|
|
|
throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function readStream(string $path) |
79
|
|
|
{ |
80
|
|
|
try { |
81
|
|
|
$fullPath = $this->fullPath($path); |
82
|
|
|
$current = $this->storage->exists($fullPath) ? $this->storage->read($fullPath) : ''; |
83
|
|
|
return $this->toStream($path, $current); |
84
|
|
|
} catch (FilesException | StorageException $ex) { |
85
|
|
|
throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function remove(string $key): bool |
90
|
|
|
{ |
91
|
|
|
try { |
92
|
|
|
return $this->storage->remove($this->fullPath($key)); |
93
|
|
|
} catch (StorageException $ex) { |
94
|
|
|
throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function fullPath(string $path): string |
99
|
|
|
{ |
100
|
|
|
return $this->keyPrefix . $path; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|