diff options
author | mnandri <mnandri@eumguccion.corporate.thoughtworks.com> | 2015-12-15 14:42:20 +0100 |
---|---|---|
committer | mnandri <mnandri@eunglick.corporate.thoughtworks.com> | 2015-12-18 11:22:34 +0100 |
commit | 93350f99193b78c6adf0b79b8565fe477912fd80 (patch) | |
tree | 3f4984504b976cecd629b59ee3cf237091002835 /service/pixelated/resources | |
parent | b7ce40f3c7f3b5c720cc4ecfb13555519ac6a108 (diff) |
backend that handles post of a single attachment file
Issue #548
Diffstat (limited to 'service/pixelated/resources')
-rw-r--r-- | service/pixelated/resources/attachments_resource.py | 28 | ||||
-rw-r--r-- | service/pixelated/resources/root_resource.py | 2 |
2 files changed, 25 insertions, 5 deletions
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 <http://www.gnu.org/licenses/>. - 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)) |