如何在 CarrierWave 进程中创建新文件?

发布于 2024-10-16 20:25:23 字数 791 浏览 2 评论 0原文

我的用户上传了一个包含 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

它成功保存了原始文件的 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

My user uploads a zip file with 3 files (A.ttf, A.svg, A.otf) and I want to store the original zip plus the 3 font files inside it. I've created the 3 versions with this code

version :ttf
    process :font => :ttf
end

version :svg
    process :font => :svg
end

version :otf
    process :font => :otf
end

It successfully saves 4 copies of the original file, all with the proper file name. However, I don't know how to get CarrierWave to store the individual files. This code doesn't work. :(

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

古镇旧梦 2024-10-23 20:25:23

好吧,在我用头撞墙几个小时之后,灯终于亮了。解决方案在于 CarrierWave 如何处理上传。当您定义版本时,CW 会使用新名称 ([版本名称]_original_filename) 复制该文件,并将其提供给 current_path 变量。您可以使用此文件引用执行任何您想要的操作(即打开文件并截断​​它,或用随机日期填充它等),完成后 CW 将为您存储该文件。

不知怎的,我错过了连接,当我意识到发生了什么时,它几乎让我失明。我在这里回答这个问题,是为了帮助其他迷失在黑暗中的可怜灵魂。并向世界展示我的无知。 :/

OK, after many hours of banging my head against the wall the light finally came on. The solution is in how CarrierWave processes the upload. When you define a version, CW duplicates the file with a new name ([version name]_original_filename) and gives it to you in the current_path variable. You can do anything you want with this file reference (ie open the file and truncate it, or fill it with random date, etc.) and when you get done CW will store the file for you.

Somehow I missed the connection and when I realized what was going on it almost blinded me. I'm answering this question here so that it might help some other poor soul lost in the dark. And to show the world my ignorance. :/

離殇 2024-10-23 20:25:23

我不知道CarrierWave。但是您正在返回 zip 文件对象。是你想要传递的内容还是你想要传递文件的名称?或者您是否希望提取文件并将提取的文件路径传回?

I dont know about CarrierWave. But you are returnng zip file object back. Is it what you want to pass or do you want the name of the file to be passed? Or else do you want the file to be extracted and pass the extracted file path back?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文