files.rb revision 89849d4f0871f414f3d7d8e926bb10e9db19fd57
bb2b38cd44b032118359afbc743efbea12f48e61bnicholesmodule OntologyVersion::Files
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes extend ActiveSupport::Concern
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes included do
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes # virtual attribute for upload
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg attr_accessible :raw_file
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes before_create :commit_raw_file, unless: :commit_oid?
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes end
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes def raw_file=(value)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes @raw_file = value
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes end
0662ed52e814f8f08ef0e09956413a792584eddffuankg
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes def commit_raw_file
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes return true if ontology.parent
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes raise "raw file missing" unless @raw_file
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes ontology.path = @raw_file.original_filename if ontology.path.nil?
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes # otherwise the file upload is broken (no implicit conversion of ActionDispatch::Http::UploadedFile into String):
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes tmp_file = if @raw_file.class == ActionDispatch::Http::UploadedFile
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes @raw_file.tempfile
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes else
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes @raw_file
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes end
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes repository.save_file(tmp_file, ontology.path, "message", user)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes end
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes def tmp_dir
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes Rails.root.join("tmp","commits",commit_oid)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes end
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes # path to the raw file
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes def raw_path
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes tmp_dir.join("raw",ontology.path)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes end
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes # path to the raw file, checks out the raw file if is missing
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes def raw_path!
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes checkout_raw!
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes raw_path
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes end
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes # path to xml file (hets output)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes def xml_path
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes tmp_dir.join("xml", ontology.path)
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes end
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes def xml_file?
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes File.exists? xml_path
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes end
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes def raw_file?
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes File.exists? raw_path
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes end
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes # checks out the raw file
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes def checkout_raw!
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes unless raw_file?
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes FileUtils.mkdir_p raw_path.dirname
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes File.open(raw_path, "w"){|f| f.write raw_data }
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes end
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes end
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg # returns the raw data directly from the repository
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes def raw_data
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes repository.read_file(ontology.path, commit_oid)[:content]
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes end
bb2b38cd44b032118359afbc743efbea12f48e61bnicholes
bb2b38cd44b032118359afbc743efbea12f48e61bnicholesend