Commit 7e71f9da authored by Ophélie Gagnard's avatar Ophélie Gagnard

Upload: Support metadata in json file.

parent c0b9a92e
......@@ -502,8 +502,10 @@ def cmd_upload(*args):
help="Upload the contents of this file, overriding --url")
parser.add_argument('--id',
help="Identifier used for the shadir URL. Overriding --prefix-key, --suffix-key and --url")
parser.add_argument('--metadata',
help="Take a file containing a json-serializable dictionary with shadir metadata.")
parser.add_argument('meta', nargs='*', metavar='KEY=VALUE',
help="Extra metadata.")
help="Extra metadata. Warning: interpreted as string.")
args = parser.parse_args(args or sys.argv[1:])
nc = NetworkcacheClient(args.config)
f = None
......@@ -517,15 +519,25 @@ def cmd_upload(*args):
f = (nc.archive if os.path.isdir(args.url) else urlopen)(args.url)
else:
parser.error('either --file or --url is required')
kw = dict(x.split('=', 1) for x in args.meta)
if args.metadata:
with open(args.metadata) as g:
try:
metadata_dict = json.loads(g.read())
if type(metadata_dict) != dict:
raise NetworkcacheException("Not a json-serializable dictionary: %s" % args.metadata)
except json.decoder.JSONDecodeError:
raise NetworkcacheException("Invalid json in %s" % args.metadata)
else:
metadata_dict = dict()
metadata_dict.update(dict(x.split('=', 1) for x in args.meta))
if args.id:
kw.setdefault('id', args.id)
metadata_dict.setdefault('id', args.id)
key = args.id
else:
kw.setdefault('url', args.url)
metadata_dict.setdefault('url', args.url)
urlmd5 = hashlib.md5(args.url.encode()).hexdigest()
key = args.prefix_key + urlmd5 + args.suffix_key
nc.upload(f, key, **kw)
nc.upload(f, key, **metadata_dict)
finally:
f is None or f.close()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment