Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Matrix
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Container Registry
Model registry
Operate
Environments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TeDomum
Matrix
Commits
5730b20c
Commit
5730b20c
authored
9 years ago
by
Erik Johnston
Browse files
Options
Downloads
Plain Diff
Merge pull request #175 from matrix-org/erikj/thumbnail_thread
Thumbnail images on a seperate thread
parents
8047fd24
5044e6c5
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
synapse/rest/media/v1/base_resource.py
+53
-45
53 additions, 45 deletions
synapse/rest/media/v1/base_resource.py
with
53 additions
and
45 deletions
synapse/rest/media/v1/base_resource.py
+
53
−
45
View file @
5730b20c
...
...
@@ -22,7 +22,7 @@ from synapse.api.errors import (
cs_error
,
Codes
,
SynapseError
)
from
twisted.internet
import
defer
from
twisted.internet
import
defer
,
threads
from
twisted.web.resource
import
Resource
from
twisted.protocols.basic
import
FileSender
...
...
@@ -274,57 +274,65 @@ class BaseMediaResource(Resource):
if
not
requirements
:
return
remote_thumbnails
=
[]
input_path
=
self
.
filepaths
.
remote_media_filepath
(
server_name
,
file_id
)
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
scales
=
set
()
crops
=
set
()
for
r_width
,
r_height
,
r_method
,
r_type
in
requirements
:
if
r_method
==
"
scale
"
:
t_width
,
t_height
=
thumbnailer
.
aspect
(
r_width
,
r_height
)
scales
.
add
((
min
(
m_width
,
t_width
),
min
(
m_height
,
t_height
),
r_type
,
))
elif
r_method
==
"
crop
"
:
crops
.
add
((
r_width
,
r_height
,
r_type
))
def
generate_thumbnails
():
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
scales
=
set
()
crops
=
set
()
for
r_width
,
r_height
,
r_method
,
r_type
in
requirements
:
if
r_method
==
"
scale
"
:
t_width
,
t_height
=
thumbnailer
.
aspect
(
r_width
,
r_height
)
scales
.
add
((
min
(
m_width
,
t_width
),
min
(
m_height
,
t_height
),
r_type
,
))
elif
r_method
==
"
crop
"
:
crops
.
add
((
r_width
,
r_height
,
r_type
))
for
t_width
,
t_height
,
t_type
in
scales
:
t_method
=
"
scale
"
t_path
=
self
.
filepaths
.
remote_media_thumbnail
(
server_name
,
file_id
,
t_width
,
t_height
,
t_type
,
t_method
)
self
.
_makedirs
(
t_path
)
t_len
=
thumbnailer
.
scale
(
t_path
,
t_width
,
t_height
,
t_type
)
remote_thumbnails
.
append
([
server_name
,
media_id
,
file_id
,
t_width
,
t_height
,
t_type
,
t_method
,
t_len
])
for
t_width
,
t_height
,
t_type
in
crops
:
if
(
t_width
,
t_height
,
t_type
)
in
scales
:
# If the aspect ratio of the cropped thumbnail matches a purely
# scaled one then there is no point in calculating a separate
# thumbnail.
continue
t_method
=
"
crop
"
t_path
=
self
.
filepaths
.
remote_media_thumbnail
(
server_name
,
file_id
,
t_width
,
t_height
,
t_type
,
t_method
)
self
.
_makedirs
(
t_path
)
t_len
=
thumbnailer
.
crop
(
t_path
,
t_width
,
t_height
,
t_type
)
remote_thumbnails
.
append
([
server_name
,
media_id
,
file_id
,
t_width
,
t_height
,
t_type
,
t_method
,
t_len
])
for
t_width
,
t_height
,
t_type
in
scales
:
t_method
=
"
scale
"
t_path
=
self
.
filepaths
.
remote_media_thumbnail
(
server_name
,
file_id
,
t_width
,
t_height
,
t_type
,
t_method
)
self
.
_makedirs
(
t_path
)
t_len
=
thumbnailer
.
scale
(
t_path
,
t_width
,
t_height
,
t_type
)
yield
self
.
store
.
store_remote_media_thumbnail
(
server_name
,
media_id
,
file_id
,
t_width
,
t_height
,
t_type
,
t_method
,
t_len
)
yield
threads
.
deferToThread
(
generate_thumbnails
)
for
t_width
,
t_height
,
t_type
in
crops
:
if
(
t_width
,
t_height
,
t_type
)
in
scales
:
# If the aspect ratio of the cropped thumbnail matches a purely
# scaled one then there is no point in calculating a separate
# thumbnail.
continue
t_method
=
"
crop
"
t_path
=
self
.
filepaths
.
remote_media_thumbnail
(
server_name
,
file_id
,
t_width
,
t_height
,
t_type
,
t_method
)
self
.
_makedirs
(
t_path
)
t_len
=
thumbnailer
.
crop
(
t_path
,
t_width
,
t_height
,
t_type
)
yield
self
.
store
.
store_remote_media_thumbnail
(
server_name
,
media_id
,
file_id
,
t_width
,
t_height
,
t_type
,
t_method
,
t_len
)
for
r
in
remote_thumbnails
:
yield
self
.
store
.
store_remote_media_thumbnail
(
*
r
)
defer
.
returnValue
({
"
width
"
:
m_width
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment