Skip to content
Snippets Groups Projects
Commit 12a65410 authored by Jared Hancock's avatar Jared Hancock
Browse files

file: Reimplement zip export for cli file module

parent f62186d9
No related branches found
No related tags found
No related merge requests found
...@@ -92,11 +92,10 @@ class Option { ...@@ -92,11 +92,10 @@ class Option {
class OutputStream { class OutputStream {
var $stream; var $stream;
function OutputStream() {
call_user_func_array(array($this, '__construct'), func_get_args());
}
function __construct($stream) { function __construct($stream) {
$this->stream = fopen($stream, 'w'); if (!($this->stream = fopen($stream, 'w')))
throw new Exception(sprintf('%s: Cannot open for writing',
$stream));
} }
function write($what) { function write($what) {
......
...@@ -12,6 +12,7 @@ class FileManager extends Module { ...@@ -12,6 +12,7 @@ class FileManager extends Module {
'list' => 'List files matching criteria', 'list' => 'List files matching criteria',
'export' => 'Export files from the system', 'export' => 'Export files from the system',
'import' => 'Load files exported via `export`', 'import' => 'Load files exported via `export`',
'zip' => 'Create a zip file of the matching files',
'dump' => 'Dump file content to stdout', 'dump' => 'Dump file content to stdout',
'load' => 'Load file contents from stdin', 'load' => 'Load file contents from stdin',
'migrate' => 'Migrate a file to another backend', 'migrate' => 'Migrate a file to another backend',
...@@ -210,6 +211,9 @@ class FileManager extends Module { ...@@ -210,6 +211,9 @@ class FileManager extends Module {
foreach ($files as $m) { foreach ($files as $m) {
$f = AttachmentFile::lookup($m->id); $f = AttachmentFile::lookup($m->id);
if ($options['verbose'])
$this->stderr->write($m->name."\n");
// TODO: Log %attachment and %ticket_attachment entries // TODO: Log %attachment and %ticket_attachment entries
$info = array('file' => $f->getInfo()); $info = array('file' => $f->getInfo());
foreach ($m->tickets as $t) foreach ($m->tickets as $t)
...@@ -242,8 +246,8 @@ class FileManager extends Module { ...@@ -242,8 +246,8 @@ class FileManager extends Module {
if (!$options['file'] || $options['file'] == '-') if (!$options['file'] || $options['file'] == '-')
$options['file'] = 'php://stdin'; $options['file'] = 'php://stdin';
if (!($stream = fopen($options['file'], 'wb'))) if (!($stream = fopen($options['file'], 'rb')))
$this->fail($options['file'].': Unable to open file for export stream'); $this->fail($options['file'].': Unable to open import stream');
while (true) { while (true) {
// Read the file header // Read the file header
...@@ -381,13 +385,45 @@ class FileManager extends Module { ...@@ -381,13 +385,45 @@ class FileManager extends Module {
} }
break; break;
case 'zip':
// Create a temporary ZIP file
$files = FileModel::objects();
$this->_applyCriteria($options, $files);
if (!$options['file'])
$this->fail('Please specify zip file with `-f`');
$zip = new ZipArchive();
if (true !== ($reason = $zip->open($options['file'],
ZipArchive::CREATE)))
$this->fail($reason.': Unable to create zip file');
foreach ($files as $m) {
$f = AttachmentFile::lookup($m->id);
if ($options['verbose'])
$this->stderr->write($m->name."\n");
$name = Format::encode(sprintf(
'%d-%s', $f->getId(), $f->getName()
), 'utf-8', 'cp437');
$zip->addFromString($name, $f->getData());
}
$zip->close();
break;
case 'expunge': case 'expunge':
$files = FileModel::objects(); $files = FileModel::objects();
$this->_applyCriteria($options, $files); $this->_applyCriteria($options, $files);
foreach ($files as $f) { foreach ($files as $m) {
$f->tickets->expunge(); // Drop associated attachment links
$f->unlink() && $f->delete(); $m->tickets->expunge();
$f = AttachmentFile::lookup($m->id);
// Drop file contents
if ($bk = $f->open())
$bk->unlink();
// Drop file record
$f->delete();
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment