diff --git a/app/controllers/packages_controller.rb b/app/controllers/packages_controller.rb index 23c24f8..ae0836c 100644 --- a/app/controllers/packages_controller.rb +++ b/app/controllers/packages_controller.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 1f65a4b..46b523c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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". diff --git a/public/images/dummy/no-screenshots-available.png b/public/images/dummy/no-screenshots-available.png new file mode 100644 index 0000000..3141c18 Binary files /dev/null and b/public/images/dummy/no-screenshots-available.png differ