From 44995c4e6a7d122a257f066633663567b718b889 Mon Sep 17 00:00:00 2001 From: Matt Jenkins Date: Fri, 5 Jun 2026 12:56:11 +0100 Subject: [PATCH] Submit derive task after all uploads completed --- .gitignore | 13 +++++++++++++ app/jobs/ArchiveJob.php | 23 +++++++++++++++++++++++ lib/Archive.php | 31 ++++++++++++++++++++++++++++--- lib/Job.php | 4 ++++ public/.htaccess | 10 ++++++++++ 5 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 public/.htaccess diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed3ab38 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +vendor +public/sys.css +public/app.js +pdf +uploads +cache +database +download +package-lock.json +composer.lock +attachments +node_modules +.env diff --git a/app/jobs/ArchiveJob.php b/app/jobs/ArchiveJob.php index b890631..14a3a65 100644 --- a/app/jobs/ArchiveJob.php +++ b/app/jobs/ArchiveJob.php @@ -64,6 +64,29 @@ class ArchiveJob extends Job { $doc->save(); } + + try { + $this->status("Waiting for bucket creation"); + $done = 0; + while ($done == 0) { + sleep(1); + $md = Archive::metadata($id); + if (property_exists($md, "created")) { + $done = 1; + } + } + } catch (Exception $e) { + $this->fail($e->getMessage()); + return; + } + + try { + Archive::derive($id); + } catch (Exception $e) { + $this->fail($e->getMessage()); + return; + } + $this->finish(); } diff --git a/lib/Archive.php b/lib/Archive.php index 9a863b5..27f7d6c 100644 --- a/lib/Archive.php +++ b/lib/Archive.php @@ -26,7 +26,7 @@ class Archive { } } - throw new Exception("Error $resp processing tasks"); + throw new Exception($req->body); } static function upload(File $file, $identifier, $filename, $metadata = [], ?Callable $callback = null) { @@ -56,6 +56,8 @@ class Archive { } } + $headers[] = "content-type:application/pdf"; + $req = new HTTPRequest(); if ($callback) { @@ -66,7 +68,7 @@ class Archive { if ($resp == 200) { return true; } - throw new Exception($req->body); + throw new Exception($resp . ": " . $req->body); } static function metadata($identifier) { @@ -84,7 +86,30 @@ class Archive { } } - throw new Exception("Error " . $resp . " during metadata get"); + throw new Exception($resp . ": " . $req->body); + } + + static function derive($identifier) { + $req = new HTTPRequest(); + + $data = [ + "identifier" => $identifier, + "cmd" => "derive.php", + "args" => [], + "priority" => 0 + ]; + + $resp = $req->post("https://archive.org/services/tasks.php", json_encode($data), [ + "Authorization: LOW " . Config::get("ARCHIVE_KEY") . ":" . Config::get("ARCHIVE_SECRET"), + "content-type:application/json", + ]); + + if ($resp == 200) { + return true; + } + + throw new Exception($resp . ": " . $req->body); + } } diff --git a/lib/Job.php b/lib/Job.php index de3a0bb..ca51d6a 100644 --- a/lib/Job.php +++ b/lib/Job.php @@ -72,6 +72,10 @@ abstract class Job { // Mark the job as failed miserably public function fail($message = "") { + if (is_terminal()) { + print(" !! " . $message . "\n"); + } + $db = DB::getInstance(); if ($message != "") { $db->update("job", $this->_jobid, [ diff --git a/public/.htaccess b/public/.htaccess new file mode 100644 index 0000000..6e1e8d5 --- /dev/null +++ b/public/.htaccess @@ -0,0 +1,10 @@ + RewriteEngine On + # Redirect Trailing Slashes If Not A Folder... + RewriteCond %{REQUEST_FILENAME} !-d + RewriteCond %{REQUEST_URI} (.+)/$ + RewriteRule ^ %1 [L,R=301] + + # Send Requests To Front Controller... + RewriteCond %{REQUEST_FILENAME} !-d + RewriteCond %{REQUEST_FILENAME} !-f + RewriteRule ^ index.php [L]