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