Authored by ccbikai(👎🏻🍜)

Merge branch 'feature/clean-cdn'

... ... @@ -9,19 +9,50 @@ const Model = require('./model');
const utils = require('utility');
const rp = require('request-promise');
const ws = require('../../lib/ws');
const Capi = require('qcloudapi-sdk')
const capi = new Capi({
SecretId: 'AKID6dwpKadiQgbDpXDtyNhppIHPO5qPv5GK',
SecretKey: 'ACJkH9mg0DBA1PYpf0E7f3g534wBsQaW',
serviceType: 'cdn'
});
class CdnCache extends Model {
constructor() {
super('cdn_page');
}
async removeCache(queryUris, objectType) {
async removeCache(queryUris, objectType, cloud) {
let self = this;
if (cloud === 'qcloud') {
self._cleanQcloudCache({
ObjectPath: queryUris,
ObjectType: objectType
})
} else {
self._requestApi({
Action: 'RefreshObjectCaches',
ObjectPath: queryUris,
ObjectType: objectType
})
}
}
async _cleanQcloudCache(params) {
let self = this;
let qCloudParams = self._getQcloudParams(params);
if (!Object.keys(qCloudParams).length) {
return;
}
capi.request(qCloudParams, function(err, res) {
if (err || res.code) {
return self._broadcast(`清理腾讯云失败:${res && res.message}`)
}
self._broadcast(`清理腾讯云提交成功,RefreshTaskId:${res.data.task_id}`)
})
}
async _requestApi(params) {
let self = this;
let cdns = await self.findAll();
... ... @@ -46,12 +77,28 @@ class CdnCache extends Model {
json: true
})
.then(function (res) {
self._broadcast(`清理提交成功,RefreshTaskId:${res.RefreshTaskId}`)
self._broadcast(`清理阿里云提交成功,RefreshTaskId:${res.RefreshTaskId}`)
})
.catch(function (err) {
self._broadcast(`清理失败:${err.response.body}`)
self._broadcast(`清理阿里云失败:${err.response.body}`)
});
}
_getUrlList(str = '') {
return str.split('\n');
}
_getQcloudParams(params) {
let param = {};
let type = params.ObjectType === 'File' ? 'urls' : 'dirs';
let list = this._getUrlList(params.ObjectPath);
param.Action = params.ObjectType === 'File' ? 'RefreshCdnUrl' : 'RefreshCdnDir';
list.forEach((url, index) => {
param[`${type}.${index}`] = url;
});
return param;
}
_getUtcTime(date) {
let year = date.getUTCFullYear();
... ...
... ... @@ -22,6 +22,13 @@ const cdnCache = {
}, {
name: '目录',
typeName: 'Directory',
}],
cloudList: [{
name: '阿里云',
cloudName: 'aliyun',
}, {
name: '腾讯云',
cloudName: 'qcloud',
}]
}
await ctx.render('action/cdn_cache', date);
... ... @@ -29,8 +36,9 @@ const cdnCache = {
async clear(ctx) {
let queryUris = ctx.request.body.query_uri;
let objectType = ctx.request.body.object_type;
let cloud = ctx.request.body.cloud || 'aliyun'; // aliyun or qcloud
if (queryUris.trim() && objectType) {
CdnCache.removeCache(queryUris, objectType);
CdnCache.removeCache(queryUris, objectType, cloud);
}
return ctx.body = {
code: 200
... ...
... ... @@ -45,6 +45,11 @@
<option value="{{typeName}}">{{name}}</option>
{{/each}}
</select>
<select id="cloud" class="form-control input-sm selcet-auto pull-left mr20">
{{#each cloudList}}
<option value="{{cloudName}}">{{name}}</option>
{{/each}}
</select>
<button class="btn btn-warning btn-clear pull-left">清除</button>
<div class="result pull-left ml20"></div>
</div>
... ... @@ -58,11 +63,13 @@
$('.btn-clear').click(function() {
var typeName = $('#objectType').val();
var uri = $('#uri').val();
var cloud = $('#cloud').val();
if (uri && typeName) {
$logs.empty();
$.post('/cdn_cache/clear', {
query_uri: uri,
object_type: typeName
object_type: typeName,
cloud: cloud
}, function(res) {
});
}
... ...
... ... @@ -54,6 +54,7 @@
"node-zookeeper-client": "^0.2.2",
"nodemailer": "^3.1.8",
"npm": "^3.10.9",
"qcloudapi-sdk": "^0.1.5",
"qn": "^1.3.0",
"qs": "^6.2.0",
"request-promise": "^4.1.1",
... ...