-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
178 lines (153 loc) · 4.31 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* Qiniu storage module for Ghost blog 1.x
* @see https://docs.ghost.org/v1.0.0/docs/using-a-custom-storage-module
*/
'use strict';
const path = require('path');
const fs = require('fs');
const urlParse = require('url').parse;
const Promise = require('bluebird');
const moment = require('moment');
const qn = require('qn');
const StorageBase = require('ghost-storage-base');
const errors = require('@tryghost/errors');
const security = require('@tryghost/security');
const getHash = require('./lib/getHash');
const logPrefix = '[QiniuStore]';
class QiniuStore extends StorageBase {
constructor(options) {
super(options);
this.options = options || {};
this.client = qn.create(this.options);
}
/**
* 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) {
_this.getFileKey(file).then((function(key) {
client.upload(fs.createReadStream(file.path), {
key: key
}, function(err, result) {
console.log(logPrefix, result);
// console.log('[' + err.code + '] ' + err.name);
err ? reject(err) : resolve(result.url);
});
}));
});
}
/**
* don't need it in Qiniu
* @param filename
* @param targetDir
* @returns {*|bluebird}
* @see https://support.qiniu.com/hc/kb/article/112817/
* TODO: if fileKey option set, should use key to check file whether exists
*/
exists(filename, targetDir) {
return new Promise(function(resolve, reject) {
resolve(false);
});
}
/*
exists(filename) {
return new Promise(function(resolve, reject) {
// send key to get image info
client.stat(filename, function(err, info) {
if (info) {
resolve(true);
} else if (err && err.code === 612) { // File not exists
resolve(false);
} else {
reject('Can\'t get file info.');
}
});
});
}*/
// 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 Qiniu, may be implemented later
* @param fileName
* @param targetDir
* @returns {*|bluebird}
*/
delete(fileName, targetDir) {
// return Promise.reject('not implemented');
return new Promise(function(resolve, reject) {
resolve(true);
});
}
/**
* Reads bytes from Qiniu 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.download(key, function(err, content, res) {
if (err) {
return reject(new errors.GhostError({
err: err,
message: `${logPrefix} Could not read image: ${options.path}`,
}));
}
resolve(content);
});
});
}
getFileKey(file) {
const keyOptions = this.options.fileKey;
let fileKey = null;
if (keyOptions) {
const getValue = function(obj) {
return typeof obj === 'function' ? obj() : obj;
};
const ext = path.extname(file.name);
let basename = path.basename(file.name, ext);
let prefix = '';
let suffix = '';
let extname = '';
if (keyOptions.prefix) {
prefix = moment().format(getValue(keyOptions.prefix))
.replace(/^\//, '');
}
if (keyOptions.suffix) {
suffix = getValue(keyOptions.suffix);
}
if (keyOptions.extname !== false) {
extname = ext.toLowerCase();
}
const contactKey = function(name) {
return prefix + name + suffix + extname;
};
if (keyOptions.hashAsBasename) {
return getHash(file).then(function(hash) {
return contactKey(hash);
});
} else if (keyOptions.safeString) {
basename = security.string.safe(basename);
}
fileKey = contactKey(basename);
}
return Promise.resolve(fileKey);
}
}
module.exports = QiniuStore;