PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / trunk
JetBackup – Backup, Restore & Migrate vtrunk
3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / src / JetBackup / Web / File / FileChunk.php
backup / src / JetBackup / Web / File Last commit date
.htaccess 1 year ago FileChunk.php 1 year ago FileChunkIterator.php 1 year ago FileDownload.php 1 year ago FileException.php 1 year ago FileStream.php 1 year ago index.html 1 year ago web.config 1 year ago
FileChunk.php
130 lines
1 <?php
2 /*
3 *
4 * JetBackup @ package
5 * Created By Idan Ben-Ezra
6 *
7 * Copyrights @ JetApps
8 * https://www.jetapps.com
9 *
10 **/
11 namespace JetBackup\Web\File;
12
13 use JetBackup\Exception\IOException;
14
15 defined( '__JETBACKUP__' ) or die( 'Restricted access' );
16
17 class FileChunk {
18
19 const CHUNK_SIZE = 5242880; // 5MB
20 const CHUNK_SIZE_PIECE = 1048576; // 1MB
21
22 private FileStream $_file;
23 private int $_chunk_start;
24 private int $_chunk_size;
25 private int $_readed;
26
27 /**
28 * Chunk constructor.
29 *
30 * @param FileStream $file
31 * @param int $chunk_size
32 *
33 * @throws IOException
34 */
35 public function __construct(FileStream $file, int $chunk_size=self::CHUNK_SIZE) {
36 $this->_file = $file;
37 $this->_chunk_start = $this->getFile()->tell();
38 $this->_readed = 0;
39
40 $size = ($this->getFile()->getSize() - $this->_chunk_start);
41 if($size < 0) throw new IOException("Chunk size length is too small ($size bytes)");
42 $this->_chunk_size = min($size, $chunk_size);
43 }
44
45 /**
46 * @return void
47 */
48 public function rewind():void {
49 $this->_readed = 0;
50 $this->getFile()->seek($this->_chunk_start);
51 }
52
53 /**
54 * @return int
55 */
56 public function getSize():int {
57 return $this->_chunk_size;
58 }
59
60 /**
61 * @return FileStream
62 */
63 public function getFile():FileStream {
64 return $this->_file;
65 }
66
67 /**
68 * @param int|false $length
69 *
70 * @return false|string
71 */
72 public function readPiece($length=false) {
73 if(!$length || $length > self::CHUNK_SIZE_PIECE) $length = self::CHUNK_SIZE_PIECE;
74 return $this->read($length);
75 }
76
77 /**
78 * @param int $length
79 *
80 * @return int
81 */
82 public function length(int $length):int {
83 if($this->_readed + $length > $this->_chunk_size) $length = $this->_chunk_size - $this->_readed;
84 return $length;
85 }
86
87 /**
88 * @param int $length
89 *
90 * @return false|string
91 */
92 public function read(int $length) {
93 $length = $this->length($length);
94 if($length <= 0) return false;
95 $this->_readed += $length;
96 return $this->getFile()->read($length);
97 }
98
99 /**
100 * @return bool
101 */
102 public function eof():bool {
103 return $this->getFile()->eof();
104 }
105
106 /**
107 * @param string $algorithm
108 *
109 * @return string
110 */
111 public function getHash(string $algorithm, $binary=false):string {
112 $this->rewind();
113 $ctx = hash_init($algorithm);
114 while (($data = $this->read(self::CHUNK_SIZE_PIECE)) !== false) hash_update($ctx, $data);
115 $this->rewind();
116 return hash_final($ctx, $binary);
117 }
118
119 /**
120 * @return string
121 */
122 public function __toString(): string {
123 return "Chunk -> " . json_encode([
124 'file' => $this->_file->getFile(),
125 'chunk_size' => $this->_chunk_size,
126 'chunk_start' => $this->_chunk_start,
127 'read' => $this->_readed,
128 ]);
129 }
130 }