forked from progapandist/imgproxy-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (47 loc) · 1.57 KB
/
index.js
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
// Helper for URL generator
function hex2a(hexx) {
var hex = hexx.toString() //force conversion
var str = ''
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16))
return str
}
// URL generator logic
function generateProxyUrl(opts) {
var encoded_url = btoa(opts.url).replace(/=/g, "").replace(/\//g, '_').replace(/\+/g, '-')
var path = "/rs:" + opts.resize + ":" + opts.width + ":" + opts.height + ":" + opts.enlarge +
"/g:" + opts.gravity + "/" + encoded_url + "." + opts.extension
var shaObj = new jsSHA("SHA-256", "BYTES")
shaObj.setHMACKey(opts.key, "HEX")
shaObj.update(hex2a(opts.salt))
shaObj.update(path)
var hmac = shaObj.getHMAC("B64").replace(/=/g, "").replace(/\//g, '_').replace(/\+/g, '-')
return opts.proxy_url + "/" + hmac + path
}
// UX
var formValues = {}
$(function () {
new Clipboard('.copy');
$('#submit').click(function (e) {
e.preventDefault()
$('input, select').each(function () {
var inputName = $(this).attr('id')
if (inputName !== 'submit' && inputName !== 'enlarge' && inputName !== 'proxy_url' ) {
formValues[inputName] = $(this).val()
}
if (inputName == 'proxy_url') {
formValues[inputName] = $(this).val().replace(/\/$/, '')
}
if (inputName == 'enlarge') {
if ($(this).is(':checked')) {
formValues[inputName] = 1
} else {
formValues[inputName] = 0
}
}
})
var proxyUrl = generateProxyUrl(formValues)
$('#result').val(proxyUrl)
formValues = {}
})
})