diff --git a/app/controllers/admin/legal_documents_controller.rb b/app/controllers/admin/legal_documents_controller.rb index ff5414e40..7aff7bcb8 100644 --- a/app/controllers/admin/legal_documents_controller.rb +++ b/app/controllers/admin/legal_documents_controller.rb @@ -3,7 +3,7 @@ class Admin::LegalDocumentsController < AdminController def show @ld = LegalDocument.find(params[:id]) - file = Base64.decode64(@ld.body) - send_data file, filename: "#{@ld.created_at}.#{@ld.document_type}" + filename = @ld.path.split('/').last + send_data File.open(@ld.path).read, filename: filename end end diff --git a/app/models/legal_document.rb b/app/models/legal_document.rb index 1201f0ddb..fc06148c3 100644 --- a/app/models/legal_document.rb +++ b/app/models/legal_document.rb @@ -3,4 +3,19 @@ class LegalDocument < ActiveRecord::Base belongs_to :documentable, polymorphic: true TYPES = %w(pdf bdoc ddoc zip rar gz tar 7z) + + attr_accessor :body + + before_save :save_to_filesystem + def save_to_filesystem + loop do + rand = SecureRandom.random_number.to_s.last(4) + self.path = "#{ENV['legal_documents_dir']}/#{Time.zone.now.to_formatted_s(:number)}_#{rand}.#{document_type}" + break unless File.file?(path) + end + + # TODO: Change path for test env + File.open(path, 'wb') { |f| f.write(Base64.decode64(body)) } unless Rails.env.test? + self.path = path + end end diff --git a/config/application-example.yml b/config/application-example.yml index ead99f0a9..ad4941eb2 100644 --- a/config/application-example.yml +++ b/config/application-example.yml @@ -3,6 +3,7 @@ app_name: '.EE Registry' zonefile_export_dir: 'export/zonefiles' bank_statement_import_dir: 'import/bank_statements' +legal_documents_dir: 'import/legal_documents' # Contact epp will not accept org value by default # and returns 2306 "Parameter value policy error" diff --git a/db/migrate/20150422132631_save_legal_docs_to_disk.rb b/db/migrate/20150422132631_save_legal_docs_to_disk.rb new file mode 100644 index 000000000..bffce06b7 --- /dev/null +++ b/db/migrate/20150422132631_save_legal_docs_to_disk.rb @@ -0,0 +1,19 @@ +class SaveLegalDocsToDisk < ActiveRecord::Migration + def change + add_column :legal_documents, :path, :string + LegalDocument.all.each do |x| + path = nil + loop do + rand = SecureRandom.random_number.to_s.last(4) + path = "#{ENV['legal_documents_dir']}/#{Time.zone.now.to_formatted_s(:number)}_#{rand}.#{x.document_type}" + break unless File.file?(path) + end + + body = x.read_attribute('body') + File.open(path, 'wb') { |f| f.write(Base64.decode64(body)) } + x.update_column('path', path) + end + + remove_column :legal_documents, :body, :text + end +end diff --git a/db/schema.rb b/db/schema.rb index 9f7501fb5..0668ebb76 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150417082723) do +ActiveRecord::Schema.define(version: 20150422132631) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -396,13 +396,13 @@ ActiveRecord::Schema.define(version: 20150417082723) do create_table "legal_documents", force: :cascade do |t| t.string "document_type" - t.text "body" t.integer "documentable_id" t.string "documentable_type" t.datetime "created_at" t.datetime "updated_at" t.string "creator_str" t.string "updator_str" + t.string "path" end add_index "legal_documents", ["documentable_type", "documentable_id"], name: "index_legal_documents_on_documentable_type_and_documentable_id", using: :btree diff --git a/spec/epp/keyrelay_spec.rb b/spec/epp/keyrelay_spec.rb index 731873d08..da5bdf570 100644 --- a/spec/epp/keyrelay_spec.rb +++ b/spec/epp/keyrelay_spec.rb @@ -5,7 +5,7 @@ describe 'EPP Keyrelay', epp: true do create_settings @registrar1 = Fabricate(:registrar1) @registrar2 = Fabricate(:registrar2) - @domain = Fabricate(:domain, registrar: @registrar2) + @domain = Fabricate(:domain, registrar: @registrar2) @epp_xml = EppXml::Keyrelay.new Fabricate(:api_user, username: 'registrar1', registrar: @registrar1) @@ -174,7 +174,7 @@ describe 'EPP Keyrelay', epp: true do docs = Keyrelay.last.legal_documents docs.count.should == 1 - docs.first.body.should == 'JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0Zp==' + docs.first.path.should_not be_blank docs.first.document_type.should == 'pdf' end