-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
108 lines (94 loc) · 2.72 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
'use strict';
const Promise = require('bluebird');
const upyun = require('upyun');
const fs = require('fs');
const urlParse = require('url').parse;
const moment = require('moment');
const BaseAdapter = require('ghost-storage-base');
class UpyunAdapter extends BaseAdapter {
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);
}
/**
* Saves the image to storage
* - image is the express image object
* - returns a promise which ultimately returns the full url to the uploaded image
*
* @param file
* @param targetDir
* @returns {*}
*/
save(file, targetDir) {
const client = this.client;
const _this = this;
return new Promise(function(resolve, reject) {
const remotePath = _this.getRemotePath(file);
const remoteDomain = _this.options.domain;
client.putFile(remotePath, fs.readFileSync(file.path)).then(function(result) {
if (_this.options.suffix !== undefined) {
resolve(remoteDomain + remotePath + _this.options.suffix);
} else {
resolve(remoteDomain + remotePath);
}
}).catch(function(error) {
reject('[' + result.data.code + '] ' + result.data.msg);
});
});
}
/**
* don't need it in Upyun
* @param filename
* @param targetDir
* @returns {*|bluebird}
* TODO: if fileKey option set, should use key to check file whether exists
*/
exists(filename, targetDir) {
return new Promise(function(resolve, reject) {
resolve(false);
});
}
// middleware for serving the files
serve() {
// a no-op, these are absolute URLs
return function(req, res, next) {
next();
};
}
/**
* Not implemented.
* @description not really delete from Upyun, may be implemented later
* @param fileName
* @param targetDir
* @returns {*|bluebird}
*/
delete(fileName, targetDir) {
return new Promise(function(resolve, reject) {
resolve(true);
});
}
/**
* Reads bytes from Upyun for a target image
*
* @param options
*/
read(options) {
options = options || {};
const client = this.client;
const key = urlParse(options.path).pathname.slice(1);
return new Promise(function(resolve, reject) {
client.getFile(key).then(function(result) {
resolve(result);
}).catch(function(error) {
reject('Could not read image');
});
});
}
getRemotePath(image) {
const folder = moment().format(this.options.folder || 'YYYY/MM/').replace(/^\//, '');
return '/' + this.options.prefix + folder + image.name;
}
}
module.exports = UpyunAdapter;