-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
76 lines (56 loc) · 1.81 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
var path = require('path'),
PassThrough = require('stream').PassThrough
var mantaPath;
function MantaBlobStore(opts) {
opts = opts || {};
if (!opts.client) throw Error("manta client required");
this.client = opts.client;
if (opts.path) {
mantaPath = opts.path;
} else {
//defaults to root path for the manta user
mantaPath = '/' + this.client.user + '/stor/';
}
}
function prepOpts(opts) {
if (typeof opts === 'string') opts = {
key: opts
};
if (opts.name && !opts.key) opts.key = opts.name;
return opts;
}
MantaBlobStore.prototype.createReadStream = function(opts) {
var options = prepOpts(opts);
var fullKey = path.join(mantaPath, options.key);
return this.client.createReadStream(fullKey);
}
MantaBlobStore.prototype.createWriteStream = function(opts, cb) {
var self = this;
var options = prepOpts(opts);
var dirname = path.dirname(options.key);
var passthrough = new PassThrough;
var fullDirname = path.join(mantaPath, dirname);
self.client.mkdirp(fullDirname, function(err) {
var fullKey = path.join(mantaPath, options.key);
var stream = self.client.createWriteStream(fullKey);
passthrough
.pipe(stream);
stream.once('close', function(res) {
cb(null, options);
});
});
return passthrough;
}
MantaBlobStore.prototype.remove = function(opts, cb) {
var options = prepOpts(opts);
var fullKey = path.join(mantaPath, options.key);
this.client.unlink(fullKey, cb);
}
MantaBlobStore.prototype.exists = function(opts, cb) {
var options = prepOpts(opts);
var fullKey = path.join(mantaPath, options.key);
this.client.get(fullKey, function(err, value) {
cb(null, !err);
});
}
module.exports = MantaBlobStore;