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='/')