forked from ministryofjustice/mojfile-uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
105 lines (95 loc) · 3.02 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
require 'sinatra'
require 'json'
require 'logstash-logger'
require_relative 'lib/moj_file'
require 'pry'
module MojFile
class Uploader < Sinatra::Base
configure do
set :raise_errors, true
set :show_exceptions, false
end
get '/healthcheck.?:format?' do
checks = healthchecks
{
service_status: checks[:service_status],
dependencies: {
external: {
av: {
detected_infected_file: checks[:detected_infected_file],
passed_clean_file: checks[:passed_clean_file]
},
s3: {
# This is here so ops do not have to go looking for the AWS S3
# status if the write_test fails. It does not change the
# service_status
S3::REGION.tr('-','_') => S3.status,
write_test: checks[:write_test]
}
}
}
}.to_json
end
get '/:collection_ref/?:folder?' do |collection_ref, folder|
list = List.call(collection_ref, folder: folder)
if list.files?
status(200)
body(list.files.to_json)
else
error = if folder
"Collection '#{collection_ref}' and/or folder '#{folder}' does not exist or is empty."
else
"Collection '#{collection_ref}' does not exist or is empty."
end
status(404)
body({
errors: [error]
}.to_json)
end
end
post '/?:collection_ref?/new' do |collection_ref|
add = Add.new(collection_ref: collection_ref,
params: body_params)
clear = add.scan_clear?
if add.valid? && clear
add.upload
status(200)
body({ collection: add.collection, key: add.filename, folder: add.folder }.to_json)
elsif !clear
status(400)
body({ errors: ['Virus scan failed'] }.to_json)
else
status(422) # Unprocessable entity
body({ errors: add.errors }.to_json)
end
end
delete '/:collection_ref/?:folder?/:filename' do |collection_ref, folder, filename|
Delete.delete!(collection: collection_ref, folder: folder, file: filename)
status(204)
end
helpers do
def body_params
JSON.parse(request.body.read)
end
# Can't see a good way around this.
# rubocop:disable Metrics/CyclomaticComplexity
def healthchecks
write_test = Add.write_test
detect_infected = Scan.healthcheck_infected
clean_file = Scan.healthcheck_clean
service_status = if write_test && detect_infected && clean_file
'ok'
else
'failed'
end
{
service_status: service_status,
write_test: write_test ? 'ok' : 'failed',
detected_infected_file: detect_infected ? 'ok' : 'failed',
passed_clean_file: clean_file ? 'ok' : 'failed'
}
end
# rubocop:enable Metrics/CyclomaticComplexity
end
end
end