This repository has been archived by the owner on Nov 28, 2020. It is now read-only.
forked from gyk001/hexo-qiniu-sync
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.js
52 lines (50 loc) · 1.86 KB
/
config.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
var defaults = require('./default');
var _ = require('lodash');
var path = require('path');
var fs = require('fs');
var log = hexo.log;
// 如果配置独立秘钥文件,则使用文件里的秘钥配置
if(hexo.config.qiniu.secret_file){
var secPath = path.normalize(path.resolve('', hexo.config.qiniu.secret_file));
var qnSec=JSON.parse(fs.readFileSync(secPath,'utf-8'))
if(qnSec.secret_key){
hexo.config.qiniu.secret_key=qnSec.secret_key;
}
if(qnSec.access_key){
hexo.config.qiniu.access_key=qnSec.access_key;
}
}
// 七牛的配置组
var qnConfig = _.defaults(hexo.config.qiniu,defaults);
if(qnConfig.offline){
// 离线状态不进行同步,覆盖同步配置,
qnConfig.sync = false;
// 离线状态渲染的的链接路径也是本地的,覆盖urlPrefix配置
qnConfig.url_Prefix = path.join(hexo.config.root, qnConfig.local_dir).replace(/\\/g, '/');
}else{
// 在线状态要看是否同步
if(qnConfig.sync){
// 同步功能必须配置空间名
if(! qnConfig.bucket){
throw new Error('enable sync must set bucket');
}
}
if(!qnConfig.urlPrefix){
// 在线渲染urlPrefix和bucket至少配置一个
if(!qnConfig.bucket){
throw new Error('bucket and urlPrefix must has one');
}else{
// 没有配置urlPrefix时根据bucket生成
qnConfig.url_Prefix = ['http://',qnConfig.bucket,'.qiniucdn.com',qnConfig.dirPrefix ? '/' + qnConfig.dirPrefix : ''].join('');
}
} else {
qnConfig.url_Prefix = qnConfig.urlPrefix;
}
}
log.i('-----------------------------------------------------------');
log.i('qiniu state: '.yellow + (qnConfig.offline ? 'offline' : 'online'));
log.i('qiniu sync: '.yellow + (qnConfig.sync ? 'true' : 'false'));
log.i('qiniu local dir: '.yellow + qnConfig.local_dir);
log.i('qiniu url: '.yellow + qnConfig.url_Prefix);
log.i('-----------------------------------------------------------');
module.exports = qnConfig;