>
@@ -135,12 +134,7 @@ def group_update_api(request):
group.group_desc = group_desc
# 群组头像更新
if group_avatar:
- _, extension = os.path.splitext(group_avatar.name)
- group_avatar_path = 'group/{uuid}_{extension}'.format(uuid=shortuuid.uuid(), extension=extension)
- if default_storage.exists(group_avatar_path):
- default_storage.delete(group_avatar_path)
- default_storage.save(group_avatar_path, group_avatar)
- group.group_avatar = group_avatar_path
+ group.group_avatar = file_save(group_avatar, prefix='group', ext='jpeg')[0]
group.save()
# Redis 群组数据缓存更新
@@ -260,18 +254,10 @@ def flyimg_upload_api(request):
return response(GroupUserStatusCode.GROUP_USER_NOT_FOUND)
if photo:
- photo_path = 'fly/{uuid}{extension}'.format(uuid=shortuuid.uuid(), extension=os.path.splitext(photo.name)[1] or 'jpeg')
+ photo_path, ext = file_save(photo, prefix='fly', ext='jpeg')
photo_thumbnail_path = photo_path.replace('.', '_thumbnail.')
photo_thumbnail2_path = photo_path.replace('.', '_thumbnail2.')
- if default_storage.exists(photo_path):
- default_storage.delete(photo_path)
- default_storage.save(photo_path, photo)
-
- # if default_storage.exists(photo_thumbnail_path):
- # default_storage.delete(photo_thumbnail_path)
- # default_storage.save(photo_thumbnail_path, photo)
-
# 群组照片缩略图生成
# 双列: 540, 40-50K
photo_w, photo_h, photo_thumbnail_w, photo_thumbnail_h = make_thumbnail(
@@ -5,7 +5,6 @@ import os |
||
| 5 | 5 |
import shortuuid |
| 6 | 6 |
from curtail_uuid import CurtailUUID |
| 7 | 7 |
from django.conf import settings |
| 8 |
-from django.core.files.storage import default_storage |
|
| 9 | 8 |
from django.db import transaction |
| 10 | 9 |
from django.shortcuts import render |
| 11 | 10 |
from django_q.tasks import async |
@@ -24,6 +23,7 @@ from utils.redis.rgroup import get_group_info, set_group_info, set_group_users_i |
||
| 24 | 23 |
from utils.redis.rkeys import (GROUP_LAST_PHOTO_PK, GROUP_USERS_DELETED_SET, GROUP_USERS_PASSED_SET, |
| 25 | 24 |
GROUP_USERS_QUIT_SET, GROUP_USERS_REFUSED_SET, UUID_LIST) |
| 26 | 25 |
from utils.redis.ruuid import generate_uuids, update_uuids |
| 26 |
+from utils.storage_utils import file_save |
|
| 27 | 27 |
from utils.thumbnail_utils import make_thumbnail |
| 28 | 28 |
from utils.watermark_utils import watermark_wrap |
| 29 | 29 |
|
@@ -93,18 +93,9 @@ def upload_photo(request): |
||
| 93 | 93 |
except LensmanInfo.DoesNotExist: |
| 94 | 94 |
return response(LensmanStatusCode.LENSMAN_NOT_FOUND) |
| 95 | 95 |
|
| 96 |
- # photo_id = curtailUUID(PhotosInfo, 'photo_id') |
|
| 96 |
+ m_photo_path, ext = file_save(photo, prefix='photo', ext='jpeg') |
|
| 97 | 97 |
|
| 98 |
- _, extension = os.path.splitext(photo.name) |
|
| 99 |
- extension = extension or 'jpeg' |
|
| 100 |
- |
|
| 101 |
- m_photo_path = 'photo/{uuid}{extension}'.format(uuid=shortuuid.uuid(), extension=extension)
|
|
| 102 |
- |
|
| 103 |
- if default_storage.exists(m_photo_path): |
|
| 104 |
- default_storage.delete(m_photo_path) |
|
| 105 |
- default_storage.save(m_photo_path, photo) |
|
| 106 |
- |
|
| 107 |
- p_photo_path = 'photo/{uuid}{extension}'.format(uuid=shortuuid.uuid(), extension=extension)
|
|
| 98 |
+ p_photo_path = 'photo/{}{}'.format(shortuuid.uuid(), ext)
|
|
| 108 | 99 |
watermark_wrap( |
| 109 | 100 |
os.path.join(settings.MEDIA_ROOT, m_photo_path).replace('\\', '/'),
|
| 110 | 101 |
settings.WATERMARK_LOGO, |
@@ -0,0 +1,15 @@ |
||
| 1 |
+# -*- coding: utf-8 -*- |
|
| 2 |
+ |
|
| 3 |
+import os |
|
| 4 |
+ |
|
| 5 |
+import shortuuid |
|
| 6 |
+from django.core.files.storage import default_storage |
|
| 7 |
+ |
|
| 8 |
+ |
|
| 9 |
+def file_save(file_, prefix='img', ext='jpeg'): |
|
| 10 |
+ ext = os.path.splitext(file_.name)[-1] or ext |
|
| 11 |
+ path = '{}/{}{}'.format(prefix, shortuuid.uuid(), ext)
|
|
| 12 |
+ if default_storage.exists(path): |
|
| 13 |
+ default_storage.delete(path) |
|
| 14 |
+ default_storage.save(path, file_) |
|
| 15 |
+ return path, ext |