This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Refactor MediaRepository to separate out storage #2767
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
47ca5eb
Split out add_file_headers
erikjohnston 1ee7879
Add some helper classes
erikjohnston ada470b
Add MediaStorage class
erikjohnston dd3092c
Use MediaStorage for local files
erikjohnston 9e20840
Use MediaStorage for remote media
erikjohnston 9d30a76
Make ThumbnailResource use MediaStorage
erikjohnston 2442e98
Make PreviewUrlResource use MediaStorage
erikjohnston 8f03aa9
Add StorageProvider concept
erikjohnston 227c491
Comments
erikjohnston 4d88958
Make class var local
erikjohnston c6c0096
Remove unused variables
erikjohnston 1e4edd1
Remove unnecessary condition
erikjohnston 81391fa
Merge branch 'develop' of github.com:matrix-org/synapse into erikj/me…
erikjohnston dcc8ede
Add missing class var
erikjohnston 85a4d78
Make Responder a context manager
erikjohnston e21370b
Correctly reraise exception
erikjohnston 694f1c1
Fix up comments
erikjohnston File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
# limitations under the License. | ||
import synapse.http.servlet | ||
|
||
from ._base import parse_media_id, respond_with_file, respond_404 | ||
from ._base import parse_media_id, respond_404 | ||
from twisted.web.resource import Resource | ||
from synapse.http.server import request_handler, set_cors_headers | ||
|
||
|
@@ -32,12 +32,12 @@ class DownloadResource(Resource): | |
def __init__(self, hs, media_repo): | ||
Resource.__init__(self) | ||
|
||
self.filepaths = media_repo.filepaths | ||
self.media_repo = media_repo | ||
self.server_name = hs.hostname | ||
self.store = hs.get_datastore() | ||
self.version_string = hs.version_string | ||
|
||
# Both of these are expected by @request_handler() | ||
self.clock = hs.get_clock() | ||
self.version_string = hs.version_string | ||
|
||
def render_GET(self, request): | ||
self._async_render_GET(request) | ||
|
@@ -57,59 +57,16 @@ def _async_render_GET(self, request): | |
) | ||
server_name, media_id, name = parse_media_id(request) | ||
if server_name == self.server_name: | ||
yield self._respond_local_file(request, media_id, name) | ||
yield self.media_repo.get_local_media(request, media_id, name) | ||
else: | ||
yield self._respond_remote_file( | ||
request, server_name, media_id, name | ||
) | ||
|
||
@defer.inlineCallbacks | ||
def _respond_local_file(self, request, media_id, name): | ||
media_info = yield self.store.get_local_media(media_id) | ||
if not media_info or media_info["quarantined_by"]: | ||
respond_404(request) | ||
return | ||
|
||
media_type = media_info["media_type"] | ||
media_length = media_info["media_length"] | ||
upload_name = name if name else media_info["upload_name"] | ||
if media_info["url_cache"]: | ||
# TODO: Check the file still exists, if it doesn't we can redownload | ||
# it from the url `media_info["url_cache"]` | ||
file_path = self.filepaths.url_cache_filepath(media_id) | ||
else: | ||
file_path = self.filepaths.local_media_filepath(media_id) | ||
|
||
yield respond_with_file( | ||
request, media_type, file_path, media_length, | ||
upload_name=upload_name, | ||
) | ||
|
||
@defer.inlineCallbacks | ||
def _respond_remote_file(self, request, server_name, media_id, name): | ||
# don't forward requests for remote media if allow_remote is false | ||
allow_remote = synapse.http.servlet.parse_boolean( | ||
request, "allow_remote", default=True) | ||
if not allow_remote: | ||
logger.info( | ||
"Rejecting request for remote media %s/%s due to allow_remote", | ||
server_name, media_id, | ||
) | ||
respond_404(request) | ||
return | ||
|
||
media_info = yield self.media_repo.get_remote_media(server_name, media_id) | ||
|
||
media_type = media_info["media_type"] | ||
media_length = media_info["media_length"] | ||
filesystem_id = media_info["filesystem_id"] | ||
upload_name = name if name else media_info["upload_name"] | ||
|
||
file_path = self.filepaths.remote_media_filepath( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
server_name, filesystem_id | ||
) | ||
|
||
yield respond_with_file( | ||
request, media_type, file_path, media_length, | ||
upload_name=upload_name, | ||
) | ||
allow_remote = synapse.http.servlet.parse_boolean( | ||
request, "allow_remote", default=True) | ||
if not allow_remote: | ||
logger.info( | ||
"Rejecting request for remote media %s/%s due to allow_remote", | ||
server_name, media_id, | ||
) | ||
respond_404(request) | ||
return | ||
|
||
yield self.media_repo.get_remote_media(request, server_name, media_id, name) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would have been helpful if you could have put the switch from
respond_with_file
torespond_with_responder
in a separate commit to the switch from_respond_local_file
toget_local_media
, ftr.