/thumbnail-with-version/package/version implemented

This commit is contained in:
Christoph Haas 2016-06-24 16:18:36 +02:00
parent 6feaa53920
commit 9603550aab
2 changed files with 27 additions and 0 deletions

View file

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

View file

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