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