diff --git a/data/model/storage.py b/data/model/storage.py index b6a542474..41eca2fd8 100644 --- a/data/model/storage.py +++ b/data/model/storage.py @@ -1,6 +1,6 @@ import logging -from peewee import JOIN_LEFT_OUTER, fn, SQL +from peewee import JOIN_LEFT_OUTER, fn, SQL, IntegrityError from data.model import (config, db_transaction, InvalidImageException, TorrentInfoDoesNotExist, DataModelException, _basequery) @@ -255,7 +255,11 @@ def get_storage_locations(uuid): def save_torrent_info(storage_object, piece_length, pieces): - TorrentInfo.create(storage=storage_object, piece_length=piece_length, pieces=pieces) + try: + TorrentInfo.create(storage=storage_object, piece_length=piece_length, pieces=pieces) + except IntegrityError: + # TorrentInfo already exists for this storage. + pass def get_torrent_info(blob): try: diff --git a/endpoints/v2/blob.py b/endpoints/v2/blob.py index 210e424f8..745adf79a 100644 --- a/endpoints/v2/blob.py +++ b/endpoints/v2/blob.py @@ -224,11 +224,17 @@ def _upload_chunk(namespace, repo_name, upload_uuid): input_fp = wrap_with_handler(input_fp, found.sha_state.update) + # Add a hasher for calculating SHA1s for torrents if this is the first chunk and/or we have + # already calculated hash data for the previous chunk(s). piece_hasher = None - # TODO remove this when all in-progress blob uploads reliably contain piece hashes - if start_offset == 0: - piece_hasher = PieceHasher(app.config['TORRENT_PIECE_SIZE'], start_offset, '', - resumablehashlib.sha1()) + if found.chunk_count == 0 or found.piece_sha_state: + initial_sha1_value = found.piece_sha_state or resumablehashlib.sha1() + initial_sha1_pieces_value = found.piece_hashes or '' + + piece_hasher = PieceHasher(app.config['TORRENT_PIECE_SIZE'], start_offset, + initial_sha1_pieces_value, + initial_sha1_value) + input_fp = wrap_with_handler(input_fp, piece_hasher.update) # If this is the first chunk and we're starting at the 0 offset, add a handler to gunzip the diff --git a/test/data/test.db b/test/data/test.db index e26e28a9b..1dfcb09e4 100644 Binary files a/test/data/test.db and b/test/data/test.db differ