Delivering proper PNG 404 images for thumbnails and large images

This commit is contained in:
Christoph Haas 2016-06-27 17:38:24 +02:00
parent d6bfba2e83
commit 205c624778
4 changed files with 40 additions and 4 deletions

View file

@ -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

View file

@ -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')

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

View file

Before

Width:  |  Height:  |  Size: 7.4 KiB

After

Width:  |  Height:  |  Size: 7.4 KiB

Before After
Before After