file md5

Brightcells преди 8 години
родител
ревизия
90fd7187d8
променени са 5 файла, в които са добавени 21 реда и са изтрити 44 реда
  1. 0 6
      group/lensman_views.py
  2. 0 2
      group/views.py
  3. 0 1
      pai2/urls_api.py
  4. 10 31
      photo/views.py
  5. 11 4
      utils/storage_utils.py

+ 0 - 6
group/lensman_views.py

@@ -2,11 +2,7 @@
2 2
 
3 3
 from __future__ import division
4 4
 
5
-import os
6
-
7
-import shortuuid
8 5
 from curtail_uuid import CurtailUUID
9
-from django.conf import settings
10 6
 from django.contrib.auth.hashers import check_password
11 7
 from django.db import transaction
12 8
 from isoweek import Week
@@ -30,8 +26,6 @@ from utils.redis.rorder import set_lensman_order_record
30 26
 from utils.redis.rprice import get_lensman_price_fixed, set_lensman_price_fixed
31 27
 from utils.redis.rprofile import set_profile_info
32 28
 from utils.storage_utils import file_save
33
-from utils.thumbnail_utils import make_thumbnail
34
-from utils.watermark_utils import watermark_wrap
35 29
 
36 30
 
37 31
 @logit

+ 0 - 2
group/views.py

@@ -2,7 +2,6 @@
2 2
 
3 3
 from __future__ import division
4 4
 
5
-import os
6 5
 import random
7 6
 
8 7
 from curtail_uuid import CurtailUUID
@@ -32,7 +31,6 @@ from utils.redis.rorder import get_lensman_order_record
32 31
 from utils.redis.rprice import get_lensman_price_fixed
33 32
 from utils.sql.raw import PAI2_HOME_API
34 33
 from utils.storage_utils import file_save
35
-from utils.thumbnail_utils import make_thumbnail
36 34
 from utils.time_utils import origin_expired_stamps
37 35
 from utils.url_utils import img_url, share_url
38 36
 

+ 0 - 1
pai2/urls_api.py

@@ -1,6 +1,5 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 
3
-from django.conf import settings
4 3
 from django.conf.urls import include, url
5 4
 
6 5
 

+ 10 - 31
photo/views.py

@@ -2,9 +2,7 @@
2 2
 
3 3
 import os
4 4
 
5
-import shortuuid
6 5
 from curtail_uuid import CurtailUUID
7
-from django.conf import settings
8 6
 from django.db import transaction
9 7
 from django.shortcuts import render
10 8
 from django_q.tasks import async
@@ -25,8 +23,6 @@ from utils.redis.rkeys import (GROUP_LAST_PHOTO_PK, GROUP_USERS_DELETED_SET, GRO
25 23
                                GROUP_USERS_QUIT_SET, GROUP_USERS_REFUSED_SET, UUID_LIST)
26 24
 from utils.redis.ruuid import generate_uuids, update_uuids
27 25
 from utils.storage_utils import file_save
28
-from utils.thumbnail_utils import make_thumbnail
29
-from utils.watermark_utils import watermark_wrap
30 26
 
31 27
 
32 28
 @logit
@@ -163,24 +159,7 @@ def session_join_api(request):
163 159
     #
164 160
     if group_created:
165 161
         for photo in photos:
166
-            photo_path = photo.p_photo_path
167
-            photo_thumbnail_path = photo_path.replace('.', '_thumbnail.')
168
-            photo_thumbnail2_path = photo_path.replace('.', '_thumbnail2.')
169
-
170
-            # 群组照片缩略图生成
171
-            # 双列: 540, 40-50K
172
-            photo_w, photo_h, photo_thumbnail_w, photo_thumbnail_h = make_thumbnail(
173
-                os.path.join(settings.MEDIA_ROOT, photo_path).replace('\\', '/'),
174
-                os.path.join(settings.MEDIA_ROOT, photo_thumbnail_path).replace('\\', '/'),
175
-                settings.THUMBNAIL_MAX_WIDTH
176
-            )
177
-
178
-            # 单列: 1080, xx-100K
179
-            photo_w, photo_h, photo_thumbnail2_w, photo_thumbnail2_h = make_thumbnail(
180
-                os.path.join(settings.MEDIA_ROOT, photo_path).replace('\\', '/'),
181
-                os.path.join(settings.MEDIA_ROOT, photo_thumbnail2_path).replace('\\', '/'),
182
-                settings.THUMBNAIL_MAX_WIDTH2
183
-            )
162
+            photo_info = file_save(photo_path=photo.p_photo_path, thumbnail=True)
184 163
 
