mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-25 23:02:26 +00:00
update & return encoded thumbnail src to web
This commit is contained in:
@@ -37,34 +37,34 @@ def allow_generate_thumbnail(request, repo_id, path):
|
||||
repo = get_repo(repo_id)
|
||||
file_size = get_file_size(repo.store_id, repo.version, file_id)
|
||||
|
||||
if not repo.encrypted and file_type == IMAGE and ENABLE_THUMBNAIL:
|
||||
# check image compressed size limit
|
||||
if file_size < THUMBNAIL_IMAGE_COMPRESSED_SIZE_LIMIT * 1024**2:
|
||||
if repo.encrypted or file_type != IMAGE or not ENABLE_THUMBNAIL:
|
||||
return False
|
||||
|
||||
# check image compressed size limit
|
||||
if file_size < THUMBNAIL_IMAGE_COMPRESSED_SIZE_LIMIT * 1024**2:
|
||||
return True
|
||||
|
||||
# get image memory cost
|
||||
token = seafile_api.get_fileserver_access_token(repo_id, file_id, 'view',
|
||||
'', use_onetime = False)
|
||||
|
||||
inner_path = gen_inner_file_get_url(token, obj_name)
|
||||
try:
|
||||
image_file = urllib2.urlopen(inner_path)
|
||||
f = StringIO(image_file.read())
|
||||
image = Image.open(f)
|
||||
width, height = image.size
|
||||
|
||||
# check image memory cost size limit
|
||||
# use RGBA as default mode(4x8-bit pixels, true colour with transparency mask)
|
||||
# every pixel will cost 4 byte in RGBA mode
|
||||
image_memory_cost = width * height * 4 / 1024 / 1024
|
||||
if image_memory_cost < THUMBNAIL_IMAGE_ORIGINAL_SIZE_LIMIT:
|
||||
return True
|
||||
|
||||
# get image memory cost
|
||||
token = seafile_api.get_fileserver_access_token(repo_id, file_id, 'view',
|
||||
'', use_onetime = False)
|
||||
|
||||
inner_path = gen_inner_file_get_url(token, obj_name)
|
||||
try:
|
||||
image_file = urllib2.urlopen(inner_path)
|
||||
f = StringIO(image_file.read())
|
||||
image = Image.open(f)
|
||||
width, height = image.size
|
||||
|
||||
# check image memory cost size limit
|
||||
# use RGBA as default mode(4x8-bit pixels, true colour with transparency mask)
|
||||
# every pixel will cost 4 byte in RGBA mode
|
||||
image_memory_cost = width * height * 4 / 1024 / 1024
|
||||
if image_memory_cost < THUMBNAIL_IMAGE_ORIGINAL_SIZE_LIMIT:
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
return False
|
||||
|
||||
return False
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
return False
|
||||
|
||||
def generate_thumbnail(request, repo_id, size, path):
|
||||
""" generate and save thumbnail if not exist
|
||||
@@ -77,21 +77,22 @@ def generate_thumbnail(request, repo_id, size, path):
|
||||
file_id = get_file_id_by_path(repo_id, path)
|
||||
thumbnail_file = os.path.join(thumbnail_dir, file_id)
|
||||
|
||||
if not os.path.exists(thumbnail_file):
|
||||
token = seafile_api.get_fileserver_access_token(repo_id, file_id, 'view',
|
||||
'', use_onetime = False)
|
||||
if os.path.exists(thumbnail_file):
|
||||
return True
|
||||
|
||||
inner_path = gen_inner_file_get_url(token, os.path.basename(path))
|
||||
try:
|
||||
image_file = urllib2.urlopen(inner_path)
|
||||
f = StringIO(image_file.read())
|
||||
image = Image.open(f)
|
||||
if image.mode not in ["1", "L", "P", "RGB", "RGBA"]:
|
||||
image = image.convert("RGB")
|
||||
image.thumbnail((size, size), Image.ANTIALIAS)
|
||||
image.save(thumbnail_file, THUMBNAIL_EXTENSION)
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
return False
|
||||
token = seafile_api.get_fileserver_access_token(repo_id, file_id, 'view',
|
||||
'', use_onetime = False)
|
||||
|
||||
return True
|
||||
inner_path = gen_inner_file_get_url(token, os.path.basename(path))
|
||||
try:
|
||||
image_file = urllib2.urlopen(inner_path)
|
||||
f = StringIO(image_file.read())
|
||||
image = Image.open(f)
|
||||
if image.mode not in ["1", "L", "P", "RGB", "RGBA"]:
|
||||
image = image.convert("RGB")
|
||||
image.thumbnail((size, size), Image.ANTIALIAS)
|
||||
image.save(thumbnail_file, THUMBNAIL_EXTENSION)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
return False
|
||||
|
Reference in New Issue
Block a user