cdn_cache.js 3.13 KB
/**
 *
 * @author: chenfeng<feng.chen@yoho.cn>
 * @date: 16/10/19
 */

'use strict';
const Model = require('./model');
const utils = require('utility');
const rp = require('request-promise');
const ws = require('../../lib/ws');

class CdnCache extends Model {
    constructor() {
        super('cdn_page');
    }
    async removeCache(queryUris, objectType) {
        let self = this;
        self._requestApi({
            Action: 'RefreshObjectCaches',
            ObjectPath: queryUris,
            ObjectType: objectType
        })
    }
    async _requestApi(params) {
        let self = this;
        let cdns = await self.findAll();

        if (!cdns.length) {
            return;
        }
        params = Object.assign(params, {
            Format: 'JSON',
            Version: '2014-11-11',
            AccessKeyId: cdns[0].keyId,
            SignatureMethod: 'HMAC-SHA1',
            TimeStamp: self._getUtcTime(new Date()),
            SignatureVersion: '1.0',
            SignatureNonce: parseInt(Math.random() * 100000, 10) + ''
        });
        let sign = self._sign(params, cdns[0].keySecret);
        params.Signature = sign;
        rp({
            uri: `${cdns[0].scheme}://${cdns[0].cdnApi}`,
            qs: params,
            json: true
        })
        .then(function (res) {
            self._broadcast(`清理提交成功,RefreshTaskId:${res.RefreshTaskId}`)
        })
        .catch(function (err) {
            self._broadcast(`清理失败:${err.response.body}`)
        });

    }
    _getUtcTime(date) {
        let year = date.getUTCFullYear();
        let month = date.getUTCMonth() + 1;
        let day = date.getUTCDate() < 10 ? '0' + date.getUTCDate() : date.getUTCDate();
        let hour = date.getUTCHours() < 10 ? '0' + date.getUTCHours() : date.getUTCHours();
        let minute = date.getUTCMinutes() < 10 ? '0' + date.getUTCMinutes() : date.getUTCMinutes();
        let second = date.getUTCSeconds() < 10 ? '0' + date.getUTCSeconds() : date.getUTCSeconds();
        month = month < 10 ? '0' + month : month;

        return `${year}-${month}-${day}T${hour}:${minute}:${second}Z`
    }
    _sign(params, keySecret) {
        let data = {};
        Object.keys(params).sort().forEach(k => data[k]=params[k]);
        let signString = '';
        Object.keys(data).forEach(k => {
            data[k] = encodeURIComponent(data[k])
            signString += `&${k}=${data[k]}`;
        });
        if (signString) {
            signString = `GET&%2F&${encodeURIComponent(signString.substring(1, signString.length))}`;
            let sign = utils.hmac('sha1', keySecret + '&', signString); 
            return sign;
        }
        return '';
    }
    _broadcast(message) {
        console.log(message)
        ws.broadcast(`/cdn_cache/log`, {
            message: message
        });
    }
    async init() {
        let count = await this.count({});
        if (count === 0) {
            await this.insert({
                scheme: 'https',
                cdnApi: 'cdn.aliyuncs.com',
                keyId: 'Mt6m3xVdWddj8bDe',
                keySecret: 'ExHpmJJ7ayJEEMz2LUffKRe3ehMGDs'
            })
        }
    }
}

module.exports = CdnCache;