Delivering proper PNG 404 images for thumbnails and large images
This commit is contained in:
parent
d6bfba2e83
commit
205c624778
4 changed files with 40 additions and 4 deletions
|
|
@ -92,11 +92,39 @@ class PackagesController < ApplicationController
|
|||
return
|
||||
end
|
||||
|
||||
# Send the thumbnail
|
||||
# TODO: Make sure it uses X-Sendfile correctly in production
|
||||
# Send the thumbnail (uses X-Sendfile or similar if possible)
|
||||
send_file @screenshot.image.path(:thumb), type: "image/png", disposition: 'inline'
|
||||
end
|
||||
|
||||
# Returns a large screenshot 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 screenshot
|
||||
@package = Package.find_by(name: params[:name])
|
||||
unless @package
|
||||
screenshot404
|
||||
return
|
||||
end
|
||||
|
||||
# Called as /thumbnail-with-version/:name/:version
|
||||
if params[:version]
|
||||
@screenshot = @package.best_screenshot_for_version(params[:version])
|
||||
# Called as /thumbnail/:name
|
||||
else
|
||||
@screenshot = @package.screenshots.first
|
||||
end
|
||||
|
||||
# Return a 404 if the package has no screenshots or the image was not found
|
||||
unless @screenshot and @screenshot.image.path
|
||||
screenshot404
|
||||
return
|
||||
end
|
||||
|
||||
# Send the thumbnail (uses X-Sendfile or similar if possible)
|
||||
send_file @screenshot.image.path(:large), 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
|
||||
|
|
@ -120,9 +148,15 @@ class PackagesController < ApplicationController
|
|||
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'),
|
||||
send_file Rails.root.join('public/images/dummy/thumbnail404.png'),
|
||||
type: "image/png",
|
||||
disposition: 'inline',
|
||||
status: 404
|
||||
end
|
||||
|
||||
def screenshot404
|
||||
send_file Rails.root.join('public/images/dummy/screenshot404.png'),
|
||||
type: "image/png",
|
||||
disposition: 'inline',
|
||||
status: 404
|
||||
|
|
|
|||
|
|
@ -28,6 +28,8 @@ Debshots::Application.routes.draw do
|
|||
get 'about' => 'welcome#about'
|
||||
get 'thumbnail/:name' => 'packages#thumbnail', as: :thumbnail_image, name: /[^\/]+/
|
||||
get 'thumbnail-with-version/:name/:version' => 'packages#thumbnail', name: /[^\/]+/, version: /\S+/
|
||||
get 'screenshot/:name' => 'packages#screenshot', as: :screenshot_image, name: /[^\/]+/
|
||||
get 'screenshot-with-version/:name/:version' => 'packages#screenshot', name: /[^\/]+/, version: /\S+/
|
||||
|
||||
# Legacy URLs
|
||||
get 'with_screenshots', to: redirect('/packages?show=with')
|
||||
|
|
|
|||
BIN
public/images/dummy/screenshot404.png
Normal file
BIN
public/images/dummy/screenshot404.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 30 KiB |
|
Before Width: | Height: | Size: 7.4 KiB After Width: | Height: | Size: 7.4 KiB |
Loading…
Add table
Add a link
Reference in a new issue