-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (57 loc) · 1.48 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
'use strict'
const Promise = require('bluebird')
const upyun = require('upyun')
const fs = require('fs');
const urlParse = require('url').parse
const moment = require('moment')
const StorageBase = require('ghost-storage-base');
class UpyunStore extends StorageBase {
constructor(options) {
super(options)
this.options = options || {}
const bucket = new upyun.Service(this.options.bucket, this.options.operator, this.options.password)
this.client = new upyun.Client(bucket)
}
save(image, targetDir) {
const key = this.generateFileKey(image.name)
return this.getStream(image.path)
.then(data => {
return this.client.putFile(key, data)
}).then(result => {
console.error(result)
return Promise.resolve(`${this.options.domian}/${key}`)
})
}
read(options) {
options = options || {}
const key = urlParse(options.path).pathname.slice(1)
return this.client.getFile(key)
}
exists() {
return Promise.resolve(false)
}
delete() {
return Promise.resolve(true)
}
serve() {
return function(req, res, next) {
next()
}
}
getStream(path) {
return new Promise((reslove, reject) => {
fs.readFile(path, (error, data) => {
if (error) {
reject(error)
} else {
reslove(data)
}
})
})
}
generateFileKey(name) {
const key = moment().format(this.options.prefix || 'YYYYMM/')
return `${key}${name}`
}
}
module.exports = UpyunStore