-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathindex.rb
119 lines (102 loc) · 2.81 KB
/
index.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
require "json"
require "base64"
require "tinify"
require "kraken-io"
class NotImageException < StandardError; end
class ApiError < StandardError; end
class NoApiError < StandardError; end
$invalidPayload = {
success: false, message: "Invalid Payload"
}
$notImage = {
success: false, message: "Input file is not an image."
}
$invalidApi = {
success: false, message: "Invalid API."
}
$noApi = {
success: false, message: "no API key was provided."
}
def tinypng(req, image)
# Get api key
if !req.variables['TINYPNG_API']
raise NoApiError
end
Tinify.key = req.variables['TINYPNG_API']
buffer = Base64.decode64(image)
result = Tinify.from_buffer(buffer).to_buffer
Base64.encode64(result)
end
def krakenio(req, image)
if !req.variables['KRAKENIO_KEY'] or !req.variables['KRAKENIO_SECRET']
raise NoApiError
end
buffer = Base64.decode64(image)
dir = Dir.mktmpdir("kraken_temp")
File.open("#{dir}/file.png", 'wb') do |f|
f.write(buffer)
end
kraken = Kraken::API.new(
:api_key => req.variables['KRAKENIO_KEY'],
:api_secret => req.variables['KRAKENIO_SECRET']
)
data = kraken.upload("#{dir}/file.png")
File.delete("#{dir}/file.png") if File.exist?("#{dir}/file.png")
if data.success
img = URI.open(data.kraked_url)
Base64.encode64(img.read)
else
if data.message.include? 'Unknown API Key'
raise ApiError
end
raise NotImageException
end
end
def main(req, res)
# Input validation
provider = nil
image = nil
begin
payload = JSON.parse(req.payload)
provider = payload["provider"]
image = payload["image"]
rescue Exception => err
return res.json($invalidPayload)
end
if provider.nil? or provider.empty? or image.nil? or image.empty?
return res.json($invalidPayload)
end
if provider == 'tinypng'
begin
base64 = tinypng(req, image)
return res.json({
success: true,
image: base64.gsub("\n",'')
})
rescue Tinify::ClientError
return res.json($notImage)
rescue Tinify::AccountError
return res.json($invalidApi)
rescue NoApiError
return res.json($noApi)
end
end
if provider == 'krakenio'
begin
base64 = krakenio(req, image)
return res.json({
success: true,
image: base64.gsub("\n",'')
})
rescue ApiError
return res.json($invalidApi)
rescue NotImageException
return res.json($notImage)
rescue NoApiError
return res.json($noApi)
end
end
res.json({
success: false, message: "Unsupported Providers."
})
end