aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRiku Voipio <riku.voipio@linaro.org>2020-07-14 16:14:01 +0300
committerKelley Spoon <kelley.spoon@linaro.org>2020-07-28 12:14:07 +0000
commitf07d675de8c6e3d7d30a046163fc62146b7972cb (patch)
treec141a53acb4242a4336120aa31f241e681859a2b
parent63ee9699a336ab07b8fe2735ae286f2af19ec22c (diff)
downloadlinaro-license-protection-f07d675de8c6e3d7d30a046163fc62146b7972cb.tar.gz
LLP: move latest/ link resolving to runtime
Instead of copying all items in link-latest operation, simply parse the latest link on fly when user is browsing Signed-off-by: Riku Voipio <riku.voipio@linaro.org> Change-Id: Ifeb2b9e95e1ddb0d40ec2d066ca5b1edb4fb1b53 Reviewed-on: https://review.linaro.org/c/infrastructure/linaro-license-protection/+/35939 Reviewed-by: Benjamin Copeland <ben.copeland@linaro.org>
-rw-r--r--license_protected_downloads/api/v3.py3
-rw-r--r--license_protected_downloads/common.py32
-rw-r--r--license_protected_downloads/views.py6
3 files changed, 37 insertions, 4 deletions
diff --git a/license_protected_downloads/api/v3.py b/license_protected_downloads/api/v3.py
index 37b8a1a..5bc43d4 100644
--- a/license_protected_downloads/api/v3.py
+++ b/license_protected_downloads/api/v3.py
@@ -77,9 +77,6 @@ class LatestLinkResource(PublishResource):
dst = os.path.join(os.path.dirname(path), link_name)
keys = b.list(dst)
b.delete_keys(keys)
- for k in items:
- newkeyname = k.name.replace(path, dst)
- b.copy_key(newkeyname, k.bucket.name, k.name)
# keep track of where the link content came from
b.new_key(dst + '/.s3_linked_from').set_contents_from_string(path)
diff --git a/license_protected_downloads/common.py b/license_protected_downloads/common.py
index a10f318..7c0beff 100644
--- a/license_protected_downloads/common.py
+++ b/license_protected_downloads/common.py
@@ -1,5 +1,6 @@
import fnmatch
import os
+import re
import boto
@@ -158,13 +159,42 @@ def _sort_artifacts(a, b):
# just do a normal string sort
return cmp(a, b)
+def s3_replace_latest(url, bucket=None):
+ ''' read .s3_linked_from file to find out the original directory to read from
+ '''
+ # not a latest url, continue
+ if not "latest" in url:
+ return url
+
+ link_url = re.sub("latest.*","latest/.s3_linked_from", url)
+
+ if bucket is None:
+ bucket = S3Artifact.get_bucket()
+
+ s3path = settings.S3_PREFIX_PATH + link_url
+
+ key = boto.s3.key.Key(bucket, s3path)
+ # if there's no key already, there's no .s3_linked_from, so we're done here
+ if key is None:
+ return url
+ try:
+ redir_loc = key.get_contents_as_string().strip()
+ # .s3_linked_from is referencing itself? Should return original url
+ if redir_loc == s3path:
+ return url
+ # scrub the s3 prefix
+ new_url = re.sub("^%s" % settings.S3_PREFIX_PATH, '', redir_loc)
+ return new_url
+ except:
+ raise Http404
+
def _s3_list(bucket, url):
prefix = settings.S3_PREFIX_PATH + url
if prefix[-1] != '/':
# s3 listing needs '/' to do a dir listing
prefix = prefix + '/'
-
+ prefix = s3_replace_latest(prefix, bucket)
for item in bucket.list(delimiter='/', prefix=prefix):
if item.name != prefix:
yield item
diff --git a/license_protected_downloads/views.py b/license_protected_downloads/views.py
index 138d10e..f0c5d32 100644
--- a/license_protected_downloads/views.py
+++ b/license_protected_downloads/views.py
@@ -28,6 +28,7 @@ from license_protected_downloads.common import (
cached_call,
dir_list,
find_artifact,
+ s3_replace_latest,
)
from license_protected_downloads.api.v1 import file_server_post
@@ -125,6 +126,11 @@ def group_auth_failed_response(request, auth_groups):
def file_server(request, path):
"""Serve up a file / directory listing or license page as required"""
path = iri_to_uri(path)
+ if "/latest" in path:
+ new_path = s3_replace_latest(path, None)
+
+ if new_path != path:
+ return redirect('/'+new_path)
if request.method == "POST":
return file_server_post(request, path)