client-precache.js
1.94 KB
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
const Router = require('koa-router');
const _ = require('lodash');
const md5 = require('md5');
const stream = require('stream');
const CDNCONFIG = require('../../../config/cdn_config');
let r = new Router();
const manifestPath = 'client/precache/statics/manifest.json';
const client = {
index: async(ctx, next) => {
await ctx.render('action/client-precache', {
manifestPath
});
},
save: async(ctx, next) => {
let files = ctx.request.body.files;
let lastRevision = ctx.request.body.lastRevision;
let trimFiles = [];
_.forEach(files, val => {
trimFiles.push(_.trim(val));
});
trimFiles = _.compact(_.uniq(trimFiles));
let revision = md5(trimFiles);
if (revision === lastRevision) {
return ctx.body = {
code: 400,
message: '请修改后再保存'
};
}
let ste = new Buffer.from(JSON.stringify({
files: trimFiles,
revision
}));
const bufferStream = new stream.PassThrough();
let readable = bufferStream.end(new Buffer.from(JSON.stringify({
files: trimFiles,
revision
})));
let config = _.cloneDeep(CDNCONFIG.main);
_.assign(config, {
key: manifestPath
});
let qnMain = require('../../../lib/qiniu')(config);
let upResult = await qnMain.uploadAsync(readable, {key: manifestPath});
if (upResult && upResult.url) {
return ctx.body = {
code: 200,
message: '修改成功',
data: {
url: upResult.url
}
};
} else {
return ctx.body = {
code: 400,
message: '修改失败'
};
}
}
}
r.get('/client', client.index);
r.post('/client/save', client.save);
module.exports = r;