Implemented the /thumbnail/PACKAGE url
Returns a 160x120 thumbnail image if possible Returns a dummy image and a 404 status code otherwise.
This commit is contained in:
parent
8b0ff06172
commit
c085df9eed
3 changed files with 32 additions and 0 deletions
|
|
@ -68,4 +68,35 @@ class PackagesController < ApplicationController
|
|||
|
||||
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
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ Debshots::Application.routes.draw do
|
|||
post 'upload_image/:name' => 'packages#upload_image', as: :upload_image
|
||||
get 'delete_screenshot/:name/:id' => 'packages#delete_screenshot', as: :delete_screenshot
|
||||
get 'about' => 'welcome#about'
|
||||
get 'thumbnail/:name' => 'packages#thumbnail', as: :thumbnail_image
|
||||
|
||||
# The priority is based upon order of creation: first created -> highest priority.
|
||||
# See how all your routes lay out with "rake routes".
|
||||
|
|
|
|||
BIN
public/images/dummy/no-screenshots-available.png
Normal file
BIN
public/images/dummy/no-screenshots-available.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 7.4 KiB |
Loading…
Add table
Add a link
Reference in a new issue