diff --git a/rophako/model/photo.py b/rophako/model/photo.py index 515f4c5..b1edfbf 100644 --- a/rophako/model/photo.py +++ b/rophako/model/photo.py @@ -293,16 +293,32 @@ def upload_from_pc(request): """ form = request.form - upload = request.files["file"] + count = 0 + status = None + for upload in request.files.getlist("file"): + count += 1 - # Make a temp filename for it. - filetype = upload.filename.rsplit(".", 1)[1] - tempfile = "{}/rophako-photo-{}.{}".format(config.TEMPDIR, int(time.time()), filetype) - logger.debug("Save incoming photo to: {}".format(tempfile)) - upload.save(tempfile) + # Make a temp filename for it. + filetype = upload.filename.rsplit(".", 1)[-1] + if not allowed_filetype(upload.filename): + return dict(success=False, error="Unsupported file extension.") - # All good so far. Process the photo. - return process_photo(form, tempfile) + tempfile = "{}/rophako-photo-{}.{}".format(config.TEMPDIR, int(time.time()), filetype) + logger.debug("Save incoming photo to: {}".format(tempfile)) + upload.save(tempfile) + + # All good so far. Process the photo. + status = process_photo(form, tempfile) + if not status["success"]: + return status + + # Multi upload? + if count > 1: + status["multi"] = True + else: + status["multi"] = False + + return status def upload_from_www(form): diff --git a/rophako/modules/photo.py b/rophako/modules/photo.py index 9775cc9..6a6c0f7 100644 --- a/rophako/modules/photo.py +++ b/rophako/modules/photo.py @@ -6,7 +6,7 @@ from flask import Blueprint, g, request, redirect, url_for, session, flash import rophako.model.user as User import rophako.model.photo as Photo -from rophako.utils import template, pretty_time, login_required +from rophako.utils import template, pretty_time, login_required, ajax_response from rophako.log import logger from config import * @@ -68,7 +68,15 @@ def upload(): """Upload a photo.""" if request.method == "POST": - # We're posting the upload. What source is the pic from? + # We're posting the upload. + + # Is this an ajax post or a direct post? + is_ajax = request.form.get("__ajax", "false") == "true" + + # Album name. + album = request.form.get("album") or request.form.get("new-album") + + # What source is the pic from? result = None location = request.form.get("location") if location == "pc": @@ -83,9 +91,24 @@ def upload(): # How'd it go? if result["success"] is not True: - flash("The upload has failed: {}".format(result["error"])) - return redirect(url_for(".upload")) - return redirect(url_for(".crop", photo=result["photo"])) + if is_ajax: + return ajax_response(False, result["error"]) + else: + flash("The upload has failed: {}".format(result["error"])) + return redirect(url_for(".upload")) + + # Good! + if is_ajax: + # Was it a multiple upload? + if result["multi"]: + return ajax_response(True, url_for(".album_index", name=album)) + else: + return ajax_response(True, url_for(".crop", photo=result["photo"])) + else: + if result["multi"]: + return redirect(url_for(".album_index", name=album)) + else: + return redirect(url_for(".crop", photo=result["photo"])) # Get the list of available albums. g.info["album_list"] = [ diff --git a/rophako/utils.py b/rophako/utils.py index b6004e3..d40adc6 100644 --- a/rophako/utils.py +++ b/rophako/utils.py @@ -10,6 +10,7 @@ import re import importlib import smtplib import markdown +import json from rophako.log import logger from config import * @@ -45,6 +46,15 @@ def admin_required(f): return decorated_function +def ajax_response(status, msg): + """Return a standard JSON response.""" + status = "ok" if status else "error" + return json.dumps(dict( + status=status, + msg=msg, + )) + + def template(name, **kwargs): """Render a template to the browser.""" diff --git a/rophako/www/photos/upload.html b/rophako/www/photos/upload.html index c0738f6..676c754 100644 --- a/rophako/www/photos/upload.html +++ b/rophako/www/photos/upload.html @@ -7,7 +7,7 @@ You can upload a photo from your computer or by pasting in the URL to a photo somewhere else on the Internet. -
+
@@ -22,7 +22,10 @@ somewhere else on the Internet.
Upload a picture from my computer
- +

+ + Or, drag images here:
+

Drag and drop images into this box
@@ -34,6 +37,7 @@ somewhere else on the Internet. Only jpeg, gif and png images are supported. There is no maximum file size limit, but be reasonable.
+

Photo Options @@ -58,13 +62,23 @@ somewhere else on the Internet.

- +

+

+ +

{% endblock %} {% block scripts %} +