或许修复了下载token失效问题

super_signature
nineven 5 years ago
parent 9d8a28ff9a
commit 42de66927f
  1. 4
      fir_ser/api/utils/TokenManager.py
  2. 4
      fir_ser/api/utils/serializer.py
  3. 2
      fir_ser/api/utils/storage/aliyunApi.py
  4. 4
      fir_ser/api/utils/storage/localApi.py
  5. 2
      fir_ser/api/utils/storage/qiniuApi.py
  6. 6
      fir_ser/api/utils/storage/storage.py

@ -18,10 +18,10 @@ from fir_ser.settings import CACHE_KEY_TEMPLATE
class DownloadToken(object):
def make_token(self,release_id,time_limit=60,key=''):
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:
if make_token_key and not force_new:
return make_token_key
else:
random_str = uuid.uuid1().__str__().split("-")[0:-1]

@ -113,7 +113,7 @@ class AppsShortSerializer(serializers.ModelSerializer):
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,key=key)
icon_url = storage.get_download_url(os.path.basename(master_release_obj.icon_url),600,key=key,force_new=True)
datainfo = {
"app_version": master_release_obj.app_version,
"icon_url": icon_url,
@ -126,7 +126,7 @@ class AppsShortSerializer(serializers.ModelSerializer):
"binary_url":master_release_obj.binary_url,
}
download_token = token_obj.make_token(master_release_obj.release_id,600,key=key)
download_token = token_obj.make_token(master_release_obj.release_id,600,key=key,force_new=True)
datainfo["download_token"] = download_token
return datainfo
else:

@ -109,7 +109,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):
def get_download_url(self,name,expires=1800,ftype=None,force_new=False):
# self.get_auth_bucket(name,expires)
private_url=self.bucket.sign_url('GET',name,expires)
return private_url

@ -19,13 +19,13 @@ class LocalStorage(object):
dtoken = DownloadToken()
return dtoken.make_token(name,expires)
def get_download_url(self,name,expires=1800,ftype=None):
def get_download_url(self,name,expires=1800,ftype=None,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)
download_url = uri+ base_url + "?token=" + dtoken.make_token(name,expires,force_new)
if ftype:
download_url = download_url + '&ftype=' + ftype
return download_url

@ -32,7 +32,7 @@ class QiNiuOss(object):
# print(token)
return token
def get_download_url(self,name,expires=1800,ftype=None):
def get_download_url(self,name,expires=1800,ftype=None,force_new=False):
#有两种方式构造base_url的形式
uri = 'http://'
if self.is_https:

@ -24,16 +24,16 @@ 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='',force_new=False):
if self.storage:
now = time.time()
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 and not force_new:
if download_val.get("time") > now - 60:
return download_val.get("download_url")
download_url = self.storage.get_download_url(filename, expires, ftype)
download_url = self.storage.get_download_url(filename, expires, ftype,force_new=False)
cache.set(down_key, {"download_url": download_url, "time": now + expires}, expires)
return download_url

Loading…
Cancel
Save