From ab5a41f6864f9ce9ec40f18d18130436767d6296 Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Sun, 2 Jul 2017 11:37:46 -0500 Subject: [PATCH] upload: Add form to upload images The upload form allows users to upload images to the Dark Chest of Wonders screenshot gallery. No authentication, etc. is performed, so this needs to be handled by the web server. --- src/dcow/app.py | 2 + src/dcow/templates/base.html.j2 | 5 +++ src/dcow/templates/upload.html.j2 | 75 +++++++++++++++++++++++++++++++ src/dcow/upload.py | 46 +++++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 src/dcow/templates/upload.html.j2 create mode 100644 src/dcow/upload.py diff --git a/src/dcow/app.py b/src/dcow/app.py index 3a03087..c77e738 100644 --- a/src/dcow/app.py +++ b/src/dcow/app.py @@ -3,6 +3,7 @@ from . import ( base, gallery, thumbnails, + upload, ) import functools import logging @@ -40,6 +41,7 @@ class Application(milla.Application): r.add_route('/', gallery.GalleryController()) r.add_route('/thumbnails/{image}', thumbnails.ThumbnailController()) + r.add_route('/upload', upload.UploadController()) def make_request(self, environ): request = super(Application, self).make_request(environ) diff --git a/src/dcow/templates/base.html.j2 b/src/dcow/templates/base.html.j2 index ec23e43..a02aefd 100644 --- a/src/dcow/templates/base.html.j2 +++ b/src/dcow/templates/base.html.j2 @@ -18,6 +18,11 @@ {% block body %} {% endblock body -%} +{% block script -%} + +{% endblock script -%} diff --git a/src/dcow/templates/upload.html.j2 b/src/dcow/templates/upload.html.j2 new file mode 100644 index 0000000..25ef142 --- /dev/null +++ b/src/dcow/templates/upload.html.j2 @@ -0,0 +1,75 @@ +{% extends "base.html.j2" %} +{% block head -%} +{{ super() }} + +{% endblock head %} +{% block body -%} +
+
+ + + +
+ +
+
+
+
+
+
+
+ +{%- if error is defined %} +{{ error.message }} +{% endif -%} + +
+{% endblock body -%} +{% block script -%} +{{ super() }} + + + + +{% endblock script -%} diff --git a/src/dcow/upload.py b/src/dcow/upload.py new file mode 100644 index 0000000..9c978f4 --- /dev/null +++ b/src/dcow/upload.py @@ -0,0 +1,46 @@ +from . import gallery +import errno +import milla.controllers +import os + + +class UploadController(milla.controllers.HTTPVerbController): + + def GET(self, request): + # XXX blueimp jQuery File Upload plugin does not work in XML parser + request.want = 'html' + response = request.ResponseClass() + response.set_payload('upload.html.j2') + return response + + def POST(self, request): + screenshot_dir = request.config['gallery.screenshot_dir'] + f = request.POST['file'] + path = os.path.join(screenshot_dir, f.filename) + try: + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL) + except (IOError, OSError) as e: + if e.errno == errno.EEXIST: + response = request.ResponseClass() + response.status_int = milla.HTTPConflict.code + response.set_payload('upload.html.j2', { + 'error': { + 'message': + 'Image {} already exists'.format(f.filename), + 'code': response.status_int, + } + }) + return response + else: + raise milla.HTTPServerError( + '{}'.format(e), + ) + else: + with os.fdopen(fd, 'wb') as q: + for d in iter(lambda: f.file.read(4096), b''): + q.write(d) + + if request.want == 'json': + raise milla.HTTPCreated + else: + raise milla.HTTPSeeOther(location='/')