11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/better_record/concerns/controllers/uploadable.rb', line 11
def csv_upload(job, whitelisted_params, upload_key, prefix, job_sym = :staff_id)
uploaded = whitelisted_params[upload_key]
job_id = whitelisted_params[job_sym]
@file_stats = {
name: uploaded.original_filename,
"mime-type" => uploaded.content_type,
size: view_context.number_to_human_size(uploaded.size)
}
if verify_file(whitelisted_params, upload_key)
File.open(Rails.root.join('public', 'import_csv', "#{prefix}_#{Time.now.to_i}#{rand(1000..100000)}.csv"), 'wb') do |file|
uploaded = BetterRecord::Encoder.new(uploaded.read).to_utf8
file.write(uploaded)
job.perform_later file.path, job_id, @file_stats[:name]
end
return [200, 'File Uploaded']
else
return [
422,
[
'something went wrong',
'Only csv files with the correct headers are supported',
"content type: #{whitelisted_params[upload_key].content_type}", "file name: #{whitelisted_params[upload_key].original_filename}"
]
]
end
end
|