files.rb revision a11fa4196a44ddd584a6aeeb79e6b09f5cef7b48
234N/Amodule OntologyVersion::Files
234N/A extend ActiveSupport::Concern
234N/A
234N/A included do
234N/A # virtual attribute for upload
234N/A attr_accessible :raw_file
234N/A before_create :commit_raw_file, unless: :commit_oid?
234N/A end
234N/A
234N/A def raw_file=(value)
234N/A @raw_file = value
234N/A end
234N/A
234N/A def commit_raw_file
234N/A return true if ontology.parent
234N/A raise "raw file missing" unless @raw_file
234N/A ontology.path = @raw_file.original_filename if ontology.path.nil?
234N/A # otherwise the file upload is broken (no implicit conversion of ActionDispatch::Http::UploadedFile into String):
234N/A tmp_file = if @raw_file.class == ActionDispatch::Http::UploadedFile
234N/A @raw_file.tempfile
727N/A else
234N/A @raw_file
234N/A end
234N/A repository.save_file(tmp_file, ontology.path, "message", user)
234N/A end
234N/A
234N/A def tmp_dir
234N/A Rails.root.join("tmp","commits",commit_oid)
618N/A end
234N/A
234N/A # path to the raw file
844N/A def raw_path
844N/A tmp_dir.join("raw",ontology.path)
234N/A end
234N/A
234N/A # path to the raw file, checks out the raw file if is missing
234N/A def raw_path!
234N/A checkout_raw!
234N/A raw_path
234N/A end
234N/A
234N/A def xml_dir
727N/A tmp_dir.join('xml')
727N/A end
727N/A
234N/A # path to xml file (hets output)
234N/A def xml_path
234N/A return nil unless xml_name
234N/A xml_dir.join(xml_name)
234N/A end
234N/A
234N/A # path to xml file (hets output) with code positions
234N/A def code_reference_path
234N/A return nil unless pp_xml_name
234N/A xml_dir.join(pp_xml_name)
end
def xml_file?
xml_path.try(:exist?)
end
def raw_file?
File.exists? raw_path
end
# checks out the raw file
def checkout_raw!
unless raw_file?
FileUtils.mkdir_p raw_path.dirname
File.open(raw_path, "w"){|f| f.write raw_data }
end
end
# returns the raw data directly from the repository
def raw_data
repository.read_file(ontology.path, commit_oid)[:content].encoding_utf8
end
end