aead985e48027c9df1ef553b17df29dd378757aaJulian Kornbergermodule OntologyVersion::Files
aead985e48027c9df1ef553b17df29dd378757aaJulian Kornberger extend ActiveSupport::Concern
ee5342a8882c2fc7631fcffb5497e6597747887cTim Reddehase
aead985e48027c9df1ef553b17df29dd378757aaJulian Kornberger included do
aead985e48027c9df1ef553b17df29dd378757aaJulian Kornberger # virtual attribute for upload
aead985e48027c9df1ef553b17df29dd378757aaJulian Kornberger attr_accessible :raw_file
7097237ffbd7227b30272b307b6e48c78b22ecb1Julian Kornberger before_create :commit_raw_file, unless: :commit_oid?
aead985e48027c9df1ef553b17df29dd378757aaJulian Kornberger end
aead985e48027c9df1ef553b17df29dd378757aaJulian Kornberger
aead985e48027c9df1ef553b17df29dd378757aaJulian Kornberger def raw_file=(value)
aead985e48027c9df1ef553b17df29dd378757aaJulian Kornberger @raw_file = value
aead985e48027c9df1ef553b17df29dd378757aaJulian Kornberger end
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger
7097237ffbd7227b30272b307b6e48c78b22ecb1Julian Kornberger def commit_raw_file
89849d4f0871f414f3d7d8e926bb10e9db19fd57Eugen Kuksa return true if ontology.parent
7097237ffbd7227b30272b307b6e48c78b22ecb1Julian Kornberger raise "raw file missing" unless @raw_file
658151642f71e0593b7d55f124fd42ebe2cfcf9aEugen Kuksa self.path = @raw_file.original_filename if self.path.nil?
24a4fef0ec5817f68ff5dd8bc59c1ce947187fabEugen Kuksa # otherwise the file upload is broken (no implicit conversion of ActionDispatch::Http::UploadedFile into String):
ee5342a8882c2fc7631fcffb5497e6597747887cTim Reddehase tmp_file = if @raw_file.class == ActionDispatch::Http::UploadedFile
24a4fef0ec5817f68ff5dd8bc59c1ce947187fabEugen Kuksa @raw_file.tempfile
24a4fef0ec5817f68ff5dd8bc59c1ce947187fabEugen Kuksa else
24a4fef0ec5817f68ff5dd8bc59c1ce947187fabEugen Kuksa @raw_file
24a4fef0ec5817f68ff5dd8bc59c1ce947187fabEugen Kuksa end
81da36894af70bbb8d8e24b004026ad4c5c1bc99Eugen Kuksa repository.save_file(tmp_file, path, 'message', pusher)
7097237ffbd7227b30272b307b6e48c78b22ecb1Julian Kornberger end
7097237ffbd7227b30272b307b6e48c78b22ecb1Julian Kornberger
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger def tmp_dir
045a68332e7be1078224de3911d214939e470b66Julian Kornberger Ontohub::Application.config.commits_path.join(commit_oid)
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger end
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger # path to the raw file
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger def raw_path
658151642f71e0593b7d55f124fd42ebe2cfcf9aEugen Kuksa tmp_dir.join("raw",self.path)
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger end
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger # path to the raw file, checks out the raw file if is missing
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger def raw_path!
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger checkout_raw!
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger raw_path
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger end
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger def raw_file?
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger File.exists? raw_path
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger end
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger # checks out the raw file
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger def checkout_raw!
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger unless raw_file?
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger FileUtils.mkdir_p raw_path.dirname
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger File.open(raw_path, "w"){|f| f.write raw_data }
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger end
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger end
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger # returns the raw data directly from the repository
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger def raw_data
f2c5529a09f91b3f5d46ac41d008159448b863c6Eugen Kuksa repository.get_file(self.path, commit_oid).content.encoding_utf8
ac2169141f0b549fc8917a4b1d778f4ba3cab0bfJulian Kornberger end
ee5342a8882c2fc7631fcffb5497e6597747887cTim Reddehase
a3923a34a7ada40e7db50fd8e58af228616f62c0Tim Reddehaseend