· 7 years ago · Mar 07, 2018, 10:34 AM
1require 'exiftool'
2require 'fileutils'
3require 'jwt'
4module DataramaWorker
5
6 # Custom exeption
7 # I exiftool can't take type of file, then it will raise
8 # Won't be needed some later
9 class UknownFileTypeError < StandardError; end
10
11 class FileValidationWorker < GenericWorker
12 VALIDATION_DIR = 'public/uploads/validation_folder/'
13 SUPPORTED_EXTENSIONS = %w(jpeg jpg png gif raw nef cr2 svg tiff tga webp avi dv m4v mpeg qt vob webm wmv 3g2 3gp2 3gp 3gpp aif m4a mp3 ogg wav mov mp4 wma)
14 FUTURE_EXTENSIONS = %w(obj 3dm laz las e57)
15 SUPPORTED_ACTIONS = ['DETECT_FILE_TYPE']
16 SECRET_KEY = ENV['SECRET_KEY_VALIDATION_DISPATCHER']
17 ALGORITM = ENV['ALGORITM_FOR_ENCODE']
18 #SUPPORTED_FILE_TYPES = %w(image/png image/jpeg image/gif video/mp4 video/x-m4v audio/mpeg)
19 #FUTURE_FILE_TYPES = %w(3d-graphics/openNURBS 3d-graphics/wavefront-object 3d-graphics/point-cloud-data 3d-graphics/e57)
20
21 def initialize(connection)
22 super
23 end
24
25 def perform_task(delivery_info, properties, body)
26 begin
27 message = JSON.parse(body)
28 rescue JSON::ParserError => e
29 # TODO: Move bad messages to special queue for analysis
30 reject_message(delivery_info.delivery_tag)
31 @logger.error 'Bad Message Format!'
32 return false
33 end
34 @logger.info "#{body}"
35
36 # gets file extension
37 begin
38 FileUtils.mv("#{ENV['FULL_PATH_TO_DATARAMA_IMPORT']}#{VALIDATION_DIR}#{message['payload']['uuid']}", "#{ENV['FULL_PATH_TO_DATARAMA_IMPORT']}public/uploads/storage/originals")
39 file_data = Exiftool.new("#{ENV['FULL_PATH_TO_DATARAMA_IMPORT']}public/uploads/storage/originals/#{message['payload']['uuid']}").to_hash
40 if file_data[:file_type_extension].nil?
41 raise UknownFileTypeError.new
42 else
43 extension = file_data[:file_type_extension]
44 end
45 rescue UknownFileTypeError
46 reject_message(delivery_info.delivery_tag)
47 @logger.error 'Uknown file type of file!'
48 return false
49 end
50
51 if SUPPORTED_EXTENSIONS.include?(extension)
52 @logger.info "will send result to #{result_routing_key}"
53 ack_message(delivery_info.delivery_tag)
54
55 content_type = file_data[:mime_type]
56
57 #FileUtils.mv("#{ENV['FULL_PATH_TO_DATARAMA_IMPORT']}public/uploads/validation_folder/#{message['payload']['uuid']}", "#{ENV['FULL_PATH_TO_DATARAMA_IMPORT']}public/uploads/storage/#{message['payload']['uuid']}")
58
59
60 json4send = generate_json_message('DID_DETECT_SUPPORTED_FILE_TYPE', 'uuid' => message['payload']['uuid'], 'content-type' => content_type, 'metadata' =>
61 {
62 image_width: file_data[:image_width],
63 image_height: file_data[:image_height]
64 })
65 result = JWT.encode json4send, SECRET_KEY, ALGORITM
66 send_json_result(result, routing_key: result_routing_key)
67 return true
68 elsif FUTURE_EXTENSIONS.include?(extension)
69 @logger.info "will send result to #{result_routing_key}"
70 ack_message(delivery_info.delivery_tag)
71
72 result = generate_json_message('DID_DETECT_FUTURE_FILE_TYPE', 'uuid' => message['payload']['uuid'], 'content-type' => content_type)
73 send_json_result(result, routing_key: result_routing_key)
74 return true
75 else
76 @logger.info "will send result to #{result_routing_key}"
77 ack_message(delivery_info.delivery_tag)
78
79 result = generate_json_message('DID_DETECT_UNSUPPORTED_FILE_TYPE', 'uuid' => message['payload']['uuid'], 'content-type' => content_type)
80 send_json_result(result, routing_key: result_routing_key)
81 return true
82 end
83 end
84
85 def validate?(delivery_info, properties, body)
86 message = JSON.parse(body)
87
88 if SUPPORTED_ACTIONS.include?(message['action']) && message['payload'].has_key?('uuid')
89 @logger.info "Received valid message!"
90 true
91 else
92 # TODO: Move bad messages to special queue for analysis
93 reject_message(delivery_info.delivery_tag)
94 @logger.error 'Wrong action or missing info!'
95 false
96 end
97 rescue JSON::ParserError
98 # TODO: Move bad messages to special queue for analysis
99 reject_message(delivery_info.delivery_tag)
100 @logger.error 'Bad Message Format!'
101 false
102 end
103 end
104end