From eaa98f49d66341c1427f126c2b815660c4710f10 Mon Sep 17 00:00:00 2001 From: nineven Date: Wed, 8 Apr 2020 10:06:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8F=8C=E9=87=8D=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E8=BF=87=E6=9C=9F=E4=B8=8D=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fir_ser/api/utils/TokenManager.py | 7 +++---- fir_ser/api/utils/serializer.py | 8 +++++--- fir_ser/api/utils/storage/caches.py | 13 ++++++------- fir_ser/api/utils/storage/storage.py | 11 ++++++----- fir_ser/api/views/download.py | 2 +- fir_ser/fir_ser/settings.py | 9 +++++---- 6 files changed, 26 insertions(+), 24 deletions(-) diff --git a/fir_ser/api/utils/TokenManager.py b/fir_ser/api/utils/TokenManager.py index 8ecaaf9..8b40df5 100644 --- a/fir_ser/api/utils/TokenManager.py +++ b/fir_ser/api/utils/TokenManager.py @@ -19,9 +19,8 @@ from fir_ser.settings import CACHE_KEY_TEMPLATE class DownloadToken(object): - def make_token(self,release_id,time_limit=60): - - token_key = "%s%s"%(CACHE_KEY_TEMPLATE.get("make_token_key"),release_id) + def make_token(self,release_id,time_limit=60,key=''): + token_key = "_".join([key.lower(), CACHE_KEY_TEMPLATE.get("make_token_key"),release_id]) make_token_key = cache.get(token_key) if make_token_key: return make_token_key @@ -40,7 +39,7 @@ class DownloadToken(object): def verify_token(self,token,release_id): try: values = cache.get(token) - if release_id in values.get("data",None): + if values and release_id in values.get("data",None): return True except Exception as e: print(e) diff --git a/fir_ser/api/utils/serializer.py b/fir_ser/api/utils/serializer.py index 553d0ea..1f65914 100644 --- a/fir_ser/api/utils/serializer.py +++ b/fir_ser/api/utils/serializer.py @@ -43,10 +43,12 @@ class AppsSerializer(serializers.ModelSerializer): if master_release_obj: icon_url = "" - + key='' + if self.context.get("key", None) and self.context.get("key") != "undefined": + key=self.context.get("key", '') if self.context.get("storage", None) and self.context.get("storage") != "undefined": storage = self.context.get("storage", None) - icon_url = storage.get_download_url(os.path.basename(master_release_obj.icon_url),600) + icon_url = storage.get_download_url(os.path.basename(master_release_obj.icon_url),600,key=key) datainfo = { "app_version": master_release_obj.app_version, "icon_url": icon_url, @@ -60,7 +62,7 @@ class AppsSerializer(serializers.ModelSerializer): "binary_url":master_release_obj.binary_url, } - download_token = token_obj.make_token(master_release_obj.release_id,600) + download_token = token_obj.make_token(master_release_obj.release_id,600,key=key) datainfo["download_token"] = download_token return datainfo diff --git a/fir_ser/api/utils/storage/caches.py b/fir_ser/api/utils/storage/caches.py index 74c0192..273ec7e 100644 --- a/fir_ser/api/utils/storage/caches.py +++ b/fir_ser/api/utils/storage/caches.py @@ -11,16 +11,16 @@ from fir_ser.settings import CACHE_KEY_TEMPLATE from api.utils.storage.storage import Storage,LocalStorage from api.utils.crontab import run -def get_download_url_by_cache(app_obj, filename, limit, isdownload=False): +def get_download_url_by_cache(app_obj, filename, limit, isdownload=True): now = time.time() + if isdownload is None: + local_storage = LocalStorage('localhost', False) + return local_storage.get_download_url(filename, limit, 'plist') download_val = cache.get("%s_%s" % ('download_url', filename)) if download_val: if download_val.get("time") > now - 60: return download_val.get("download_url") else: - if isdownload: - local_storage = LocalStorage('localhost', False) - return local_storage.get_download_url(filename, limit, 'plist') user_obj = UserInfo.objects.filter(pk=app_obj.get("user_id")).first() storage = Storage(user_obj) return storage.get_download_url(filename, limit) @@ -45,7 +45,6 @@ def get_app_download_by_cache(app_id): cache.incr(key) return download_times + 1 - def del_cache_response_by_short(short,app_id): - cache.delete("%s%s"%(CACHE_KEY_TEMPLATE.get("download_short_key"),short)) - cache.delete("%s%s" % (CACHE_KEY_TEMPLATE.get("app_instance"), app_id)) \ No newline at end of file + cache.delete("_".join([CACHE_KEY_TEMPLATE.get("download_short_key"),short])) + cache.delete("_".join([CACHE_KEY_TEMPLATE.get("app_instance"),app_id])) diff --git a/fir_ser/api/utils/storage/storage.py b/fir_ser/api/utils/storage/storage.py index dfff827..fa5cf16 100644 --- a/fir_ser/api/utils/storage/storage.py +++ b/fir_ser/api/utils/storage/storage.py @@ -9,7 +9,7 @@ from .aliyunApi import AliYunOss from .qiniuApi import QiNiuOss from .localApi import LocalStorage import json,time -from fir_ser import settings +from fir_ser.settings import THIRD_PART_CONFIG,CACHE_KEY_TEMPLATE from django.core.cache import cache @@ -21,16 +21,17 @@ class Storage(object): if self.storage: return self.storage.get_upload_token(filename, expires) - def get_download_url(self, filename, expires=900,ftype=None): + def get_download_url(self,filename, expires=900,ftype=None,key=''): if self.storage: now = time.time() - download_val = cache.get("%s_%s"%('download_url',filename)) + down_key = "_".join([key.lower(),CACHE_KEY_TEMPLATE.get('download_url_key'),filename]) + download_val = cache.get(down_key) if download_val: if download_val.get("time") > now - 60: return download_val.get("download_url") download_url=self.storage.get_download_url(filename, expires,ftype) - cache.set("%s_%s"%('download_url',filename),{"download_url":download_url,"time":now+expires},expires) + cache.set(down_key,{"download_url":download_url,"time":now+expires},expires) return download_url def delete_file(self, filename, apptype=None): @@ -63,7 +64,7 @@ class Storage(object): if admin_storage: return self.get_storage(UserInfo) else: - storage_lists = settings.THIRD_PART_CONFIG.get('storage') + storage_lists = THIRD_PART_CONFIG.get('storage') for storage in storage_lists: if storage.get("active",None): storage_type= storage.get('type',None) diff --git a/fir_ser/api/views/download.py b/fir_ser/api/views/download.py index e3dbad4..a9ad0de 100644 --- a/fir_ser/api/views/download.py +++ b/fir_ser/api/views/download.py @@ -81,7 +81,7 @@ class ShortDownloadView(APIView): res.msg="该应用不存在" return Response(res.dict) - app_serializer = AppsSerializer(app_obj,context={"release_id":release_id,"storage":Storage(app_obj.user_id)}) + app_serializer = AppsSerializer(app_obj,context={"key":"ShortDownloadView","release_id":release_id,"storage":Storage(app_obj.user_id)}) res.data = app_serializer.data return Response(res.dict) diff --git a/fir_ser/fir_ser/settings.py b/fir_ser/fir_ser/settings.py index 45993bd..e84dae6 100644 --- a/fir_ser/fir_ser/settings.py +++ b/fir_ser/fir_ser/settings.py @@ -234,10 +234,11 @@ THIRD_PART_CONFIG = { } CACHE_KEY_TEMPLATE={ - 'download_times_key':'app_download_times_', - 'make_token_key':'make_token_', - 'download_short_key':'download_short_', - 'app_instance_key':'app_instance_' + 'download_times_key':'app_download_times', + 'make_token_key':'make_token', + 'download_short_key':'download_short', + 'app_instance_key':'app_instance', + 'download_url_key':'download_url' } SYNC_CACHE_TO_DATABASE={