diff --git a/app/controllers/packages_controller.rb b/app/controllers/packages_controller.rb index 1275edd..b7cafbb 100644 --- a/app/controllers/packages_controller.rb +++ b/app/controllers/packages_controller.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index f591dc0..b997f73 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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') diff --git a/public/images/dummy/screenshot404.png b/public/images/dummy/screenshot404.png new file mode 100644 index 0000000..18b455d Binary files /dev/null and b/public/images/dummy/screenshot404.png differ diff --git a/public/images/dummy/no-screenshots-available.png b/public/images/dummy/thumbnail404.png similarity index 100% rename from public/images/dummy/no-screenshots-available.png rename to public/images/dummy/thumbnail404.png