-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
101 lines (86 loc) · 3 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
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
var pkgcloud = require("pkgcloud"),
Writable = require("stream").Writable,
_ = require("underscore");
function getClient(credentials) {
return pkgcloud.storage.createClient({
provider: 'openstack',
username: credentials.userId,
password: credentials.password,
authUrl: credentials.auth_url,
tenantId: credentials.projectId,
region: credentials.region,
version: "2"
});
}
module.exports = function SwiftStore(globalOpts) {
globalOpts = globalOpts || {};
var adapter = {
read: function(options, file, response) {
var client = getClient(options.credentials);
client.download({
container: options.container,
remote: file,
stream: response
});
},
rm: function(fd, cb) { return cb(new Error('TODO')); },
ls: function(options, callback) {
var client = getClient(options.credentials);
client.getFiles(options.container, function (error, files) {
return callback(error, files);
});
},
receive: function(options) {
var receiver = Writable({
objectMode: true
});
receiver._write = function onFile(__newFile, encoding, done) {
var client = getClient(options.credentials);
console.log("Uploading file with name", __newFile.filename);
__newFile.pipe(client.upload({
container: options.container,
remote: __newFile.filename
}, function(err, value) {
console.log(err);
console.log(value);
if( err ) {
console.log( err);
receiver.emit( 'error', err );
return;
}
done();
}));
__newFile.on("end", function(err, value) {
console.log("finished uploading", __newFile.filename);
receiver.emit('finish', err, value );
done();
});
};
return receiver;
},
ensureContainerExists: function(credentials, containerName, callback) {
var client = getClient(credentials);
client.getContainers(function (error, containers) {
if (error) {
callback(error);
return;
}
if (containers.length === 0) {
client.createContainer(containerName, callback);
}
else {
var found = _.find(containers, function (container) {
return container.name === containerName;
});
if (found === undefined) {
client.createContainer(containerName, callback);
}
else {
callback(null);
}
}
});
}
}
return adapter;
}