如何将其他变量传递给 Rails 中的 CarrierWave 上传器?
How do you pass additional variables to a CarrierWave uploader in Rails?
我看到这个问题多年来被问过几次(例如 Upload path based on a record value for Carrier wave Direct, Passing a parameter to the uploader / accessing a model's attribute from within the uploader / letting the user pick the thumbnail size),但我确信我一定是把它复杂化了,因为它看起来是一个非常简单的问题...
我有一个非常简单的 Video
模型可以安装上传器:
class Video < ApplicationRecord
mount_uploader :file, VideoUploader
end
在控制器中,我允许两个参数:
def video_params
params.require(:video).permit(:title, :file)
end
在实际的 VideoUploader
中,我似乎可以使用 class 内置函数(例如 original_filename
)访问从 :file
列派生的许多变量,并且我可以使用 ffmpeg 参数处理文件。但是,我希望参数是基于 :title
字符串的条件,并且我 没有 知道如何限定它的范围或访问它。确保这些方法可以访问此变量的绝对最简单的方法是什么?
编辑:这是上传代码:
class VideoUploader < CarrierWave::Uploader::Base
require 'streamio-ffmpeg'
include CarrierWave::Video
case @title # not working
when "tblend_glitch"
process encode_video: [:mp4,
resolution: "1280x960",
custom: %w(-to 5 -vf scale=-2:720,tblend=all_mode=difference,tblend=all_mode=difference,tblend=all_mode=difference,spp=4:10,tblend=all_mode=average,tblend=all_mode=difference,tblend=all_mode=difference,tblend=all_mode=difference,spp=4:10,tblend=all_mode=average,tblend=all_mode=difference,tblend=all_mode=difference,tblend=all_mode=difference,spp=4:10,tblend=all_mode=average,tblend=all_mode=difference,tblend=all_mode=difference,tblend=all_mode=difference)]
...
end
def full_filename(for_file)
super.chomp(File.extname(super)) + '.mp4'
end
def filename
original_filename.chomp(File.extname(original_filename)) + '.mp4'
end
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
谢谢!
您应该能够使用 model
方法访问上传器中的实例。
您还没有定义 @title
— 它是 nil
。您可以使用以下代码创建条件版本。
class VideoUploader < CarrierWave::Uploader::Base
version :tblend, if: :tblend_glitch? do # use a better version name
process encode_video: [:mp4,
resolution: "1280x960",
custom: %w(-to 5 -vf scale=-2:720,tblend=all_mode=difference,tblend=all_mode=difference,tblend=all_mode=difference,spp=4:10,tblend=all_mode=average,tblend=all_mode=difference,tblend=all_mode=difference,tblend=all_mode=difference,spp=4:10,tblend=all_mode=average,tblend=all_mode=difference,tblend=all_mode=difference,tblend=all_mode=difference,spp=4:10,tblend=all_mode=average,tblend=all_mode=difference,tblend=all_mode=difference,tblend=all_mode=difference)]
end
# rest of the code
private
def tblend_glitch?
model.title == 'tblend_glitch'
end
end
参考:https://github.com/carrierwaveuploader/carrierwave#conditional-versions
我看到这个问题多年来被问过几次(例如 Upload path based on a record value for Carrier wave Direct, Passing a parameter to the uploader / accessing a model's attribute from within the uploader / letting the user pick the thumbnail size),但我确信我一定是把它复杂化了,因为它看起来是一个非常简单的问题...
我有一个非常简单的 Video
模型可以安装上传器:
class Video < ApplicationRecord
mount_uploader :file, VideoUploader
end
在控制器中,我允许两个参数:
def video_params
params.require(:video).permit(:title, :file)
end
在实际的 VideoUploader
中,我似乎可以使用 class 内置函数(例如 original_filename
)访问从 :file
列派生的许多变量,并且我可以使用 ffmpeg 参数处理文件。但是,我希望参数是基于 :title
字符串的条件,并且我 没有 知道如何限定它的范围或访问它。确保这些方法可以访问此变量的绝对最简单的方法是什么?
编辑:这是上传代码:
class VideoUploader < CarrierWave::Uploader::Base
require 'streamio-ffmpeg'
include CarrierWave::Video
case @title # not working
when "tblend_glitch"
process encode_video: [:mp4,
resolution: "1280x960",
custom: %w(-to 5 -vf scale=-2:720,tblend=all_mode=difference,tblend=all_mode=difference,tblend=all_mode=difference,spp=4:10,tblend=all_mode=average,tblend=all_mode=difference,tblend=all_mode=difference,tblend=all_mode=difference,spp=4:10,tblend=all_mode=average,tblend=all_mode=difference,tblend=all_mode=difference,tblend=all_mode=difference,spp=4:10,tblend=all_mode=average,tblend=all_mode=difference,tblend=all_mode=difference,tblend=all_mode=difference)]
...
end
def full_filename(for_file)
super.chomp(File.extname(super)) + '.mp4'
end
def filename
original_filename.chomp(File.extname(original_filename)) + '.mp4'
end
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
谢谢!
您应该能够使用 model
方法访问上传器中的实例。
您还没有定义 @title
— 它是 nil
。您可以使用以下代码创建条件版本。
class VideoUploader < CarrierWave::Uploader::Base
version :tblend, if: :tblend_glitch? do # use a better version name
process encode_video: [:mp4,
resolution: "1280x960",
custom: %w(-to 5 -vf scale=-2:720,tblend=all_mode=difference,tblend=all_mode=difference,tblend=all_mode=difference,spp=4:10,tblend=all_mode=average,tblend=all_mode=difference,tblend=all_mode=difference,tblend=all_mode=difference,spp=4:10,tblend=all_mode=average,tblend=all_mode=difference,tblend=all_mode=difference,tblend=all_mode=difference,spp=4:10,tblend=all_mode=average,tblend=all_mode=difference,tblend=all_mode=difference,tblend=all_mode=difference)]
end
# rest of the code
private
def tblend_glitch?
model.title == 'tblend_glitch'
end
end
参考:https://github.com/carrierwaveuploader/carrierwave#conditional-versions