diff options
Diffstat (limited to '')
-rw-r--r-- | archivist/cli.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/archivist/cli.py b/archivist/cli.py index 9299c94..fee0cd9 100644 --- a/archivist/cli.py +++ b/archivist/cli.py @@ -1,8 +1,10 @@ import click import os +import sys from functools import reduce import operator as op +import pathlib from .virtual_prefixes import query as query_virtual, is_virtual, register_prefixes from .utils import ProxyCommand @@ -263,6 +265,52 @@ def add_doc(file, tags, create_tags, ignore_missing_tags): Document.create_from_file(file, tags, direction = Document.Direction.IN, mimetype = mimetype) +@doc.command('store') +@click.argument('id', type=int) +@click.argument('path', type=click.Path(file_okay=True, dir_okay=True, writable=True, allow_dash=True)) +@click.option('--parents', '-p', is_flag=True, help = "Create intermediate directories, if needed.") +def store_doc(id, path, parents): + from .model import Document + + try: + d = Document.get(id = id) + except Document.DoesNotExist: + print("Document with ID #%s does not exist" % id) + return + + if path in (b'-', '-'): + click.get_binary_stream('stdout').write(d.content.blob) + else: + p = pathlib.Path(path) + if p.is_dir(): + p = p / d.path.name + + if parents: + p.parent.mkdir(parents=True) + elif not p.parent.exists(): + raise click.UsageError("Directory %s does not exist (use option '-p'?)." % p.parent) + + with p.open(mode='wb') as f: + f.write(d.content.blob) + +@doc.command('open') +@click.argument('id', type=int) +def open_doc(id): + from .model import Document + import tempfile + + try: + d = Document.get(id = id) + except Document.DoesNotExist: + print("Document with ID #%s does not exist" % id) + return + + with tempfile.TemporaryDirectory() as tmpdir: + filename = pathlib.Path(tmpdir, d.path.name) + with filename.open('xb') as outfile: + outfile.write(d.content.blob) + click.launch(str(filename), wait=True) + @doc.command('find') @click.argument('tags', type=TAG, nargs=-1) def find_doc(tags): |