You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
7.9 KiB
188 lines
7.9 KiB
5 years ago
|
#!/usr/bin/env python
|
||
|
# -*- coding:utf-8 -*-
|
||
|
# project: 3月
|
||
|
# author: liuyu
|
||
|
# date: 2020/3/23
|
||
|
|
||
3 years ago
|
import logging
|
||
|
import time
|
||
|
|
||
3 years ago
|
from common.base.baseutils import get_dict_from_filter_fields
|
||
|
from common.base.magic import run_function_by_locker
|
||
|
from common.cache.storage import CloudStorageCache, LocalStorageCache, DownloadUrlCache
|
||
3 years ago
|
from common.core.sysconfig import Config
|
||
|
from common.libs.storage.aliyunApi import AliYunOss, AliYunCdn
|
||
|
from common.libs.storage.localApi import LocalStorage
|
||
|
from common.libs.storage.qiniuApi import QiNiuOss
|
||
5 years ago
|
|
||
3 years ago
|
logger = logging.getLogger(__name__)
|
||
5 years ago
|
|
||
|
|
||
3 years ago
|
def get_storage_auth(storage_obj):
|
||
|
filter_fields = ['access_key', 'secret_key', 'bucket_name', 'domain_name', 'is_https', 'endpoint',
|
||
|
'sts_role_arn', 'cnd_auth_key', 'download_auth_type']
|
||
|
return get_dict_from_filter_fields(filter_fields, storage_obj.__dict__)
|
||
|
|
||
|
|
||
3 years ago
|
@run_function_by_locker()
|
||
3 years ago
|
def get_storage(user, assigned_storage_obj, use_default_storage):
|
||
|
if use_default_storage:
|
||
|
return get_storage_form_conf(user)
|
||
|
if assigned_storage_obj:
|
||
|
storage_obj = assigned_storage_obj
|
||
|
else:
|
||
|
storage_obj = user.storage
|
||
|
|
||
|
if storage_obj:
|
||
|
auth = get_storage_auth(storage_obj)
|
||
|
storage_type = storage_obj.storage_type
|
||
3 years ago
|
storage_cache = CloudStorageCache(auth, user.uid)
|
||
|
storage_key, new_storage_obj = storage_cache.get_storage_key_and_cache()
|
||
|
|
||
3 years ago
|
if new_storage_obj and not assigned_storage_obj:
|
||
3 years ago
|
logger.info(f"user {user} get storage obj {storage_key} cache {new_storage_obj}")
|
||
3 years ago
|
return new_storage_obj
|
||
|
else:
|
||
|
if storage_type == 1:
|
||
|
new_storage_obj = QiNiuOss(**auth)
|
||
|
elif storage_type == 2:
|
||
|
new_storage_obj = AliYunOss(**auth)
|
||
|
else:
|
||
|
new_storage_obj = LocalStorage(**auth)
|
||
3 years ago
|
logger.warning(f"user {user} make storage obj key:{storage_key} obj: {new_storage_obj}")
|
||
3 years ago
|
new_storage_obj.storage_type = storage_type
|
||
3 years ago
|
storage_cache.set_storage_cache(new_storage_obj, 600)
|
||
3 years ago
|
return new_storage_obj
|
||
|
else:
|
||
3 years ago
|
logger.info(f"user {user} has not storage obj, so get default")
|
||
3 years ago
|
# 不需要管理存储,直接从配置文件获取默认存储
|
||
|
return get_storage_form_conf(user)
|
||
|
|
||
|
|
||
5 years ago
|
class Storage(object):
|
||
3 years ago
|
def __init__(self, user, assigned_storage_obj=None, use_default_storage=False):
|
||
5 years ago
|
try:
|
||
3 years ago
|
locker = {
|
||
|
'locker_key': f"make_storage_cache_{user.uid}",
|
||
|
"timeout": 60 * 5, "blocking_timeout": 6}
|
||
|
self.storage = get_storage(user, assigned_storage_obj, use_default_storage, locker=locker)
|
||
5 years ago
|
except Exception as e:
|
||
3 years ago
|
logger.error(f"get {user} storage failed Exception:{e}")
|
||
5 years ago
|
self.storage = None
|
||
5 years ago
|
|
||
5 years ago
|
def get_upload_token(self, filename, expires=900):
|
||
5 years ago
|
if self.storage:
|
||
|
return self.storage.get_upload_token(filename, expires)
|
||
|
|
||
5 years ago
|
def get_download_url(self, filename, expires=900, key='', force_new=False):
|
||
5 years ago
|
if self.storage:
|
||
5 years ago
|
now = time.time()
|
||
3 years ago
|
download_cache = DownloadUrlCache(key, filename)
|
||
|
download_val = download_cache.get_storage_cache()
|
||
5 years ago
|
if download_val and not force_new:
|
||
5 years ago
|
if download_val.get("time") > now - 60:
|
||
|
return download_val.get("download_url")
|
||
4 years ago
|
if self.storage.__dict__.get("download_auth_type", None) == 2:
|
||
|
cdn_obj = AliYunCdn(self.storage.cnd_auth_key, self.storage.is_https, self.storage.domain_name)
|
||
|
download_url = cdn_obj.get_cdn_download_token(filename, expires)
|
||
|
else:
|
||
|
download_url = self.storage.get_download_url(filename, expires, force_new=True)
|
||
3 years ago
|
download_cache.set_storage_cache({"download_url": download_url, "time": now + expires}, expires)
|
||
5 years ago
|
return download_url
|
||
5 years ago
|
|
||
3 years ago
|
def delete_file(self, filename, app_type=None):
|
||
5 years ago
|
if self.storage:
|
||
3 years ago
|
if app_type is not None:
|
||
|
if app_type == 0:
|
||
5 years ago
|
filename = filename + '.apk'
|
||
|
else:
|
||
|
filename = filename + '.ipa'
|
||
5 years ago
|
try:
|
||
3 years ago
|
logger.warning(f"storage {self.storage} delete file {filename}")
|
||
5 years ago
|
return self.storage.del_file(filename)
|
||
|
except Exception as e:
|
||
3 years ago
|
logger.error(f"delete file {filename} failed Exception {e}")
|
||
5 years ago
|
|
||
3 years ago
|
def rename_file(self, old_filename, new_filename):
|
||
5 years ago
|
if self.storage:
|
||
5 years ago
|
try:
|
||
3 years ago
|
return self.storage.rename_file(old_filename, new_filename)
|
||
5 years ago
|
except Exception as e:
|
||
3 years ago
|
logger.error(f"rename {old_filename} to {new_filename} failed Exception {e}")
|
||
5 years ago
|
|
||
5 years ago
|
def upload_file(self, local_file_full_path):
|
||
|
if self.storage:
|
||
|
try:
|
||
|
return self.storage.upload_file(local_file_full_path)
|
||
|
except Exception as e:
|
||
3 years ago
|
logger.error(f"oss upload {local_file_full_path} failed Exception {e}")
|
||
5 years ago
|
|
||
4 years ago
|
def download_file(self, file_name, local_file_full_path):
|
||
|
if self.storage:
|
||
|
try:
|
||
|
return self.storage.download_file(file_name, local_file_full_path)
|
||
|
except Exception as e:
|
||
3 years ago
|
logger.error(f"oss download {local_file_full_path} failed Exception {e}")
|
||
4 years ago
|
|
||
5 years ago
|
def get_storage_type(self):
|
||
5 years ago
|
if self.storage:
|
||
|
return self.storage.storage_type
|
||
5 years ago
|
|
||
3 years ago
|
def get_storage_uuid(self):
|
||
|
if self.storage:
|
||
|
if self.storage.storage_type == 1:
|
||
|
return self.storage.access_key
|
||
|
elif self.storage.storage_type == 2:
|
||
|
return self.storage.access_key_id
|
||
|
else:
|
||
|
return self.storage.domain_name
|
||
|
return id(self)
|
||
|
|
||
4 years ago
|
|
||
|
def get_local_storage(clean_cache=False):
|
||
3 years ago
|
for storage in Config.STORAGE:
|
||
4 years ago
|
storage_type = storage.get('type', None)
|
||
|
if storage_type == 0:
|
||
|
auth = storage.get('auth', {})
|
||
3 years ago
|
storage_cache = LocalStorageCache(auth, "_system_")
|
||
|
storage_key, new_storage_obj = storage_cache.get_storage_key_and_cache()
|
||
4 years ago
|
if clean_cache:
|
||
3 years ago
|
logger.info(f"system clean local storage obj cache storage_key {storage_key}")
|
||
3 years ago
|
storage_cache.del_storage_cache()
|
||
4 years ago
|
if new_storage_obj:
|
||
3 years ago
|
logger.info(f"system get local storage obj cache {new_storage_obj}")
|
||
4 years ago
|
return new_storage_obj
|
||
|
else:
|
||
|
new_storage_obj = LocalStorage(**auth)
|
||
|
new_storage_obj.storage_type = 3
|
||
3 years ago
|
storage_cache.set_storage_cache(new_storage_obj, 600)
|
||
3 years ago
|
logger.info(f"system get local storage obj, from settings storage {new_storage_obj}")
|
||
4 years ago
|
return new_storage_obj
|
||
3 years ago
|
|
||
|
|
||
|
def get_storage_form_conf(user):
|
||
3 years ago
|
for storage in Config.STORAGE:
|
||
3 years ago
|
if storage.get("active", None):
|
||
|
storage_type = storage.get('type', None)
|
||
|
auth = storage.get('auth', {})
|
||
3 years ago
|
storage_cache = CloudStorageCache(auth, 'default')
|
||
|
storage_key, new_storage_obj = storage_cache.get_storage_key_and_cache()
|
||
3 years ago
|
if new_storage_obj:
|
||
3 years ago
|
logger.info(f"user {user} get default storage {storage_key} obj cache {new_storage_obj} ")
|
||
3 years ago
|
return new_storage_obj
|
||
|
else:
|
||
|
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
|
||
3 years ago
|
storage_cache.set_storage_cache(new_storage_obj, 600)
|
||
3 years ago
|
logger.warning(
|
||
|
f"user {user} has not storage obj, from settings get default storage key:{storage_key} obj:{new_storage_obj}")
|
||
3 years ago
|
return new_storage_obj
|
||
|
return None
|