Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.90 KB | None | 0 0
  1.  def validate_and_setup_metadata_of_submission
  2.     case File.extname(file_cache.to_s)
  3.       when '.zip'
  4.         validate_and_setup_metadata_of_submission_for_zip_file
  5.       when '.tex'
  6.         validate_and_setup_metadata_of_submission_for_tex_file
  7.       else
  8.         return
  9.     end
  10.   end
  11.   # rubocop:enable all
  12.  
  13.   def validate_and_setup_metadata_of_submission_for_zip_file
  14.     begin
  15.       Zip::File.open("./public/uploads/tmp/#{file_cache}") do |zip_file|
  16.         if zip_file.glob('*.tex').size != 1
  17.           errors.add(:many_tex_files, 'The zip-file contain less or more than the one tex-file.')
  18.           break
  19.         end
  20.         tex_file = zip_file.glob('*.tex').first
  21.         stream_from_tex_file_utf8 = tex_file.get_input_stream.read.force_encoding('utf-8')
  22.         metadata_from_tex_file = MetadataExtractor.new(stream_from_tex_file_utf8)
  23.         if metadata_from_tex_file.data_about_article.empty? || metadata_from_tex_file.authors.empty?
  24.           errors.add(:no_metadata, 'The tex-file does not the metadata.')
  25.           break
  26.         end
  27.  
  28.         setup_metadata_of_sumbmission(metadata_from_tex_file)
  29.       end
  30.     rescue Zip::Error
  31.       errors.add(:incorrect_zip_file, 'The zip-file is not correct')
  32.     end
  33.   end
  34.  
  35.   def validate_and_setup_metadata_of_submission_for_tex_file
  36.     metadata_from_tex_file = MetadataExtractor.new(self.file.read.force_encoding('utf-8'))
  37.     if metadata_from_tex_file.data_about_article.empty? || metadata_from_tex_file.authors.empty?
  38.       errors.add(:no_metadata, 'The tex-file does not the metadata.')
  39.     end
  40.     setup_metadata_of_sumbmission(metadata_from_tex_file)
  41.   end
  42.  
  43.   def setup_metadata_of_sumbmission(metadata_from_tex_file)
  44.     self.name = metadata_from_tex_file.data_about_article[:title]
  45.     authors.destroy_all
  46.     metadata_from_tex_file.authors.each_with_index do |author|
  47.       authors.new(name: author[:author].to_s)
  48.     end
  49.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement