:art: Qiniu Uncompressed

huangqimin001 1 月之前
父节点
当前提交
38e1ca68da
共有 1 个文件被更改,包括 38 次插入0 次删除
  1. 38 0
      commands/management/commands/qiniuuncompressed.py

+ 38 - 0
commands/management/commands/qiniuuncompressed.py

@@ -0,0 +1,38 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+import os
4
+
5
+from django_six import CompatibilityBaseCommand
6
+
7
+from utils.qiniucdn import upload_file_path
8
+
9
+
10
+class Command(CompatibilityBaseCommand):
11
+    def handle(self, *args, **options):
12
+
13
+        dirs = ['media/user/member_activity/7YmTP98A9qBdtTg5VdxTv5/contribution/edbFSK5', 'media/qn/test']
14
+
15
+        for dir in dirs:
16
+            try:
17
+                files = os.listdir(dir)
18
+            except Exception as e:
19
+                print(e)
20
+                continue
21
+
22
+            for file in files:
23
+                print(file)
24
+                ext = file.split('.')[-1]
25
+                if not ext:
26
+                    continue
27
+                if ext.lower() not in ['jpg', 'jpeg', 'png', 'heic']:
28
+                    continue
29
+                try:
30
+                    file_path = '{}/{}'.format(dir, file)
31
+                    if os.path.exists(file_path):
32
+                        qiniu_key = 'uncompressed/{}'.format(file_path)
33
+                        # print(file_path)
34
+                        # print(qiniu_key)
35
+                        upload_file_path(file_path, key=qiniu_key, bucket='tamron', compress=False)
36
+                        os.remove(file_path)
37
+                except Exception:
38
+                    pass