Checks if Paperclip complained about an invalid file (e.g. no PNG) Checks if the upload is a duplicate.
114 lines
3.2 KiB
Ruby
114 lines
3.2 KiB
Ruby
class PackagesController < ApplicationController
|
|
def list
|
|
@packages = query_packages
|
|
@packages = @packages.paginate(page: params[:page], per_page: 6)
|
|
render 'packages/index-list.slim'
|
|
end
|
|
|
|
def grid
|
|
@packages = query_packages
|
|
@packages = @packages.paginate(page: params[:page], per_page: 24)
|
|
render 'packages/index-grid.slim'
|
|
end
|
|
|
|
def details
|
|
@package = Package.find_by(name: params[:name])
|
|
end
|
|
|
|
def moderate
|
|
# TODO
|
|
end
|
|
|
|
def upload
|
|
raise "name not given" unless params[:name]
|
|
@package = Package.find_by(name: params[:name])
|
|
end
|
|
|
|
def upload_image
|
|
@package = Package.find_by(name: params[:name])
|
|
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)
|
|
# 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
|
|
|
|
redirect_to package_path
|
|
end
|
|
|
|
def delete_screenshot
|
|
@screenshot = Screenshot.find_by(id: params[:id])
|
|
@screenshot.destroy
|
|
|
|
redirect_to package_path
|
|
end
|
|
|
|
# Returns a 160x120 thumbnail image if posssible.
|
|
# If the package is not found it returns a dummy image along with status 404.
|
|
# If the package is found but has no screenshots then it also returns a
|
|
# dummy image along with status 404.
|
|
def thumbnail
|
|
@package = Package.find_by(name: params[:name])
|
|
unless @package
|
|
thumbnail404
|
|
return
|
|
end
|
|
@screenshot = @package.screenshots.first
|
|
unless @screenshot.image.path
|
|
thumbnail404
|
|
return
|
|
end
|
|
|
|
# Send the thumbnail
|
|
# TODO: Make sure it uses X-Sendfile correctly in production
|
|
send_file @screenshot.image.path(:thumb), type: "image/png", disposition: 'inline'
|
|
end
|
|
|
|
private
|
|
|
|
# Send a dummy thumbnail reading "No screenshot available. Sorry."
|
|
# TODO: Make sure it uses X-Sendfile correctly in production
|
|
def thumbnail404
|
|
send_file Rails.root.join('public/images/dummy/no-screenshots-available.png'),
|
|
type: "image/png",
|
|
disposition: 'inline',
|
|
status: 404
|
|
end
|
|
|
|
# Return packages matching the criteria given by parameters
|
|
def query_packages
|
|
packages = Package.includes(:screenshots)
|
|
|
|
# text search
|
|
if params[:search].present?
|
|
logger.debug "Searching for #{params[:search]}"
|
|
packages = packages.general_search(params[:search])
|
|
end
|
|
|
|
case params[:show]
|
|
when 'with'
|
|
packages = packages.with_screenshots
|
|
logger.debug 'Limiting packages to those with screenshots'
|
|
when 'without'
|
|
packages = packages.without_screenshots
|
|
logger.debug 'Limiting packages to those without screenshots'
|
|
end
|
|
|
|
return packages
|
|
end
|
|
end
|