Joined thumbnail_with_version with thumbnail dev to remove redundant code

This commit is contained in:
Christoph Haas 2016-06-27 16:00:28 +02:00
parent e864415a03
commit 0c4a0d9a3a
2 changed files with 7 additions and 26 deletions

View file

@ -77,34 +77,15 @@ class PackagesController < ApplicationController
thumbnail404
return
end
@screenshot = @package.screenshots.first
# Return a 404 if the package has no screenshots or the image was not found
unless @screenshot and @screenshot.image.path
thumbnail404
return
# 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
# 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
# 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

View file

@ -27,7 +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+/
get 'thumbnail-with-version/:name/:version' => 'packages#thumbnail', name: /[^\/]+/, version: /\S+/
# Legacy URLs
get 'with_screenshots', to: redirect('/packages?show=with')