mirror of
https://github.com/haiwen/seahub.git
synced 2025-07-19 09:37:51 +00:00
Fix avatar png bug and clean code.
This commit is contained in:
parent
9c71c002a0
commit
c3296df1c1
192
avatar/models.py
192
avatar/models.py
@ -1,3 +1,4 @@
|
||||
from abc import abstractmethod
|
||||
import datetime
|
||||
import os
|
||||
|
||||
@ -8,7 +9,6 @@ from django.utils.hashcompat import md5_constructor
|
||||
from django.utils.encoding import smart_str
|
||||
from django.db.models import signals
|
||||
|
||||
#from django.contrib.auth.models import User
|
||||
from seahub.base.accounts import CcnetUser
|
||||
|
||||
try:
|
||||
@ -30,42 +30,24 @@ from avatar.settings import (AVATAR_STORAGE_DIR, AVATAR_RESIZE_METHOD,
|
||||
AVATAR_THUMB_QUALITY, AUTO_GENERATE_AVATAR_SIZES,
|
||||
GROUP_AVATAR_STORAGE_DIR)
|
||||
|
||||
def group_avatar_file_path(instance=None, filename=None, size=None, ext=None):
|
||||
tmppath = [GROUP_AVATAR_STORAGE_DIR]
|
||||
if AVATAR_HASH_USERDIRNAMES:
|
||||
tmp = md5_constructor(instance.user.username).hexdigest()
|
||||
tmppath.extend([tmp[0], tmp[1], instance.group_id])
|
||||
else:
|
||||
tmppath.append(instance.group_id)
|
||||
|
||||
if not filename:
|
||||
# Filename already stored in database
|
||||
filename = instance.avatar.name
|
||||
if ext and AVATAR_HASH_FILENAMES:
|
||||
# An extension was provided, probably because the thumbnail
|
||||
# is in a different format than the file. Use it. Because it's
|
||||
# only enabled if AVATAR_HASH_FILENAMES is true, we can trust
|
||||
# it won't conflict with another filename
|
||||
(root, oldext) = os.path.splitext(filename)
|
||||
filename = root + "." + ext
|
||||
else:
|
||||
# File doesn't exist yet
|
||||
if AVATAR_HASH_FILENAMES:
|
||||
(root, ext) = os.path.splitext(filename)
|
||||
filename = md5_constructor(smart_str(filename)).hexdigest()
|
||||
filename = filename + ext
|
||||
if size:
|
||||
tmppath.extend(['resized', str(size)])
|
||||
tmppath.append(os.path.basename(filename))
|
||||
return os.path.join(*tmppath)
|
||||
|
||||
def avatar_file_path(instance=None, filename=None, size=None, ext=None):
|
||||
tmppath = [AVATAR_STORAGE_DIR]
|
||||
if AVATAR_HASH_USERDIRNAMES:
|
||||
tmp = md5_constructor(instance.user.username).hexdigest()
|
||||
tmppath.extend([tmp[0], tmp[1], instance.emailuser])
|
||||
if isinstance(instance, Avatar):
|
||||
tmppath = [AVATAR_STORAGE_DIR]
|
||||
if AVATAR_HASH_USERDIRNAMES:
|
||||
tmp = md5_constructor(instance.user.username).hexdigest()
|
||||
tmppath.extend([tmp[0], tmp[1], instance.emailuser])
|
||||
else:
|
||||
tmppath.append(instance.emailuser)
|
||||
elif isinstance(instance, GroupAvatar):
|
||||
tmppath = [GROUP_AVATAR_STORAGE_DIR]
|
||||
if AVATAR_HASH_USERDIRNAMES:
|
||||
tmp = md5_constructor(instance.group_id).hexdigest()
|
||||
tmppath.extend([tmp[0], tmp[1], instance.group_id])
|
||||
else:
|
||||
tmppath.append(instance.group_id)
|
||||
else:
|
||||
tmppath.append(instance.emailuser)
|
||||
return ""
|
||||
|
||||
if not filename:
|
||||
# Filename already stored in database
|
||||
filename = instance.avatar.name
|
||||
@ -95,7 +77,58 @@ def find_extension(format):
|
||||
|
||||
return format
|
||||
|
||||
class Avatar(models.Model):
|
||||
class AvatarBase(object):
|
||||
"""
|
||||
Base class for avatar.
|
||||
"""
|
||||
def thumbnail_exists(self, size):
|
||||
return self.avatar.storage.exists(self.avatar_name(size))
|
||||
|
||||
def create_thumbnail(self, size, quality=None):
|
||||
# invalidate the cache of the thumbnail with the given size first
|
||||
if isinstance(self, Avatar):
|
||||
invalidate_cache(self.emailuser, size)
|
||||
|
||||
try:
|
||||
orig = self.avatar.storage.open(self.avatar.name, 'rb').read()
|
||||
image = Image.open(StringIO(orig))
|
||||
except IOError:
|
||||
return # What should we do here? Render a "sorry, didn't work" img?
|
||||
quality = quality or AVATAR_THUMB_QUALITY
|
||||
(w, h) = image.size
|
||||
if w != size or h != size:
|
||||
if w > h:
|
||||
diff = (w - h) / 2
|
||||
image = image.crop((diff, 0, w - diff, h))
|
||||
else:
|
||||
diff = (h - w) / 2
|
||||
image = image.crop((0, diff, w, h - diff))
|
||||
if image.mode != "RGBA":
|
||||
image = image.convert("RGBA")
|
||||
image = image.resize((size, size), AVATAR_RESIZE_METHOD)
|
||||
thumb = StringIO()
|
||||
image.save(thumb, AVATAR_THUMB_FORMAT, quality=quality)
|
||||
thumb_file = ContentFile(thumb.getvalue())
|
||||
else:
|
||||
thumb_file = ContentFile(orig)
|
||||
thumb = self.avatar.storage.save(self.avatar_name(size), thumb_file)
|
||||
|
||||
def avatar_url(self, size):
|
||||
return self.avatar.storage.url(self.avatar_name(size))
|
||||
|
||||
@abstractmethod
|
||||
def save(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def avatar_name(self, size):
|
||||
ext = find_extension(AVATAR_THUMB_FORMAT)
|
||||
return avatar_file_path(
|
||||
instance=self,
|
||||
size=size,
|
||||
ext=ext
|
||||
)
|
||||
|
||||
class Avatar(models.Model, AvatarBase):
|
||||
emailuser = models.CharField(max_length=255)
|
||||
primary = models.BooleanField(default=False)
|
||||
avatar = models.ImageField(max_length=1024, upload_to=avatar_file_path, blank=True)
|
||||
@ -120,98 +153,21 @@ class Avatar(models.Model):
|
||||
def delete(self, *args, **kwargs):
|
||||
invalidate_cache(self.emailuser)
|
||||
super(Avatar, self).delete(*args, **kwargs)
|
||||
|
||||
def thumbnail_exists(self, size):
|
||||
return self.avatar.storage.exists(self.avatar_name(size))
|
||||
|
||||
def create_thumbnail(self, size, quality=None):
|
||||
# invalidate the cache of the thumbnail with the given size first
|
||||
invalidate_cache(self.emailuser, size)
|
||||
try:
|
||||
orig = self.avatar.storage.open(self.avatar.name, 'rb').read()
|
||||
image = Image.open(StringIO(orig))
|
||||
except IOError:
|
||||
return # What should we do here? Render a "sorry, didn't work" img?
|
||||
quality = quality or AVATAR_THUMB_QUALITY
|
||||
(w, h) = image.size
|
||||
if w != size or h != size:
|
||||
if w > h:
|
||||
diff = (w - h) / 2
|
||||
image = image.crop((diff, 0, w - diff, h))
|
||||
else:
|
||||
diff = (h - w) / 2
|
||||
image = image.crop((0, diff, w, h - diff))
|
||||
if image.mode != "RGB":
|
||||
image = image.convert("RGB")
|
||||
image = image.resize((size, size), AVATAR_RESIZE_METHOD)
|
||||
thumb = StringIO()
|
||||
image.save(thumb, AVATAR_THUMB_FORMAT, quality=quality)
|
||||
thumb_file = ContentFile(thumb.getvalue())
|
||||
else:
|
||||
thumb_file = ContentFile(orig)
|
||||
thumb = self.avatar.storage.save(self.avatar_name(size), thumb_file)
|
||||
|
||||
def avatar_url(self, size):
|
||||
return self.avatar.storage.url(self.avatar_name(size))
|
||||
|
||||
def avatar_name(self, size):
|
||||
ext = find_extension(AVATAR_THUMB_FORMAT)
|
||||
return avatar_file_path(
|
||||
instance=self,
|
||||
size=size,
|
||||
ext=ext
|
||||
)
|
||||
|
||||
class GroupAvatar(models.Model):
|
||||
class GroupAvatar(models.Model, AvatarBase):
|
||||
group_id = models.CharField(max_length=255)
|
||||
avatar = models.ImageField(max_length=1024, upload_to=group_avatar_file_path, blank=True)
|
||||
avatar = models.ImageField(max_length=1024, upload_to=avatar_file_path, blank=True)
|
||||
date_uploaded = models.DateTimeField(default=datetime.datetime.now)
|
||||
|
||||
def __unicode__(self):
|
||||
return _(u'Avatar for %s') % self.group_id
|
||||
|
||||
def thumbnail_exists(self, size):
|
||||
return self.avatar.storage.exists(self.avatar_name(size))
|
||||
|
||||
def create_thumbnail(self, size, quality=None):
|
||||
try:
|
||||
orig = self.avatar.storage.open(self.avatar.name, 'rb').read()
|
||||
image = Image.open(StringIO(orig))
|
||||
except IOError:
|
||||
return # What should we do here? Render a "sorry, didn't work" img?
|
||||
quality = quality or AVATAR_THUMB_QUALITY
|
||||
(w, h) = image.size
|
||||
if w != size or h != size:
|
||||
if w > h:
|
||||
diff = (w - h) / 2
|
||||
image = image.crop((diff, 0, w - diff, h))
|
||||
else:
|
||||
diff = (h - w) / 2
|
||||
image = image.crop((0, diff, w, h - diff))
|
||||
if image.mode != "RGB":
|
||||
image = image.convert("RGB")
|
||||
image = image.resize((size, size), AVATAR_RESIZE_METHOD)
|
||||
thumb = StringIO()
|
||||
image.save(thumb, AVATAR_THUMB_FORMAT, quality=quality)
|
||||
thumb_file = ContentFile(thumb.getvalue())
|
||||
else:
|
||||
thumb_file = ContentFile(orig)
|
||||
thumb = self.avatar.storage.save(self.avatar_name(size), thumb_file)
|
||||
|
||||
def avatar_url(self, size):
|
||||
return self.avatar.storage.url(self.avatar_name(size))
|
||||
|
||||
def avatar_name(self, size):
|
||||
ext = find_extension(AVATAR_THUMB_FORMAT)
|
||||
return group_avatar_file_path(
|
||||
instance=self,
|
||||
size=size,
|
||||
ext=ext
|
||||
)
|
||||
def save(self, *args, **kwargs):
|
||||
super(GroupAvatar, self).save(*args, **kwargs)
|
||||
|
||||
def create_default_thumbnails(instance=None, created=False, **kwargs):
|
||||
if created:
|
||||
for size in AUTO_GENERATE_AVATAR_SIZES:
|
||||
instance.create_thumbnail(size)
|
||||
|
||||
signals.post_save.connect(create_default_thumbnails, sender=Avatar)
|
||||
#signals.post_save.connect(create_default_thumbnails, sender=Avatar)
|
||||
|
@ -23,7 +23,7 @@ AVATAR_GRAVATAR_BACKUP = getattr(settings, 'AVATAR_GRAVATAR_BACKUP', True)
|
||||
AVATAR_GRAVATAR_DEFAULT = getattr(settings, 'AVATAR_GRAVATAR_DEFAULT', None)
|
||||
AVATAR_MAX_AVATARS_PER_USER = getattr(settings, 'AVATAR_MAX_AVATARS_PER_USER', 42)
|
||||
AVATAR_MAX_SIZE = getattr(settings, 'AVATAR_MAX_SIZE', 1024 * 1024)
|
||||
AVATAR_THUMB_FORMAT = getattr(settings, 'AVATAR_THUMB_FORMAT', "JPEG")
|
||||
AVATAR_THUMB_FORMAT = getattr(settings, 'AVATAR_THUMB_FORMAT', "png")
|
||||
AVATAR_THUMB_QUALITY = getattr(settings, 'AVATAR_THUMB_QUALITY', 85)
|
||||
AVATAR_HASH_FILENAMES = getattr(settings, 'AVATAR_HASH_FILENAMES', False)
|
||||
AVATAR_HASH_USERDIRNAMES = getattr(settings, 'AVATAR_HASH_USERDIRNAMES', False)
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
{% block main_panel %}
|
||||
<div class="avatar-op">
|
||||
<h2>修改小组 {{ group.group_name }} 的图标</h2>
|
||||
<h2>修改小组 <a href="{{ SITE_ROOT }}group/{{ group.id }}/">{{ group.group_name }}</a> 的图标</h2>
|
||||
<div class="avatar-op-con">
|
||||
<h3>当前图标:</h3>
|
||||
<img src="{% grp_avatar_url group.id %}" alt="当前图标" class="avatar" />
|
||||
|
@ -155,9 +155,10 @@ PREVIEW_FILEEXT = {
|
||||
|
||||
# Avatar
|
||||
AVATAR_STORAGE_DIR = 'avatars'
|
||||
GROUP_AVATAR_STORAGE_DIR = 'avatars/groups'
|
||||
AVATAR_GRAVATAR_BACKUP = False
|
||||
AVATAR_DEFAULT_URL = '/avatars/default.jpg'
|
||||
AUTO_GENERATE_AVATAR_SIZES = (80, 60, 48, 16)
|
||||
GROUP_AVATAR_DEFAULT_URL = 'avatars/groups/default.png'
|
||||
AVATAR_MAX_AVATARS_PER_USER = 1
|
||||
AVATAR_CACHE_TIMEOUT = 0
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user