debshots/app/controllers/packages_controller.rb
Christoph Haas c085df9eed Implemented the /thumbnail/PACKAGE url
Returns a 160x120 thumbnail image if possible
Returns a dummy image and a 404 status code otherwise.
2015-04-19 12:56:58 +02:00

102 lines
2.7 KiB
Ruby

class PackagesController < ApplicationController
def index
@packages = Package.includes(:screenshots)
if params[:query]
@packages = @packages.text_search(params[:query])
end
unless params[:search].blank?
logger.debug "Searching for #{params[:search]}"
@packages = @packages.general_search(params[:search])
end
# Limit the packages to those that have approved screenshots.
# Also eager-load the screenshots.
if params[:show]=='onlywith'
@packages = @packages.where("screenshots.approved"=>true)
end
if params[:show]=='with'
@packages = @packages.with_screenshots
logger.debug 'Limiting packages to those with screenshots'
elsif params[:show]=='without'
@packages = @packages.without_screenshots
logger.debug 'Limiting packages to those without screenshots'
end
end
def list
index
@packages = @packages.paginate(page: params[:page], per_page: 6)
render 'packages/index-list.slim'
end
def grid
index
@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|
@package.screenshots.create(image: img)
# 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."
def thumbnail404
send_file Rails.root.join('public/images/dummy/no-screenshots-available.png'),
type: "image/png",
disposition: 'inline',
status: 404
end
end