Browse Source

优化云存转移云盘接口

peng 2 years ago
parent
commit
7533dd45d8
1 changed files with 33 additions and 12 deletions
  1. 33 12
      Controller/IcloudService/IcloudService.py

+ 33 - 12
Controller/IcloudService/IcloudService.py

@@ -4,6 +4,7 @@
 @Time : 2023-6-7 18:26:35
 @File :IcloudMeal.py
 """
+import logging
 import time
 
 from django.db.models import Sum, Q
@@ -19,6 +20,8 @@ from Ansjer.config import ACCESS_KEY_ID, SECRET_ACCESS_KEY, REGION_NAME, SERVER_
     AWS_SECRET_ACCESS_KEY
 from Service.VodHlsService import SplitVodHlsObject
 
+logger = logging.getLogger('info')
+
 
 class IcloudServiceView(View):
     def get(self, request, *args, **kwargs):
@@ -55,6 +58,7 @@ class IcloudServiceView(View):
         @param response: 响应对象
         @return: response
         """
+        logger.info('开始云存转移到云盘')
         uid = request_dict.get('uid', None)
         channel = request_dict.get('channel', None)
         start_time = request_dict.get('start_time', None)
@@ -69,7 +73,7 @@ class IcloudServiceView(View):
             return response.json(173)
         use_details_id = use_details_qs[0]['id']
         target_bucket_id = use_details_qs[0]['bucket_id']
-        use_size = use_details_qs[0]['use_size']
+        use_size = float(use_details_qs[0]['use_size'])
         now_time = int(time.time())
         nickname = device_qs[0]['NickName']
         # 判断云盘是否还有容量
@@ -78,11 +82,13 @@ class IcloudServiceView(View):
             'size').aggregate(total_size=Sum('size'))['total_size']
         all_size = all_size * 1024 if all_size else 0
         if use_size > all_size:
+            logger.info('{}用户套餐总容量为:{},已使用容量为:{}'.format(uid, all_size, use_size))
             return response.json(910)
         split_vod_hls_obj = SplitVodHlsObject()
         vod_hls = split_vod_hls_obj.get_vod_hls_data(uid=uid, channel=channel, start_time=start_time).values(
             'bucket_id', 'fg', 'sec')
         if not vod_hls.exists():
+            logger.info('{}用户查无此云存:{}'.format(uid, start_time))
             return response.json(173)
         source_bucket_id = vod_hls[0]['bucket_id']
         fg = int(vod_hls[0]['fg'])
@@ -114,18 +120,33 @@ class IcloudServiceView(View):
                                                                           i=i)
                     ts_list.append(ts_file)
                     ts_size += s3_obj.get_object_size(source_bucket_name, ts_file)  # 获取存储对象字节,单位B
-            ts_size = ts_size/1024/1024
-            temp_size = round(ts_size + use_size, 2)
+            ts_size = round(ts_size / 1024 / 1024, 2)  # 字节转换为MB单位
+            temp_size = ts_size + use_size
             if temp_size > all_size:
+                logger.info('{}用户无空间上传,套餐容量为:{},使用容量为:{}'.format(uid, all_size, temp_size))
                 return response.json(910)
-            for source_key in ts_list:
-                target_key = '{user_id}/ts_file/'.format(user_id=user_id) + source_key
-                s3_obj.copy_single_obj(source_bucket_name, source_key, target_bucket_name, target_key)
-            IcloudStorageRecord.objects.create(user_id=user_id, uid=uid, channel=channel, time_stamp=start_time,
-                                               nickname=nickname,
-                                               sec=sec, bucket_id=target_bucket_id, fg=fg, size=ts_size, store_type=1)
-            use_details_qs.update(use_size=temp_size)
-            return response.json(0)
+            start_time = int(start_time) * 1000  # 转换单位为毫秒
+            icloud_record_qs = IcloudStorageRecord.objects.filter(user_id=user_id, uid=uid, channel=channel,
+                                                                  time_stamp=start_time,
+                                                                  size=ts_size, store_type=1)
+            if not icloud_record_qs.exists():
+                for source_key in ts_list:
+                    ts_name = source_key.split('/')[-1]
+                    target_key = '{user_id}/ts_file/{uid}/vod{channel}/{time}/{ts_name}'.format(user_id=user_id,
+                                                                                                uid=uid,
+                                                                                                channel=channel,
+                                                                                                time=start_time,
+                                                                                                ts_name=ts_name)
+                    s3_obj.copy_single_obj(source_bucket_name, source_key, target_bucket_name, target_key)
+
+                IcloudStorageRecord.objects.create(user_id=user_id, uid=uid, channel=channel, time_stamp=start_time,
+                                                   nickname=nickname,
+                                                   sec=sec, bucket_id=target_bucket_id, fg=fg, size=ts_size,
+                                                   store_type=1)
+                use_details_qs.update(use_size=temp_size)
+                return response.json(0)
+            else:
+                return response.json(174)
         except Exception as e:
-            print(e)
+            logger.info('云存转移云盘异常:{}'.format(repr(e)))
             return response.json(500)