X-Git-Url: http://git.veekun.com/zzz-floof.git/blobdiff_plain/ac3d66885419d71d6e6d16080fcf09ecffa66fe9..dcf6146e9914ff3c166f242b72f04a8934d65148:/floof/controllers/art.py diff --git a/floof/controllers/art.py b/floof/controllers/art.py index 41298c8..5b347b3 100644 --- a/floof/controllers/art.py +++ b/floof/controllers/art.py @@ -1,22 +1,26 @@ import logging -from pylons import request, response, session, tmpl_context as c, h +from pylons import config, request, response, session, tmpl_context as c, h from pylons.controllers.util import abort, redirect from pylons import url from floof.lib.base import BaseController, render log = logging.getLogger(__name__) -import elixir +from floof.lib import file_storage as storage from floof.model.users import User from floof.model import Art, Rating, UserRelation from floof.model.comments import Discussion from floof.model.users import User, UserRelationship +import elixir +#import magic +import os.path +import PIL +import PIL.Image from sqlalchemy import func from sqlalchemy.exceptions import IntegrityError from sqlalchemy.orm.exc import NoResultFound - from wtforms.validators import ValidationError from wtforms import * @@ -79,32 +83,66 @@ class ArtController(BaseController): # TODO: login required def create(self): c.form = ArtUploadForm(request.params) - if c.form.validate(): + if not c.form.validate(): + ## TODO: JavaScript should be added to the upload form so that it is + ## impossible to submit the form when it contains any invalid users, + ## so this never happens. Only autocompled usernames should be allowed. + return render("/art/new.mako") - c.art = Art(uploader=c.user, **request.params) - c.art.discussion = Discussion(count=0) + # Save the file + upload = request.params['file'] + hash = storage.save_file('art/original', upload.file) - for artist in c.form.by.data: - UserRelation(user=artist, kind="by", creator=c.user, art=c.art) + # Create a thumbnail and a medium view + img = PIL.Image.open( + config['app_conf']['static_root'] + + storage.get_path('art/original', hash)[1:] + ) + for subdir, config_size in [('medium', config['medium_size']), + ('thumbnail', config['thumbnail_size'])]: + path = config['app_conf']['static_root'] + storage.get_path("art/{0}".format(subdir), hash) - file = request.params['file'] + dir, _ = os.path.split(path) + if not os.path.exists(dir): + os.makedirs(dir) - try: - elixir.session.commit() - redirect(url('show_art', id=c.art.id)) - except IntegrityError: - # hurr, there must be a better way to do this but I am lazy right now - hash = c.art.hash - elixir.session.rollback() - duplicate_art = Art.get_by(hash=hash) - h.flash("We already have that one.") - redirect(url('show_art', id=duplicate_art.id)) + size = int(config_size) + shrunken_img = img.copy() + shrunken_img.thumbnail((size, size), PIL.Image.ANTIALIAS) + shrunken_img.save(path, img.format) + + #magicker = magic.Magic(mime=True) + buffer = upload.file.read(64) + upload.file.seek(0) + mimetype = 'image/unknown' #magickery.from_buffer(buffer) + + # XXX Ensure we can actually handle the mimetype + + print mimetype + c.art = Art( + uploader=c.user, + original_filename=upload.filename, + hash=hash, + mimetype=mimetype, + ) + c.art.discussion = Discussion(count=0) + + for artist in c.form.by.data: + UserRelation(user=artist, kind="by", creator=c.user, art=c.art) + + + try: + elixir.session.commit() + redirect(url('show_art', id=c.art.id)) + except IntegrityError: + # XXX Check this as early as possible, and clean up the filesystem! + # Right now this replaces the original file! + hash = c.art.hash + elixir.session.rollback() + duplicate_art = Art.get_by(hash=hash) + h.flash("We already have that one.") + redirect(url('show_art', id=duplicate_art.id)) - else: - ## TODO: JavaScript should be added to the upload form so that it is - ## impossible to submit the form when it contains any invalid users, - ## so this never happens. Only autocompled usernames should be allowed. - return render("/art/new.mako") def show(self, id):