forked from gyk001/hexo-qiniu-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.js
225 lines (200 loc) · 6.03 KB
/
sync.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
var fs = require('fs');
var qiniu = require('node-qiniu');
var path = require('path');
var log = hexo.log;
var config = require('./config');
var chokidar = require('chokidar');
var getEtag = require('./qetag');
var minimatch = require('minimatch');
var publicDir = hexo.public_dir;
var sourceDir = hexo.source_dir;
var ignoring_log = config.ignoring_log;
var ignoring_files = config.ignoring_files || [];
var local_dir = config.local_dir ? config.local_dir : 'cdn';
var dirPrefix = config.dirPrefix ? config.dirPrefix : '';
local_dir = path.join(local_dir, '.').replace(/[\\\/]/g, path.sep);
var update_exist = config.update_exist ? config.update_exist : false;
var need_upload_nums = 0;
var scan_mode = false;
// 引入七牛 Node.js SDK
// 设置全局参数,包括必须的 AccessKey 和 SecretKey,
qiniu.config({
access_key: config.access_key,
secret_key: config.secret_key
});
// 获得空间对象
var imagesBucket = qiniu.bucket(config.bucket);
/**
* 上传文件
* file为本地路径(绝对路径或相对路径都可)
* name为远程文件名
*/
var upload_file = function (file,name) {
imagesBucket.putFile(name, file, function(err, reply) {
if (err) {
log.w('Upload err: '.red + err);
return console.error(err);
}
log.i('Upload finished: '.green + reply.key);
});
};
/**
* 上传前预先检查
* file为本地路径(绝对路径或相对路径都可)
* name为远程文件名
*/
var check_upload = function (file,name) {
var res = imagesBucket.key(name);
res.stat(function(err, stat) {
if (err) {
log.e('get file stat err: '.cyan + name + '\n' + err);
return;
}
getEtag(file, function (hash) {
//先判断七牛是否已存在文件
if (stat.hash) {
if (!update_exist) {
return;
}
if (stat.hash != hash) {
res.remove(function(err) {
if (err) {
return console.error(err);
}
need_upload_nums++;
if (scan_mode) return;
log.i('Need upload update file: '.yellow + file);
});
upload_file(file,name);
}
} else {
need_upload_nums++;
if (scan_mode) return;
log.i('Need upload file: '.yellow + file);
upload_file(file,name);
}
});
});
};
/**
* 文件系统监听
* 只监听添加文件和文件修改
* 其中在每次监听初始化时,遍历到的文件都会触发添加文件事件
*/
var watch = function () {
log.i('Now start qiniu watch.'.yellow);
var watcher = chokidar.watch(local_dir, {ignored: /[\/\\]\./, persistent: true});
watcher.on('add', function( file) {
var name = path.join(dirPrefix, file.replace(local_dir, '')).replace(/\\/g, '/').replace(/^\//g, '');
check_upload(file, name);
});
watcher.on('change', function(file) {
var name2 = path.join(dirPrefix, file.replace(local_dir, '')).replace(/\\/g, '/').replace(/^\//g, '');
check_upload(file, name2);
});
};
/**
* 忽略指定文件
* @param {String} path 文件路径
* @return {Boolean}
*/
function isIgnoringFiles(path){
if (!ignoring_files.length) return false;
for (var i = 0, l = ignoring_files.length; i < l; i++){
if (minimatch(path, ignoring_files[i])) return true;
}
return false;
}
/**
* 遍历目录进行上传
*/
var sync = function (dir) {
if (!dir) {
dir='';
log.i('Now start qiniu sync.'.yellow);
}
var files = fs.readdirSync(path.join(local_dir,dir));
files.forEach(function(file) {
var fname = path.join(local_dir + '', dir + '', file + '');
var stat = fs.lstatSync(fname);
if(stat.isDirectory() == true) {
sync(path.join(dir + '', file + ''));
} else {
var name = path.join(dirPrefix, fname.replace(local_dir, '')).replace(/\\/g, '/').replace(/^\//g, '');
if (!isIgnoringFiles(name)) {
check_upload(fname, name);
} else {
ignoring_log && log.i(name + ' ignoring.'.yellow);
}
}
})
};
/**
* 遍历目录进行上传(会覆盖已上传且版本不同的资源)
*/
var sync2 = function () {
update_exist = true;
sync();
};
/**
* 遍历目录扫描需上传文件
*/
var scan = function () {
scan_mode = true;
sync();
};
/**
* 获得扫描结果
*/
var scan_end = function () {
log.i('Need upload file num: '.yellow + need_upload_nums + (need_upload_nums>0 ? '\nPlease run `hexo qiniu sync` to sync.' : '').green.bold);
};
/**
* 链接目录
*/
var symlink = function (publicdir){
var dirpath = path.join(publicdir ? publicDir : sourceDir, local_dir);
fs.exists(dirpath, function(exists){
if (!exists) {
fs.symlinkSync(local_dir, dirpath, 'junction');
if (!fs.existsSync(dirpath)) {
log.e('Can\'t make link fail!'.red);
log.w('Maybe do not have permission.'.red);
if (process.platform === 'win32') {
log.e('Please ensure that run in administrator mode!'.red);
}
}
} else {
log.w('Dir exists,can\'t symlink:'.red + dirpath);
}
});
};
/**
* 取消链接目录
*/
var unsymlink = function (dirpath){
fs.exists(dirpath, function(exists){
if (exists) {
issymlink = fs.lstatSync(dirpath).isSymbolicLink();
if (issymlink) {
fs.unlink(dirpath);
}
}
});
};
/**
* 取消链接所有目录
*/
var unsymlinkall = function (){
unsymlink( path.join(publicDir, local_dir));
unsymlink( path.join(sourceDir, local_dir));
};
module.exports = {
sync:sync,
sync2:sync2,
scan:scan,
scan_end:scan_end,
watch:watch,
symlink:symlink,
unsymlink:unsymlinkall
};