From f4fd04ec1064103f100f5b0ba7e5feb60165c8cc Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Wed, 14 Sep 2022 11:32:57 -0700 Subject: [PATCH] if container is already present and complete, skip it.. --- ui/fixtures/cmd.container.json | 5 +++++ ui/medashare/cli.py | 27 ++++++++++++++++++--------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/ui/fixtures/cmd.container.json b/ui/fixtures/cmd.container.json index 206eca3..05c4adf 100644 --- a/ui/fixtures/cmd.container.json +++ b/ui/fixtures/cmd.container.json @@ -69,5 +69,10 @@ "special": "verify store object cnt", "comment": "should only have one container and six files files, and a metadata", "count": 9 +}, +{ + "title": "that it gets skipped when complete", + "cmd": [ "container", "somedir.torrent" ], + "stderr": "Warning, container already complete, skipping 'somedir.torrent'.\n" } ] diff --git a/ui/medashare/cli.py b/ui/medashare/cli.py index 0b1a036..dd9ada8 100644 --- a/ui/medashare/cli.py +++ b/ui/medashare/cli.py @@ -1261,6 +1261,24 @@ def cmd_list(options, persona, objstr, cache): @init_datastructs def cmd_container(options, persona, objstr, cache): for i in options.files: + with open(i, 'rb') as fp: + torrent = bencode.bdecode(fp.read()) + bencodedinfo = bencode.bencode(torrent['info']) + infohash = hashlib.sha1(bencodedinfo).hexdigest() + + # XXX - not entirely happy w/ URI + uri = 'magnet:?xt=urn:btih:%s&dn=%s' % (infohash, + torrent['info']['name'].decode('utf-8')) + + try: + cont = objstr.by_id(Container.make_id(uri)) + except KeyError: + pass + else: + if not 'incomplete' in cont: + print('Warning, container already complete, skipping %s.' % repr(i), file=sys.stderr) + continue + good, bad = validate_file(i) if bad: @@ -1283,15 +1301,6 @@ def cmd_container(options, persona, objstr, cache): # XXX - ensure only one is added? hashes.extend(fobj.hashes) - with open(i, 'rb') as fp: - torrent = bencode.bdecode(fp.read()) - bencodedinfo = bencode.bencode(torrent['info']) - infohash = hashlib.sha1(bencodedinfo).hexdigest() - - # XXX - not entirely happy w/ URI - uri = 'magnet:?xt=urn:btih:%s&dn=%s' % (infohash, - torrent['info']['name'].decode('utf-8')) - kwargs = dict(files=files, hashes=hashes, uri=uri)