From 7821d10929af7dfbdb61d14e35fffb38c55e0dce Mon Sep 17 00:00:00 2001 From: nineven Date: Sun, 10 May 2020 20:35:24 +0800 Subject: [PATCH] fix a bug --- fir_client/src/restful/index.js | 12 ++--- fir_ser/api/utils/TokenManager.py | 14 ++--- fir_ser/api/utils/middlewares.py | 10 ++-- fir_ser/api/utils/serializer.py | 2 +- fir_ser/api/utils/storage/aliyunApi.py | 2 +- fir_ser/api/utils/storage/caches.py | 4 +- fir_ser/api/utils/storage/localApi.py | 7 ++- fir_ser/api/utils/storage/qiniuApi.py | 2 +- fir_ser/api/utils/storage/storage.py | 6 +-- fir_ser/api/views/download.py | 71 +++++++++++++------------- fir_ser/api/views/receiveudids.py | 9 ++-- fir_ser/fir_ser/settings.py | 2 + 12 files changed, 72 insertions(+), 69 deletions(-) diff --git a/fir_client/src/restful/index.js b/fir_client/src/restful/index.js index 2938bf0..17349f9 100644 --- a/fir_client/src/restful/index.js +++ b/fir_client/src/restful/index.js @@ -88,12 +88,12 @@ function ErrorMsg(error) { } if (error.response && error.response.status === 403) { router.push({name: 'FirLogin'}); - } - - if (error.message === 'Network Error') { - alert('网络连接失败'); - } else { - alert(error) + }else{ + if (error.message === 'Network Error') { + alert('网络连接失败'); + } else { + alert(error) + } } } diff --git a/fir_ser/api/utils/TokenManager.py b/fir_ser/api/utils/TokenManager.py index 2471563..1326dc4 100644 --- a/fir_ser/api/utils/TokenManager.py +++ b/fir_ser/api/utils/TokenManager.py @@ -21,20 +21,20 @@ class DownloadToken(object): def make_token(self, release_id, time_limit=60, key='', force_new=False): 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 and not force_new: - return make_token_key + token = cache.get(token_key) + if token and not force_new: + return token else: random_str = uuid.uuid1().__str__().split("-")[0:-1] user_ran_str = uuid.uuid5(uuid.NAMESPACE_DNS, release_id).__str__().split("-") user_ran_str.extend(random_str) - new_str = "".join(user_ran_str) - cache.set(new_str, { + token = "".join(user_ran_str) + cache.set(token, { "atime": time.time() + time_limit, "data": release_id }, time_limit) - cache.set(token_key, new_str, time_limit - 1) - return new_str + cache.set(token_key, token, time_limit - 1) + return token def verify_token(self, token, release_id): try: diff --git a/fir_ser/api/utils/middlewares.py b/fir_ser/api/utils/middlewares.py index 5a85d9f..ad9de5b 100644 --- a/fir_ser/api/utils/middlewares.py +++ b/fir_ser/api/utils/middlewares.py @@ -8,9 +8,11 @@ class CorsMiddleWare(MiddlewareMixin): response["Access-Control-Allow-Methods"] = "GET,POST,DELETE,PUT" response["Access-Control-Allow-Headers"] = "Content-Type,AUTHORIZATION" - response["Access-Control-Allow-Origin"] = request.META.get("HTTP_ORIGIN") - response["Access-Control-Allow-Credentials"] = 'true' - - response["Cache-Control"] = "no-cache" + try: + response["Access-Control-Allow-Origin"] = request.META.get("HTTP_ORIGIN") + response["Access-Control-Allow-Credentials"] = 'true' + response["Cache-Control"] = "no-cache" + except Exception as e: + print(e) return response diff --git a/fir_ser/api/utils/serializer.py b/fir_ser/api/utils/serializer.py index df93ed9..65b4f89 100644 --- a/fir_ser/api/utils/serializer.py +++ b/fir_ser/api/utils/serializer.py @@ -170,7 +170,7 @@ class AppReleaseSerializer(serializers.ModelSerializer): def get_download_token(self, obj): - download_token = token_obj.make_token(obj.release_id, 300) + download_token = token_obj.make_token(obj.release_id, 600) return download_token diff --git a/fir_ser/api/utils/storage/aliyunApi.py b/fir_ser/api/utils/storage/aliyunApi.py index 887260e..b468cf3 100644 --- a/fir_ser/api/utils/storage/aliyunApi.py +++ b/fir_ser/api/utils/storage/aliyunApi.py @@ -110,7 +110,7 @@ class AliYunOss(object): auth = oss2.StsAuth(self.token.access_key_id, self.token.access_key_secret, self.token.security_token) self.bucket = oss2.Bucket(auth, uri + url, self.bucket_name, is_cname=is_cname) - def get_download_url(self, name, expires=1800, ftype=None, force_new=False): + def get_download_url(self, name, expires=1800, force_new=False): # self.get_auth_bucket(name,expires) private_url = self.bucket.sign_url('GET', name, expires) return private_url diff --git a/fir_ser/api/utils/storage/caches.py b/fir_ser/api/utils/storage/caches.py index 618bcea..b0445d9 100644 --- a/fir_ser/api/utils/storage/caches.py +++ b/fir_ser/api/utils/storage/caches.py @@ -37,7 +37,7 @@ def get_download_url_by_cache(app_obj, filename, limit, isdownload=True, key='', developerid=SuperSign_obj.developerid).first() if APPToDeveloper_obj: return local_storage.get_download_url( - APPToDeveloper_obj.binary_file + "." + filename.split(".")[-1], limit, download_url_type) + APPToDeveloper_obj.binary_file + "." + download_url_type, limit) else: return "" else: @@ -45,7 +45,7 @@ def get_download_url_by_cache(app_obj, filename, limit, isdownload=True, key='', else: return "" - return local_storage.get_download_url(filename, limit, download_url_type) + return local_storage.get_download_url(filename.split(".")[0]+ "." + download_url_type, limit) down_key = "_".join([key.lower(), CACHE_KEY_TEMPLATE.get('download_url_key'), filename]) download_val = cache.get(down_key) if download_val: diff --git a/fir_ser/api/utils/storage/localApi.py b/fir_ser/api/utils/storage/localApi.py index e58b05e..2c3211b 100644 --- a/fir_ser/api/utils/storage/localApi.py +++ b/fir_ser/api/utils/storage/localApi.py @@ -20,15 +20,14 @@ class LocalStorage(object): dtoken = DownloadToken() return dtoken.make_token(name, expires) - def get_download_url(self, name, expires=1800, ftype=None, force_new=False): + def get_download_url(self, name, expires=600, force_new=False): dtoken = DownloadToken() base_url = '/'.join([self.domain_name, 'download', name]) uri = 'http://' if self.is_https: uri = 'https://' - download_url = uri + base_url + "?token=" + dtoken.make_token(name, expires, force_new=force_new) - if ftype: - download_url = download_url + '&ftype=' + ftype + download_url = "%s%s?%s=%s" % ( + uri, base_url, settings.DATA_DOWNLOAD_KEY, dtoken.make_token(name, expires, force_new=force_new)) return download_url def del_file(self, name): diff --git a/fir_ser/api/utils/storage/qiniuApi.py b/fir_ser/api/utils/storage/qiniuApi.py index d9673c2..0d07273 100644 --- a/fir_ser/api/utils/storage/qiniuApi.py +++ b/fir_ser/api/utils/storage/qiniuApi.py @@ -33,7 +33,7 @@ class QiNiuOss(object): # print(token) return token - def get_download_url(self, name, expires=1800, ftype=None, force_new=False): + def get_download_url(self, name, expires=1800, force_new=False): # 有两种方式构造base_url的形式 uri = 'http://' if self.is_https: diff --git a/fir_ser/api/utils/storage/storage.py b/fir_ser/api/utils/storage/storage.py index b00dc43..0bcfe6b 100644 --- a/fir_ser/api/utils/storage/storage.py +++ b/fir_ser/api/utils/storage/storage.py @@ -4,7 +4,7 @@ # author: liuyu # date: 2020/3/23 -from api.models import AppStorage, UserInfo +from api.models import UserInfo from .aliyunApi import AliYunOss from .qiniuApi import QiNiuOss from .localApi import LocalStorage @@ -24,7 +24,7 @@ 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='', force_new=False): + def get_download_url(self, filename, expires=900, key='', force_new=False): if self.storage: now = time.time() down_key = "_".join([key.lower(), CACHE_KEY_TEMPLATE.get('download_url_key'), filename]) @@ -33,7 +33,7 @@ class Storage(object): if download_val.get("time") > now - 60: return download_val.get("download_url") - download_url = self.storage.get_download_url(filename, expires, ftype, force_new=False) + download_url = self.storage.get_download_url(filename, expires,force_new=True) cache.set(down_key, {"download_url": download_url, "time": now + expires}, expires) return download_url diff --git a/fir_ser/api/views/download.py b/fir_ser/api/views/download.py index 932733f..683d2a6 100644 --- a/fir_ser/api/views/download.py +++ b/fir_ser/api/views/download.py @@ -31,8 +31,8 @@ class DownloadView(APIView): def get(self, request, filename): res = BaseResponse() - downtoken = request.query_params.get("token", None) - ftype = request.query_params.get("ftype", None) + downtoken = request.query_params.get(settings.DATA_DOWNLOAD_KEY, None) + ftype = filename.split(".")[-1] if not downtoken: res.code = 1004 res.msg = "缺失token" @@ -40,7 +40,38 @@ class DownloadView(APIView): dtoken = DownloadToken() if dtoken.verify_token(downtoken, filename): - if not ftype: + if ftype == 'plist': + release_id = filename.split('.')[0] + apptodev_obj = APPToDeveloper.objects.filter(binary_file=release_id).first() + if apptodev_obj: + release_obj = AppReleaseInfo.objects.filter(is_master=True, app_id=apptodev_obj.app_id).first() + else: + release_obj = AppReleaseInfo.objects.filter(release_id=release_id).first() + if release_obj: + storage = Storage(release_obj.app_id.user_id) + bundle_id = release_obj.app_id.bundle_id + app_version = release_obj.app_version + name = release_obj.app_id.name + ios_plist_bytes = make_resigned(storage.get_download_url(filename.split('.')[0]+".ipa"), + storage.get_download_url(release_obj.icon_url), bundle_id, + app_version, name) + response = FileResponse(ios_plist_bytes) + response['Content-Type'] = "application/x-plist" + response['Content-Disposition'] = 'attachment; filename=' + make_random_uuid() + return response + res.msg = "plist release_id error" + elif ftype == 'mobileconifg': + release_obj = AppReleaseInfo.objects.filter(release_id=filename.split('.')[0]).first() + if release_obj: + bundle_id = release_obj.app_id.bundle_id + udid_url = get_post_udid_url(request, release_obj.app_id.short) + ios_udid_mobileconfig = make_udid_mobileconfig(udid_url, bundle_id) + response = FileResponse(ios_udid_mobileconfig) + response['Content-Type'] = "application/x-apple-aspen-config" + response['Content-Disposition'] = 'attachment; filename=' + make_random_uuid() + '.mobileconfig' + return response + res.msg = "mobileconifg release_id error" + else: file_path = os.path.join(settings.MEDIA_ROOT, filename) try: response = FileResponse(open(file_path, 'rb')) @@ -50,38 +81,6 @@ class DownloadView(APIView): response['content_type'] = "application/octet-stream" response['Content-Disposition'] = 'attachment; filename=' + filename return response - else: - if ftype == 'plist': - release_id = filename.split('.')[0] - apptodev_obj = APPToDeveloper.objects.filter(binary_file=release_id).first() - if apptodev_obj: - release_obj = AppReleaseInfo.objects.filter(is_master=True, app_id=apptodev_obj.app_id).first() - else: - release_obj = AppReleaseInfo.objects.filter(release_id=release_id).first() - if release_obj: - storage = Storage(release_obj.app_id.user_id) - bundle_id = release_obj.app_id.bundle_id - app_version = release_obj.app_version - name = release_obj.app_id.name - ios_plist_bytes = make_resigned(storage.get_download_url(filename), - storage.get_download_url(release_obj.icon_url), bundle_id, - app_version, name) - response = FileResponse(ios_plist_bytes) - response['Content-Type'] = "application/x-plist" - response['Content-Disposition'] = 'attachment; filename=' + make_random_uuid() - return response - res.msg = "plist release_id error" - elif ftype == 'mobileconifg': - release_obj = AppReleaseInfo.objects.filter(release_id=filename.split('.')[0]).first() - if release_obj: - bundle_id = release_obj.app_id.bundle_id - udid_url = get_post_udid_url(request, release_obj.app_id.short) - ios_udid_mobileconfig = make_udid_mobileconfig(udid_url, bundle_id) - response = FileResponse(ios_udid_mobileconfig) - response['Content-Type'] = "application/x-apple-aspen-config" - response['Content-Disposition'] = 'attachment; filename=' + make_random_uuid() + '.mobileconfig' - return response - res.msg = "mobileconifg release_id error" res.code = 1004 res.msg = "token校验失败" @@ -166,7 +165,7 @@ class InstallView(APIView): udid=udid) res.data = {"download_url": download_url} - if download_url != "" and not download_url.endswith("mobileconifg"): + if download_url != "" and "mobileconifg" not in download_url: set_app_download_by_cache(app_id) return Response(res.dict) else: diff --git a/fir_ser/api/views/receiveudids.py b/fir_ser/api/views/receiveudids.py index 800c67d..dcd05bb 100644 --- a/fir_ser/api/views/receiveudids.py +++ b/fir_ser/api/views/receiveudids.py @@ -15,6 +15,7 @@ class IosUDIDView(View): def post(self, request, short): stream_f = str(request.body) format_udid_info = udid_bytes_to_dict(stream_f) + server_domain = get_redirect_server_domain(request) try: app_info = Apps.objects.filter(short=short).first() @@ -25,11 +26,11 @@ class IosUDIDView(View): ios_obj = IosUtils(format_udid_info, app_info.user_id, app_info) ios_obj.resign() else: - return Http404 + return HttpResponsePermanentRedirect( + "%s/%s" % (server_domain, short)) else: - return Http404 - + return HttpResponsePermanentRedirect( + "%s/%s" % (server_domain, short)) except Exception as e: print(e) - server_domain = get_redirect_server_domain(request) return HttpResponsePermanentRedirect("%s/%s?udid=%s" % (server_domain, short, format_udid_info.get("udid"))) diff --git a/fir_ser/fir_ser/settings.py b/fir_ser/fir_ser/settings.py index 03aa346..b3f2830 100644 --- a/fir_ser/fir_ser/settings.py +++ b/fir_ser/fir_ser/settings.py @@ -236,6 +236,8 @@ CACHE_KEY_TEMPLATE = { 'developer_auth_code_key': 'developer_auth_code', } +DATA_DOWNLOAD_KEY="d_token" + SYNC_CACHE_TO_DATABASE = { 'download_times': 10 # 下载次数同步时间 }