Skip to content

Commit 60604f6

Browse files
ngotJacksonTian
authored andcommitted
refactor: move qn util to helper
1 parent a06f038 commit 60604f6

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

app/common/upload.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

app/controller/topic.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ const fs = require('fs');
77
const awaitWriteStream = require('await-stream-ready').write;
88
const sendToWormhole = require('stream-wormhole');
99

10-
const qnUpload = require('../common/upload');
11-
1210
class TopicController extends Controller {
1311
/**
1412
* Topic page
@@ -427,7 +425,7 @@ class TopicController extends Controller {
427425
// 如果有七牛云的配置,优先上传七牛云
428426
if (config.qn_access) {
429427
try {
430-
const upload = qnUpload(config.qn_access);
428+
const upload = this.ctx.helper.qnUpload(config.qn_access);
431429
const result = await upload(stream);
432430
ctx.body = {
433431
success: true,

app/extend/helper.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const validator = require('validator');
55
const jsxss = require('xss');
66
const moment = require('moment');
77
const bcrypt = require('bcryptjs');
8+
const util = require('util');
9+
const qn = require('qn');
810

911
moment.locale('zh-cn'); // 使用中文
1012

@@ -112,3 +114,16 @@ exports.bhash = str => {
112114
exports.bcompare = (str, hash) => {
113115
return bcrypt.compareSync(str, hash);
114116
};
117+
118+
let qnClientUpload;
119+
120+
exports.qnUpload = options => {
121+
// 7牛 client
122+
if (qnClientUpload) return qnClientUpload;
123+
let qnClient;
124+
if (options.qn_access && options.qn_access.secretKey !== 'your secret key') {
125+
qnClient = qn.create(options.qn_access);
126+
}
127+
qnClientUpload = util.promisify(qnClient.upload);
128+
return qnClientUpload;
129+
};

0 commit comments

Comments
 (0)