Added archive.org upload

This commit is contained in:
2026-06-04 16:30:29 +01:00
parent 563d9cc96d
commit e1ae5556a9
7 changed files with 254 additions and 16 deletions
+17
View File
@@ -0,0 +1,17 @@
<?php
class ArchiveController {
public static function routes() {
Routes::add_web("GET", "/archive/upload/{id}", ["ArchiveController", "upload"], ["Auth", "can_moderate"]);
}
public static function upload($id) {
$doc = new Document($id);
if ($doc) {
$job = new ArchiveJob("document:" . $doc->id);
$job->document = $doc->id;
$job->queue();
}
return redirect("/document/" . $doc->id);
}
}
+66
View File
@@ -0,0 +1,66 @@
<?php
class ArchiveJob extends Job {
public $document;
public function run() {
$doc = new Document($this->document);
$meta = [];
$meta['title'] = sprintf("uri(\"%s\")", rawurlencode(trim($doc->title . " " . $doc->subtitle . " " . $doc->subsubtitle)));
$meta['creator'] = "Digital Equipment Corporation";
$meta['description'] = sprintf("uri(\"%s\")", rawurlencode($doc->overview_md()));
$doc->load("revisions");
$doc->archive = "P";
$worked = 0;
$id = $doc->internal_id;
if (Config::get("SANDBOX")) {
$id = sprintf("decpdf_test_%s_%08d", $id, time());
}
$this->status($id);
$doc->load("revisions");
foreach ($doc->revisions as $r) {
if ($r->archived == "N") {
try {
$this->status("Uploading " . $r->filename());
$r->archived = "P";
$r->save();
Archive::upload($r->getPDF(), $id, $r->filename(), $meta);
$r->refresh();
$r->archived = "Y";
$r->save();
$worked++;
} catch (Exception $e) {
$r->refresh();
$r->archived = "F";
$r->save();
$this->status($e->getMessage());
}
}
}
if ($worked == 0) {
$doc->refresh();
$doc->archived = "F";
$doc->save();
$this->fail("No files uploaded");
return;
} else {
$doc->refresh();
$doc->archived = "Y";
$doc->archive_url = $id;
$doc->save();
}
$this->finish();
}
}
+87
View File
@@ -0,0 +1,87 @@
<?php
require_once(__DIR__ . "/File.php");
require_once(__DIR__ . "/HTTPRequest.php");
class Archive {
static function tasks($identifier = null) {
$req = new HTTPRequest();
if ($identifier) {
$resp = $req->get("https://archive.org/services/tasks.php?summary=1&catalog=1&identifier=" . $identifier, [
"Authorization: LOW " . Config::get("ARCHIVE_KEY") . ":" . Config::get("ARCHIVE_SECRET")
]);
} else {
$resp = $req->get("https://archive.org/services/tasks.php?summary=1&catalog=1", [
"Authorization: LOW " . Config::get("ARCHIVE_KEY") . ":" . Config::get("ARCHIVE_SECRET")
]);
}
if ($resp == 200) {
$j = json_decode($req->body);
if ($j) {
return $j;
}
}
throw new Exception("Error $resp processing tasks");
}
static function upload(File $file, $identifier, $filename, $metadata = []) {
$headers = [];
$headers[] = "Authorization: LOW " . Config::get("ARCHIVE_KEY") . ":" . Config::get("ARCHIVE_SECRET");
$headers[] = "x-archive-auto-make-bucket:1";
$headers[] = "x-archive-queue-derive:0";
$headers[] = "x-archive-interactive-priority:1";
$metadata["mediatype"] = "texts";
if (Config::get("SANDBOX")) {
$metadata["collection"] = "test_collection";
} else {
$metadata["collection"] = "opensource";
}
foreach ($metadata as $k=>$v) {
if (is_array($v)) {
$i = 1;
foreach ($v as $q) {
$headers[] = sprintf("x-archive-meta-%02d-%s:%s", $i, str_replace("_", "--", $k), $q);
$i++;
}
} else {
$headers[] = sprintf("x-archive-meta-%s:%s", str_replace("_", "--", $k), $v);
}
}
$req = new HTTPRequest();
$resp = $req->put("https://s3.us.archive.org/" . $identifier . "/" . $filename, $file->path(), $headers);
if ($resp == 200) {
return true;
}
throw new Exception($resp->body);
}
static function metadata($identifier) {
$req = new HTTPRequest();
$resp = $req->get("https://archive.org/metadata/" . $identifier, [
"Authorization: LOW " . Config::get("ARCHIVE_KEY") . ":" . Config::get("ARCHIVE_SECRET")
]);
if ($resp == 200) {
$j = json_decode($req->body);
if ($j) {
return $j;
}
}
throw new Exception("Error " . $resp . " during metadata get");
}
}
+52
View File
@@ -37,6 +37,8 @@ class HTTPRequest {
$this->body = substr($response, $header_size); $this->body = substr($response, $header_size);
$this->headers = []; $this->headers = [];
$hl = explode("\n", $header);
foreach ($hl as $h) { foreach ($hl as $h) {
$h = trim($h); $h = trim($h);
if (preg_match('/^HTTP\/([^\s]+)\s+(\d+)\s*/', $h, $m)) { if (preg_match('/^HTTP\/([^\s]+)\s+(\d+)\s*/', $h, $m)) {
@@ -109,4 +111,54 @@ class HTTPRequest {
} }
public function put($url, $file, $headers = [], $headersonly = false) {
$fh = fopen($file, "r");
curl_reset($this->ch);
curl_setopt($this->ch, CURLOPT_URL, $url);
curl_setopt($this->ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:138.0) Gecko/20100101 Firefox/138.0");
curl_setopt($this->ch, CURLOPT_PUT, true);
curl_setopt($this->ch, CURLOPT_FILETIME, true);
curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, $this->connect_timeout);
curl_setopt($this->ch, CURLOPT_TIMEOUT, $this->transfer_timeout);
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->ch, CURLOPT_NOBODY, $headersonly);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->ch, CURLOPT_HEADER, true);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->ch, CURLOPT_INFILE, $fh);
curl_setopt($this->ch, CURLOPT_INFILESIZE, filesize($file));
$this->status = 0;
$response = curl_exec($this->ch);
$header_size = curl_getinfo($this->ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$this->body = substr($response, $header_size);
$this->headers = [];
$hl = explode("\n", $header);
foreach ($hl as $h) {
$h = trim($h);
if (preg_match('/^HTTP\/([^\s]+)\s+(\d+)\s*/', $h, $m)) {
$this->version = $m[1];
$this->status = $m[2];
continue;
}
if (str_starts_with($h, " ")) {
$this->headers[$curr] .= " " . trim($h);
continue;
}
if (preg_match('/^([^:]+):\s+(.*)$/', $h, $m)) {
$curr = strtolower($m[1]);
$this->headers[$curr] = $m[2];
}
}
return $this->status;
}
} }
+6
View File
@@ -82,6 +82,12 @@ class Model implements JsonSerializable {
Model::$_cache->addServer($host, $port, $weight); Model::$_cache->addServer($host, $port, $weight);
} }
public function refresh() {
if ($this->id) {
$this->_load_record($this->id);
}
}
private function _load_record($id) { private function _load_record($id) {
$class = get_called_class(); $class = get_called_class();
+5 -6
View File
@@ -41,12 +41,6 @@ Routes::add_web("GET", "/merge/{id}", ["DocumentController", "merge"], ["Auth",
Routes::add_web("GET", "/explode/{id}", ["DocumentController", "separate"], ["Auth", "can_moderate"]); Routes::add_web("GET", "/explode/{id}", ["DocumentController", "separate"], ["Auth", "can_moderate"]);
Routes::add_web("GET", "/test", function($_request) {
print("<pre>");
print_r($_request);
exit(0);
});
Routes::add_web("GET", "/privacy", function() { return blade("privacy"); }); Routes::add_web("GET", "/privacy", function() { return blade("privacy"); });
@@ -108,3 +102,8 @@ Routes::add_web("GET", "/system/{id}/add/{doc}", ["SystemController", "system_ad
Routes::add_web("GET", "/system/{id}/del/{doc}", ["SystemController", "system_del_doc"], ["Auth", "logged_in"]); Routes::add_web("GET", "/system/{id}/del/{doc}", ["SystemController", "system_del_doc"], ["Auth", "logged_in"]);
Routes::add_web("GET", "/extract/revision/{id}", ["RevisionController", "split_revision"], ["Auth", "can_moderate"]); Routes::add_web("GET", "/extract/revision/{id}", ["RevisionController", "split_revision"], ["Auth", "can_moderate"]);
ArchiveController::routes();
+21 -10
View File
@@ -34,20 +34,31 @@
<h2 class="text-center">{{ $doc->subtitle }}</h2> <h2 class="text-center">{{ $doc->subtitle }}</h2>
<h3 class="text-center">{{ $doc->subsubtitle }}</h3> <h3 class="text-center">{{ $doc->subsubtitle }}</h3>
<div class="row" id="droppable"> <div class="row" id="droppable">
<div class="col-lg-6 col-12"> <div class="col-lg-6 col-12">
@if (Auth::can_moderate()) @if (Auth::can_moderate())
<div class="btn-group"> <div class="btn-group">
<documentedit docid="{{ $doc->id }}"></documentedit> <documentedit docid="{{ $doc->id }}"></documentedit>
<a href="/upload/attachment/{{ $doc->id }}" class="btn btn-primary" title="Upload attachment"><i class="fa-solid fa-file-arrow-up"></i></a> <a href="/archive/upload/{{ $doc->id }}" class="btn btn-primary" title="Upload to archive.org"><i class="fa-solid fa-building-columns"></i></a>
<a href="/overview/create/{{ $doc->id }}/stay" class="btn btn-primary" title="Create Overview with AI"><i class="fa-solid fa-wand-magic"></i></a> <a href="/upload/attachment/{{ $doc->id }}" class="btn btn-primary" title="Upload attachment"><i class="fa-solid fa-file-arrow-up"></i></a>
<a href="/overview/create/{{ $doc->id }}/move" class="btn btn-primary" title="Create Overview with AI and move it"><i class="fa-solid fa-wand-magic-sparkles"></i></a> <a href="/overview/create/{{ $doc->id }}/stay" class="btn btn-primary" title="Create Overview with AI"><i class="fa-solid fa-wand-magic"></i></a>
<a href="/merge/{{ $doc->id }}" class="btn btn-primary" title="Merge Revisions"><i class="fa fa-link"></i></a> <a href="/overview/create/{{ $doc->id }}/move" class="btn btn-primary" title="Create Overview with AI and move it"><i class="fa-solid fa-wand-magic-sparkles"></i></a>
<a href="/explode/{{ $doc->id }}" class="btn btn-primary" title="Separate Revisions"><i class="fa fa-unlink"></i></a> <a href="/merge/{{ $doc->id }}" class="btn btn-primary" title="Merge Revisions"><i class="fa fa-link"></i></a>
</div> <a href="/explode/{{ $doc->id }}" class="btn btn-primary" title="Separate Revisions"><i class="fa fa-unlink"></i></a>
@endif </div>
@endif
<h5 class="text-center">Order Number: {{ $doc->internal_id }}</h5> <h5 class="text-center">Order Number: {{ $doc->internal_id }}</h5>
@if ($doc->archived == "Y")
<div class="text-center">
<a class="btn btn-secondary" href="https://archive.org/details/{{ $doc->archive_url }}">
<i class="mx-2 fa-solid fa-building-columns"></i>
View this document on archive.org
</a>
</div>
@endif
@if (Auth::can_moderate()) @if (Auth::can_moderate())
<table class="table"> <table class="table">
<tbody> <tbody>