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 / Upload / Upload.php
backup / src / JetBackup / Upload Last commit date
.htaccess 1 year ago Upload.php 3 months ago index.html 1 year ago web.config 1 year ago
Upload.php
124 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\Upload;
12
13 use JetBackup\Data\DBObject;
14 use JetBackup\Data\SleekStore;
15 use JetBackup\Entities\Util;
16 use JetBackup\Exception\IOException;
17 use JetBackup\Factory;
18 use JetBackup\JetBackup;
19 use SleekDB\QueryBuilder;
20
21 defined("__JETBACKUP__") or die("Restricted Access.");
22
23 class Upload extends DBObject {
24
25 const COLLECTION = 'upload';
26
27 const UNIQUE_ID = 'unique_id';
28 const FILENAME = 'filename';
29 const CREATED = 'created';
30 const SIZE = 'size';
31 const UPLOADED = 'uploaded';
32
33 public function __construct($_id=null) {
34 parent::__construct(self::COLLECTION);
35 if($_id) $this->_loadById((int) $_id);
36 }
37
38 public function loadByUploadId($unique_id) {
39 $this->_load([[self::UNIQUE_ID, '=', $unique_id]]);
40 }
41
42 public function setUniqueId(string $id) { $this->set(self::UNIQUE_ID, $id); }
43 public function getUniqueId():string { return $this->get(self::UNIQUE_ID); }
44
45 public function setFilename(string $name) { $this->set(self::FILENAME, $name); }
46 public function getFilename():string { return $this->get(self::FILENAME); }
47
48 public function setCreated(int $created) { $this->set(self::CREATED, $created); }
49 public function getCreated():int { return $this->get(self::CREATED, 0); }
50
51 public function setSize(int $size) { $this->set(self::SIZE, $size); }
52 public function getSize():int { return $this->get(self::SIZE, 0); }
53
54 public function setUploaded(int $uploaded) { $this->set(self::UPLOADED, $uploaded); }
55 public function getUploaded():int { return $this->get(self::UPLOADED, 0); }
56
57 /**
58 * @return string
59 * @throws IOException
60 */
61 public function getFileLocation():string {
62 if(!$this->getUniqueId()) throw new IOException("No upload id was found");
63 $directory = Factory::getLocations()->getTempDir() . JetBackup::SEP . $this->getUniqueId();
64 if(!is_dir($directory)) mkdir($directory, 0700);
65 Util::secureFolder($directory);
66
67 $safeFilename = basename($this->getFilename());
68 if($safeFilename === '' || $safeFilename === '.' || $safeFilename === '..') throw new IOException("Invalid filename provided");
69
70 return $directory . JetBackup::SEP . $safeFilename;
71 }
72
73 /**
74 * @param string $data
75 *
76 * @return void
77 * @throws IOException
78 * @throws \SleekDB\Exceptions\IOException
79 * @throws \SleekDB\Exceptions\InvalidArgumentException
80 */
81 public function writeChunk(string $file):void {
82 if($this->isCompleted()) throw new IOException("This file is already fully uploaded");
83 if(!file_exists($file)) throw new IOException("The provided chunk file does not exist");
84
85 $length = filesize($file);
86 $uploaded = $this->getUploaded();
87
88 if($uploaded + $length > $this->getSize()) throw new IOException("File data provided is exceeded the provided file size");
89
90 $target = $this->getFileLocation();
91
92 $fd = fopen($target, 'a');
93 if(!$fd) throw new IOException("Failed to open file '$target'");
94 fseek($fd, $uploaded);
95
96 $file_fd = fopen($file, 'rb');
97 if(!$file_fd) throw new IOException("Failed to open chunk file '$file'");
98
99 while(!feof($file_fd)) fwrite($fd, fread($file_fd, 1024));
100
101 fclose($fd);
102 fclose($file_fd);
103
104 $this->setUploaded($uploaded + $length);
105 $this->save();
106 }
107
108 public function isCompleted():bool {
109 return $this->getUploaded() >= $this->getSize();
110 }
111
112 public static function db():SleekStore {
113 return new SleekStore(self::COLLECTION);
114 }
115
116 public static function query():QueryBuilder {
117 return self::db()->createQueryBuilder();
118 }
119
120 public function save():void {
121 if(!$this->getUniqueId()) $this->setUniqueId(Util::generateUniqueId());
122 parent::save();
123 }
124 }