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");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -37,6 +37,8 @@ class HTTPRequest {
|
||||
$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)) {
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,6 +82,12 @@ class Model implements JsonSerializable {
|
||||
Model::$_cache->addServer($host, $port, $weight);
|
||||
}
|
||||
|
||||
public function refresh() {
|
||||
if ($this->id) {
|
||||
$this->_load_record($this->id);
|
||||
}
|
||||
}
|
||||
|
||||
private function _load_record($id) {
|
||||
|
||||
$class = get_called_class();
|
||||
|
||||
Reference in New Issue
Block a user