-
Notifications
You must be signed in to change notification settings - Fork 0
/
thumbor.js
119 lines (106 loc) · 3 KB
/
thumbor.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
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
var crypto = require('crypto');
var exec = require('child_process').exec;
var url = require('./url');
var base64 = require('base64').encode;
var CryptoUrl = exports.CryptoUrl = function(options){
this.properties = {};
this.setOptions(options);
}
var prototype = {
setOptions: function(options){
for (opt in options){
this[opt](options[opt]);
}
},
set: function(name, value){
this.properties[name] = value;
return this;
},
get: function(name){
return this.properties[name];
},
setOrGet: function(name, value){
if (value == null){
return this.get(name);
} else {
return this.set(name, value);
}
},
keyLength: 16,
key: function(value){
if (value == null){
return this.get('key');
}
while (value.length < this.keyLength){
value += value;
}
return this.set('key', value.slice(0, this.keyLength));
},
_padKey: function(key){
var diff = key.length % this.keyLength;
for (var i = diff; i--;){
key += '{';
}
return key;
},
buildUrl: function(){
return url.urlFor(this.properties);
},
toUrl: function() {
var url = this.buildUrl();
console.log('url', url);
console.log('key', this.key());
var cipher = crypto.createCipher('AES-128-ECB', this.key());
var padded = this._padKey(url);
console.log('padded', padded);
encrypted = cipher.update(url);
console.log('encrypted', encrypted);
based = new Buffer(encrypted).toString('base64');
console.log(based);
return '/' + based + '/' + this.imageUrl();
},
generate: function(callback){
//var command = [
//"import base64",
//"import sys",
//"from Crypto.Cipher import *",
//"cypher = AES.new('"+ this.key() +"')",
//"sys.stdout.write(base64.urlsafe_b64encode(cypher.encrypt('"+ this._padKey(url) +"')))"
//];
//var cipher = crypto.createCipher('aes-128-ecb', this.key());
//var enc = cipher.update(this._padKey(url), 'binary', 'base64');
//exec('python -c "' + command.join(';') + '"', function(error, stdout, stderr){
//callback.call(this, error, '/' + stdout.trim() + '/' + this.imageUrl());
//}.bind(this));
}
};
for (method in prototype){
CryptoUrl.prototype[method] = prototype[method];
}
var methods = [
'key',
'width',
'height',
'smart',
'meta',
'flipHorizontal',
'flipVertical',
'halign',
'valign',
'cropLeft',
'cropTop',
'cropRight',
'cropBottom',
'imageUrl'
];
var createMethod = function(method){
return function(value){
return this.setOrGet(method, value);
};
}
for (var i = methods.length; i--;){
var method = methods[i];
if (!CryptoUrl.prototype[method]){
CryptoUrl.prototype[method] = createMethod(method);
}
}