summaryrefslogtreecommitdiff
path: root/service/pixelated
diff options
context:
space:
mode:
authorPatrick Maia <patrickjourdanmaia@gmail.com>2014-11-11 17:06:39 +0000
committerPatrick Maia <patrickjourdanmaia@gmail.com>2014-11-11 18:02:10 +0000
commit833f28aef3d62810edfe0124448c760189cc3efa (patch)
tree0394bb38c676cf7e80c62b8b741bc897ef3f7a68 /service/pixelated
parent75efc35c03c1c94103ce6ef4fd0dbe859e54cc63 (diff)
Card #30 - migrates attachments controller to twisted
Diffstat (limited to 'service/pixelated')
-rw-r--r--service/pixelated/controllers/attachments_controller.py24
1 files changed, 16 insertions, 8 deletions
diff --git a/service/pixelated/controllers/attachments_controller.py b/service/pixelated/controllers/attachments_controller.py
index 1d5360f7..4965f166 100644
--- a/service/pixelated/controllers/attachments_controller.py
+++ b/service/pixelated/controllers/attachments_controller.py
@@ -14,12 +14,11 @@
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
-from flask import send_file
-from flask import request
-
import io
import re
-from twisted.web.server import NOT_DONE_YET
+
+from twisted.protocols.basic import FileSender
+from twisted.python.log import err
class AttachmentsController:
@@ -28,12 +27,21 @@ class AttachmentsController:
self.querier = querier
def attachment(self, request, attachment_id):
- encoding = request.args.get('encoding', [''])[0]
+ encoding = request.args.get('encoding')
attachment = self.querier.attachment(attachment_id, encoding)
- request.setRawHeader('Content-Type', self._extract_mimetype(attachment['content-type']))
- request.write(io.BytesIO(attachment['content']))
- return NOT_DONE_YET
+ content_type = self._extract_mimetype(attachment['content-type'])
+ request.setHeader('Content-Type', content_type)
+ bytes_io = io.BytesIO(attachment['content'])
+ d = FileSender().beginFileTransfer(bytes_io, request)
+
+ def cbFinished(ignored):
+ bytes_io.close()
+ request.finish()
+
+ d.addErrback(err).addCallback(cbFinished)
+
+ return d
def _extract_mimetype(self, content_type):
match = re.compile('([A-Za-z-]+\/[A-Za-z-]+)').search(content_type)