From 93350f99193b78c6adf0b79b8565fe477912fd80 Mon Sep 17 00:00:00 2001 From: mnandri Date: Tue, 15 Dec 2015 14:42:20 +0100 Subject: backend that handles post of a single attachment file Issue #548 --- service/pixelated/adapter/services/mail_service.py | 5 ++++ .../pixelated/resources/attachments_resource.py | 28 ++++++++++++++++++---- service/pixelated/resources/root_resource.py | 2 +- 3 files changed, 30 insertions(+), 5 deletions(-) (limited to 'service/pixelated') diff --git a/service/pixelated/adapter/services/mail_service.py b/service/pixelated/adapter/services/mail_service.py index f0bc056b..964e4354 100644 --- a/service/pixelated/adapter/services/mail_service.py +++ b/service/pixelated/adapter/services/mail_service.py @@ -34,6 +34,11 @@ class MailService(object): mails = yield self.mail_store.all_mails() defer.returnValue(mails) + @defer.inlineCallbacks + def attachment_id(self, _file): + _attachment_id = yield 'mocked_for_now' + defer.returnValue(_attachment_id) + @defer.inlineCallbacks def mails(self, query, window_size, page): mail_ids, total = self.search_engine.search(query, window_size, page) diff --git a/service/pixelated/resources/attachments_resource.py b/service/pixelated/resources/attachments_resource.py index a78022ec..667d030e 100644 --- a/service/pixelated/resources/attachments_resource.py +++ b/service/pixelated/resources/attachments_resource.py @@ -13,19 +13,20 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . - import io - import re + +from twisted.internet import defer from twisted.protocols.basic import FileSender from twisted.python.log import msg from twisted.web import server from twisted.web.resource import Resource -from twisted.internet import defer +from twisted.web.server import NOT_DONE_YET +from pixelated.resources import respond_json_deferred -class AttachmentResource(Resource): +class AttachmentResource(Resource): isLeaf = True def __init__(self, mail_service, attachment_id): @@ -38,6 +39,7 @@ class AttachmentResource(Resource): msg(failure, 'attachment not found') request.code = 404 request.finish() + encoding = request.args.get('encoding', [None])[0] filename = request.args.get('filename', [self.attachment_id])[0] request.setHeader(b'Content-Type', b'application/force-download') @@ -67,6 +69,7 @@ class AttachmentResource(Resource): class AttachmentsResource(Resource): + BASE_URL = 'attachment' def __init__(self, mail_service): Resource.__init__(self) @@ -74,3 +77,20 @@ class AttachmentsResource(Resource): def getChild(self, attachment_id, request): return AttachmentResource(self.mail_service, attachment_id) + + def render_POST(self, request): + _file = request.args['attachment'][0] + + deferred = self.mail_service.attachment_id(_file) + + def send_location(attachment_id): + request.headers['Location'] = '/%s/%s'% (self.BASE_URL, attachment_id) + respond_json_deferred({"attachment_id": attachment_id}, request, status_code=201) + + def error_handler(error): + respond_json_deferred({"message": "Something went wrong. Attachement not saved."}, request, status_code=500) + + deferred.addCallback(send_location) + deferred.addErrback(error_handler) + + return NOT_DONE_YET diff --git a/service/pixelated/resources/root_resource.py b/service/pixelated/resources/root_resource.py index f1e5d02b..56da5cc7 100644 --- a/service/pixelated/resources/root_resource.py +++ b/service/pixelated/resources/root_resource.py @@ -41,7 +41,7 @@ class RootResource(Resource): self.putChild('assets', File(self._static_folder)) self.putChild('keys', KeysResource(keymanager)) - self.putChild('attachment', AttachmentsResource(mail_service)) + self.putChild(AttachmentsResource.BASE_URL, AttachmentsResource(mail_service)) self.putChild('contacts', ContactsResource(search_engine)) self.putChild('features', FeaturesResource()) self.putChild('tags', TagsResource(search_engine)) -- cgit v1.2.3