hotfix.js
3.45 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
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
/**
* app hotfix 配置
* @author: jiangfeng<jeff.jiang@yoho.cn>
* @date: 2016/8/19
*/
'use strict';
import Router from 'koa-router';
import md5File from 'md5-file';
import md5 from 'md5';
import {Hotfix} from '../../models';
import qn from '../../../lib/qiniu'
import path from 'path';
import Operation from '../../logger/operation'
const env = process.env.NODE_ENV || 'development';
let r = new Router();
const PRIVATE_KEY = 'fd4ad5fcsa0de589af23234ks1923ks';
const hf = {
async list_page(ctx) {
let type = ctx.params.type;
let hotfixs = await Hotfix.cfind({
type: type
}).sort({
time: -1
}).exec();
await ctx.render('action/hotfix_list', {hotfixs: hotfixs, type: type});
},
async new_page(ctx) {
let type = ctx.params.type;
await ctx.render('action/hotfix_form', {type: type});
},
async save(ctx) {
let type = ctx.params.type;
let body = ctx.request.body;
let {appName, appVersion, patchVersion, _files} = body;
let batchFile;
if (_files && _files.batch) {
batchFile = _files.batch;
} else {
return ctx.body = {
code: 400,
message: '未上传补丁文件'
};
}
let filecode = md5File.sync(batchFile.path);
filecode = md5(filecode + 'yohopatch2016');
let hotfixRaw = {
type: type,
appName: appName,
appVersion: appVersion,
patchVersion: patchVersion,
filecode: filecode,
time: new Date()
};
let batchKey = `app-hotfix2/${env}/${appName}/${type.toLowerCase()}/${appVersion}`;
if (patchVersion) {
batchKey = path.join(batchKey, patchVersion);
}
batchKey = path.join(batchKey, batchFile.name);
let apiFileKey = `app-hotfix2/${env}/${appName}/${type.toLowerCase()}/${appVersion}/api.json`;
let batchUpload = await qn.uploadFileAsync(batchFile.path, {key: batchKey});
if (batchUpload && batchUpload.url) {
let apiData = {
code: 200,
data: {
url: batchUpload.url,
patchv: patchVersion,
filecode: filecode
}
};
hotfixRaw.patchFile = batchUpload.url;
apiData.md5 = md5(PRIVATE_KEY + ':' + JSON.stringify(apiData.data));
console.log(apiFileKey);
let apiUpload = await qn.key(apiFileKey).uploadAsync(JSON.stringify(apiData), {key: apiFileKey});
if (apiUpload && apiUpload.url) {
hotfixRaw.apiFile = apiUpload.url;
} else {
return ctx.body = {
code: 400,
message: '上传api文件失败, 请重试'
};
}
} else {
return ctx.body = {
code: 400,
message: '上传补丁文件失败, 请重试'
};
}
await Hotfix.insert(hotfixRaw);
Operation.action(ctx.session.user, 'NEW_HOTFIX', '新增hotfix', {
type: type,
appName: appName,
appVersion: appVersion,
batchFile: hotfixRaw.patchFile
});
return ctx.body = {
code: 200
};
}
};
r.get('/:type', hf.list_page);
r.get('/new/:type', hf.new_page);
r.post('/save/:type', hf.save);
export default r;