Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def validate_and_setup_metadata_of_submission
- case File.extname(file_cache.to_s)
- when '.zip'
- validate_and_setup_metadata_of_submission_for_zip_file
- when '.tex'
- validate_and_setup_metadata_of_submission_for_tex_file
- else
- return
- end
- end
- # rubocop:enable all
- def validate_and_setup_metadata_of_submission_for_zip_file
- begin
- Zip::File.open("./public/uploads/tmp/#{file_cache}") do |zip_file|
- if zip_file.glob('*.tex').size != 1
- errors.add(:many_tex_files, 'The zip-file contain less or more than the one tex-file.')
- break
- end
- tex_file = zip_file.glob('*.tex').first
- stream_from_tex_file_utf8 = tex_file.get_input_stream.read.force_encoding('utf-8')
- metadata_from_tex_file = MetadataExtractor.new(stream_from_tex_file_utf8)
- if metadata_from_tex_file.data_about_article.empty? || metadata_from_tex_file.authors.empty?
- errors.add(:no_metadata, 'The tex-file does not the metadata.')
- break
- end
- setup_metadata_of_sumbmission(metadata_from_tex_file)
- end
- rescue Zip::Error
- errors.add(:incorrect_zip_file, 'The zip-file is not correct')
- end
- end
- def validate_and_setup_metadata_of_submission_for_tex_file
- metadata_from_tex_file = MetadataExtractor.new(self.file.read.force_encoding('utf-8'))
- if metadata_from_tex_file.data_about_article.empty? || metadata_from_tex_file.authors.empty?
- errors.add(:no_metadata, 'The tex-file does not the metadata.')
- end
- setup_metadata_of_sumbmission(metadata_from_tex_file)
- end
- def setup_metadata_of_sumbmission(metadata_from_tex_file)
- self.name = metadata_from_tex_file.data_about_article[:title]
- authors.destroy_all
- metadata_from_tex_file.authors.each_with_index do |author|
- authors.new(name: author[:author].to_s)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement