Showing information about failure or success of image uploads

This commit is contained in:
Christoph Haas 2015-04-27 00:21:08 +02:00
parent e64c31b8af
commit 19e51445fd

View file

@ -26,22 +26,27 @@ class PackagesController < ApplicationController
def upload_image
@package = Package.find_by(name: params[:name])
successful_upload_count = 0
params[:screenshot][:image].each do |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)
# 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
# flash['alert'] = "Sorry - you uploaded a duplicate screenshot. I am ignoring it."
# Check if the image was valid
unless new_screenshot.valid?
flash['alert'] = "Sorry - you uploaded an invalid file. Was it really a PNG?"
errors = new_screenshot.errors[:image].join(' and ')
flash['alert'] = "Sorry - the image #{errors}"
else
new_screenshot.save
successful_upload_count += 1
end
if successful_upload_count > 0
flash['info'] = "#{successful_upload_count} #{'screenshot'.pluralize(successful_upload_count)} uploaded successfully."
end
# TODO: add logging