carrierwave setting content_type incorrectly

2020-08-08 11:13发布

I am using Carrierwave with fog storage in my Rails app, and I'm using the Carrierwave-Video and Carrierwave-Video-Thumbnailer gems to enable video uploading and thumbnail creation from the uploaded video.

When I upload an mp4 file, I expect it to transcode it to mp4, create a webm version, and create two differently-sized thumbnail images (.png). Right now, all the files are successfully uploaded to Amazon S3, but the content-type is getting processed as video/quicktime for all files. This is causing an issue for me because I want to play webm files in Firefox using the HTML 5 Video tag, and it's not able to play them since the content type of the video is set to video/quicktime rather than video/webm.

This is my uploader file, video_path_uploader.rb

require 'carrierwave/processing/mime_types'

class VideoPathUploader < CarrierWave::Uploader::Base
  include CarrierWave::Video
  include CarrierWave::Video::Thumbnailer
  include CarrierWave::MimeTypes

  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  # storage :file
  storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  process encode_video: [:mp4]

  process :set_content_type

  def filename
    result = [original_filename.gsub(/\.\w+$/, ""), 'mp4'].join('.') if original_filename
    result
  end

  version :webm do
    process :encode_video => [:webm]
    process :set_content_type
    def full_filename(for_file)
      "#{File.basename(for_file, File.extname(for_file))}.webm"
    end
  end


  version :thumb do 
    process thumbnail: [{format: 'png', quality: 10, size: 158, logger: Rails.logger}]
    def full_filename for_file
       png_name for_file, version_name
    end
    process :set_content_type
    # process resize_to_limit: [105, 158]
  end

  version :square_thumb do
     process thumbnail: [{format: 'png', quality: 10, size: 105, strip: false, logger: Rails.logger}]
    def full_filename for_file
      png_name for_file, version_name
    end
    process :set_content_type
    # process resize_to_fill: [105, 105]
  end

  def png_name for_file, version_name
    %Q{#{version_name}_#{for_file.chomp(File.extname(for_file))}.png}
  end

end

My rails log when I upload a video:

Running transcoding...
ffmpeg -y -i /Documents/Website/public/uploads/tmp/1382543537-3350-3237/Untitled.mov -vcodec libx264 -acodec libfaac -s 640x360  -qscale 0 -preset slow -g 30 -aspect 1.7777777777777777 /Documents/Website/public/uploads/tmp/1382543537-3350-3237/tmpfile.mp4

Transcoding of /Documents/Website/public/uploads/tmp/1382543537-3350-3237/Untitled.mov to /Documents/Website/public/uploads/tmp/1382543537-3350-3237/tmpfile.mp4 succeeded

Running transcoding...
ffmpeg -y -i /Documents/Website/public/uploads/tmp/1382543537-3350-3237/webm_Untitled.mov -vcodec libvpx -acodec libvorbis -s 640x360  -b 1500k -ab 160000 -f webm -g 30 -aspect 1.7777777777777777 /Documents/Website/public/uploads/tmp/1382543537-3350-3237/tmpfile.webm

Transcoding of /Documents/Website/public/uploads/tmp/1382543537-3350-3237/webm_Untitled.mov to /Documents/Website/public/uploads/tmp/1382543537-3350-3237/tmpfile.webm succeeded

Invalid Excon request keys: :scheme, :host
/.rvm/gems/ruby-1.9.3-p448/gems/excon-0.27.6/lib/excon/connection.rb:232:in `request'
/.rvm/gems/ruby-1.9.3-p448/gems/fog-1.16.0/lib/fog/core/connection.rb:57:in `request'
/.rvm/gems/ruby-1.9.3-p448/gems/fog-1.16.0/lib/fog/core/deprecated/connection.rb:20:in `request'
/.rvm/gems/ruby-1.9.3-p448/gems/fog-1.16.0/lib/fog/aws/storage.rb:513:in `request'
/.rvm/gems/ruby-1.9.3-p448/gems/fog-1.16.0/lib/fog/aws/requests/storage/put_object.rb:40:in `put_object'
/.rvm/gems/ruby-1.9.3-p448/gems/fog-1.16.0/lib/fog/aws/models/storage/file.rb:211:in `save'
/.rvm/gems/ruby-1.9.3-p448/gems/fog-1.16.0/lib/fog/core/collection.rb:52:in `create'
/.rvm/gems/ruby-1.9.3-p448/gems/carrierwave-0.9.0/lib/carrierwave/storage/fog.rb:260:in `store'
/.rvm/gems/ruby-1.9.3-p448/gems/carrierwave-0.9.0/lib/carrierwave/storage/fog.rb:80:in `store!'
/.rvm/gems/ruby-1.9.3-p448/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:59:in `block in store!'
/.rvm/gems/ruby-1.9.3-p448/gems/carrierwave-0.9.0/lib/carrierwave/uploader/callbacks.rb:17:in `with_callbacks'
/.rvm/gems/ruby-1.9.3-p448/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:58:in `store!'
/.rvm/gems/ruby-1.9.3-p448/gems/carrierwave-0.9.0/lib/carrierwave/mount.rb:371:in `store!'
/.rvm/gems/ruby-1.9.3-p448/gems/carrierwave-0.9.0/lib/carrierwave/mount.rb:223:in `store_video_path!'
/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-3.2.15/lib/active_support/callbacks.rb:460:in `_run__3293958342697848958__save__4139166616116124146__callbacks'
/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-3.2.15/lib/active_support/callbacks.rb:405:in `__run_callback'
/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-3.2.15/lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-3.2.15/lib/active_support/callbacks.rb:81:in `run_callbacks'
/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-...


Started POST "/videos" for 127.0.0.1 at 2013-10-23 11:52:17 -0400
Processing by VideosController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"jv5yj+KWLjlCOpBmTkpypdHjgZtTlc+s1cA973b1MF4=", "video"=>{"project_id"=>"10", "step_id"=>"97", "saved"=>"true", "embed_url"=>"", "video_path"=>#<ActionDispatch::Http::UploadedFile:0x007fecd6120e38 @original_filename="Untitled.mov", @content_type="video/quicktime", @headers="Content-Disposition: form-data; name=\"video[video_path]\"; filename=\"Untitled.mov\"\r\nContent-Type: video/quicktime\r\n", @tempfile=#<File:/var/folders/dc/c0nfvwy96lq7p4ll94mklnmr0000gp/T/RackMultipart20131023-3350-2gln6f>>}}
  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Project Load (0.4ms)  SELECT "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT 1  [["id", "10"]]
Running....ffmpegthumbnailer -i /Documents/Website/public/uploads/tmp/1382543537-3350-3237/thumb_Untitled.mov -o /Documents/Website/public/uploads/tmp/1382543537-3350-3237/tmpfile.png -c png -q 10 -s 158
Success!
Running....ffmpegthumbnailer -i /Documents/Website/public/uploads/tmp/1382543537-3350-3237/square_thumb_Untitled.mov -o /Documents/Website/public/uploads/tmp/1382543537-3350-3237/tmpfile.png -c png -q 10 -s 105
Success!
   (0.2ms)  BEGIN
  SQL (53.5ms)  INSERT INTO "videos" ("created_at", "embed_url", "image_id", "project_id", "saved", "step_id", "thumbnail_url", "updated_at", "video_path") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"  [["created_at", Wed, 23 Oct 2013 11:52:25 EDT -04:00], ["embed_url", ""], ["image_id", nil], ["project_id", 10], ["saved", true], ["step_id", 97], ["thumbnail_url", nil], ["updated_at", Wed, 23 Oct 2013 11:52:25 EDT -04:00], ["video_path", "Untitled.mp4"]]
   (0.6ms)  COMMIT
   (0.3ms)  SELECT COUNT(*) FROM "images" WHERE "images"."project_id" = 10 AND "images"."step_id" = 97
   (0.1ms)  BEGIN
  SQL (5.4ms)  INSERT INTO "images" ("caption", "created_at", "image_path", "original_id", "position", "project_id", "saved", "sound_id", "step_id", "updated_at", "video_id", "video_thumbnail") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id"  [["caption", nil], ["created_at", Wed, 23 Oct 2013 11:52:29 EDT -04:00], ["image_path", "thumb_Untitled.png"], ["original_id", nil], ["position", 0], ["project_id", 10], ["saved", true], ["sound_id", nil], ["step_id", 97], ["updated_at", Wed, 23 Oct 2013 11:52:29 EDT -04:00], ["video_id", 158], ["video_thumbnail", nil]]
   (0.5ms)  COMMIT
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT
   (0.1ms)  BEGIN
   (0.4ms)  UPDATE "videos" SET "image_id" = 348, "updated_at" = '2013-10-23 15:52:30.679474' WHERE "videos"."id" = 158
   (0.4ms)  COMMIT
  Project Load (0.3ms)  SELECT "projects".* FROM "projects" WHERE "projects"."id" = 10 LIMIT 1
  SQL (0.7ms)  UPDATE "projects" SET "updated_at" = '2013-10-23 15:52:30.684551' WHERE "projects"."id" = 10
  Step Load (0.4ms)  SELECT "steps".* FROM "steps" WHERE "steps"."id" = 97 LIMIT 1
  Image Load (0.2ms)  SELECT "images".* FROM "images" WHERE "images"."step_id" = 97
  Video Load (0.2ms)  SELECT "videos".* FROM "videos" WHERE "videos"."image_id" = 348 LIMIT 1
  Rendered images/_edit_image.html.erb (3.8ms)
  Rendered images/create.js.erb (12.3ms)
Completed 200 OK in 13069.3ms (Views: 15.3ms | ActiveRecord: 64.6ms)

And this is what is rendered on my page:

<video controls>
    <source src="https://...s3.amazonaws.com/video/video_path/158/Untitled.webm" class="348" id="video_348" type="video/webm">
    <source src="https://...s3.amazonaws.com/video/video_path/158/Untitled.mp4" class="348" id="video_348" type="video/mp4">
</video>

2条回答
手持菜刀,她持情操
2楼-- · 2020-08-08 11:31

If you're feeling ambitious, I think you may be able to isolate this to a bug in carrierwave itself preventing set_content_type from working in versions.

On a quick review, I think cache_versions! uses the base file's original_filename instead of the full_filename you're defining. And that's the value set_content_type uses to look up the content type sent to s3.

查看更多
淡お忘
3楼-- · 2020-08-08 11:42

I forgot to update my answer to this once I figured out a way to manually set the content type. This is what I ended up doing:

require 'carrierwave/processing/mime_types'
class VideoPathUploader < CarrierWave::Uploader::Base
  include CarrierWave::MimeTypes
  process :encode

  def encode
    video = MiniExiftool.new(@file.path)
    orientation = video.rotation
    model.rotation = orientation
    Rails.logger.debug("orientation: #{orientation}")
    Rails.logger.debug("video wxh #{video.imagewidth} #{video.imageheight}")

    if orientation == 90 && video.imagewidth.to_f > video.imageheight.to_f
      Rails.logger.debug("rotating video")
      aspect_ratio = video.imageheight.to_f / video.imagewidth.to_f 
      encode_video(:mp4, custom: "-vf transpose=1", resolution: :same, aspect: aspect_ratio)
    else
      aspect_ratio = video.imagewidth.to_f / video.imageheight.to_f
      encode_video(:mp4, resolution: :same, aspect: aspect_ratio)
    end

    instance_variable_set(:@content_type, "video/mp4")
    :set_content_type_mp4
  end

  version :webm do
    process :encode_video => [:webm]
    process :set_content_type_webm
    def full_filename(for_file)
      "#{File.basename(for_file, File.extname(for_file))}.webm"
    end
  end

 def set_content_type_mp4(*args)
    Rails.logger.debug "#{file.content_type}"
    self.file.instance_variable_set(:@content_type, "video/mp4")
  end

  def set_content_type_webm(*args)
    Rails.logger.debug "#{file.content_type}"
    self.file.instance_variable_set(:@content_type, "video/webm")
  end
end

Hope this helps someone!

查看更多
登录 后发表回答