1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\UploadPerPartes\DataStorage; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\UploadPerPartes\Exceptions\UploadException; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class VolumeBasic |
11
|
|
|
* @package kalanis\UploadPerPartes\DataStorage |
12
|
|
|
* Processing info file on disk volume |
13
|
|
|
* Filesystem behaves oddly - beware of fucked up caching! |
14
|
|
|
* When someone got idea how to test it without ignoring failed states, please tell me. |
15
|
|
|
*/ |
16
|
|
|
class VolumeBasic extends AStorage |
17
|
|
|
{ |
18
|
1 |
|
public function exists(string $location): bool |
19
|
|
|
{ |
20
|
1 |
|
return is_file($location); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $location |
25
|
|
|
* @param string $content |
26
|
|
|
* @param int<0, max>|null $seek |
27
|
|
|
* @throws UploadException |
28
|
|
|
*/ |
29
|
4 |
|
public function addPart(string $location, string $content, ?int $seek = null): void |
30
|
|
|
{ |
31
|
4 |
|
if (is_null($seek)) { // append to end |
32
|
3 |
|
$pointer = @fopen($location, 'ab'); |
33
|
3 |
|
if (false == $pointer) { |
34
|
1 |
|
throw new UploadException($this->getUppLang()->uppCannotOpenFile($location)); |
35
|
|
|
} |
36
|
2 |
|
if (false === @fwrite($pointer, $content)) { |
37
|
|
|
// @codeCoverageIgnoreStart |
38
|
|
|
/** @scrutinizer ignore-unhandled */@fclose($pointer); |
39
|
|
|
throw new UploadException($this->getUppLang()->uppCannotWriteFile($location)); |
40
|
|
|
// @codeCoverageIgnoreEnd |
41
|
|
|
} |
42
|
2 |
|
/** @scrutinizer ignore-unhandled */@fclose($pointer); |
43
|
|
|
} else { // append from position |
44
|
1 |
|
$pointer = @fopen($location, 'rb+'); |
45
|
1 |
|
if (false === $pointer) { |
46
|
1 |
|
throw new UploadException($this->getUppLang()->uppCannotOpenFile($location)); |
47
|
|
|
} |
48
|
|
|
// @codeCoverageIgnoreStart |
49
|
|
|
$position = @fseek($pointer, $seek); |
50
|
|
|
if (-1 == $position) { |
51
|
|
|
/** @scrutinizer ignore-unhandled */@fclose($pointer); |
52
|
|
|
throw new UploadException($this->getUppLang()->uppCannotSeekFile($location)); |
53
|
|
|
} |
54
|
|
|
// @codeCoverageIgnoreEnd |
55
|
|
|
// @codeCoverageIgnoreStart |
56
|
|
|
if (false === @fwrite($pointer, $content)) { |
57
|
|
|
/** @scrutinizer ignore-unhandled */@fclose($pointer); |
58
|
|
|
throw new UploadException($this->getUppLang()->uppCannotWriteFile($location)); |
59
|
|
|
} |
60
|
|
|
/** @scrutinizer ignore-unhandled */@fclose($pointer); |
61
|
|
|
// @codeCoverageIgnoreEnd |
62
|
|
|
} |
63
|
2 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $location |
67
|
|
|
* @param int $offset |
68
|
|
|
* @param int|null $limit |
69
|
|
|
* @throws UploadException |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
1 |
|
public function getPart(string $location, int $offset, ?int $limit = null): string |
73
|
|
|
{ |
74
|
1 |
|
$pointer = @fopen($location, 'rb'); |
75
|
1 |
|
if (false == $pointer) { |
76
|
|
|
// @codeCoverageIgnoreStart |
77
|
|
|
throw new UploadException($this->getUppLang()->uppCannotOpenFile($location)); |
78
|
|
|
} |
79
|
|
|
// @codeCoverageIgnoreEnd |
80
|
1 |
|
if (empty($limit)) { |
81
|
1 |
|
$stat = @fseek($pointer, 0, SEEK_END); |
82
|
1 |
|
if (-1 == $stat) { |
83
|
|
|
// @codeCoverageIgnoreStart |
84
|
|
|
/** @scrutinizer ignore-unhandled */@fclose($pointer); |
85
|
|
|
throw new UploadException($this->getUppLang()->uppCannotSeekFile($location)); |
86
|
|
|
} |
87
|
|
|
// @codeCoverageIgnoreEnd |
88
|
1 |
|
$limit = @ftell($pointer) - $offset; |
89
|
|
|
} |
90
|
|
|
// @codeCoverageIgnoreStart |
91
|
1 |
|
$position = @fseek($pointer, $offset, SEEK_SET); |
92
|
1 |
|
if (-1 == $position) { |
93
|
|
|
/** @scrutinizer ignore-unhandled */@fclose($pointer); |
94
|
|
|
throw new UploadException($this->getUppLang()->uppCannotSeekFile($location)); |
95
|
|
|
} |
96
|
|
|
// @codeCoverageIgnoreEnd |
97
|
1 |
|
$data = @fread($pointer, intval($limit)); // @phpstan-ignore-line |
98
|
|
|
|
99
|
1 |
|
if (false === $data) { |
100
|
|
|
// @codeCoverageIgnoreStart |
101
|
|
|
throw new UploadException($this->getUppLang()->uppCannotReadFile($location)); |
102
|
|
|
// @codeCoverageIgnoreEnd |
103
|
|
|
} |
104
|
1 |
|
return $data; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $location |
109
|
|
|
* @param int $offset |
110
|
|
|
* @throws UploadException |
111
|
|
|
*/ |
112
|
1 |
|
public function truncate(string $location, int $offset): void |
113
|
|
|
{ |
114
|
1 |
|
$pointer = @fopen($location, 'rb+'); |
115
|
1 |
|
if (false !== $pointer) { |
116
|
1 |
|
$stat = @rewind($pointer); |
117
|
1 |
|
if (false === $stat) { |
118
|
|
|
// @codeCoverageIgnoreStart |
119
|
|
|
/** @scrutinizer ignore-unhandled */@fclose($pointer); |
120
|
|
|
throw new UploadException($this->getUppLang()->uppCannotTruncateFile($location)); |
121
|
|
|
} |
122
|
|
|
// @codeCoverageIgnoreEnd |
123
|
1 |
|
if (!ftruncate($pointer, $offset)) { // @phpstan-ignore-line |
124
|
|
|
// @codeCoverageIgnoreStart |
125
|
|
|
/** @scrutinizer ignore-unhandled */@fclose($pointer); |
126
|
|
|
throw new UploadException($this->getUppLang()->uppCannotTruncateFile($location)); |
127
|
|
|
} |
128
|
|
|
// @codeCoverageIgnoreEnd |
129
|
1 |
|
/** @scrutinizer ignore-unhandled */@rewind($pointer); |
130
|
1 |
|
/** @scrutinizer ignore-unhandled */@fclose($pointer); |
131
|
|
|
} |
132
|
1 |
|
} |
133
|
|
|
|
134
|
2 |
|
public function remove(string $location): void |
135
|
|
|
{ |
136
|
2 |
|
if (!@unlink($location)) { |
137
|
1 |
|
throw new UploadException($this->getUppLang()->uppCannotRemoveData($location)); |
138
|
|
|
} |
139
|
2 |
|
} |
140
|
|
|
} |
141
|
|
|
|