Rails:如何在 Heroku 上使用系统 zip 从 xml 模板制作 docx?
我在本地工作,使用 system "cd tmp/template; zip -r ../#{@filename} * 将模板文件存储在
压缩文件,将 .docx(zip 存档)发送到 S3,然后发送到浏览器。问题是 Heroku 找不到这些文件。在创建 xml 文件之前,我从另一个位置复制模板目录 (#{Rails.root}/tmp
中"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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
难道您不能在
#{Rails.root}/tmp
中创建一个名为#{Process.pid}_docx/something_nice/
的目录吗?将您需要的内容复制(或符号链接)到:然后
然后您就拥有了:
具有一个漂亮的内部结构,不包括您的 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:Then
And then you have:
With a nice pretty internal structure that doesn't include your PID.