1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_files\Processing\Storage\Files; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_files\FilesException; |
7
|
|
|
use kalanis\kw_files\Interfaces; |
8
|
|
|
use kalanis\kw_files\Traits\TCheckModes; |
9
|
|
|
use kalanis\kw_files\Traits\TLang; |
10
|
|
|
use kalanis\kw_paths\ArrayPath; |
11
|
|
|
use kalanis\kw_paths\Extras\TPathTransform; |
12
|
|
|
use kalanis\kw_storage\Interfaces\IStorage; |
13
|
|
|
use kalanis\kw_storage\StorageException; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class AFiles |
18
|
|
|
* @package kalanis\kw_files\Processing\Storage\Files |
19
|
|
|
* Process files in storages - deffer when you can access them directly or must be a middleman there |
20
|
|
|
*/ |
21
|
|
|
abstract class AFiles implements Interfaces\IProcessFiles |
22
|
|
|
{ |
23
|
|
|
use TCheckModes; |
24
|
|
|
use TLang; |
25
|
|
|
use TPathTransform; |
26
|
|
|
|
27
|
|
|
protected IStorage $storage; |
28
|
|
|
|
29
|
38 |
|
public function __construct(IStorage $storage, ?Interfaces\IFLTranslations $lang = null) |
30
|
|
|
{ |
31
|
38 |
|
$this->storage = $storage; |
32
|
38 |
|
$this->setFlLang($lang); |
33
|
|
|
} |
34
|
|
|
|
35
|
17 |
|
public function saveFile(array $targetName, string $content, ?int $offset = null, int $mode = 0): bool |
36
|
|
|
{ |
37
|
17 |
|
$this->checkSupportedModes($mode); |
38
|
17 |
|
$path = $this->getStorageSeparator() . $this->filledName($this->compactName($targetName, $this->getStorageSeparator())); |
39
|
|
|
try { |
40
|
17 |
|
$dstArr = new ArrayPath(); |
41
|
17 |
|
$dstArr->setArray($targetName); |
42
|
17 |
|
$tgt = $this->compactName($dstArr->getArrayDirectory(), $this->getStorageSeparator()); |
43
|
17 |
|
if (!empty($tgt) && !$this->storage->exists($this->getStorageSeparator() . $tgt)) { |
44
|
|
|
// parent dir |
45
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotSaveFile($path)); |
46
|
|
|
} |
47
|
|
|
|
48
|
16 |
|
$target = ''; |
49
|
16 |
|
if (FILE_APPEND == $mode) { |
50
|
2 |
|
if ($this->storage->exists($path)) { |
51
|
2 |
|
$target = $this->storage->read($path); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
16 |
|
if (!is_null($offset)) { |
56
|
|
|
// put it somewhere, left the rest intact |
57
|
4 |
|
$target = str_pad(substr($target, 0, $offset), $offset, "\0"); |
58
|
|
|
} |
59
|
16 |
|
return $this->storage->write($path, $target . $content); |
60
|
4 |
|
} catch (StorageException $ex) { |
61
|
3 |
|
throw new FilesException($this->getFlLang()->flCannotSaveFile($path), $ex->getCode(), $ex); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
12 |
|
public function readFile(array $entry, ?int $offset = null, ?int $length = null): string |
66
|
|
|
{ |
67
|
12 |
|
$path = $this->getStorageSeparator() . $this->filledName($this->compactName($entry, $this->getStorageSeparator())); |
68
|
|
|
try { |
69
|
12 |
|
$content = $this->storage->read($path); |
70
|
|
|
// shit with substr... that needed undefined params was from some java dude?! |
71
|
8 |
|
if (!is_null($length)) { |
72
|
2 |
|
return mb_substr($content, intval($offset), $length); |
73
|
|
|
} |
74
|
8 |
|
if (!is_null($offset)) { |
75
|
2 |
|
return mb_substr($content, $offset); |
76
|
|
|
} |
77
|
8 |
|
return $content; |
78
|
4 |
|
} catch (StorageException $ex) { |
79
|
4 |
|
throw new FilesException($this->getFlLang()->flCannotLoadFile($path), $ex->getCode(), $ex); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
6 |
|
public function deleteFile(array $entry): bool |
84
|
|
|
{ |
85
|
6 |
|
$path = $this->getStorageSeparator() . $this->filledName($this->compactName($entry, $this->getStorageSeparator())); |
86
|
|
|
try { |
87
|
6 |
|
return $this->storage->remove($path); |
88
|
2 |
|
} catch (StorageException $ex) { |
89
|
2 |
|
throw new FilesException($this->getFlLang()->flCannotRemoveFile($path), $ex->getCode(), $ex); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
35 |
|
protected function getStorageSeparator(): string |
94
|
|
|
{ |
95
|
35 |
|
return DIRECTORY_SEPARATOR; |
96
|
|
|
} |
97
|
|
|
|
98
|
33 |
|
protected function filledName(string $path): string |
99
|
|
|
{ |
100
|
33 |
|
$sepLen = mb_strlen($this->getStorageSeparator()); |
101
|
33 |
|
$beginning = mb_substr($path, 0, $sepLen); |
102
|
33 |
|
return ($this->getStorageSeparator() == $beginning) ? mb_substr($path, $sepLen) : $path; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|