-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (48 loc) · 1.32 KB
/
index.js
File metadata and controls
57 lines (48 loc) · 1.32 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
'use strict'
const crypto = require('co-crypto')
const fs = require('fs')
const cofs = require('co-fs')
const fse = require('co-fs-extra')
const pjoin = require('path').join
class FileStore {
constructor (opts) {
opts = Object.assign({}, opts)
if (!opts.baseDir) throw Error('`baseDir` has to be specified')
this._baseDir = opts.baseDir
}
// Get the internal path for a given fileId
_filePath (fileId) {
return pjoin(this._baseDir, fileId)
}
// Return an id for later access to file
// f can be a path to file, a buffer or a Stream
* addFile (f) {
const results = yield [
crypto.randomBytes(16),
fse.ensureDir(this._baseDir)
]
const fileId = results[0].toString('hex')
const filePath = this._filePath(fileId)
if (f instanceof Buffer) {
yield cofs.writeFile(filePath, f)
} else if (typeof f === 'string') {
yield fse.copy(f, filePath)
} else {
throw Error('unsupported argument type')
}
return fileId
}
* listFiles () {
return yield cofs.readdir(this._baseDir)
}
* getFile (fileId) {
const path = this._filePath(fileId)
return (yield cofs.readFile(path))
}
* getFileStream (fileId) {
const path = this._filePath(fileId)
yield cofs.stat(path)
return fs.createReadStream(path)
}
}
module.exports = FileStore