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\IProcessFiles; |
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\IPassDirs; |
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 IProcessFiles |
22
|
|
|
{ |
23
|
|
|
use TPathTransform; |
24
|
|
|
use TLang; |
25
|
|
|
|
26
|
|
|
/** @var IStorage|IPassDirs */ |
27
|
|
|
protected $storage = null; |
28
|
|
|
|
29
|
13 |
|
public function saveFile(array $targetName, $content): bool |
30
|
|
|
{ |
31
|
13 |
|
$path = $this->getStorageSeparator() . $this->filledName($this->compactName($targetName, $this->getStorageSeparator())); |
32
|
|
|
try { |
33
|
13 |
|
$dstArr = new ArrayPath(); |
34
|
13 |
|
$dstArr->setArray($targetName); |
35
|
13 |
|
$tgt = $this->compactName($dstArr->getArrayDirectory(), $this->getStorageSeparator()); |
36
|
13 |
|
if (!empty($tgt) && !$this->storage->exists($this->getStorageSeparator() . $tgt)) { |
37
|
1 |
|
throw new FilesException($this->getLang()->flCannotSaveFile($path)); |
38
|
|
|
} |
39
|
|
|
|
40
|
12 |
|
return $this->storage->write($path, $content); |
41
|
4 |
|
} catch (StorageException $ex) { |
42
|
3 |
|
throw new FilesException($this->getLang()->flCannotSaveFile($path), $ex->getCode(), $ex); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
8 |
|
public function readFile(array $entry, ?int $offset = null, ?int $length = null) |
47
|
|
|
{ |
48
|
8 |
|
$path = $this->getStorageSeparator() . $this->filledName($this->compactName($entry, $this->getStorageSeparator())); |
49
|
|
|
try { |
50
|
8 |
|
$content = $this->storage->read($path); |
51
|
4 |
|
if (false === $content) { |
52
|
1 |
|
throw new FilesException($this->getLang()->flCannotLoadFile($path)); |
53
|
3 |
|
} elseif (is_resource($content)) { |
54
|
1 |
|
if (!is_null($length) || !is_null($offset)) { |
55
|
1 |
|
$stream = fopen('php://temp', 'rb+'); |
56
|
1 |
|
rewind($content); |
57
|
1 |
|
if (false === $stream) { |
58
|
|
|
// @codeCoverageIgnoreStart |
59
|
|
|
throw new FilesException($this->getLang()->flCannotLoadFile($path)); |
60
|
|
|
} |
61
|
|
|
// @codeCoverageIgnoreEnd |
62
|
1 |
|
if (false === stream_copy_to_stream($content, $stream, (is_null($length) ? -1 : $length), intval($offset))) { |
63
|
|
|
// @codeCoverageIgnoreStart |
64
|
|
|
throw new FilesException($this->getLang()->flCannotGetFilePart($path)); |
65
|
|
|
} |
66
|
|
|
// @codeCoverageIgnoreEnd |
67
|
1 |
|
return $stream; |
68
|
|
|
} else { |
69
|
1 |
|
return $content; |
70
|
|
|
} |
71
|
|
|
} else { |
72
|
|
|
// shit with substr... that needed undefined params was from some java dude?! |
73
|
2 |
|
if (!is_null($length)) { |
74
|
2 |
|
return mb_substr(strval($content), intval($offset), $length); |
75
|
|
|
} |
76
|
2 |
|
if (!is_null($offset)) { |
77
|
2 |
|
return mb_substr(strval($content), $offset); |
78
|
|
|
} |
79
|
2 |
|
return strval($content); |
80
|
|
|
} |
81
|
5 |
|
} catch (StorageException $ex) { |
82
|
4 |
|
throw new FilesException($this->getLang()->flCannotLoadFile($path), $ex->getCode(), $ex); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
6 |
|
public function deleteFile(array $entry): bool |
87
|
|
|
{ |
88
|
6 |
|
$path = $this->getStorageSeparator() . $this->filledName($this->compactName($entry, $this->getStorageSeparator())); |
89
|
|
|
try { |
90
|
6 |
|
return $this->storage->remove($path); |
91
|
2 |
|
} catch (StorageException $ex) { |
92
|
2 |
|
throw new FilesException($this->getLang()->flCannotRemoveFile($path), $ex->getCode(), $ex); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
33 |
|
protected function getStorageSeparator(): string |
97
|
|
|
{ |
98
|
33 |
|
return DIRECTORY_SEPARATOR; |
99
|
|
|
} |
100
|
|
|
|
101
|
31 |
|
protected function filledName(string $path): string |
102
|
|
|
{ |
103
|
31 |
|
$sepLen = mb_strlen($this->getStorageSeparator()); |
104
|
31 |
|
$beginning = mb_substr($path, 0, $sepLen); |
105
|
31 |
|
return ($this->getStorageSeparator() == $beginning) ? mb_substr($path, $sepLen) : $path; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|