|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_files\Traits; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_files\FilesException; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Trait TStreamToPos |
|
11
|
|
|
* @package kalanis\kw_menu\Traits |
|
12
|
|
|
* Write stream at expected position |
|
13
|
|
|
*/ |
|
14
|
|
|
trait TStreamToPos |
|
15
|
|
|
{ |
|
16
|
|
|
use TLang; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param resource $original stream with original content, usually file handler |
|
20
|
|
|
* @param resource $added stream with what will be added, also file handler |
|
21
|
|
|
* @param int|null $offset where it will be added |
|
22
|
|
|
* @throws FilesException |
|
23
|
|
|
* @return resource |
|
24
|
|
|
*/ |
|
25
|
12 |
|
public function addStreamToPosition($original, $added, ?int $offset = null) |
|
26
|
|
|
{ |
|
27
|
12 |
|
if (!is_null($offset)) { |
|
28
|
|
|
// put it somewhere, left the rest intact |
|
29
|
10 |
|
$originalLength = fstat($original)['size']; |
|
30
|
10 |
|
$addedLength = fstat($added)['size']; |
|
31
|
|
|
// original data in the beginning |
|
32
|
10 |
|
$destination = fopen('php://temp', 'rb+'); // the target storage |
|
33
|
10 |
|
if ((0 !== $offset) && (false === stream_copy_to_stream($original, $destination, $offset, 0))) { |
|
34
|
|
|
// @codeCoverageIgnoreStart |
|
35
|
|
|
throw new FilesException($this->getLang()->flCannotWriteFile('temp')); |
|
36
|
|
|
} |
|
37
|
|
|
// @codeCoverageIgnoreEnd |
|
38
|
|
|
|
|
39
|
|
|
// filler - when the size is too much |
|
40
|
10 |
|
if ($originalLength < $offset) { |
|
41
|
6 |
|
$begin = fopen('php://temp', 'rb+'); |
|
42
|
6 |
|
/** @scrutinizer ignore-unhandled */@fwrite($begin, str_repeat("\0", intval(abs($offset - $originalLength)))); |
|
43
|
6 |
|
/** @scrutinizer ignore-unhandled */@rewind($begin); |
|
44
|
6 |
|
if (false === stream_copy_to_stream($begin, $destination, -1, 0)) { |
|
45
|
|
|
// @codeCoverageIgnoreStart |
|
46
|
|
|
throw new FilesException($this->getLang()->flCannotWriteFile('temp')); |
|
47
|
|
|
} |
|
48
|
|
|
// @codeCoverageIgnoreEnd |
|
49
|
6 |
|
$originalLength += fstat($begin)['size']; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// amended data itself |
|
53
|
10 |
|
if (false === stream_copy_to_stream($added, $destination, -1, 0)) { |
|
54
|
|
|
// @codeCoverageIgnoreStart |
|
55
|
|
|
throw new FilesException($this->getLang()->flCannotWriteFile('temp')); |
|
56
|
|
|
} |
|
57
|
|
|
// @codeCoverageIgnoreEnd |
|
58
|
|
|
|
|
59
|
|
|
// the rest from the original |
|
60
|
10 |
|
if ($originalLength > $offset + $addedLength) { |
|
61
|
4 |
|
$rest = fopen('php://temp', 'rb+'); // the temporary storage for the end |
|
62
|
4 |
|
if (false === stream_copy_to_stream($original, $rest, -1, $offset + $addedLength)) { |
|
63
|
|
|
// @codeCoverageIgnoreStart |
|
64
|
|
|
throw new FilesException($this->getLang()->flCannotWriteFile('temp')); |
|
65
|
|
|
} |
|
66
|
4 |
|
/** @scrutinizer ignore-unhandled */@rewind($rest); |
|
67
|
|
|
// @codeCoverageIgnoreEnd |
|
68
|
4 |
|
if (false === stream_copy_to_stream($rest, $destination, -1, 0)) { |
|
69
|
|
|
// @codeCoverageIgnoreStart |
|
70
|
|
|
throw new FilesException($this->getLang()->flCannotWriteFile('temp')); |
|
71
|
|
|
} |
|
72
|
|
|
// @codeCoverageIgnoreEnd |
|
73
|
|
|
} |
|
74
|
10 |
|
return $destination; |
|
75
|
|
|
} else { |
|
76
|
2 |
|
return $added; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|