files.rb revision 81da36894af70bbb8d8e24b004026ad4c5c1bc99
2N/Amodule OntologyVersion::Files
2N/A extend ActiveSupport::Concern
2N/A
2N/A included do
2N/A # virtual attribute for upload
2N/A attr_accessible :raw_file
2N/A before_create :commit_raw_file, unless: :commit_oid?
2N/A end
2N/A
2N/A def raw_file=(value)
2N/A @raw_file = value
2N/A end
2N/A
2N/A def commit_raw_file
2N/A return true if ontology.parent
2N/A raise "raw file missing" unless @raw_file
2N/A self.path = @raw_file.original_filename if self.path.nil?
2N/A # otherwise the file upload is broken (no implicit conversion of ActionDispatch::Http::UploadedFile into String):
2N/A tmp_file = if @raw_file.class == ActionDispatch::Http::UploadedFile
2N/A @raw_file.tempfile
58N/A else
2N/A @raw_file
2N/A end
2N/A repository.save_file(tmp_file, path, 'message', pusher)
2N/A end
23N/A
23N/A def tmp_dir
23N/A Ontohub::Application.config.commits_path.join(commit_oid)
23N/A end
23N/A
32N/A # path to the raw file
32N/A def raw_path
32N/A tmp_dir.join("raw",self.path)
34N/A end
32N/A
34N/A # path to the raw file, checks out the raw file if is missing
32N/A def raw_path!
32N/A checkout_raw!
58N/A raw_path
32N/A end
225N/A
225N/A def raw_file?
225N/A File.exists? raw_path
225N/A end
225N/A
225N/A # checks out the raw file
225N/A def checkout_raw!
225N/A unless raw_file?
225N/A FileUtils.mkdir_p raw_path.dirname
225N/A File.open(raw_path, "w"){|f| f.write raw_data }
2N/A end
38N/A end
38N/A
38N/A # returns the raw data directly from the repository
38N/A def raw_data
38N/A repository.get_file(self.path, commit_oid).content.encoding_utf8
38N/A end
38N/A
83N/Aend
83N/A