185 164
             # 群组照片记录创建
186 165
             group_photo = GroupPhotoInfo.objects.create(
@@ -188,15 +167,15 @@ def session_join_api(request):
188 167
                 user_id=user_id,
189 168
                 nickname=user.final_nickname,
190 169
                 avatar=user.avatar,
191
-                photo_path=photo_path,
192
-                photo_w=photo_w,
193
-                photo_h=photo_h,
194
-                photo_thumbnail_path=photo_thumbnail_path,
195
-                photo_thumbnail_w=photo_thumbnail_w,
196
-                photo_thumbnail_h=photo_thumbnail_h,
197
-                photo_thumbnail2_path=photo_thumbnail2_path,
198
-                photo_thumbnail2_w=photo_thumbnail2_w,
199
-                photo_thumbnail2_h=photo_thumbnail2_h,
170
+                photo_path=photo_info.photo_path,
171
+                photo_w=photo_info.photo_w,
172
+                photo_h=photo_info.photo_h,
173
+                photo_thumbnail_path=photo_info.photo_thumbnail_path,
174
+                photo_thumbnail_w=photo_info.photo_thumbnail_w,
175
+                photo_thumbnail_h=photo_info.photo_thumbnail_h,
176
+                photo_thumbnail2_path=photo_info.photo_thumbnail2_path,
177
+                photo_thumbnail2_w=photo_info.photo_thumbnail2_w,
178
+                photo_thumbnail2_h=photo_info.photo_thumbnail2_h,
200 179
                 photo_from=GroupPhotoInfo.SESSION_GROUP,
201 180
                 session_id=photo.session_id,
202 181
                 lensman_id=photo.lensman_id,

+ 11 - 4
utils/storage_utils.py

@@ -22,21 +22,27 @@ class DotDict(dict):
22 22
 
23 23
 
24 24
 @transaction.atomic
25
-def file_save(file_, prefix='img', ext='jpeg', watermark=False, thumbnail=False):
26
-    ext = os.path.splitext(file_.name)[-1] or ext
25
+def file_save(photo=None, photo_path=None, prefix='img', ext='jpeg', watermark=False, thumbnail=False):
26
+    # Photo
27
+    photo = photo or default_storage.open(photo_path)
27 28
 
28
-    photo, created = PhotoUUIDInfo.objects.select_for_update().get_or_create(photo_md5=calculate_md5(file_))
29
+    # Ext
30
+    ext = os.path.splitext(photo.name)[-1] or ext
29 31
 
32
+    photo, created = PhotoUUIDInfo.objects.select_for_update().get_or_create(photo_md5=calculate_md5(photo))
33
+
34
+    # 照片路径
30 35
     photo_path = photo.photo_path
31 36
     if not photo_path:
32 37
         photo_path = '{}/{}{}'.format(prefix, shortuuid.uuid(), ext)
33 38
         if default_storage.exists(photo_path):
34 39
             default_storage.delete(photo_path)
35
-        default_storage.save(photo_path, file_)
40
+        default_storage.save(photo_path, photo)
36 41
 
37 42
         photo.photo_path = photo_path
38 43
         photo.save()
39 44
 
45
+    # 水印
40 46
     if watermark:
41 47
         if not photo.photo_watermark_path:
42 48
             photo_watermark_path = 'photo/{}{}'.format(shortuuid.uuid(), ext)
@@ -48,6 +54,7 @@ def file_save(file_, prefix='img', ext='jpeg', watermark=False, thumbnail=False)
48 54
             photo.photo_watermark_path
49 55
             photo.save()
50 56
 
57
+    # 缩略图
51 58
     if thumbnail:
52 59
         if not photo.photo_thumbnail_path:
53 60
             # 双列: 540, 40-50K