|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\UploadPerPartes\Target\Local\TemporaryStorage; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\UploadPerPartes\Interfaces; |
|
7
|
|
|
use kalanis\UploadPerPartes\Traits\TLang; |
|
8
|
|
|
use kalanis\UploadPerPartes\Uploader\Data; |
|
9
|
|
|
use kalanis\UploadPerPartes\UploadException; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class TemporaryStorage |
|
14
|
|
|
* @package kalanis\UploadPerPartes\Target\Local\TemporaryStorage |
|
15
|
|
|
* Actions over temporary storage |
|
16
|
|
|
*/ |
|
17
|
|
|
class TemporaryStorage |
|
18
|
|
|
{ |
|
19
|
|
|
use TLang; |
|
20
|
|
|
|
|
21
|
|
|
protected Interfaces\ITemporaryStorage $storage; |
|
22
|
|
|
protected KeyEncoders\AEncoder $keyModifier; |
|
23
|
|
|
|
|
24
|
20 |
|
public function __construct( |
|
25
|
|
|
Interfaces\ITemporaryStorage $storage, |
|
26
|
|
|
KeyEncoders\AEncoder $keyModifier, |
|
27
|
|
|
?Interfaces\IUppTranslations $lang = null |
|
28
|
|
|
) |
|
29
|
|
|
{ |
|
30
|
20 |
|
$this->storage = $storage; |
|
31
|
20 |
|
$this->keyModifier = $keyModifier; |
|
32
|
20 |
|
$this->setUppLang($lang); |
|
33
|
20 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param Data $data |
|
37
|
|
|
* @throws UploadException |
|
38
|
|
|
* @return Data |
|
39
|
|
|
*/ |
|
40
|
14 |
|
public function fillData(Data $data): Data |
|
41
|
|
|
{ |
|
42
|
14 |
|
return $this->keyModifier->toData($data); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param Data $data |
|
47
|
|
|
* @throws UploadException |
|
48
|
|
|
* @return bool |
|
49
|
|
|
*/ |
|
50
|
15 |
|
public function exists(Data $data): bool |
|
51
|
|
|
{ |
|
52
|
15 |
|
return $this->storage->exists($this->keyModifier->fromData($data)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param Data $data |
|
57
|
|
|
* @param int<0, max> $fromByte |
|
58
|
|
|
* @throws UploadException |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
4 |
|
public function checksumData(Data $data, int $fromByte): string |
|
62
|
|
|
{ |
|
63
|
4 |
|
$data = $this->storage->readData($this->keyModifier->fromData($data), $fromByte, $data->bytesPerPart); |
|
64
|
4 |
|
if (empty($data)) { |
|
65
|
3 |
|
throw new UploadException($this->getUppLang()->uppChecksumIsEmpty()); |
|
66
|
|
|
} |
|
67
|
2 |
|
return $data; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param Data $data |
|
72
|
|
|
* @param int<0, max> $fromByte |
|
73
|
|
|
* @throws UploadException |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
3 |
|
public function truncate(Data $data, int $fromByte): bool |
|
77
|
|
|
{ |
|
78
|
3 |
|
return $this->storage->truncate($this->keyModifier->fromData($data), $fromByte); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param Data $data |
|
83
|
|
|
* @param string $content decoded content from client |
|
84
|
|
|
* @throws UploadException |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
5 |
|
public function upload(Data $data, string $content): bool |
|
88
|
|
|
{ |
|
89
|
5 |
|
return $this->storage->append($this->keyModifier->fromData($data), $content); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param Data $data |
|
94
|
|
|
* @throws UploadException |
|
95
|
|
|
* @return resource stream |
|
96
|
|
|
*/ |
|
97
|
6 |
|
public function read(Data $data) |
|
98
|
|
|
{ |
|
99
|
6 |
|
return $this->storage->readStream($this->keyModifier->fromData($data)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param Data $data |
|
104
|
|
|
* @throws UploadException |
|
105
|
|
|
* @return bool |
|
106
|
|
|
*/ |
|
107
|
11 |
|
public function remove(Data $data) |
|
108
|
|
|
{ |
|
109
|
11 |
|
return $this->storage->remove($this->keyModifier->fromData($data)); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|