修复bug,后端增加 存储迁移和清理方法

pull/5/head
nineven 4 years ago
parent f902020079
commit 641b7643de
  1. 39
      fir_ser/api/utils/utils.py
  2. 8
      fir_ser/api/views/storage.py

@ -241,7 +241,7 @@ def check_storage_additionalparameter(request, res):
except Exception as e: except Exception as e:
logger.error("user:%s additionalparameters %s dumps failed Exception:%s" % ( logger.error("user:%s additionalparameters %s dumps failed Exception:%s" % (
request.user, data.get('additionalparameter', ''), e)) request.user, data.get('additionalparameter', ''), e))
return True, res return True, data
def change_storage_and_change_head_img(user_obj, new_storage_obj): def change_storage_and_change_head_img(user_obj, new_storage_obj):
@ -256,20 +256,22 @@ def download_files_form_oss(storage_obj, org_file):
else: else:
logger.error("download file %s failed %s" % (org_file, req.content)) logger.error("download file %s failed %s" % (org_file, req.content))
return False return False
with open(org_file + ".check.tmp", "wb") as f:
f.write(req.content)
try: try:
with open(org_file + ".check.tmp", "wb") as f:
for chunk in req.iter_content(chunk_size=5120):
if chunk:
f.write(chunk)
logger.info("save download file %s success" % org_file)
if os.path.isfile(org_file): if os.path.isfile(org_file):
os.remove(org_file) os.remove(org_file)
os.rename(os.path.join(org_file + ".check.tmp"), org_file) os.rename(os.path.join(org_file + ".check.tmp"), org_file)
return True return True
except Exception as e: except Exception as e:
logger.error("check org file and mv file %s failed Exception %s" % (org_file, e)) logger.error("check download file and move file %s failed Exception %s" % (org_file, e))
return False return False
def migrating_storage_file_data(user_obj, filename, new_storage_obj): def migrating_storage_file_data(user_obj, filename, new_storage_obj, clean_old_data=True):
local_file_full_path = os.path.join(MEDIA_ROOT, filename) local_file_full_path = os.path.join(MEDIA_ROOT, filename)
old_storage_obj = Storage(user_obj) old_storage_obj = Storage(user_obj)
if not new_storage_obj: if not new_storage_obj:
@ -279,25 +281,46 @@ def migrating_storage_file_data(user_obj, filename, new_storage_obj):
if old_storage_obj.get_storage_type() == 3: if old_storage_obj.get_storage_type() == 3:
if new_storage_obj.get_storage_type() == 3: if new_storage_obj.get_storage_type() == 3:
# 都是本地存储,无需操作
pass pass
else: else:
# 本地向云存储上传,并删除本地数据 # 本地向云存储上传,并删除本地数据
new_storage_obj.upload_file(local_file_full_path) new_storage_obj.upload_file(local_file_full_path)
if clean_old_data:
delete_local_files(filename) delete_local_files(filename)
else: else:
if new_storage_obj.get_storage_type() == 3: if new_storage_obj.get_storage_type() == 3:
# 云存储下载 本地,并删除云存储 # 云存储下载 本地,并删除云存储
if download_files_form_oss(old_storage_obj, local_file_full_path): if download_files_form_oss(old_storage_obj, local_file_full_path):
if clean_old_data:
old_storage_obj.delete_file(filename) old_storage_obj.delete_file(filename)
else: else:
# 云存储互传,先下载本地,然后上传新云存储,删除本地和老云存储 # 云存储互传,先下载本地,然后上传新云存储,删除本地和老云存储
if download_files_form_oss(old_storage_obj, local_file_full_path): if download_files_form_oss(old_storage_obj, local_file_full_path):
new_storage_obj.upload_file(local_file_full_path) new_storage_obj.upload_file(local_file_full_path)
delete_local_files(filename) delete_local_files(filename)
if clean_old_data:
old_storage_obj.delete_file(filename) old_storage_obj.delete_file(filename)
def migrating_storage_data(user_obj, new_storage_obj): def migrating_storage_data(user_obj, new_storage_obj, clean_old_data):
for app_release_obj in AppReleaseInfo.objects.filter(app_id__user_id=user_obj).all(): for app_release_obj in AppReleaseInfo.objects.filter(app_id__user_id=user_obj).all():
#迁移APP数据
filename = get_filename_from_apptype(app_release_obj.release_id, app_release_obj.release_type) filename = get_filename_from_apptype(app_release_obj.release_id, app_release_obj.release_type)
migrating_storage_file_data(user_obj, filename, new_storage_obj) migrating_storage_file_data(user_obj, filename, new_storage_obj, clean_old_data)
migrating_storage_file_data(user_obj, app_release_obj.icon_url, new_storage_obj, clean_old_data)
#迁移超级签数据
for apptodev_obj in APPToDeveloper.objects.filter(app_id=app_release_obj.app_id).all():
filename = get_filename_from_apptype(apptodev_obj.binary_file, app_release_obj.release_type)
migrating_storage_file_data(user_obj, filename, new_storage_obj, clean_old_data)
def clean_storage_data(user_obj, storage_obj=None):
storage_obj = Storage(user_obj, storage_obj)
for app_release_obj in AppReleaseInfo.objects.filter(app_id__user_id=user_obj).all():
storage_obj.delete_file(app_release_obj.release_id, app_release_obj.release_type)
storage_obj.delete_file(app_release_obj.icon_url)
for apptodev_obj in APPToDeveloper.objects.filter(app_id=app_release_obj.app_id).all():
storage_obj.delete_file(apptodev_obj.binary_file, app_release_obj.release_type)

@ -59,9 +59,9 @@ class StorageView(APIView):
res = BaseResponse() res = BaseResponse()
data = request.data data = request.data
logger.info("user %s add new storage data:%s" % (request.user, data)) logger.info("user %s add new storage data:%s" % (request.user, data))
status, msg = check_storage_additionalparameter(request, res) status, data = check_storage_additionalparameter(request, res)
if not status: if not status:
return Response(msg.dict) return Response(data.dict)
serializer = StorageSerializer(data=data, context={'user_obj': request.user}) serializer = StorageSerializer(data=data, context={'user_obj': request.user})
if serializer.is_valid(): if serializer.is_valid():
storage_obj = serializer.save() storage_obj = serializer.save()
@ -116,9 +116,9 @@ class StorageView(APIView):
res.msg = '存储正在使用中,无法修改' res.msg = '存储正在使用中,无法修改'
res.code = 1007 res.code = 1007
return Response(res.dict) return Response(res.dict)
status, msg = check_storage_additionalparameter(request, res) status, data = check_storage_additionalparameter(request, res)
if not status: if not status:
return Response(msg.dict) return Response(data.dict)
storage_obj = AppStorage.objects.filter(id=storage_id, user_id=request.user).first() storage_obj = AppStorage.objects.filter(id=storage_id, user_id=request.user).first()
storage_obj_bak = AppStorage.objects.filter(id=storage_id, user_id=request.user).first() storage_obj_bak = AppStorage.objects.filter(id=storage_id, user_id=request.user).first()
serializer = StorageSerializer(instance=storage_obj, data=data, context={'user_obj': request.user}, serializer = StorageSerializer(instance=storage_obj, data=data, context={'user_obj': request.user},

Loading…
Cancel
Save