Added validations for uploaded screenshots.

Checks if Paperclip complained about an invalid file (e.g. no PNG)
Checks if the upload is a duplicate.
This commit is contained in:
Christoph Haas 2015-04-26 01:42:22 +02:00
parent 96fcb9c940
commit e8c4694ef0

View file

@ -27,7 +27,23 @@ class PackagesController < ApplicationController
def upload_image
@package = Package.find_by(name: params[:name])
params[:screenshot][:image].each do |img|
@package.screenshots.create(image: img)
new_screenshot = @package.screenshots.new(image: img)
# Check if the image is already present by checking its checksum.
if @package.screenshots.find_by(image_fingerprint: new_screenshot.image_fingerprint)
# logger.error "Uploaded image exists already for package #{@package.name}. Rejecting."
flash['alert'] = "Sorry - you uploaded a duplicate screenshot. I am ignoring it."
else
new_screenshot.save
end
# Check if the image was valid
unless new_screenshot.valid?
flash['alert'] = "Sorry - you uploaded an invalid file. Was it really a PNG?"
else
new_screenshot.save
end
# TODO: add logging
# TODO: what do we do if the user uploads an invalid image? tell them?
end