From 9603550aabbaf6e72e73169b096bba33249367ec Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Fri, 24 Jun 2016 16:18:36 +0200 Subject: [PATCH] /thumbnail-with-version/package/version implemented --- app/controllers/packages_controller.rb | 26 ++++++++++++++++++++++++++ config/routes.rb | 1 + 2 files changed, 27 insertions(+) diff --git a/app/controllers/packages_controller.rb b/app/controllers/packages_controller.rb index 626d230..b7b7043 100644 --- a/app/controllers/packages_controller.rb +++ b/app/controllers/packages_controller.rb @@ -94,6 +94,32 @@ class PackagesController < ApplicationController send_file @screenshot.image.path(:thumb), type: "image/png", disposition: 'inline' end + # Similar to 'def thumbnail'. But tries to find a screenshot that matches the given version best. + # This algorithm collects all image + # versions of a package and determines the (second) newest version. + # E.g. if there are version 1.0 and 2.0 and the user is looking for + # a screenshot of version 1.5 then the 1.0 version is returned. + # This way the user does not see a screenshot of version 2.0 because + # 2.0 might contain features that were not there in version 1.5. + def thumbnail_with_version + @package = Package.find_by(name: params[:name]) + unless @package + thumbnail404 + return + end + @screenshot = @package.best_screenshot_for_version(params[:version]) + + # Return a 404 if the package has no screenshots or the image was not found + unless @screenshot and @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 + # Receives a form with a simple text field 'description' so that users can update # the description of their screenshot. def update_screenshot_description diff --git a/config/routes.rb b/config/routes.rb index e1f1d01..ad07a4b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -27,6 +27,7 @@ Debshots::Application.routes.draw do get 'approve_screenshot/:id' => 'packages#approve_screenshot', as: :approve_screenshot get 'about' => 'welcome#about' get 'thumbnail/:name' => 'packages#thumbnail', as: :thumbnail_image, name: /[^\/]+/ + get 'thumbnail-with-version/:name/:version' => 'packages#thumbnail_with_version', name: /[^\/]+/, version: /\S+/ # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes".