182 lines
5.6 KiB
Ruby
182 lines
5.6 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 upload
|
|
raise "name not given" unless params[:name]
|
|
@package = Package.find_by(name: params[:name])
|
|
end
|
|
|
|
# POST target of the screenshots upload form.
|
|
# Checks upload images and creates a new Screenshot record for it.
|
|
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 was valid
|
|
if new_screenshot.valid?
|
|
new_screenshot.uploaderhash = session[:token]
|
|
new_screenshot.uploaderip = session[:ip]
|
|
new_screenshot.save
|
|
successful_upload_count += 1
|
|
Log.log "Screenshot #{new_screenshot.id} uploaded successfully."
|
|
else
|
|
errors = new_screenshot.errors[:image].join(' and ')
|
|
flash['alert'] = "Sorry - the image #{errors}"
|
|
end
|
|
|
|
if successful_upload_count > 0
|
|
flash['notice'] = "#{successful_upload_count} #{'screenshot'.pluralize(successful_upload_count)} uploaded successfully."
|
|
end
|
|
end
|
|
|
|
redirect_to package_path
|
|
end
|
|
|
|
def delete_screenshot
|
|
# Is the user allowed to delete the screenshot?
|
|
@screenshot = Screenshot.find(params[:id])
|
|
|
|
if user_can_alter_screenshot?
|
|
@screenshot.destroy
|
|
flash['notice'] = "Screenshot deleted."
|
|
redirect_to :back
|
|
else
|
|
head :forbidden
|
|
end
|
|
end
|
|
|
|
def approve_screenshot
|
|
@screenshot = Screenshot.find(params[:id])
|
|
@screenshot.approve_screenshot!
|
|
flash['notice'] = "Screenshot approved."
|
|
redirect_to :back
|
|
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
|
|
|
|
# Return a 404 if the package has no screenshots or the image was not found
|
|
unless @screenshot and @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
|
|
|
|
# Similar to 'def thumbnail'. But tries to find a screenshot that matches the given version best.
|
|
# This algorithm collects all image
|
|
# versions of a package and determines the (second) newest version.
|
|
# E.g. if there are version 1.0 and 2.0 and the user is looking for
|
|
# a screenshot of version 1.5 then the 1.0 version is returned.
|
|
# This way the user does not see a screenshot of version 2.0 because
|
|
# 2.0 might contain features that were not there in version 1.5.
|
|
def thumbnail_with_version
|
|
@package = Package.find_by(name: params[:name])
|
|
unless @package
|
|
thumbnail404
|
|
return
|
|
end
|
|
@screenshot = @package.best_screenshot_for_version(params[:version])
|
|
|
|
# Return a 404 if the package has no screenshots or the image was not found
|
|
unless @screenshot and @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
|
|
|
|
# Receives a form with a simple text field 'description' so that users can update
|
|
# the description of their screenshot.
|
|
def update_screenshot_description
|
|
@screenshot = Screenshot.find(params[:id])
|
|
@screenshot.description = params[:description]
|
|
@screenshot.save!
|
|
flash['notice'] = "Description updated."
|
|
redirect_to :back
|
|
end
|
|
|
|
# Receive an anonymous report from a user to have a screenshot removed.
|
|
def report_screenshot
|
|
@screenshot = Screenshot.find(params[:id])
|
|
@screenshot.delete_reason = params[:delete_reason]
|
|
@screenshot.markedfordelete = true
|
|
@screenshot.save!
|
|
flash['notice'] = "Screenshot reported. The moderators will deal with it."
|
|
redirect_to :back
|
|
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
|
|
|
|
# Check if the user is allowed to do changed to a screenshot
|
|
def user_can_alter_screenshot?
|
|
# - Is this the user's own screenshot?
|
|
# - Is the user an admin (=logged in)?
|
|
@screenshot.uploaderhash == session[:token] or user_signed_in?
|
|
end
|
|
end
|