Save documents on disk

This commit is contained in:
Martin Lensment 2015-04-22 17:42:51 +03:00
parent a99d36f683
commit 79f76b2ccf
6 changed files with 41 additions and 6 deletions

View file

@ -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

View file

@ -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

View file

@ -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"

View file

@ -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

View file

@ -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

View file

@ -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