Added archive.org upload
This commit is contained in:
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user