.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
FileStream.php
132 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 | defined( '__JETBACKUP__' ) or die( 'Restricted access' ); |
| 14 | |
| 15 | class FileStream { |
| 16 | |
| 17 | const CHUNK_SIZE_PIECE = 1048576; // 1MB |
| 18 | |
| 19 | private string $_file; |
| 20 | private $_fd; |
| 21 | private int $_size; |
| 22 | |
| 23 | /** |
| 24 | * @param string $file |
| 25 | * @param string $mode |
| 26 | * |
| 27 | * @throws FileException |
| 28 | */ |
| 29 | public function __construct(string $file, string $mode="r") { |
| 30 | if(!file_exists($file)) throw new FileException("Provided file not exists"); |
| 31 | $this->_file = $file; |
| 32 | $this->_fd = fopen($this->_file, $mode); |
| 33 | if($this->getDescriptor() === false) throw new FileException("Failed to open file stream"); |
| 34 | $this->_size = filesize($this->_file); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @return string |
| 39 | */ |
| 40 | public function getFile():string { |
| 41 | return $this->_file; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @return int |
| 46 | */ |
| 47 | public function getSize():int { |
| 48 | return $this->_size; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @return false|string |
| 53 | */ |
| 54 | public function getMimeType() { |
| 55 | if(function_exists('mime_content_type')) return mime_content_type($this->_file); |
| 56 | else { |
| 57 | $file = new \CURLFile($this->_file); |
| 58 | return $file->getMimeType() ?: 'text/plain'; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @return false|resource |
| 64 | */ |
| 65 | public function getDescriptor() { |
| 66 | return $this->_fd; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @return false|int |
| 71 | */ |
| 72 | public function tell() { |
| 73 | return ftell($this->getDescriptor()); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @param int|null $length |
| 78 | * |
| 79 | * @return string |
| 80 | */ |
| 81 | public function read(?int $length=null):string { |
| 82 | if($length === null) $length = $this->getSize(); |
| 83 | return fread($this->getDescriptor(), $length); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @return bool |
| 88 | */ |
| 89 | public function eof():bool { |
| 90 | return feof($this->getDescriptor()); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @param int $offset |
| 95 | * @param int $whence |
| 96 | * |
| 97 | * @return int |
| 98 | */ |
| 99 | public function seek(int $offset, int $whence=SEEK_SET):int { |
| 100 | return fseek($this->getDescriptor(), $offset, $whence); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @return void |
| 105 | */ |
| 106 | public function rewind():void { |
| 107 | $this->seek(0); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @return void |
| 112 | */ |
| 113 | public function close():void { |
| 114 | if (is_resource($this->_fd)) fclose($this->_fd); |
| 115 | $this->_fd = null; |
| 116 | } |
| 117 | |
| 118 | public function getHash(string $algorithm, $binary=false):string { |
| 119 | $this->rewind(); |
| 120 | $ctx = hash_init($algorithm); |
| 121 | while (!$this->eof()) hash_update($ctx, $this->read(self::CHUNK_SIZE_PIECE)); |
| 122 | $this->rewind(); |
| 123 | return hash_final($ctx, $binary); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * |
| 128 | */ |
| 129 | public function __destruct() { |
| 130 | $this->close(); |
| 131 | } |
| 132 | } |