about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--.travis.yml5
-rw-r--r--app/models/media_attachment.rb21
-rw-r--r--lib/paperclip/gif_transcoder.rb6
-rw-r--r--spec/fixtures/files/attachment.gifbin0 -> 108560 bytes
-rw-r--r--spec/models/media_attachment_spec.rb22
5 files changed, 44 insertions, 10 deletions
diff --git a/.travis.yml b/.travis.yml
index 564155c66..9725f4993 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,5 +1,7 @@
 language: ruby
 cache: bundler
+dist: trusty
+sudo: required
 
 notifications:
   email: false
@@ -24,8 +26,9 @@ bundler_args: --without development production --retry=3 --jobs=3
 
 before_install:
   - sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
+  - sudo add-apt-repository -y ppa:mc3man/trusty-media
   - sudo apt-get -qq update
-  - sudo apt-get -qq install g++-4.8
+  - sudo apt-get -qq install g++-4.8 ffmpeg
 install:
   - nvm install
   - npm install -g yarn
diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb
index bb16adb5b..181e01674 100644
--- a/app/models/media_attachment.rb
+++ b/app/models/media_attachment.rb
@@ -106,13 +106,18 @@ class MediaAttachment < ApplicationRecord
   end
 
   def set_type_and_extension
-    if file.blank?
-      self.type = :unknown
-    else
-      self.type = VIDEO_MIME_TYPES.include?(file_content_type) ? :video : :image
-      extension = Paperclip::Interpolations.content_type_extension(file, :original)
-      basename  = Paperclip::Interpolations.basename(file, :original)
-      file.instance_write :file_name, [basename, extension].delete_if(&:empty?).join('.')
-    end
+    self.type = VIDEO_MIME_TYPES.include?(file_content_type) ? :video : :image
+    extension = appropriate_extension
+    basename  = Paperclip::Interpolations.basename(file, :original)
+    file.instance_write :file_name, [basename, extension].delete_if(&:empty?).join('.')
+  end
+
+  def appropriate_extension
+    mime_type = MIME::Types[file.content_type]
+
+    extensions_for_mime_type = mime_type.empty? ? [] : mime_type.first.extensions
+    original_extension       = Paperclip::Interpolations.extension(file, :original)
+
+    extensions_for_mime_type.include?(original_extension) ? original_extension : extensions_for_mime_type.first
   end
 end
diff --git a/lib/paperclip/gif_transcoder.rb b/lib/paperclip/gif_transcoder.rb
index 8337448b2..cbe53b27b 100644
--- a/lib/paperclip/gif_transcoder.rb
+++ b/lib/paperclip/gif_transcoder.rb
@@ -7,7 +7,11 @@ module Paperclip
     def make
       num_frames = identify('-format %n :file', file: file.path).to_i
 
-      return file unless options[:style] == :original && num_frames > 1
+      unless options[:style] == :original && num_frames > 1
+        tmp_file = Paperclip::TempfileFactory.new.generate(attachment.instance.file_file_name)
+        tmp_file << file.read
+        return tmp_file
+      end
 
       final_file = Paperclip::Transcoder.make(file, options, attachment)
 
diff --git a/spec/fixtures/files/attachment.gif b/spec/fixtures/files/attachment.gif
new file mode 100644
index 000000000..2937f5abe
--- /dev/null
+++ b/spec/fixtures/files/attachment.gif
Binary files differdiff --git a/spec/models/media_attachment_spec.rb b/spec/models/media_attachment_spec.rb
index 5995aa4f4..d98f7e696 100644
--- a/spec/models/media_attachment_spec.rb
+++ b/spec/models/media_attachment_spec.rb
@@ -1,5 +1,27 @@
 require 'rails_helper'
 
 RSpec.describe MediaAttachment, type: :model do
+  describe 'animated gif conversion' do
+    let(:media) { MediaAttachment.create(account: Fabricate(:account), file: attachment_fixture('avatar.gif')) }
 
+    it 'sets type to gifv' do
+      expect(media.type).to eq 'gifv'
+    end
+
+    it 'converts original file to mp4' do
+      expect(media.file_content_type).to eq 'video/mp4'
+    end
+  end
+
+  describe 'non-animated gif non-conversion' do
+    let(:media) { MediaAttachment.create(account: Fabricate(:account), file: attachment_fixture('attachment.gif')) }
+
+    it 'sets type to image' do
+      expect(media.type).to eq 'image'
+    end
+
+    it 'leaves original file as-is' do
+      expect(media.file_content_type).to eq 'image/gif'
+    end
+  end
 end