Factor out thumbnail()
This commit is contained in:
parent
33d83f3615
commit
459085184c
|
@ -225,41 +225,44 @@ class BaseMediaResource(Resource):
|
||||||
else:
|
else:
|
||||||
return ()
|
return ()
|
||||||
|
|
||||||
|
def _generate_thumbnail(self, input_path, t_path, t_width, t_height,
|
||||||
|
t_method, t_type):
|
||||||
|
thumbnailer = Thumbnailer(input_path)
|
||||||
|
m_width = thumbnailer.width
|
||||||
|
m_height = thumbnailer.height
|
||||||
|
|
||||||
|
if m_width * m_height >= self.max_image_pixels:
|
||||||
|
logger.info(
|
||||||
|
"Image too large to thumbnail %r x %r > %r",
|
||||||
|
m_width, m_height, self.max_image_pixels
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
if t_method == "crop":
|
||||||
|
t_len = thumbnailer.crop(t_path, t_width, t_height, t_type)
|
||||||
|
elif t_method == "scale":
|
||||||
|
t_len = thumbnailer.scale(t_path, t_width, t_height, t_type)
|
||||||
|
else:
|
||||||
|
t_len = None
|
||||||
|
|
||||||
|
return t_len
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def _generate_local_exact_thumbnail(self, media_id, t_width, t_height,
|
def _generate_local_exact_thumbnail(self, media_id, t_width, t_height,
|
||||||
t_method, t_type):
|
t_method, t_type):
|
||||||
input_path = self.filepaths.local_media_filepath(media_id)
|
input_path = self.filepaths.local_media_filepath(media_id)
|
||||||
|
|
||||||
def thumbnail():
|
t_path = self.filepaths.local_media_thumbnail(
|
||||||
thumbnailer = Thumbnailer(input_path)
|
media_id, t_width, t_height, t_type, t_method
|
||||||
m_width = thumbnailer.width
|
)
|
||||||
m_height = thumbnailer.height
|
self._makedirs(t_path)
|
||||||
|
|
||||||
if m_width * m_height >= self.max_image_pixels:
|
t_len = yield threads.deferToThread(
|
||||||
logger.info(
|
self._generate_thumbnail,
|
||||||
"Image too large to thumbnail %r x %r > %r",
|
input_path, t_path, t_width, t_height, t_method, t_type
|
||||||
m_width, m_height, self.max_image_pixels
|
)
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
t_path = self.filepaths.local_media_thumbnail(
|
if t_len:
|
||||||
media_id, t_width, t_height, t_type, t_method
|
|
||||||
)
|
|
||||||
self._makedirs(t_path)
|
|
||||||
|
|
||||||
if t_method == "crop":
|
|
||||||
t_len = thumbnailer.crop(t_path, t_width, t_height, t_type)
|
|
||||||
elif t_method == "scale":
|
|
||||||
t_len = thumbnailer.scale(t_path, t_width, t_height, t_type)
|
|
||||||
else:
|
|
||||||
t_len = None
|
|
||||||
|
|
||||||
return t_len, t_path
|
|
||||||
|
|
||||||
res = yield threads.deferToThread(thumbnail)
|
|
||||||
|
|
||||||
if res:
|
|
||||||
t_len, t_path = res
|
|
||||||
yield self.store.store_local_thumbnail(
|
yield self.store.store_local_thumbnail(
|
||||||
media_id, t_width, t_height, t_type, t_method, t_len
|
media_id, t_width, t_height, t_type, t_method, t_len
|
||||||
)
|
)
|
||||||
|
@ -271,36 +274,17 @@ class BaseMediaResource(Resource):
|
||||||
t_width, t_height, t_method, t_type):
|
t_width, t_height, t_method, t_type):
|
||||||
input_path = self.filepaths.remote_media_filepath(server_name, file_id)
|
input_path = self.filepaths.remote_media_filepath(server_name, file_id)
|
||||||
|
|
||||||
def thumbnail():
|
t_path = self.filepaths.remote_media_thumbnail(
|
||||||
thumbnailer = Thumbnailer(input_path)
|
server_name, file_id, t_width, t_height, t_type, t_method
|
||||||
m_width = thumbnailer.width
|
)
|
||||||
m_height = thumbnailer.height
|
self._makedirs(t_path)
|
||||||
|
|
||||||
if m_width * m_height >= self.max_image_pixels:
|
t_len = yield threads.deferToThread(
|
||||||
logger.info(
|
self._generate_thumbnail,
|
||||||
"Image too large to thumbnail %r x %r > %r",
|
input_path, t_path, t_width, t_height, t_method, t_type
|
||||||
m_width, m_height, self.max_image_pixels
|
)
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
t_path = self.filepaths.remote_media_thumbnail(
|
if t_len:
|
||||||
server_name, file_id, t_width, t_height, t_type, t_method
|
|
||||||
)
|
|
||||||
self._makedirs(t_path)
|
|
||||||
|
|
||||||
if t_method == "crop":
|
|
||||||
t_len = thumbnailer.crop(t_path, t_width, t_height, t_type)
|
|
||||||
elif t_method == "scale":
|
|
||||||
t_len = thumbnailer.scale(t_path, t_width, t_height, t_type)
|
|
||||||
else:
|
|
||||||
t_len = None
|
|
||||||
|
|
||||||
return t_path, t_len
|
|
||||||
|
|
||||||
res = yield threads.deferToThread(thumbnail)
|
|
||||||
|
|
||||||
if res:
|
|
||||||
t_path, t_len = res
|
|
||||||
yield self.store.store_remote_media_thumbnail(
|
yield self.store.store_remote_media_thumbnail(
|
||||||
server_name, media_id, file_id,
|
server_name, media_id, file_id,
|
||||||
t_width, t_height, t_type, t_method, t_len
|
t_width, t_height, t_type, t_method, t_len
|
||||||
|
|
Loading…
Reference in New Issue