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