Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

allow user to specify frame_index for generating thumbnails from videos or pdfs #2155

Closed
wants to merge 9 commits into from
30 changes: 25 additions & 5 deletions lib/paperclip/thumbnail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ module Paperclip
class Thumbnail < Processor

attr_accessor :current_geometry, :target_geometry, :format, :whiny, :convert_options,
:source_file_options, :animated, :auto_orient
:source_file_options, :animated, :auto_orient, :frame_index

# List of multi frame formats to check against the source file type
# this is not an exhaustive list, should be updated to include more formats
MULTI_FRAME_FORMATS = %w(mkv avi mp4 mov mpg mpeg gif)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

# List of formats that we need to preserve animation
ANIMATED_FORMATS = %w(gif)

Expand All @@ -25,6 +29,7 @@ class Thumbnail < Processor
# +whiny+ - whether to raise an error when processing fails. Defaults to true
# +format+ - the desired filename extension
# +animated+ - whether to merge all the layers in the image. Defaults to true
# +frame_index+ - the frame index of the source file to render as the thumbnail

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [85/80]

def initialize(file, options = {}, attachment = nil)
super

Expand All @@ -36,17 +41,21 @@ def initialize(file, options = {}, attachment = nil)
@convert_options = options[:convert_options]
@whiny = options.fetch(:whiny, true)
@format = options[:format]
@frame_index = options.fetch(:frame_index, 0)
@animated = options.fetch(:animated, true)
@auto_orient = options.fetch(:auto_orient, true)
if @auto_orient && @current_geometry.respond_to?(:auto_orient)
@current_geometry.auto_orient
end

@source_file_options = @source_file_options.split(/\s+/) if @source_file_options.respond_to?(:split)
@convert_options = @convert_options.split(/\s+/) if @convert_options.respond_to?(:split)

@current_format = File.extname(@file.path)
@basename = File.basename(@file.path, @current_format)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

if !multi_frame_format?
@frame_index = 0
end
end

# Returns true if the +target_geometry+ is meant to crop.
Expand Down Expand Up @@ -75,8 +84,12 @@ def make
parameters << ":dest"

parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")

success = convert(parameters, :source => "#{File.expand_path(src.path)}#{'[0]' unless animated?}", :dest => File.expand_path(dst.path))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

desired_frame = animated? ? "" : "[#{@frame_index.to_s}]"
success = convert(parameters,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless assignment to variable - success.

:source => "#{File.expand_path(src.path)}#{desired_frame}",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [85/80]
Use the new Ruby 1.9 hash syntax.

:dest => File.expand_path(dst.path),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the new Ruby 1.9 hash syntax.

)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align ) with (.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tute do you what they want here for alignment?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align ) with (.

rescue Cocaine::ExitStatusError => e
raise Paperclip::Error, "There was an error processing the thumbnail for #{@basename}" if @whiny
rescue Cocaine::CommandNotFoundError => e
Expand All @@ -101,9 +114,16 @@ def transformation_command

protected

# Return true if the source file format is animated
def multi_frame_format?
# removing the leading . from the extension
ext = @current_format.to_s[1..@current_format.length]
MULTI_FRAME_FORMATS.include?(ext)
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

# Return true if the format is animated
def animated?
@animated && (ANIMATED_FORMATS.include?(@format.to_s) || @format.blank?) && identified_as_animated?
@animated && (ANIMATED_FORMATS.include?(@format.to_s) || @format.blank?) && identified_as_animated?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [105/80]

end

# Return true if ImageMagick's +identify+ returns an animated format
Expand Down
33 changes: 33 additions & 0 deletions spec/paperclip/thumbnail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,39 @@ def to_s
assert_equal "50x50", `#{cmd}`.chomp
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

context "with a specified frame_index" do
before do
@thumb = Paperclip::Thumbnail.new(@file,
geometry: "50x50",
frame_index: 5,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align the elements of a hash literal if they span more than one line.

format: :jpg,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align the elements of a hash literal if they span more than one line.
Avoid comma after the last parameter of a method call, unless each item is on its own line.

)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align ) with (.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align ) with (.

end

it "creates the thumbnail from the frame index when sent #make" do
@thumb.make
assert_equal @thumb.frame_index, 5
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

context "with a specified frame_index out of bounds" do
before do
@thumb = Paperclip::Thumbnail.new(@file,
geometry: "50x50",
frame_index: 20,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align the elements of a hash literal if they span more than one line.

format: :jpg,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align the elements of a hash literal if they span more than one line.
Avoid comma after the last parameter of a method call, unless each item is on its own line.

)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align ) with (.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align ) with (.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tute can you help me out with what Hound wants here? I have tried a few different options for aligning the ( ), but it keeps reporting errors

end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

it "errors when trying to create the thumbnail" do
assert_raises(Paperclip::Error) do
silence_stream(STDERR) do
@thumb.make
end
end
end
end
end

context "with a really long file name" do
Expand Down