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