<s> from . base_resource import BaseMediaResource , parse_media_id <EOL> from synapse . http . server import request_handler <EOL> from twisted . web . server import NOT_DONE_YET <EOL> from twisted . internet import defer <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> class DownloadResource ( BaseMediaResource ) : <EOL> def render_GET ( self , request ) : <EOL> self . _async_render_GET ( request ) <EOL> return NOT_DONE_YET <EOL> @ request_handler <EOL> @ defer . inlineCallbacks <EOL> def _async_render_GET ( self , request ) : <EOL> server_name , media_id , name = parse_media_id ( request ) <EOL> if server_name == self . server_name : <EOL> yield self . _respond_local_file ( request , media_id , name ) <EOL> else : <EOL> yield self . _respond_remote_file ( <EOL> request , server_name , media_id , name <EOL> ) <EOL> @ defer . inlineCallbacks <EOL> def _respond_local_file ( self , request , media_id , name ) : <EOL> media_info = yield self . store . get_local_media ( media_id ) <EOL> if not media_info : <EOL> self . _respond_404 ( request ) <EOL> return <EOL> media_type = media_info [ "<STR_LIT>" ] <EOL> media_length = media_info [ "<STR_LIT>" ] <EOL> upload_name = name if name else media_info [ "<STR_LIT>" ] <EOL> file_path = self . filepaths . local_media_filepath ( media_id ) <EOL> yield self . _respond_with_file ( <EOL> request , media_type , file_path , media_length , <EOL> upload_name = upload_name , <EOL> ) <EOL> @ defer . inlineCallbacks <EOL> def _respond_remote_file ( self , request , server_name , media_id , name ) : <EOL> media_info = yield self . _get_remote_media ( server_name , media_id ) <EOL> media_type = media_info [ "<STR_LIT>" ] <EOL> media_length = media_info [ "<STR_LIT>" ] <EOL> filesystem_id = media_info [ "<STR_LIT>" ] <EOL> upload_name = name if name else media_info [ "<STR_LIT>" ] <EOL> file_path = self . filepaths . remote_media_filepath ( <EOL> server_name , filesystem_id <EOL> ) <EOL> yield self . _respond_with_file ( <EOL> request , media_type , file_path , media_length , <EOL> upload_name = upload_name , <EOL> ) </s>
