mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-25 14:50:29 +00:00
Merge pull request #2039 from haiwen/avatar_handle_error
[avatar] Quick fix for avatar image truncated issue which will cause …
This commit is contained in:
@@ -3,6 +3,7 @@ from abc import abstractmethod
|
|||||||
import datetime
|
import datetime
|
||||||
import hashlib
|
import hashlib
|
||||||
import os
|
import os
|
||||||
|
import logging
|
||||||
|
|
||||||
from seahub.base.fields import LowerCaseCharField
|
from seahub.base.fields import LowerCaseCharField
|
||||||
|
|
||||||
@@ -31,6 +32,9 @@ from seahub.avatar.settings import (AVATAR_STORAGE_DIR, AVATAR_RESIZE_METHOD,
|
|||||||
AVATAR_THUMB_QUALITY, AUTO_GENERATE_AVATAR_SIZES,
|
AVATAR_THUMB_QUALITY, AUTO_GENERATE_AVATAR_SIZES,
|
||||||
GROUP_AVATAR_STORAGE_DIR)
|
GROUP_AVATAR_STORAGE_DIR)
|
||||||
|
|
||||||
|
# Get an instance of a logger
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
def avatar_file_path(instance=None, filename=None, size=None, ext=None):
|
def avatar_file_path(instance=None, filename=None, size=None, ext=None):
|
||||||
if isinstance(instance, Avatar):
|
if isinstance(instance, Avatar):
|
||||||
tmppath = [AVATAR_STORAGE_DIR]
|
tmppath = [AVATAR_STORAGE_DIR]
|
||||||
@@ -80,40 +84,42 @@ class AvatarBase(object):
|
|||||||
"""
|
"""
|
||||||
def thumbnail_exists(self, size):
|
def thumbnail_exists(self, size):
|
||||||
return self.avatar.storage.exists(self.avatar_name(size))
|
return self.avatar.storage.exists(self.avatar_name(size))
|
||||||
|
|
||||||
def create_thumbnail(self, size, quality=None):
|
def create_thumbnail(self, size, quality=None):
|
||||||
# invalidate the cache of the thumbnail with the given size first
|
# invalidate the cache of the thumbnail with the given size first
|
||||||
if isinstance(self, Avatar):
|
if isinstance(self, Avatar):
|
||||||
invalidate_cache(self.emailuser, size)
|
invalidate_cache(self.emailuser, size)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
orig = self.avatar.storage.open(self.avatar.name, 'rb').read()
|
orig = self.avatar.storage.open(self.avatar.name, 'rb').read()
|
||||||
image = Image.open(StringIO(orig))
|
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
|
||||||
quality = quality or AVATAR_THUMB_QUALITY
|
(w, h) = image.size
|
||||||
(w, h) = image.size
|
if w != size or h != size:
|
||||||
if w != size or h != size:
|
if w > h:
|
||||||
if w > h:
|
diff = (w - h) / 2
|
||||||
diff = (w - h) / 2
|
image = image.crop((diff, 0, w - diff, h))
|
||||||
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:
|
else:
|
||||||
diff = (h - w) / 2
|
thumb_file = ContentFile(orig)
|
||||||
image = image.crop((0, diff, w, h - diff))
|
thumb = self.avatar.storage.save(self.avatar_name(size), thumb_file)
|
||||||
if image.mode != "RGBA":
|
except Exception as e:
|
||||||
image = image.convert("RGBA")
|
logger.error(e)
|
||||||
image = image.resize((size, size), AVATAR_RESIZE_METHOD)
|
return # What should we do here? Render a "sorry, didn't work" img?
|
||||||
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):
|
def avatar_url(self, size):
|
||||||
return self.avatar.storage.url(self.avatar_name(size))
|
return self.avatar.storage.url(self.avatar_name(size))
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user