B B
B B

Reputation: 839

RoR, Carrierwave: video files no longer loading after uploader column changed

I have a video upload app built in Ruby on Rails and hosted on Heroku. It uses the Carrierwave gem to upload video files to AWS S3 and carrierwave-backgrounder, sidekiq and redis to handle the process in a background worker to get around Heroku's timeout issues.

I initially used a string column on the Video table for the VideoUploader, before being told that multiple file upload was needed on the form. I decided to make a new array column called :video_files to store the file uploads in.

Now that I've got the gems working together and pushed to Heroku, new file uploads work fine. But old files on the :video_file column are no longer loading, although their file names are still present in the column when inspected in the rails console. Is it possible that changing

mount_uploader :video_file, VideoUploader to mount_uploaders :video_files, VideoUploader

has changed how the Video class accesses :video_file? Can A model have two Uploaders, or should I write a migration moving files from :video_file over into an array in :video_files?

Video.rb:

class Video < ApplicationRecord

  mount_uploaders :video_files, VideoUploader
  process_in_background :video_files

end

carrierwave.rb

CarrierWave.configure do |config|
  config.fog_credentials = {
    provider:              'AWS',                        # required
    aws_access_key_id:     ENV.fetch('AWS_ACCESS_KEY_ID'),                        # required unless using use_iam_profile
    aws_secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),                        # required unless using use_iam_profile
    use_iam_profile:       true,                         # optional, defaults to false
    region:                ENV.fetch('AWS_REGION')                  # optional, defaults to 'us-east-1'
  }
  config.fog_directory  = ENV.fetch('S3_BUCKET_NAME')                                    # required
  config.fog_public     = false                                                 # optional, defaults to true
  config.fog_attributes = { cache_control: "public, max-age=#{365.days.to_i}" } # optional, defaults to {}
  # For an application which utilizes multiple servers but does not need caches persisted across requests,
  # uncomment the line :file instead of the default :storage.  Otherwise, it will use AWS as the temp cache store.
  # config.cache_storage = :file
end

VideoUploader.rb

class VideoUploader < CarrierWave::Uploader::Base
  include ::CarrierWave::Backgrounder::Delay
  cache_storage CarrierWave::Storage::File

  def cache_dir
    "uploads_cache/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  storage :fog

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def extension_allowlist
    %w(mp4 mov avi)
  end
end

initializers/carrierwave_backgrounder.rb

CarrierWave::Backgrounder.configure do |c|
  c.backend :sidekiq, queue: :carrierwave
end
initializers/sidekiq.rb

Sidekiq.configure_server do |config|
  config.redis = {
    url: ENV["REDIS_URL"],
    ssl_params: { verify_mode: OpenSSL::SSL::VERIFY_NONE }
  }
end

Sidekiq.configure_client do |config|
  config.redis = {
      url: ENV["REDIS_URL"],
      ssl_params: { verify_mode: OpenSSL::SSL::VERIFY_NONE }
  }
end

sidekiq.yml

:queues:
  - [carrierwave, 1]
  - default

redis.rb

$redis = Redis.new(url: ENV["REDIS_URL"], ssl_params: { verify_mode: OpenSSL::SSL::VERIFY_NONE })

Procfile

worker: bundle exec sidekiq -c 2

Upvotes: 0

Views: 29

Answers (0)

Related Questions