forked from jaredly/hexo-admin
-
Notifications
You must be signed in to change notification settings - Fork 9
/
upload.js
46 lines (42 loc) · 1.09 KB
/
upload.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
var qiniu = require("qiniu");
module.exports = function(options){
var C = {
qiniu: options
};
qiniu.conf.ACCESS_KEY = C.qiniu.ACCESS_KEY;
qiniu.conf.SECRET_KEY = C.qiniu.SECRET_KEY;
//构建上传策略函数
function uptoken( key) {
var putPolicy = new qiniu.rs.PutPolicy(C.qiniu.bucket+":"+key);
return putPolicy.token();
}
//构造上传函数
function uploadFile(uptoken, key, body, done) {
var extra = new qiniu.io.PutExtra();
// extra.mimeType = 'image/png';
qiniu.io.putFile(uptoken,key, body, extra, function(err, ret) {
if(ret){
if(ret.code){
done(ret);
return;
}
ret.url = 'http://' + C.qiniu.domain + '/'+ ret.key;
}
if(err)
console.log(err)
console.log(ret)
done(err, ret)
});
}
function syncQiniu(filename, data, done){
//生成上传 Token
var token = uptoken(filename);
//调用uploadFile上传
uploadFile(token, filename, data, done);
}
return {
upload: function(filename, data, done ) {
syncQiniu(filename, data, done)
}
};
}