.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 | } |