Files

116 lines
2.8 KiB
PHP

<?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($req->body);
}
static function upload(File $file, $identifier, $filename, $metadata = [], ?Callable $callback = null) {
$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);
}
}
$headers[] = "content-type:application/pdf";
$req = new HTTPRequest();
if ($callback) {
$req->setCallback($callback);
}
$resp = $req->put("https://s3.us.archive.org/" . $identifier . "/" . $filename, $file->path(), $headers);
if ($resp == 200) {
return true;
}
throw new Exception($resp . ": " . $req->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($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);
}
}