diff --git a/endpoints/verbs.py b/endpoints/verbs.py index 39718ded2..86c7a5d55 100644 --- a/endpoints/verbs.py +++ b/endpoints/verbs.py @@ -15,7 +15,7 @@ from storage import Storage from util.registry.queuefile import QueueFile from util.registry.queueprocess import QueueProcess -from util.registry.torrent import make_torrent, private_torrent_name +from util.registry.torrent import make_torrent, per_user_torrent_filename, public_torrent_filename from formats.squashed import SquashedDockerImage from formats.aci import ACIImage from endpoints.v2.blob import BLOB_DIGEST_ROUTE @@ -284,7 +284,8 @@ def get_squashed_tag(namespace, repository, tag): @process_auth def get_tag_torrent(namespace, repository, digest): permission = ReadRepositoryPermission(namespace, repository) - if not permission.can() and not model.repository.repository_is_public(namespace, repository): + public_repo = model.repository.repository_is_public(namespace, repository) + if not permission.can() and not public_repo: abort(403) user = get_authenticated_user() @@ -307,12 +308,14 @@ def get_tag_torrent(namespace, repository, digest): except model.TorrentInfoDoesNotExist: abort(404) - name = private_torrent_name(user.uuid, blob.uuid) + if public_repo: + name = public_torrent_filename(blob.uuid) + else: + name = per_user_torrent_filename(user.uuid, blob.uuid) torrent_file = make_torrent(name, webseed, blob.image_size, torrent_info.piece_length, torrent_info.pieces) headers = {'Content-Type': 'application/x-bittorrent', - 'Content-Disposition': 'attachment; filename={0}.torrent'.format(name), - 'Content-Length': len(torrent_file)} + 'Content-Disposition': 'attachment; filename={0}.torrent'.format(name)} - return make_response((torrent_file, 200, headers)) + return make_response(torrent_file, 200, headers) diff --git a/util/registry/torrent.py b/util/registry/torrent.py index e279e5cd9..2f9e2f79a 100644 --- a/util/registry/torrent.py +++ b/util/registry/torrent.py @@ -10,7 +10,10 @@ from app import app TRACKER_ANNOUNCE_URL = app.config.get('BT_TRACKER_ANNOUNCE_URL') NAMING_SALT = app.config.get('BT_NAMING_SALT') -def private_torrent_name(user_uuid, blob_uuid): +def public_torrent_filename(blob_uuid): + return hashlib.sha256(blob_uuid).hexdigest() + +def per_user_torrent_filename(user_uuid, blob_uuid): return hashlib.sha256(blob_uuid + user_uuid + NAMING_SALT).hexdigest()