hotfix.js 3.49 KB
/**
 * app hotfix 配置
 * @author: jiangfeng<jeff.jiang@yoho.cn>
 * @date: 2016/8/19
 */
'use strict';

const Router = require('koa-router');
const md5File = require('md5-file');
const md5 = require( 'md5');
const {Hotfix} = require('../../models');
const qn = require('../../../lib/qiniu');
const path = require('path');
const Operation = require('../../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);

module.exports = r;