-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
112 lines (92 loc) · 2.38 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
102
103
104
105
106
107
108
109
110
111
112
var util = require('util')
var fs = require('fs')
var path = require('path')
var Promise = require('bluebird')
var OSS = require('ali-oss').Wrapper
var utils = require('./utils')
var baseStore = require('ghost-storage-base')
class OssStore extends baseStore {
constructor (config) {
super(config)
this.options = config || {}
this.client = new OSS(this.options)
}
save (file, targetDir) {
var client = this.client
var origin = this.options.origin
var key = this.getFileKey(file)
return new Promise(function (resolve, reject) {
return client.put(
key,
fs.createReadStream(file.path)
)
.then(function (result) {
// console.log(result)
if(origin){
resolve(utils.joinUrl(origin, result.name))
}else{
resolve(result.url)
}
})
.catch(function (err) {
// console.log(err)
reject(false)
})
})
}
exists (filename) {
// console.log('exists',filename)
var client = this.client
return new Promise(function (resolve, reject) {
return client.head(filename).then(function (result) {
// console.log(result)
resolve(true)
}).catch(function (err) {
// console.log(err)
reject(false)
})
})
}
serve (options) {
return function (req, res, next) {
next();
}
}
delete (filename) {
var client = this.client
// console.log('del',filename)
return new Promise(function (resolve, reject) {
return client.delete(filename).then(function (result) {
// console.log(result)
resolve(true)
}).catch(function (err) {
// console.log(err)
reject(false)
})
})
}
read () {
}
getFileKey (file) {
var keyOptions = this.options.fileKey
if (keyOptions) {
var getValue = function (obj) {
return typeof obj === 'function' ? obj() : obj
};
var ext = path.extname(file.name)
var name = path.basename(file.name, ext)
if (keyOptions.safeString) {
name = utils.safeString(name)
}
if (keyOptions.prefix) {
name = path.join(keyOptions.prefix, name);
}
if (keyOptions.suffix) {
name += getValue(keyOptions.suffix)
}
return name + ext.toLowerCase();
}
return null;
}
}
module.exports = OssStore