-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsissy.js
More file actions
248 lines (237 loc) · 7.07 KB
/
sissy.js
File metadata and controls
248 lines (237 loc) · 7.07 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/***************************************************
*
* Sissy: Yet Another S3 Module for Node
* Dmytri Kleiner <dk@trick.ca>
*
* This program is free software.
* It comes without any warranty, to the extent permitted by
* applicable law. You can redistribute it and/or modify it under the
* terms of the Do What The Fuck You Want To Public License v2.
* See http://sam.zoy.org/wtfpl/COPYING for more details.
*
* This work was originaly commissiond by SoundCloud
*
* What is SoundCloud?
* http://soundcloud.com/tour
*
***********************************/
var events = require('events'),
fs = require('fs'),
path = require('path'),
sys = require('sys'),
dns = require('dns'),
net = require('net'),
http = require('http'),
crypto = require('crypto');
try {
mime = require('node-mime/mime');
} catch (err) {
mime = false;
console.log("Warning: Mime Module Not Found");
}
/* Bucky is an S3 Bucket */
var Bucky = function(account, host, bucket, options) {
options = options || function(){};
this.account = account;
this.host = host;
this.bucket = bucket;
this.storage_type = options.storage_type || 'STANDARD';
this.acl = options.acl || 'private';
if (this.check_md5 == undefined) {
this.check_md5 = true;
} else {
this.check_md5 = options.check_md5;
}
};
sys.inherits(Bucky, events.EventEmitter);
Bucky.prototype.authorize = function(method, target, headers, amz_headers) {
var bucky = this,
content_type = headers['Content-Type'] || '',
md5 = headers['Content-MD5'] || '',
current_date = headers.Date || new Date().toUTCString(),
amz_headers_list = [];
if (amz_headers) {
for (var header in amz_headers){
var key = header.toString().toLowerCase(),
value = amz_headers[header];
if (key == 'x-amz-date') {
current_date = '';
} else if(value instanceof Array) {
value = value.join(',');
}
headers[key] = value;
amz_headers_list.push(key + ':' + value);
}
var amz_headers_string = amz_headers_list.sort().join('\n');
}
var authorization_string = method + "\n" + md5 + "\n" + content_type + "\n" + current_date + "\n";
if (amz_headers) {
authorization_string += amz_headers_string + "\n";
}
authorization_string += '/' + bucky.bucket + '/' + target;
hmac = crypto.createHmac('sha1', bucky.account.secret_key),
hmac.update(authorization_string);
headers.Authorization = 'AWS ' + bucky.account.access_key + ":" + hmac.digest(encoding = 'base64');
};
Bucky.prototype.upload_file = function(file, target){
var bucky = this;
target = target || path.basename(file);
fs.stat(file, function(err, stats){
if (err) {
bucky.emit('error', err);
} else {
var send = function(md5) {
var read_stream = fs.createReadStream(file);
bucky.upload(read_stream, target, stats.size, md5)
};
if(bucky.check_md5 === true){
var hash = crypto.createHash('md5');
var file_stream = fs.createReadStream(file);
file_stream.on('data', function(data){
hash.update(data);
});
file_stream.on('error', function(err){
bucky.emit('error', err);
});
file_stream.on('end', function(){
send(hash.digest(encoding = 'base64'));
});
} else {
send();
}
}
});
};
Bucky.prototype.upload = function(read_stream, target, content_length, md5){
read_stream.pause();
var bucky = this;
dns.resolve4(bucky.host, function(err, addresses) {
if (err) {
bucky.emit('error', err);
} else {
net_stream = net.createConnection(80, host=addresses[0]);
net_stream.on('error', function(err) {
bucky.emit('error', err);
});
read_stream.on('error', function(err) {
bucky.emit('error', err);
});
bucky.put(read_stream, net_stream, target, content_length, md5);
}
});
};
Bucky.prototype.put = function(read_stream, net_stream, target, content_length, md5) {
var bucky = this, streaming = false, ok = false,
headers = {
'Date': new Date().toUTCString(),
'Host': bucky.host,
'Expect': '100-continue',
};
if (mime) {
headers['Content-Type'] = mime.lookup(target);
}
if (content_length) {
headers['Content-Length'] = content_length;
}
if (md5) {
headers['Content-MD5'] = md5;
}
amz_headers = {
'x-amz-storage-class': bucky.storage_type,
'x-amz-acl': bucky.acl
};
bucky.authorize('PUT', target, headers, amz_headers);
net_stream.on('data', function (data) {
if (!streaming) {
streaming = true;
var continue_header = /^HTTP\/1\.1 100\s+continue/i;
if (continue_header.test(data)) {
read_stream.resume();
} else {
read_stream.destroy();
net_stream.end();
err = Error(data);
bucky.emit('error', err);
}
} else if (!ok) {
ok = true;
var ok_header = /^HTTP\/1\.1 200\s+OK/i;
if (ok_header.test(data)) {
net_stream.on('end', function() {
bucky.emit('complete');
});
}
}
});
net_stream.on('connect', function() {
var header_string = "PUT " + '/' + target + " HTTP/1.1" + "\n"
for(var header in headers) {
if (headers[header] !== '') {
header_string += header + ': ' + headers[header] + "\r\n";
}
}
net_stream.write(header_string += "\r\n");
sys.pump(read_stream, net_stream, function(err) {
if (err) {
read_stream.end();
bucky.emit('error', err);
}
});
});
};
Bucky.prototype.download_file = function(file, target, range){
var bucky = this;
target = target || path.basename(file);
var write_stream = fs.createWriteStream(target);
write_stream.on('open', function(fd) {
bucky.get(write_stream, file, range);
});
write_stream.on('close', function() {
fs.stat(file, function(err, stat) {
if (!err && stat.size == 0) {
fs.unlink(file);
}
});
});
write_stream.on('error', function(ex){
write_stream.destroy();
bucky.emit('error', ex);
});
};
Bucky.prototype.get = function(write_stream, file, range){
var bucky = this, expected = '200';
http_client = http.createClient(80, bucky.host);
var headers = {
'Date': new Date().toUTCString(),
'Host': bucky.host,
};
if (range) {
expected = '206';
headers['Range'] = range;
}
bucky.authorize('GET', file, headers);
var request = http_client.request('GET', '/' + file, headers);
request.end();
request.on('response', function (response) {
if (response.statusCode == expected) {
response.on('end', function(){
write_stream.end();
bucky.emit('complete');
});
sys.pump(response, write_stream);
} else {
write_stream.end();
err = Error(response.statusCode + JSON.stringify(response.headers));
bucky.emit('error', err);
}
});
};
/* Sissy is an S3 Account */
var Sissy = function(access_key, secret_key) {
this.access_key = access_key;
this.secret_key = secret_key;
};
Sissy.prototype.bucket = function(host, bucket, options) {
return new Bucky(this, host, bucket, options);
}
exports.Sissy = Sissy;