1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\UploadPerPartes\DataStorage; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_files\Access\CompositeAdapter; |
7
|
|
|
use kalanis\kw_files\FilesException; |
8
|
|
|
use kalanis\kw_files\Traits\TToString; |
9
|
|
|
use kalanis\kw_paths\PathsException; |
10
|
|
|
use kalanis\UploadPerPartes\Exceptions\UploadException; |
11
|
|
|
use kalanis\UploadPerPartes\Interfaces\IUPPTranslations; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Files |
16
|
|
|
* @package kalanis\UploadPerPartes\DataStorage |
17
|
|
|
* Processing info file on some file storage |
18
|
|
|
*/ |
19
|
|
|
class Files extends AStorage |
20
|
|
|
{ |
21
|
|
|
use TToString; |
22
|
|
|
|
23
|
|
|
/** @var CompositeAdapter */ |
24
|
|
|
protected $lib = null; |
25
|
|
|
|
26
|
8 |
|
public function __construct(CompositeAdapter $lib, ?IUPPTranslations $lang = null) |
27
|
|
|
{ |
28
|
8 |
|
$this->lib = $lib; |
29
|
8 |
|
parent::__construct($lang); |
30
|
8 |
|
} |
31
|
|
|
|
32
|
2 |
|
public function exists(string $location): bool |
33
|
|
|
{ |
34
|
|
|
try { |
35
|
2 |
|
return $this->lib->exists([$location]); |
36
|
1 |
|
} catch (FilesException | PathsException $ex) { |
37
|
1 |
|
throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
3 |
|
public function addPart(string $location, string $content, ?int $seek = null): void |
42
|
|
|
{ |
43
|
|
|
try { |
44
|
3 |
|
if (is_null($seek)) { |
45
|
|
|
// null here is to amend on the file end, on files it means total rewrite |
46
|
2 |
|
$seek = $this->lib->size([$location]); |
47
|
1 |
|
if (is_null($seek)) { |
48
|
|
|
// hot way - no seek |
49
|
1 |
|
$seek = strlen($this->toString($location, $this->lib->readFile([$location]))); |
50
|
|
|
} |
51
|
|
|
} |
52
|
2 |
|
$this->lib->saveFile([$location], $content, $seek); |
53
|
2 |
|
} catch (FilesException | PathsException $ex) { |
54
|
2 |
|
throw new UploadException($ex->getMessage(), $ex->getCode(), $ex); |
55
|
|
|
} |
56
|
1 |
|
} |
57
|
|
|
|
58
|
2 |
|
public function getPart(string $location, int $offset, ?int $limit = null): string |
59
|
|
|
{ |
60
|
|
|
try { |
61
|
2 |
|
return $this->toString($location, $this->lib->readFile([$location], $offset, $limit)); |
62
|
1 |
|
} catch (FilesException | PathsException $ex) { |
63
|
1 |
|
throw new UploadException($this->getUppLang()->uppCannotReadFile($location), $ex->getCode(), $ex); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
public function truncate(string $location, int $offset): void |
68
|
|
|
{ |
69
|
|
|
try { |
70
|
2 |
|
$this->lib->saveFile([$location], $this->lib->readFile([$location], 0, $offset)); |
71
|
1 |
|
} catch (FilesException | PathsException $ex) { |
72
|
1 |
|
throw new UploadException($this->getUppLang()->uppCannotTruncateFile($location), $ex->getCode(), $ex); |
73
|
|
|
} |
74
|
1 |
|
} |
75
|
|
|
|
76
|
3 |
|
public function remove(string $location): void |
77
|
|
|
{ |
78
|
|
|
try { |
79
|
3 |
|
$this->lib->deleteFile([$location]); |
80
|
1 |
|
} catch (FilesException | PathsException $ex) { |
81
|
1 |
|
throw new UploadException($this->getUppLang()->uppCannotRemoveData($location), $ex->getCode(), $ex); |
82
|
|
|
} |
83
|
2 |
|
} |
84
|
|
|
} |
85
|
|
|
|