|
|
@@ -10,6 +10,7 @@
|
|
|
|
|
|
const path = require('path');
|
|
|
const fs = require('fs');
|
|
|
+const sendToWormhole = require('stream-wormhole');
|
|
|
|
|
|
module.exports = app => {
|
|
|
|
|
|
@@ -142,6 +143,73 @@ module.exports = app => {
|
|
|
}
|
|
|
ctx.redirect(ctx.request.header.referer);
|
|
|
}
|
|
|
+ async exportTemplate(ctx) {
|
|
|
+ try {
|
|
|
+ const template = await ctx.service.calcTmpl.getTemplate(ctx.query.tid);
|
|
|
+ if (!template) throw '计算模板不存在';
|
|
|
+ if (template.spid !== ctx.subProject.id) throw '您无权导出该模板数据';
|
|
|
+
|
|
|
+ const Cpd = require('../lib/crypt').cpd;
|
|
|
+ const cpd = new Cpd();
|
|
|
+ const filename = `${template.name}.ctd`;
|
|
|
+ const filepath = path.join(this.ctx.app.baseDir, 'temp', filename);
|
|
|
+ await cpd.encrypt({ type: template.type, col_set: template.col_set, multi_header: template.multi_header }, filepath);
|
|
|
+ const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
|
|
|
+ let disposition = '';
|
|
|
+ if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
|
|
|
+ disposition = 'attachment; filename=' + encodeURIComponent(filename);
|
|
|
+ } else if (userAgent.indexOf('firefox') >= 0) {
|
|
|
+ disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(filename) + '"';
|
|
|
+ } else {
|
|
|
+ /* safari等其他非主流浏览器只能自求多福了 */
|
|
|
+ disposition = 'attachment; filename=' + new Buffer(filename).toString('binary');
|
|
|
+ }
|
|
|
+ ctx.response.set({
|
|
|
+ 'Content-Type': 'application/octet-stream',
|
|
|
+ 'Content-Disposition': disposition,
|
|
|
+ });
|
|
|
+ ctx.body = await fs.readFileSync(filepath);
|
|
|
+ } catch(err) {
|
|
|
+ ctx.log(err);
|
|
|
+ ctx.postError(err, '导出模板数据失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ async importTemplate(ctx) {
|
|
|
+ const template = await ctx.service.calcTmpl.getTemplate(ctx.query.tid);
|
|
|
+ if (!template) throw '计算模板不存在';
|
|
|
+
|
|
|
+ const create_time = Date.parse(new Date()) / 1000;
|
|
|
+ const filepath = path.join(this.ctx.app.baseDir, 'temp', `计算模板${create_time}.ctd`);
|
|
|
+ let stream, index = 0;
|
|
|
+ try {
|
|
|
+ const parts = ctx.multipart({ autoFields: true });
|
|
|
+ stream = await parts();
|
|
|
+ while (stream !== undefined) {
|
|
|
+ if (!stream.filename) throw '未发现上传文件!';
|
|
|
+ // 保存文件
|
|
|
+ await ctx.helper.saveStreamFile(stream, filepath);
|
|
|
+ await sendToWormhole(stream);
|
|
|
+ ++index;
|
|
|
+ if (Array.isArray(parts.field.size) && index < parts.field.size.length) {
|
|
|
+ stream = await parts();
|
|
|
+ } else {
|
|
|
+ stream = undefined;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const Cpd = require('../lib/crypt').cpd;
|
|
|
+ const cpd = new Cpd();
|
|
|
+ const data = await cpd.decrypt(filepath);
|
|
|
+ if (template.type !== data.type) throw '导入的模板文件与当前模板类型不一致,不可导入';
|
|
|
+
|
|
|
+ const result =await ctx.service.calcTmpl.saveTemplate({ update: { id: template.id, col_set: data.col_set, multi_header: data.multi_header } });
|
|
|
+ ctx.body = { err: 0, mgs: '', data: result };
|
|
|
+ } catch (err) {
|
|
|
+ // 失败需要消耗掉stream 以防卡死
|
|
|
+ if (stream) await sendToWormhole(stream);
|
|
|
+ ctx.log(err);
|
|
|
+ ctx.ajaxErrorBody(err, '导入模板文件失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
// ---------------------------------------------------
|
|
|
}
|
|
|
|