summary refs log tree commit diff
path: root/synapse/media/v1
diff options
context:
space:
mode:
authorMark Haines <mark.haines@matrix.org>2014-12-02 19:51:47 +0000
committerMark Haines <mark.haines@matrix.org>2014-12-02 19:51:47 +0000
commit5da65085d106e98cf7b762836cb300d01226bf92 (patch)
treea6de5abe0e3485800f382221e861961578a82d2f /synapse/media/v1
parentWrite the upload portion of version 1 of the media repository (diff)
downloadsynapse-5da65085d106e98cf7b762836cb300d01226bf92.tar.xz
Get uploads working with new media repo
Diffstat (limited to 'synapse/media/v1')
-rw-r--r--synapse/media/v1/__init__.py0
-rw-r--r--synapse/media/v1/media_repository.py23
-rw-r--r--synapse/media/v1/upload_resource.py14
3 files changed, 15 insertions, 22 deletions
diff --git a/synapse/media/v1/__init__.py b/synapse/media/v1/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/synapse/media/v1/__init__.py
diff --git a/synapse/media/v1/media_repository.py b/synapse/media/v1/media_repository.py
index 9c36a8e933..0f4eeef278 100644
--- a/synapse/media/v1/media_repository.py
+++ b/synapse/media/v1/media_repository.py
@@ -13,27 +13,17 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from synapse.http.server import respond_with_json_bytes
+from .upload_resource import UploadResource
+from .filepath import MediaFilePaths
 
-from synapse.util.stringutils import random_string
-from synapse.api.errors import (
-    cs_exception, SynapseError, CodeMessageException, Codes, cs_error
-)
+from twisted.web.resource import Resource
 
-from twisted.protocols.basic import FileSender
-from twisted.web import server, resource
-from twisted.internet import defer
-
-import base64
-import json
 import logging
-import os
-import re
 
 logger = logging.getLogger(__name__)
 
 
-class MediaRepository():
+class MediaRepositoryResource(Resource):
     """Profiles file uploading and downloading.
 
     Uploads are POSTed to a resource which returns a token which is used to GET
@@ -68,5 +58,6 @@ class MediaRepository():
     """
 
     def __init__(self, hs):
-        filepaths = MediaFilePaths
-
+        Resource.__init__(self)
+        filepaths = MediaFilePaths(hs.config.media_store_path)
+        self.putChild("upload", UploadResource(hs, filepaths))
diff --git a/synapse/media/v1/upload_resource.py b/synapse/media/v1/upload_resource.py
index 3721a0173d..d9d7825b2b 100644
--- a/synapse/media/v1/upload_resource.py
+++ b/synapse/media/v1/upload_resource.py
@@ -23,6 +23,8 @@ from synapse.api.errors import (
 from twisted.web import server, resource
 from twisted.internet import defer
 
+import os
+
 import logging
 
 logger = logging.getLogger(__name__)
@@ -31,8 +33,9 @@ class UploadResource(resource.Resource):
 
     def __init__(self, hs, filepaths):
         self.auth = hs.get_auth()
+        self.clock = hs.get_clock()
         self.store = hs.get_datastore()
-        self.max_upload_size = hs.config.max_upload_size()
+        self.max_upload_size = hs.config.max_upload_size
         self.filepaths = filepaths
 
     def render_POST(self, request):
@@ -45,10 +48,8 @@ class UploadResource(resource.Resource):
 
     @defer.inlineCallbacks
     def _async_render_POST(self, request):
-
-        auth_user = yield self.auth.get_user_by_req(request)
-
         try:
+            auth_user = yield self.auth.get_user_by_req(request)
             # TODO: The checks here are a bit late. The content will have
             # already been uploaded to a tmp file at this point
             content_length = request.getHeader("Content-Length")
@@ -62,7 +63,7 @@ class UploadResource(resource.Resource):
                     code=413,
                 )
 
-            headers = request.requestHeaders()
+            headers = request.requestHeaders
 
             if headers.hasHeader("Content-Type"):
                 media_type = headers.getRawHeaders("Content-Type")[0]
@@ -78,7 +79,8 @@ class UploadResource(resource.Resource):
 
             media_id = random_string(24)
 
-            fname = self.filepaths.local_media_file_path(media_id)
+            fname = self.filepaths.local_media_filepath(media_id)
+            os.makedirs(os.path.dirname(fname))
 
             # This shouldn't block for very long because the content will have
             # already been uploaded at this point.