Permalink
Browse files

上传图片增加七牛和又拍云的支持

  • Loading branch information...
excaliburhan authored and lizheming committed Nov 25, 2016
1 parent 8ab6bbb commit d9b5f16be22d19929bebd54272bb7268146ed6ed
View
@@ -73,7 +73,8 @@ VALUES
('title','Firekylin 系统','网站标题'),
('twitter_url','','微博地址'),
('navigation', '[{"label":"首页","url":"/","option":"home"},{"label":"归档","url":"/archives/","option":"archive"},{"label":"标签","url":"/tags","option":"tags"},{"label":"关于","url":"/about","option":"user"},{"label":"友链","url":"/links","option":"link"}]', '导航菜单'),
('two_factor_auth','','是否开启二步验证');
('two_factor_auth','','是否开启二步验证')
('upload',NULL,'上传配置,默认为本地,也可以选择七牛或者又拍云');
/*!40000 ALTER TABLE `fk_options` ENABLE KEYS */;
UNLOCK TABLES;
View
@@ -23,6 +23,7 @@
"nunjucks": "^2.3.0",
"phpass": "^0.1.1",
"push-to-firekylin": "^0.2.2",
"qiniu": "^6.1.13",
"request": "^2.69.0",
"semver": "^5.1.0",
"source-map-support": "^0.4.0",
@@ -71,6 +72,7 @@
"stc-uglify": "^1.0.1",
"style-loader": "^0.13.0",
"superagent": "^1.7.2",
"upyun": "^2.0.4",
"webpack": "^1.12.12"
},
"repository": "https://github.com/75team/firekylin",
@@ -5,13 +5,21 @@ import moment from 'moment';
import request from 'request';
import xml2js from 'xml2js';
import toMarkdown from 'to-markdown';
import qiniu from 'qiniu';
import upyun from 'upyun';
request.defaults({
strictSSL: false,
rejectUnauthorized: false
});
export default class extends Base {
uploadConfig = {};
async __before() {
this.uploadConfig = await this.getUploadConfig();
}
async postAction() {
/** 处理远程抓取 **/
if( this.post('fileUrl') ) {
@@ -23,6 +31,15 @@ export default class extends Base {
}
let file = this.file('file');
// qiniu && upyun
const uploadConfig = this.uploadConfig;
if (uploadConfig.type === 'qiniu') {
return this.uploadByQiniu(file.path, uploadConfig);
} else if (uploadConfig.type === 'upyun') {
return this.uploadByUpyun(file.path, uploadConfig);
}
if( !file ) {
if( this.post('fileUrl') ) {
return this.getFileByUrl( this.post('fileUrl') );
@@ -46,6 +63,87 @@ export default class extends Base {
return this.success(path.join('/static/upload', destDir, basename));
}
// 获取上传设置
async getUploadConfig() {
const options = await this.model('options').getOptions();
return options.upload;
}
// 域名不带http/https自动补全http
getAbsOrigin(origin) {
const reg = /^https?:\/\/.+/;
if (!reg.test(origin)) {
return `http://${origin}`;
}
return origin;
}
// 七牛相关方法
async qiniuUpload(filename, config) {
qiniu.conf.ACCESS_KEY = config.accessKey;
qiniu.conf.SECRET_KEY = config.secretKey;
const prefix = config.prefix ? `${config.prefix}/` : '';
const dir = moment(new Date()).format("YYYYMMDD");
const basename = path.basename(filename);
const savePath = `${prefix}${dir}/${basename}`;
const token = new qiniu.rs.PutPolicy(`${config.bucket}:${savePath}`).token();
const extra = new qiniu.io.PutExtra();
return new Promise((resolve, reject) => {
qiniu.io.putFile(token, savePath, filename, extra, (err, ret) => {
if (err) {
reject(err);
} else {
const origin = this.getAbsOrigin(config.origin);
const compeletePath = `${origin}/${ret.key}`;
resolve(compeletePath);
}
});
});
}
async uploadByQiniu(filename, config) {
const result = await this.qiniuUpload(filename, config).catch((err) => {
return this.fail("FILE_UPLOAD_ERROR");
})
return this.success(result);
}
// 又拍云相关方法
async upyunUpload(filename, config) {
const upyunInstance = new upyun(
config.upyunBucket, config.operater, config.password, 'v0.api.upyun.com', { apiVersion: 'v2' }
);
const prefix = config.upyunPrefix ? `${config.upyunPrefix}/` : '';
const dir = moment(new Date()).format("YYYYMMDD");
const basename = path.basename(filename);
const remotePath = `${prefix}${dir}/${basename}`;
return new Promise((resolve, reject) => {
upyunInstance.putFile(remotePath, filename, null, false, {
'save-key': '/{year}{mon}{day}/{filename}{.suffix}'
}, (err, res) => {
if (err) {
reject(err);
} else {
if (res.statusCode === 200) {
const origin = this.getAbsOrigin(config.upyunOrigin);
const compeletePath = `${origin}/${remotePath}`;
resolve(compeletePath);
} else {
reject(res);
}
}
});
});
}
async uploadByUpyun(filename, config) {
const result = await this.upyunUpload(filename, config).catch((err) => {
return this.fail("FILE_UPLOAD_ERROR");
})
return this.success(result);
}
async getFileByUrl(url) {
let fn = think.promisify(request.get);
let result = await fn({
@@ -153,7 +251,7 @@ export default class extends Base {
} else {
summary = item['content:encoded'][0];
}
let post = {
title: item.title[0],
pathname: decodeURIComponent(item['wp:post_name'][0]),
@@ -40,6 +40,13 @@ export default class extends think.model.base {
if(!ret.comment){
ret.comment = {type: 'disqus'};
}
// upload settings
if(ret.upload && think.isString(ret.upload)){
ret.upload = JSON.parse(ret.upload);
}
if(!ret.upload){
ret.upload = {type: 'local'};
}
if(ret.push_sites && think.isString(ret.push_sites)){
ret.push_sites = JSON.parse(ret.push_sites);
}
View
@@ -36,6 +36,7 @@ ul,ol{padding:0;list-style:none;}
.options-general{max-width: 600px;}
.options-comment{max-width: 600px;}
.options-comment .form-group{padding-left: 1px;}
.options-upload{max-width: 600px;}
.form-group {overflow: hidden;}
.options-2fa{max-width: 600px;}
.options-2fa ul li {list-style-type: disc;margin-left: 18px;}
@@ -5,6 +5,7 @@ export default Reflux.createActions({
auth: {children: ['completed', 'failed'], asyncResult: true},
qrcode: {children: ['completed', 'failed'], asyncResult: true},
comment: {children: ['completed', 'failed'], asyncResult: true},
upload: {children: ['completed', 'failed'], asyncResult: true},
navigation: {children: ['completed', 'failed'], asyncResult: true},
defaultCategory: {children: ['completed', 'failed'], asyncResult: true}
});
@@ -41,6 +41,7 @@ import Options from './component/options';
import OptionsGeneral from './component/options_general';
import Options2fa from './component/options_2fa';
import OptionsComment from './component/options_comment';
import OptionsUpload from './component/options_upload';
import Import from './component/import';
import OptionsAnalytic from './component/options_analytic';
import OptionsPush from './component/options_push';
@@ -106,6 +107,7 @@ ReactDOM.render((
<Route path="general" component={OptionsGeneral} />
<Route path="two_factor_auth" component={Options2fa} />
<Route path="comment" component={OptionsComment} />
<Route path="upload" component={OptionsUpload} />
<Route path="analytic" component={OptionsAnalytic} />
<Route path="push" component={OptionsPush} />
<Route path="import" component={Import} />
Oops, something went wrong.

0 comments on commit d9b5f16

Please sign in to comment.