from = $from; parent::__construct("download"); } public function generate_to() { $filename = ROOT . "/download/file-" . time() . "-" . rand() . ".pdf"; return $filename; } public function run() { $this->to = $this->generate_to(); $ch = curl_init(); $this->status("Downloading: 0%"); $fd = fopen($this->to, "w"); curl_setopt($ch, CURLOPT_URL, $this->from); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); curl_setopt($ch, CURLOPT_PRIVATE, $this); curl_setopt($ch, CURLOPT_TIMEOUT, 3600); curl_setopt($ch, CURLOPT_FILETIME, true); curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, [$this, 'download_progress']); curl_setopt($ch, CURLOPT_NOPROGRESS, false); curl_setopt($ch, CURLOPT_FILE, $fd); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); print("Running...\n"); $r = curl_exec($ch); fclose($fd); $file = new File($this->to); $sha256 = $file->hash(); $rev = Revision::find([["sha256", "=", $sha256]])->first(); if ($rev) { $this->status("Duplicate file"); $this->fail(); print("Finished (duplicate)\n"); return; } $u = new Upload(); $u->filename = basename($this->from); $u->sha256 = $sha256; $u->source = $this->from; $u->ocr = "N"; $u->ai = "N"; $u->owner = $this->getOwner(); $u->save(); $file->rename(ROOT . "/uploads/" . $u->id . ".pdf"); $j = new ImportGeminiJob($u->id); $j->queue($u->owner); $j = new ImportOCRJob($u->id); $j->queue($u->owner); $this->status("Finished"); print("Finished\n"); $this->finish(); } function download_progress($ch, $download_size, $downloaded, $upload_size, $uploaded) { if ($download_size == 0) return; $pct = round($downloaded / $download_size * 100); if ($pct != $this->_pct) { $this->status("Downloading: " . $pct . "%"); $this->_pct = $pct; } } }