This repository was archived by the owner on Feb 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathblob-store.js
More file actions
65 lines (52 loc) · 1.36 KB
/
blob-store.js
File metadata and controls
65 lines (52 loc) · 1.36 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
var path = require('path')
var url = require('url')
function BlobStore (store, options) {
options = options || {}
this.store = store
this.path = options.path || '.'
this.buildPath = options.buildPath || function (p) {
return p.pathname.split('/').slice(1).join('_')
}
}
BlobStore.prototype.iriToPath = function (iri) {
var parsed = url.parse(iri)
return path.join(this.path, this.buildPath(parsed))
}
BlobStore.prototype.exists = function (iri) {
var self = this
return new Promise(function (resolve, reject) {
self.store.exists({key: self.iriToPath(iri)}, function (error, exists) {
if (error) {
reject(error)
} else {
resolve(exists)
}
})
})
}
BlobStore.prototype.createReadStream = function (iri) {
var self = this
try {
return self.store.createReadStream({key: self.iriToPath(iri)})
} catch(error) {
console.error(error.stack)
return null
}
}
BlobStore.prototype.createWriteStream = function (iri) {
var self = this
return self.store.createWriteStream({key: self.iriToPath(iri)})
}
BlobStore.prototype.remove = function (iri) {
var self = this
return new Promise(function (resolve, reject) {
self.store.remove({key: self.iriToPath(iri)}, function (error) {
if (error) {
reject(error)
} else {
resolve()
}
})
})
}
module.exports = BlobStore