# -*- coding: utf-8 -*- import os import shortuuid from django.core.files.storage import default_storage from django.db import transaction from filemd5 import calculate_md5 from photo.models import PhotoUUIDInfo @transaction.atomic def file_save(file_, prefix='img', ext='jpeg'): ext = os.path.splitext(file_.name)[-1] or ext photo, created = PhotoUUIDInfo.objects.select_for_update().get_or_create(photo_md5=calculate_md5(file_)) if not photo.photo_path: path = '{}/{}{}'.format(prefix, shortuuid.uuid(), ext) if default_storage.exists(path): default_storage.delete(path) default_storage.save(path, file_) photo.photo_path = path photo.save() return photo.photo_path, ext