about summary refs log tree commit diff
path: root/lib/paperclip
diff options
context:
space:
mode:
Diffstat (limited to 'lib/paperclip')
-rw-r--r--lib/paperclip/gif_transcoder.rb2
-rw-r--r--lib/paperclip/lazy_thumbnail.rb28
2 files changed, 29 insertions, 1 deletions
diff --git a/lib/paperclip/gif_transcoder.rb b/lib/paperclip/gif_transcoder.rb
index 629e37581..62787983c 100644
--- a/lib/paperclip/gif_transcoder.rb
+++ b/lib/paperclip/gif_transcoder.rb
@@ -16,7 +16,7 @@ module Paperclip
 
       final_file = Paperclip::Transcoder.make(file, options, attachment)
 
-      attachment.instance.file_file_name    = 'media.mp4'
+      attachment.instance.file_file_name    = File.basename(attachment.instance.file_file_name, '.*') + '.mp4'
       attachment.instance.file_content_type = 'video/mp4'
       attachment.instance.type              = MediaAttachment.types[:gifv]
 
diff --git a/lib/paperclip/lazy_thumbnail.rb b/lib/paperclip/lazy_thumbnail.rb
new file mode 100644
index 000000000..aafa21343
--- /dev/null
+++ b/lib/paperclip/lazy_thumbnail.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+module Paperclip
+  class LazyThumbnail < Paperclip::Thumbnail
+    def make
+      return File.open(@file.path) unless needs_convert?
+
+      min_side = [@current_geometry.width, @current_geometry.height].min
+      options[:geometry] = "#{min_side.to_i}x#{min_side.to_i}#" if @target_geometry.square? && min_side < @target_geometry.width
+
+      Paperclip::Thumbnail.make(file, options, attachment)
+    end
+
+    private
+
+    def needs_convert?
+      needs_different_geometry? || needs_different_format?
+    end
+
+    def needs_different_geometry?
+      !@target_geometry.nil? && @current_geometry.width != @target_geometry.width && @current_geometry.height != @target_geometry.height
+    end
+
+    def needs_different_format?
+      @format.present? && @current_format != @format
+    end
+  end
+end