Ric*_*urt 3 ruby-on-rails process carrierwave
我的用户上传了一个包含3个文件(A.ttf,A.svg,A.otf)的zip文件,我想将原始zip和3个字体文件存储在其中.我用这段代码创建了3个版本
version :ttf
process :font => :ttf
end
version :svg
process :font => :svg
end
version :otf
process :font => :otf
end
Run Code Online (Sandbox Code Playgroud)
它成功保存了原始文件的4个副本,所有副本都具有正确的文件名.但是,我不知道如何让CarrierWave存储单个文件.此代码不起作用.:(
def font(format)
new_file = nil
# Loop through the zip file and extract the files
Zip::ZipFile.open(@file.file) do |files|
files.each do |f|
next unless f.file?
filename = f.name.split("/").last
ext = filename.split('.').last
# Save the file with the proper file extension
new_file = f if ext == format
end
# Return the file to be stored by CarrierWave
new_file
end
Run Code Online (Sandbox Code Playgroud)
好吧,经过几个小时的撞击墙壁后,光线终于亮了起来.解决方案是CarrierWave处理上传的方式.定义版本时,CW使用新名称([版本名称] _original_filename)复制文件,并在current_path
变量中将其提供给您 .您可以使用此文件引用执行任何操作(即打开文件并截断它,或用随机日期填写它等),当您完成后,CW将为您存储文件.
不知怎的,我错过了连接,当我意识到发生了什么事时,它几乎让我蒙羞.我在这里回答这个问题,以便它可以帮助其他一些在黑暗中迷失的可怜的灵魂.并向世人展示我的无知.:/