Make the content repo work with in daemon mode. Return the full url on upload. Update the webclient to use new content repo api.

paul/schema_breaking_changes
Erik Johnston 2014-08-22 10:25:27 +01:00
parent 53f4fbd99a
commit acf5127604
3 changed files with 24 additions and 9 deletions

View File

@ -56,7 +56,7 @@ class SynapseHomeServer(HomeServer):
return File("webclient") # TODO configurable? return File("webclient") # TODO configurable?
def build_resource_for_content_repo(self): def build_resource_for_content_repo(self):
return ContentRepoResource("uploads", self.auth) return ContentRepoResource(self, self.upload_dir, self.auth)
def build_db_pool(self): def build_db_pool(self):
""" Set up all the dbs. Since all the *.sql have IF NOT EXISTS, so we """ Set up all the dbs. Since all the *.sql have IF NOT EXISTS, so we
@ -257,7 +257,8 @@ def setup():
hs = SynapseHomeServer( hs = SynapseHomeServer(
args.host, args.host,
db_name=db_name upload_dir=os.path.abspath("uploads"),
db_name=db_name,
) )
# This object doesn't need to be saved because it's set as the handler for # This object doesn't need to be saved because it's set as the handler for

View File

@ -212,8 +212,9 @@ class ContentRepoResource(resource.Resource):
""" """
isLeaf = True isLeaf = True
def __init__(self, directory, auth): def __init__(self, hs, directory, auth):
resource.Resource.__init__(self) resource.Resource.__init__(self)
self.hs = hs
self.directory = directory self.directory = directory
self.auth = auth self.auth = auth
@ -250,7 +251,8 @@ class ContentRepoResource(resource.Resource):
file_ext = re.sub("[^a-z]", "", file_ext) file_ext = re.sub("[^a-z]", "", file_ext)
suffix += "." + file_ext suffix += "." + file_ext
file_path = os.path.join(self.directory, prefix + main_part + suffix) file_name = prefix + main_part + suffix
file_path = os.path.join(self.directory, file_name)
logger.info("User %s is uploading a file to path %s", logger.info("User %s is uploading a file to path %s",
auth_user.to_string(), auth_user.to_string(),
file_path) file_path)
@ -259,8 +261,8 @@ class ContentRepoResource(resource.Resource):
attempts = 0 attempts = 0
while os.path.exists(file_path): while os.path.exists(file_path):
main_part = random_string(24) main_part = random_string(24)
file_path = os.path.join(self.directory, file_name = prefix + main_part + suffix
prefix + main_part + suffix) file_path = os.path.join(self.directory, file_name)
attempts += 1 attempts += 1
if attempts > 25: # really? Really? if attempts > 25: # really? Really?
raise SynapseError(500, "Unable to create file.") raise SynapseError(500, "Unable to create file.")
@ -272,11 +274,14 @@ class ContentRepoResource(resource.Resource):
# servers. # servers.
# TODO: A little crude here, we could do this better. # TODO: A little crude here, we could do this better.
filename = request.path.split(self.directory + "/")[1] filename = request.path.split('/')[-1]
# be paranoid # be paranoid
filename = re.sub("[^0-9A-z.-_]", "", filename) filename = re.sub("[^0-9A-z.-_]", "", filename)
file_path = self.directory + "/" + filename file_path = self.directory + "/" + filename
logger.debug("Searching for %s", file_path)
if os.path.isfile(file_path): if os.path.isfile(file_path):
# filename has the content type # filename has the content type
base64_contentype = filename.split(".")[1] base64_contentype = filename.split(".")[1]
@ -304,6 +309,10 @@ class ContentRepoResource(resource.Resource):
self._async_render(request) self._async_render(request)
return server.NOT_DONE_YET return server.NOT_DONE_YET
def render_OPTIONS(self, request):
respond_with_json_bytes(request, 200, {}, send_cors=True)
return server.NOT_DONE_YET
@defer.inlineCallbacks @defer.inlineCallbacks
def _async_render(self, request): def _async_render(self, request):
try: try:
@ -313,8 +322,13 @@ class ContentRepoResource(resource.Resource):
with open(fname, "wb") as f: with open(fname, "wb") as f:
f.write(request.content.read()) f.write(request.content.read())
# FIXME (erikj): These should use constants.
file_name = os.path.basename(fname)
url = "http://%s/matrix/content/%s" % (self.hs.hostname, file_name)
respond_with_json_bytes(request, 200, respond_with_json_bytes(request, 200,
json.dumps({"content_token": fname}), json.dumps({"content_token": url}),
send_cors=True) send_cors=True)
except CodeMessageException as e: except CodeMessageException as e:

View File

@ -33,7 +33,7 @@ angular.module('mFileUpload', ['matrixService', 'mUtilities'])
console.log("Uploading " + file.name + "... to /matrix/content"); console.log("Uploading " + file.name + "... to /matrix/content");
matrixService.uploadContent(file).then( matrixService.uploadContent(file).then(
function(response) { function(response) {
var content_url = location.origin + "/matrix/content/" + response.data.content_token; var content_url = response.data.content_token;
console.log(" -> Successfully uploaded! Available at " + content_url); console.log(" -> Successfully uploaded! Available at " + content_url);
deferred.resolve(content_url); deferred.resolve(content_url);
}, },