From 34d8014bacaf4bde94fc2330037e1d8b6c3f1f2c Mon Sep 17 00:00:00 2001 From: Matt Jenkins Date: Thu, 4 Jun 2026 23:23:31 +0100 Subject: [PATCH] Added progress --- app/jobs/ArchiveJob.php | 14 +++++++++++++- lib/Archive.php | 5 ++++- lib/HTTPRequest.php | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/app/jobs/ArchiveJob.php b/app/jobs/ArchiveJob.php index b0416d6..b890631 100644 --- a/app/jobs/ArchiveJob.php +++ b/app/jobs/ArchiveJob.php @@ -4,6 +4,9 @@ class ArchiveJob extends Job { public $document; + private $currentFile; + private static $me; + public function run() { $doc = new Document($this->document); @@ -32,7 +35,9 @@ class ArchiveJob extends Job { $this->status("Uploading " . $r->filename()); $r->archived = "P"; $r->save(); - Archive::upload($r->getPDF(), $id, $r->filename(), $meta); + $this->currentFile = $r->filename(); + ArchiveJob::$me = $this; + Archive::upload($r->getPDF(), $id, $r->filename(), $meta, [$this, "progress"]); $r->refresh(); $r->archived = "Y"; $r->save(); @@ -62,4 +67,11 @@ class ArchiveJob extends Job { $this->finish(); } + public function progress($ch, $todl, $dl, $toul, $ul) { + if ($toul > 0) { + $txt = sprintf("Uploading %s: %d%%", $this->currentFile, $ul * 100 / $toul); + $this->status(sprintf("Uploading %s: %d%%", $this->currentFile, $ul * 100 / $toul)); + } + } + } diff --git a/lib/Archive.php b/lib/Archive.php index 1d345d3..9a863b5 100644 --- a/lib/Archive.php +++ b/lib/Archive.php @@ -29,7 +29,7 @@ class Archive { throw new Exception("Error $resp processing tasks"); } - static function upload(File $file, $identifier, $filename, $metadata = []) { + static function upload(File $file, $identifier, $filename, $metadata = [], ?Callable $callback = null) { $headers = []; $headers[] = "Authorization: LOW " . Config::get("ARCHIVE_KEY") . ":" . Config::get("ARCHIVE_SECRET"); @@ -58,6 +58,9 @@ class Archive { $req = new HTTPRequest(); + if ($callback) { + $req->setCallback($callback); + } $resp = $req->put("https://s3.us.archive.org/" . $identifier . "/" . $filename, $file->path(), $headers); if ($resp == 200) { diff --git a/lib/HTTPRequest.php b/lib/HTTPRequest.php index b1164ca..1ff7ac9 100644 --- a/lib/HTTPRequest.php +++ b/lib/HTTPRequest.php @@ -10,6 +10,8 @@ class HTTPRequest { public $connect_timeout = 2; public $transfer_timeout = 300; + public $callback = null; + public $ch = null; public function __construct() { @@ -29,6 +31,11 @@ class HTTPRequest { curl_setopt($this->ch, CURLOPT_HEADER, true); curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true); + if ($this->callback) { + curl_setopt($this->ch, CURLOPT_XFERINFOFUNCTION, $this->callback); + curl_setopt($this->ch, CURLOPT_NOPROGRESS, false); + } + $this->status = 0; $response = curl_exec($this->ch); @@ -78,6 +85,11 @@ class HTTPRequest { curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data); + if ($this->callback) { + curl_setopt($this->ch, CURLOPT_XFERINFOFUNCTION, $this->callback); + curl_setopt($this->ch, CURLOPT_NOPROGRESS, false); + } + $this->status = 0; $response = curl_exec($this->ch); @@ -130,6 +142,11 @@ class HTTPRequest { curl_setopt($this->ch, CURLOPT_INFILE, $fh); curl_setopt($this->ch, CURLOPT_INFILESIZE, filesize($file)); + if ($this->callback) { + curl_setopt($this->ch, CURLOPT_XFERINFOFUNCTION, $this->callback); + curl_setopt($this->ch, CURLOPT_NOPROGRESS, false); + } + $this->status = 0; $response = curl_exec($this->ch); @@ -161,4 +178,8 @@ class HTTPRequest { return $this->status; } + + public function setCallback(Callable $callback) { + $this->callback = $callback; + } }