Files

108 lines
2.2 KiB
PHP

<?php
class ArchiveJob extends Job {
public $document;
private $currentFile;
private static $me;
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");
$num = 1;
foreach ($doc->revisions as $r) {
try {
$filename = $num . "," . $r->filename();
$this->status("Uploading " . $filename);
$r->archived = "P";
$r->save();
$this->currentFile = $filename;
ArchiveJob::$me = $this;
Archive::upload($r->getPDF(), $id, $filename, $meta, [$this, "progress"]);
$r->refresh();
$r->archived = "Y";
$r->save();
$worked++;
} catch (Exception $e) {
$r->refresh();
$r->archived = "F";
$r->save();
$this->fail($e->getMessage());
return;
}
$num++;
}
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();
}
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->status("Uploaded as $id");
$this->finish();
}
public function progress($ch, $todl, $dl, $toul, $ul) {
if ($toul > 0) {
$txt = sprintf("Uploading %s: %d%%", $this->currentFile, $ul * 100 / $toul);
$this->status(sprintf("Uploading %s: %d%%", $this->currentFile, $ul * 100 / $toul));
}
}
}