class Dradis::Plugins::Projects::Upload::Package::Importer

In this module you will find the implementation details that enable you to upload a project archive (generated using ProjectExport::Processor::full_project)

Public Instance Methods

import(params={}) click to toggle source
# File lib/dradis/plugins/projects/upload/package.rb, line 16
def import(params={})
  package = params[:file]
  success = false

  # Unpack the archive in a temporary location
  temporary_dir = Rails.root.join('tmp', 'zip')
  FileUtils.mkdir temporary_dir

  begin
    logger.info { 'Uncompressing the file...' }
    #TODO: this could be improved by only uncompressing the XML, then parsing
    # it to get the node_lookup table and then uncompressing each entry to its
    # final destination
    Dir.chdir(temporary_dir) do
      Zip::File.foreach(package) do |entry|
        path = temporary_dir.join(entry.name)
        FileUtils.mkdir_p(File.dirname(path))
        entry.extract
        logger.info { "\t#{entry.name}" }
      end
    end
    logger.info { 'Done.' }


    logger.info { 'Loading XML template file...' }
    template_file = Rails.root.join('tmp', 'zip', 'dradis-repository.xml')
    importer    = Template::Importer.new(
                    options.merge plugin: Dradis::Plugins::Projects::Upload::Template
                  )
    lookup_table = importer.import(file: template_file)
    logger.info { 'Done.' }


    logger.info { 'Moving attachments to their final destinations...' }
    lookup_table[:nodes].each do |oldid,newid|
      if File.directory? Rails.root.join('tmp', 'zip', oldid)
        FileUtils.mkdir_p Attachment.pwd.join(newid.to_s)

        Dir.glob(Rails.root.join('tmp', 'zip', oldid, '*')).each do |attachment|
          FileUtils.mv(attachment, Attachment.pwd.join(newid.to_s))
        end
      end
    end
    logger.info { 'Done.' }

    success = true
  rescue Exception => e
    logger.error { e.message }
    success = false
  ensure
    # clean up the temporary files
    FileUtils.rm_rf(Rails.root.join('tmp', 'zip'))
  end

  return success
end