Class: Tractive::AttachmentExporter
- Inherits:
-
Object
- Object
- Tractive::AttachmentExporter
- Defined in:
- lib/tractive/attachment_exporter.rb
Instance Method Summary collapse
-
#export ⇒ Object
export the images from the database into a folder.
-
#generate_script ⇒ Object
Produce a shell script to export attachments, to be executed on the Trac instance.
-
#initialize(cfg, db) ⇒ AttachmentExporter
constructor
A new instance of AttachmentExporter.
Constructor Details
#initialize(cfg, db) ⇒ AttachmentExporter
Returns a new instance of AttachmentExporter.
8 9 10 11 |
# File 'lib/tractive/attachment_exporter.rb', line 8 def initialize(cfg, db) @cfg = cfg @db = db end |
Instance Method Details
#export ⇒ Object
export the images from the database into a folder
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/tractive/attachment_exporter.rb', line 40 def export output_dir = @cfg.dig("attachments", "export_folder") || "#{Dir.pwd}/tmp/trac" trac_url = @cfg.dig("attachments", "url") raise("attachments url is required in config.yaml to export attachments.") unless trac_url FileUtils.mkdir_p output_dir = Attachment..for_export # using URI::Parser.new because URI.encode raise warning: URI.escape is obsolete uri_parser = URI::Parser.new .each do || $logger.info "Saving attachments of ticket #{.id}... " FileUtils.mkdir_p "#{output_dir}/#{.id}" File.binwrite( "#{output_dir}/#{.id}/#{.filename}", URI.open(uri_parser.escape("#{trac_url}/#{.id}/#{.filename}")).read ) end end |
#generate_script ⇒ Object
Produce a shell script to export attachments, to be executed on the Trac instance
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/tractive/attachment_exporter.rb', line 15 def generate_script outfile = @cfg.dig("attachments", "export_script") outfolder = @cfg.dig("attachments", "export_folder") raise("missing attachments.export_script entry in configuration") unless outfile raise("missing attachments.export_folder entry in configuration") unless outfolder = Attachment..for_export export_commands = .map do || %(mkdir -p #{outfolder}/#{[:id]} trac-admin /trac attachment export ticket:#{[:id]} '#{[:filename]}' > '#{outfolder}/#{[:id]}/#{[:filename]}') end File.open(outfile, "w") do |f| f.puts("mkdir -p #{outfolder}") f.puts(export_commands.join("\n")) end $logger.info "created attachment exporter in #{outfile}" rescue StandardError => e $logger.error(e.) exit(1) end |