Rails:如何在 Heroku 上使用系统 zip 从 xml 模板制作 docx?

发布于 2024-11-16 11:34:22 字数 1606 浏览 2 评论 0原文

我在本地工作,使用 system "cd tmp/template; zip -r ../#{@filename} * 将模板文件存储在 #{Rails.root}/tmp 中" 压缩文件,将 .docx(zip 存档)发送到 S3,然后发送到浏览器。问题是 Heroku 找不到这些文件。在创建 xml 文件之前,我从另一个位置复制模板目录 (system "cp -R support/ser_template tmp/")。我了解 Heroku 的 只读文件系统,但我无法使用 # {Process.pid} 弄乱了我的文件名(Word 要求将 xml 文件命名为 document.xml)。

我是否可以将模板文件存储在 Amazon 上并仍然使用 Heroku 的系统 zip 实用程序? RubyZip 无法创建正确的 docx 存档< /a>.

编辑:这是代码:

require 'aws/s3'

class WordDocument
  include ConnectS3

  def initialize(content)
    connect_s3
    @pid = Process.pid
    @filename = "SER_" + Time.now.strftime("%Y%m%d-%H%M%S") + '.docx'
    system "cp -R #{Rails.root}/support/ser_template #{temp_path}"
    xml = File.open(xml_path, 'w')
    xml.puts content
    xml.close
    system "cd #{temp_path}; zip -r #{@filename} *"
    docx = File.open(temp_path + "/" + @filename, 'r')
    AWS::S3::S3Object.store(s3_path, docx, @s3_credentials["bucket"], :use_virtual_directories => true)
    AWS::S3::S3Object.grant_torrent_access_to s3_path, @s3_credentials["bucket"]
  end

  def temp_path
    "#{Rails.root}/tmp/#{@pid}_ser"
  end

  def xml_path
    temp_path + "/word/document.xml"
  end

  def path
    "https://s3.amazonaws.com/" + @s3_credentials["bucket"] + s3_path
  end

  def s3_path
    '/section_editor_reports/' + @filename
  end
end

I have this working locally, storing the template files in #{Rails.root}/tmp, using system "cd tmp/template; zip -r ../#{@filename} *" to zip up the files, sending the .docx (zip archive) to S3 and then to the browser. The problem is that Heroku is not finding the files. Before I create the xml file, I am copying the template directory from another location (system "cp -R support/ser_template tmp/"). I understand Heroku's read-only filesystem but I can't have #{Process.pid}'s littering my filenames (Word requires the xml file to be named document.xml).

Is there anyway I can store the template files on Amazon and still use Heroku's system zip utility? RubyZip does not create proper docx archives.

Edit: here is the code:

require 'aws/s3'

class WordDocument
  include ConnectS3

  def initialize(content)
    connect_s3
    @pid = Process.pid
    @filename = "SER_" + Time.now.strftime("%Y%m%d-%H%M%S") + '.docx'
    system "cp -R #{Rails.root}/support/ser_template #{temp_path}"
    xml = File.open(xml_path, 'w')
    xml.puts content
    xml.close
    system "cd #{temp_path}; zip -r #{@filename} *"
    docx = File.open(temp_path + "/" + @filename, 'r')
    AWS::S3::S3Object.store(s3_path, docx, @s3_credentials["bucket"], :use_virtual_directories => true)
    AWS::S3::S3Object.grant_torrent_access_to s3_path, @s3_credentials["bucket"]
  end

  def temp_path
    "#{Rails.root}/tmp/#{@pid}_ser"
  end

  def xml_path
    temp_path + "/word/document.xml"
  end

  def path
    "https://s3.amazonaws.com/" + @s3_credentials["bucket"] + s3_path
  end

  def s3_path
    '/section_editor_reports/' + @filename
  end
end

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

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

发布评论

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

评论(1

深海蓝天 2024-11-23 11:34:22

难道您不能在 #{Rails.root}/tmp 中创建一个名为 #{Process.pid}_docx/something_nice/ 的目录吗?将您需要的内容复制(或符号链接)到:

#{Rails.root}/tmp/#{Process.pid}_docx/something_nice/

然后

system "cd #{Rails.root}/tmp/#{Process.pid}_docx/; zip -r x.zip something_nice"

然后您就拥有了:

#{Rails.root}/tmp/#{Process.pid}_docx/x.zip

具有一个漂亮的内部结构,不包括您的 PID。

Can't you just a create directory within #{Rails.root}/tmp called, say, #{Process.pid}_docx/something_nice/? Copy (or symlink) what you need into:

#{Rails.root}/tmp/#{Process.pid}_docx/something_nice/

Then

system "cd #{Rails.root}/tmp/#{Process.pid}_docx/; zip -r x.zip something_nice"

And then you have:

#{Rails.root}/tmp/#{Process.pid}_docx/x.zip

With a nice pretty internal structure that doesn't include your PID.

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