Merge branch 'develop' of github.com:matrix-org/synapse into erikj/receipts
commit
304343f4d7
|
@ -207,7 +207,7 @@ class JsonResource(HttpServer, resource.Resource):
|
||||||
incoming_requests_counter.inc(request.method, servlet_classname)
|
incoming_requests_counter.inc(request.method, servlet_classname)
|
||||||
|
|
||||||
args = [
|
args = [
|
||||||
urllib.unquote(u).decode("UTF-8") for u in m.groups()
|
urllib.unquote(u).decode("UTF-8") if u else u for u in m.groups()
|
||||||
]
|
]
|
||||||
|
|
||||||
callback_return = yield callback(request, *args)
|
callback_return = yield callback(request, *args)
|
||||||
|
|
|
@ -27,9 +27,11 @@ from twisted.web.resource import Resource
|
||||||
from twisted.protocols.basic import FileSender
|
from twisted.protocols.basic import FileSender
|
||||||
|
|
||||||
from synapse.util.async import ObservableDeferred
|
from synapse.util.async import ObservableDeferred
|
||||||
|
from synapse.util.stringutils import is_ascii
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
import cgi
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -37,8 +39,13 @@ logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
def parse_media_id(request):
|
def parse_media_id(request):
|
||||||
try:
|
try:
|
||||||
server_name, media_id = request.postpath
|
# This allows users to append e.g. /test.png to the URL. Useful for
|
||||||
return (server_name, media_id)
|
# clients that parse the URL to see content type.
|
||||||
|
server_name, media_id = request.postpath[:2]
|
||||||
|
if len(request.postpath) > 2 and is_ascii(request.postpath[-1]):
|
||||||
|
return server_name, media_id, request.postpath[-1]
|
||||||
|
else:
|
||||||
|
return server_name, media_id, None
|
||||||
except:
|
except:
|
||||||
raise SynapseError(
|
raise SynapseError(
|
||||||
404,
|
404,
|
||||||
|
@ -128,12 +135,21 @@ class BaseMediaResource(Resource):
|
||||||
media_type = headers["Content-Type"][0]
|
media_type = headers["Content-Type"][0]
|
||||||
time_now_ms = self.clock.time_msec()
|
time_now_ms = self.clock.time_msec()
|
||||||
|
|
||||||
|
content_disposition = headers.get("Content-Disposition", None)
|
||||||
|
if content_disposition:
|
||||||
|
_, params = cgi.parse_header(content_disposition[0],)
|
||||||
|
upload_name = params.get("filename", None)
|
||||||
|
if upload_name and not is_ascii(upload_name):
|
||||||
|
upload_name = None
|
||||||
|
else:
|
||||||
|
upload_name = None
|
||||||
|
|
||||||
yield self.store.store_cached_remote_media(
|
yield self.store.store_cached_remote_media(
|
||||||
origin=server_name,
|
origin=server_name,
|
||||||
media_id=media_id,
|
media_id=media_id,
|
||||||
media_type=media_type,
|
media_type=media_type,
|
||||||
time_now_ms=self.clock.time_msec(),
|
time_now_ms=self.clock.time_msec(),
|
||||||
upload_name=None,
|
upload_name=upload_name,
|
||||||
media_length=length,
|
media_length=length,
|
||||||
filesystem_id=file_id,
|
filesystem_id=file_id,
|
||||||
)
|
)
|
||||||
|
@ -144,7 +160,7 @@ class BaseMediaResource(Resource):
|
||||||
media_info = {
|
media_info = {
|
||||||
"media_type": media_type,
|
"media_type": media_type,
|
||||||
"media_length": length,
|
"media_length": length,
|
||||||
"upload_name": None,
|
"upload_name": upload_name,
|
||||||
"created_ts": time_now_ms,
|
"created_ts": time_now_ms,
|
||||||
"filesystem_id": file_id,
|
"filesystem_id": file_id,
|
||||||
}
|
}
|
||||||
|
@ -157,11 +173,16 @@ class BaseMediaResource(Resource):
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def _respond_with_file(self, request, media_type, file_path,
|
def _respond_with_file(self, request, media_type, file_path,
|
||||||
file_size=None):
|
file_size=None, upload_name=None):
|
||||||
logger.debug("Responding with %r", file_path)
|
logger.debug("Responding with %r", file_path)
|
||||||
|
|
||||||
if os.path.isfile(file_path):
|
if os.path.isfile(file_path):
|
||||||
request.setHeader(b"Content-Type", media_type.encode("UTF-8"))
|
request.setHeader(b"Content-Type", media_type.encode("UTF-8"))
|
||||||
|
if upload_name:
|
||||||
|
request.setHeader(
|
||||||
|
b"Content-Disposition",
|
||||||
|
b"inline; filename=%s" % (upload_name.encode("utf-8"),),
|
||||||
|
)
|
||||||
|
|
||||||
# cache for at least a day.
|
# cache for at least a day.
|
||||||
# XXX: we might want to turn this off for data we don't want to
|
# XXX: we might want to turn this off for data we don't want to
|
||||||
|
|
|
@ -32,14 +32,16 @@ class DownloadResource(BaseMediaResource):
|
||||||
@request_handler
|
@request_handler
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def _async_render_GET(self, request):
|
def _async_render_GET(self, request):
|
||||||
server_name, media_id = parse_media_id(request)
|
server_name, media_id, name = parse_media_id(request)
|
||||||
if server_name == self.server_name:
|
if server_name == self.server_name:
|
||||||
yield self._respond_local_file(request, media_id)
|
yield self._respond_local_file(request, media_id, name)
|
||||||
else:
|
else:
|
||||||
yield self._respond_remote_file(request, server_name, media_id)
|
yield self._respond_remote_file(
|
||||||
|
request, server_name, media_id, name
|
||||||
|
)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def _respond_local_file(self, request, media_id):
|
def _respond_local_file(self, request, media_id, name):
|
||||||
media_info = yield self.store.get_local_media(media_id)
|
media_info = yield self.store.get_local_media(media_id)
|
||||||
if not media_info:
|
if not media_info:
|
||||||
self._respond_404(request)
|
self._respond_404(request)
|
||||||
|
@ -47,24 +49,28 @@ class DownloadResource(BaseMediaResource):
|
||||||
|
|
||||||
media_type = media_info["media_type"]
|
media_type = media_info["media_type"]
|
||||||
media_length = media_info["media_length"]
|
media_length = media_info["media_length"]
|
||||||
|
upload_name = name if name else media_info["upload_name"]
|
||||||
file_path = self.filepaths.local_media_filepath(media_id)
|
file_path = self.filepaths.local_media_filepath(media_id)
|
||||||
|
|
||||||
yield self._respond_with_file(
|
yield self._respond_with_file(
|
||||||
request, media_type, file_path, media_length
|
request, media_type, file_path, media_length,
|
||||||
|
upload_name=upload_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def _respond_remote_file(self, request, server_name, media_id):
|
def _respond_remote_file(self, request, server_name, media_id, name):
|
||||||
media_info = yield self._get_remote_media(server_name, media_id)
|
media_info = yield self._get_remote_media(server_name, media_id)
|
||||||
|
|
||||||
media_type = media_info["media_type"]
|
media_type = media_info["media_type"]
|
||||||
media_length = media_info["media_length"]
|
media_length = media_info["media_length"]
|
||||||
filesystem_id = media_info["filesystem_id"]
|
filesystem_id = media_info["filesystem_id"]
|
||||||
|
upload_name = name if name else media_info["upload_name"]
|
||||||
|
|
||||||
file_path = self.filepaths.remote_media_filepath(
|
file_path = self.filepaths.remote_media_filepath(
|
||||||
server_name, filesystem_id
|
server_name, filesystem_id
|
||||||
)
|
)
|
||||||
|
|
||||||
yield self._respond_with_file(
|
yield self._respond_with_file(
|
||||||
request, media_type, file_path, media_length
|
request, media_type, file_path, media_length,
|
||||||
|
upload_name=upload_name,
|
||||||
)
|
)
|
||||||
|
|
|
@ -36,7 +36,7 @@ class ThumbnailResource(BaseMediaResource):
|
||||||
@request_handler
|
@request_handler
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def _async_render_GET(self, request):
|
def _async_render_GET(self, request):
|
||||||
server_name, media_id = parse_media_id(request)
|
server_name, media_id, _ = parse_media_id(request)
|
||||||
width = parse_integer(request, "width")
|
width = parse_integer(request, "width")
|
||||||
height = parse_integer(request, "height")
|
height = parse_integer(request, "height")
|
||||||
method = parse_string(request, "method", "scale")
|
method = parse_string(request, "method", "scale")
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
|
|
||||||
from synapse.http.server import respond_with_json, request_handler
|
from synapse.http.server import respond_with_json, request_handler
|
||||||
|
|
||||||
from synapse.util.stringutils import random_string
|
from synapse.util.stringutils import random_string, is_ascii
|
||||||
from synapse.api.errors import SynapseError
|
from synapse.api.errors import SynapseError
|
||||||
|
|
||||||
from twisted.web.server import NOT_DONE_YET
|
from twisted.web.server import NOT_DONE_YET
|
||||||
|
@ -84,6 +84,12 @@ class UploadResource(BaseMediaResource):
|
||||||
code=413,
|
code=413,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
upload_name = request.args.get("filename", None)
|
||||||
|
if upload_name:
|
||||||
|
upload_name = upload_name[0]
|
||||||
|
if upload_name and not is_ascii(upload_name):
|
||||||
|
raise SynapseError(400, "filename must be ascii")
|
||||||
|
|
||||||
headers = request.requestHeaders
|
headers = request.requestHeaders
|
||||||
|
|
||||||
if headers.hasHeader("Content-Type"):
|
if headers.hasHeader("Content-Type"):
|
||||||
|
@ -99,7 +105,7 @@ class UploadResource(BaseMediaResource):
|
||||||
# TODO(markjh): parse content-dispostion
|
# TODO(markjh): parse content-dispostion
|
||||||
|
|
||||||
content_uri = yield self.create_content(
|
content_uri = yield self.create_content(
|
||||||
media_type, None, request.content.read(),
|
media_type, upload_name, request.content.read(),
|
||||||
content_length, auth_user
|
content_length, auth_user
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -408,9 +408,11 @@ class EventFederationStore(SQLBaseStore):
|
||||||
keyvalues={
|
keyvalues={
|
||||||
"event_id": event_id,
|
"event_id": event_id,
|
||||||
},
|
},
|
||||||
retcol="depth"
|
retcol="depth",
|
||||||
|
allow_none=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if depth:
|
||||||
queue.put((-depth, event_id))
|
queue.put((-depth, event_id))
|
||||||
|
|
||||||
while not queue.empty() and len(event_results) < limit:
|
while not queue.empty() and len(event_results) < limit:
|
||||||
|
|
|
@ -33,3 +33,12 @@ def random_string_with_symbols(length):
|
||||||
return ''.join(
|
return ''.join(
|
||||||
random.choice(_string_with_symbols) for _ in xrange(length)
|
random.choice(_string_with_symbols) for _ in xrange(length)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def is_ascii(s):
|
||||||
|
try:
|
||||||
|
s.encode("ascii")
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
Loading…
Reference in New Issue