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 / Download / Download.php
backup / src / JetBackup / Download Last commit date
.htaccess 1 year ago Download.php 10 months ago index.html 1 year ago web.config 1 year ago
Download.php
191 lines
1 <?php
2
3 namespace JetBackup\Download;
4
5 use Exception;
6 use JetBackup\Alert\Alert;
7 use JetBackup\CLI\CLI;
8 use JetBackup\Data\DBObject;
9 use JetBackup\Data\SleekStore;
10 use JetBackup\Downloader\Downloader;
11 use JetBackup\Entities\Util;
12 use JetBackup\Exception\DBException;
13 use JetBackup\Exception\DownloaderException;
14 use JetBackup\Factory;
15 use JetBackup\JetBackup;
16 use SleekDB\Exceptions\InvalidArgumentException;
17 use SleekDB\Exceptions\IOException;
18 use SleekDB\QueryBuilder;
19
20 if (!defined( '__JETBACKUP__')) die('Direct access is not allowed');
21
22 class Download extends DBObject {
23
24 const COLLECTION = 'download';
25
26 const CREATED = 'created';
27 const FILENAME = 'filename';
28 const SIZE = 'size';
29 const LOCATION = 'location';
30
31 /**
32 * @param int|null $_id
33 *
34 * @throws IOException
35 * @throws InvalidArgumentException
36 * @throws DBException
37 */
38 public function __construct( ?int $_id=null) {
39 parent::__construct(self::COLLECTION);
40 if($_id) $this->_loadById($_id);
41 }
42
43 /**
44 * @param int $value
45 *
46 * @return void
47 */
48 public function setCreated(int $value):void { $this->set(self::CREATED, $value); }
49
50 /**
51 * @return int
52 */
53 public function getCreated():int { return $this->get(self::CREATED, 0); }
54
55 /**
56 * @param string $value
57 *
58 * @return void
59 */
60 public function setFilename(string $value):void { $this->set(self::FILENAME, $value); }
61
62 /**
63 * @return string
64 */
65 public function getFilename():string { return $this->get(self::FILENAME); }
66
67 /**
68 * @param int $size
69 *
70 * @return void
71 */
72 public function setSize(int $size):void { $this->set(self::SIZE, $size); }
73
74 /**
75 * @return int
76 */
77 public function getSize():int { return $this->get(self::SIZE, 0); }
78
79 /**
80 * @param string $value
81 *
82 * @return void
83 */
84 public function setLocation(string $value):void { $this->set(self::LOCATION, $value); }
85
86 /**
87 * @return string
88 */
89 public function getLocation():string { return $this->get(self::LOCATION); }
90
91 /**
92 * @return SleekStore
93 */
94 public static function db():SleekStore {
95 return new SleekStore(self::COLLECTION);
96 }
97
98 /**
99 * @return QueryBuilder
100 */
101 public static function query():QueryBuilder {
102 return self::db()->createQueryBuilder();
103 }
104
105 /**
106 * @param int $ttl
107 *
108 * @return void
109 * @throws DBException
110 * @throws IOException
111 * @throws InvalidArgumentException
112 */
113 public static function deleteByTTL(int $ttl) : void {
114 if (!$ttl) return;
115
116 $items = self::query()
117 ->where([self::CREATED, '<', time() - $ttl])
118 ->getQuery()
119 ->fetch();
120 if (empty($items)) return;
121 foreach ($items as $item) {
122 $download = new Download($item[JetBackup::ID_FIELD]);
123 $file = Factory::getLocations()->getDownloadsDir() . JetBackup::SEP . $download->getLocation();
124 if (file_exists($file)) @unlink($file);
125 Alert::add('System Cleanup', "Download item ". $download->getFilename() . " removed", Alert::LEVEL_INFORMATION);
126 $download->delete();
127 }
128 }
129
130
131 /**
132 * @return void
133 * @throws DownloaderException
134 */
135 public function download():void {
136 if (!$this->getFilename()) throw new DownloaderException('Download file not found');
137 $downloader = new Downloader(Factory::getLocations()->getDownloadsDir() . JetBackup::SEP . $this->getLocation(), $this->getFilename());
138 $downloader->download();
139 }
140
141 /**
142 * @param string $source
143 * @param string|null $prefix
144 *
145 * @return Download
146 * @throws IOException
147 * @throws InvalidArgumentException
148 */
149 public static function create(string $source, ?string $prefix = null):Download {
150
151 $size = filesize($source);
152 $id = Util::generateUniqueId();
153 $downloads_dir = Factory::getLocations()->getDownloadsDir();
154 if(!is_dir($downloads_dir)) mkdir($downloads_dir, 0700);
155
156 rename($source, $downloads_dir . JetBackup::SEP . $id);
157
158 $download = new Download();
159 $download->setCreated(time());
160 $download->setFilename(($prefix ? $prefix . "_" : "") . basename($source));
161 $download->setLocation($id);
162 $download->setSize($size);
163 $download->save();
164
165 return $download;
166 }
167
168 /**
169 * @return array
170 * @throws Exception
171 */
172 public function getDisplay():array {
173 return [
174 JetBackup::ID_FIELD => $this->getId(),
175 self::CREATED => $this->getCreated(),
176 self::FILENAME => $this->getFilename(),
177 self::LOCATION => $this->getLocation(),
178 self::SIZE => Util::bytesToHumanReadable($this->getSize()),
179 ];
180 }
181
182 public function getDisplayCLI(): array {
183 return [
184 'ID' => $this->getId(),
185 'Created' => CLI::date($this->getCreated()),
186 'Filename' => $this->getFilename(),
187 'Location' => $this->getLocation(),
188 'Size' => $this->getSize(),
189 ];
190 }
191 }