From 917ed49f74f25ffb21bc7d749fc1c897e601b7b7 Mon Sep 17 00:00:00 2001 From: nineven Date: Wed, 8 Apr 2020 13:05:38 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/FirAppInfostimeline.vue | 1 + fir_ser/api/utils/auth.py | 12 ++- fir_ser/api/utils/crontab/sync_cache.py | 2 +- fir_ser/api/utils/storage/caches.py | 16 ++-- fir_ser/api/utils/storage/storage.py | 73 +++++++++++-------- fir_ser/api/views/download.py | 9 +-- fir_ser/api/views/login.py | 4 +- fir_ser/fir_ser/settings.py | 4 +- 8 files changed, 63 insertions(+), 58 deletions(-) diff --git a/fir_client/src/components/FirAppInfostimeline.vue b/fir_client/src/components/FirAppInfostimeline.vue index 7cac420..ea106d0 100644 --- a/fir_client/src/components/FirAppInfostimeline.vue +++ b/fir_client/src/components/FirAppInfostimeline.vue @@ -168,6 +168,7 @@ this.release_apps = data.data.release_apps; this.currentapp = data.data.currentapp; } else if (data.code === 1003) { + loading.close(); this.$router.push({name: 'FirApps'}); } loading.close(); diff --git a/fir_ser/api/utils/auth.py b/fir_ser/api/utils/auth.py index 5dc3852..f394738 100644 --- a/fir_ser/api/utils/auth.py +++ b/fir_ser/api/utils/auth.py @@ -1,12 +1,8 @@ -from django.utils.translation import ugettext_lazy as _ from django.core.cache import cache - -import datetime from rest_framework.authentication import BaseAuthentication -from rest_framework import exceptions from rest_framework.exceptions import AuthenticationFailed from api.models import Token, UserInfo -import pytz +from fir_ser.settings import CACHE_KEY_TEMPLATE import base64 class ExpiringTokenAuthentication(BaseAuthentication): @@ -26,7 +22,9 @@ class ExpiringTokenAuthentication(BaseAuthentication): if not auth_token: raise AuthenticationFailed({"code": 1001, "error": "缺少token"}) - cacheuserinfo = cache.get(auth_token) + auth_key = "_".join([CACHE_KEY_TEMPLATE.get('user_auth_token_key'), auth_token]) + + cacheuserinfo = cache.get(auth_key) if not cacheuserinfo: raise AuthenticationFailed({"code": 1001, "error": "无效的token"}) if user_name != cacheuserinfo.get('username',None): @@ -37,7 +35,7 @@ class ExpiringTokenAuthentication(BaseAuthentication): if not user_obj: raise AuthenticationFailed({"code": 1001, "error": "无效的token"}) if user_obj.is_active: - cache.set(auth_token, cacheuserinfo, 3600 * 24 * 7) + cache.set(auth_key, cacheuserinfo, 3600 * 24 * 7) return user_obj,auth_token else: raise AuthenticationFailed({"code": 1001, "error": "用户被禁用"}) diff --git a/fir_ser/api/utils/crontab/sync_cache.py b/fir_ser/api/utils/crontab/sync_cache.py index 7ffaabd..5b3a6eb 100644 --- a/fir_ser/api/utils/crontab/sync_cache.py +++ b/fir_ser/api/utils/crontab/sync_cache.py @@ -11,7 +11,7 @@ from fir_ser.settings import CACHE_KEY_TEMPLATE def sync_download_times(): down_tem_key = CACHE_KEY_TEMPLATE.get("download_times_key") - key = "%s%s" %(down_tem_key,'*') + key = "_".join([down_tem_key,'*']) for app_download in cache.iter_keys(key): app_id = app_download.split(down_tem_key)[1] Apps.objects.filter(app_id=app_id).update(count_hits=cache.get(app_download)) diff --git a/fir_ser/api/utils/storage/caches.py b/fir_ser/api/utils/storage/caches.py index 273ec7e..9d5bce1 100644 --- a/fir_ser/api/utils/storage/caches.py +++ b/fir_ser/api/utils/storage/caches.py @@ -27,24 +27,24 @@ def get_download_url_by_cache(app_obj, filename, limit, isdownload=True): def get_app_instance_by_cache(app_id, limit): - app_obj_cache = cache.get("%s_%s" % ('app_instance', app_id)) + app_key="_".join([CACHE_KEY_TEMPLATE.get("app_instance_key"),app_id]) + app_obj_cache = cache.get(app_key) if not app_obj_cache: app_obj_cache = Apps.objects.filter(app_id=app_id).values("pk", 'user_id', 'type').first() - cache.set("%s_%s" % ('app_instance', app_id), app_obj_cache, limit) + cache.set(app_key, app_obj_cache, limit) return app_obj_cache def get_app_download_by_cache(app_id): - down_tem_key = CACHE_KEY_TEMPLATE.get("download_times_key") - key = "%s%s" %(down_tem_key,app_id) - download_times = cache.get(key) + down_tem_key = "_".join([CACHE_KEY_TEMPLATE.get("download_times_key"),app_id]) + download_times = cache.get(down_tem_key) if not download_times: download_times=Apps.objects.filter(app_id=app_id).values("count_hits").first().get('count_hits') - cache.set(key, download_times + 1, 900) + cache.set(down_tem_key, download_times + 1, 900) else: - cache.incr(key) + cache.incr(down_tem_key) return download_times + 1 def del_cache_response_by_short(short,app_id): cache.delete("_".join([CACHE_KEY_TEMPLATE.get("download_short_key"),short])) - cache.delete("_".join([CACHE_KEY_TEMPLATE.get("app_instance"),app_id])) + cache.delete("_".join([CACHE_KEY_TEMPLATE.get("app_instance_key"),app_id])) diff --git a/fir_ser/api/utils/storage/storage.py b/fir_ser/api/utils/storage/storage.py index fa5cf16..95c295f 100644 --- a/fir_ser/api/utils/storage/storage.py +++ b/fir_ser/api/utils/storage/storage.py @@ -8,8 +8,8 @@ from api.models import AppStorage, UserInfo from .aliyunApi import AliYunOss from .qiniuApi import QiNiuOss from .localApi import LocalStorage -import json,time -from fir_ser.settings import THIRD_PART_CONFIG,CACHE_KEY_TEMPLATE +import json, time, base64 +from fir_ser.settings import THIRD_PART_CONFIG, CACHE_KEY_TEMPLATE from django.core.cache import cache @@ -21,17 +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,key=''): + def get_download_url(self, filename, expires=900, ftype=None, key=''): if self.storage: now = time.time() - down_key = "_".join([key.lower(),CACHE_KEY_TEMPLATE.get('download_url_key'),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(down_key,{"download_url":download_url,"time":now+expires},expires) + download_url = self.storage.get_download_url(filename, expires, ftype) + cache.set(down_key, {"download_url": download_url, "time": now + expires}, expires) return download_url def delete_file(self, filename, apptype=None): @@ -47,15 +47,23 @@ class Storage(object): self.storage_obj = user.storage if self.storage_obj: auth = self.get_storage_auth(self.storage_obj) + storage_key = "_".join([CACHE_KEY_TEMPLATE.get('user_storage_key'), + base64.b64encode(json.dumps(auth).encode("utf-8")).decode("utf-8")[0:64]]) storage_type = self.storage_obj.storage_type - if storage_type == 1: - new_storage_obj = QiNiuOss(**auth) - elif storage_type == 2: - new_storage_obj = AliYunOss(**auth) + new_storage_obj = cache.get(storage_key) + if new_storage_obj: + return new_storage_obj else: - new_storage_obj = LocalStorage(**auth) - new_storage_obj.storage_type = storage_type - return new_storage_obj + if storage_type == 1: + new_storage_obj = QiNiuOss(**auth) + elif storage_type == 2: + new_storage_obj = AliYunOss(**auth) + else: + new_storage_obj = LocalStorage(**auth) + + new_storage_obj.storage_type = storage_type + cache.set(storage_key, new_storage_obj, 600) + return new_storage_obj else: return self.get_default_storage() @@ -66,25 +74,28 @@ class Storage(object): else: storage_lists = THIRD_PART_CONFIG.get('storage') for storage in storage_lists: - if storage.get("active",None): - storage_type= storage.get('type',None) - auth = storage.get('auth',{}) - if storage_type == 1: - new_storage_obj = QiNiuOss(**auth) - new_storage_obj.storage_type=1 - elif storage_type == 2: - new_storage_obj = AliYunOss(**auth) - new_storage_obj.storage_type=2 + if storage.get("active", None): + storage_type = storage.get('type', None) + auth = storage.get('auth', {}) + storage_key = "_".join([CACHE_KEY_TEMPLATE.get('user_storage_key'), + base64.b64encode(json.dumps(auth).encode("utf-8")).decode("utf-8")[0:64]]) + new_storage_obj = cache.get(storage_key) + if new_storage_obj: + return new_storage_obj else: - new_storage_obj = LocalStorage(**auth) - new_storage_obj.storage_type=3 - - return new_storage_obj + if storage_type == 1: + new_storage_obj = QiNiuOss(**auth) + new_storage_obj.storage_type = 1 + elif storage_type == 2: + new_storage_obj = AliYunOss(**auth) + new_storage_obj.storage_type = 2 + else: + new_storage_obj = LocalStorage(**auth) + new_storage_obj.storage_type = 3 + cache.set(storage_key, new_storage_obj, 600) + return new_storage_obj return None - - - def get_storage_type(self): if self.storage: return self.storage.storage_type @@ -95,7 +106,7 @@ class Storage(object): 'secret_key': storage_obj.secret_key, 'bucket_name': storage_obj.bucket_name, 'domain_name': storage_obj.domain_name, - 'is_https':storage_obj.is_https + 'is_https': storage_obj.is_https } try: additionalparameters = json.loads(storage_obj.additionalparameters) @@ -103,5 +114,3 @@ class Storage(object): print(e) additionalparameters = {} return {**auth_dict, **additionalparameters} - - diff --git a/fir_ser/api/views/download.py b/fir_ser/api/views/download.py index a9ad0de..55ff415 100644 --- a/fir_ser/api/views/download.py +++ b/fir_ser/api/views/download.py @@ -88,14 +88,7 @@ class ShortDownloadView(APIView): #key的设置 def calculate_cache_key(self, view_instance, view_method, request, args, kwargs): - id = "download" - rtn = '_'.join([ - id, - "short", - kwargs.get("short",None) - ]) - # print( request.META) - return rtn + return "_".join([settings.CACHE_KEY_TEMPLATE.get("download_short_key"), kwargs.get("short", None)]) class InstallView(APIView): ''' diff --git a/fir_ser/api/views/login.py b/fir_ser/api/views/login.py index 7339e66..68c5693 100644 --- a/fir_ser/api/views/login.py +++ b/fir_ser/api/views/login.py @@ -10,6 +10,7 @@ from api.utils.TokenManager import DownloadToken,generateNumericTokenOfLength from api.utils.auth import ExpiringTokenAuthentication from api.utils.response import BaseResponse from django.middleware import csrf +from fir_ser.settings import CACHE_KEY_TEMPLATE def get_token(request): token = csrf.get_token(request) @@ -39,7 +40,8 @@ class LoginView(APIView): now = datetime.datetime.now() user_info = UserInfo.objects.get(pk=user.pk) - cache.set(key,{'uid':user_info.uid,'username':user_info.username},3600*24*7) + auth_key = "_".join([CACHE_KEY_TEMPLATE.get('user_auth_token_key'),key]) + cache.set(auth_key,{'uid':user_info.uid,'username':user_info.username},3600*24*7) Token.objects.create(user=user, **{"access_token": key, "created": now}) serializer = UserInfoSerializer(user_info,) diff --git a/fir_ser/fir_ser/settings.py b/fir_ser/fir_ser/settings.py index e84dae6..1df4e27 100644 --- a/fir_ser/fir_ser/settings.py +++ b/fir_ser/fir_ser/settings.py @@ -238,7 +238,9 @@ CACHE_KEY_TEMPLATE={ 'make_token_key':'make_token', 'download_short_key':'download_short', 'app_instance_key':'app_instance', - 'download_url_key':'download_url' + 'download_url_key':'download_url', + 'user_storage_key':'storage_auth', + 'user_auth_token_key':'user_auth_token' } SYNC_CACHE_TO_DATABASE={