Authored by ccbikai(👎🏻🍜)

Merge branch 'feature/clean-cdn'

@@ -9,17 +9,48 @@ const Model = require('./model'); @@ -9,17 +9,48 @@ const Model = require('./model');
9 const utils = require('utility'); 9 const utils = require('utility');
10 const rp = require('request-promise'); 10 const rp = require('request-promise');
11 const ws = require('../../lib/ws'); 11 const ws = require('../../lib/ws');
  12 +const Capi = require('qcloudapi-sdk')
  13 +
  14 +const capi = new Capi({
  15 + SecretId: 'AKID6dwpKadiQgbDpXDtyNhppIHPO5qPv5GK',
  16 + SecretKey: 'ACJkH9mg0DBA1PYpf0E7f3g534wBsQaW',
  17 + serviceType: 'cdn'
  18 +});
12 19
13 class CdnCache extends Model { 20 class CdnCache extends Model {
14 constructor() { 21 constructor() {
15 super('cdn_page'); 22 super('cdn_page');
16 } 23 }
17 - async removeCache(queryUris, objectType) { 24 + async removeCache(queryUris, objectType, cloud) {
18 let self = this; 25 let self = this;
19 - self._requestApi({  
20 - Action: 'RefreshObjectCaches',  
21 - ObjectPath: queryUris,  
22 - ObjectType: objectType 26 +
  27 + if (cloud === 'qcloud') {
  28 + self._cleanQcloudCache({
  29 + ObjectPath: queryUris,
  30 + ObjectType: objectType
  31 + })
  32 + } else {
  33 + self._requestApi({
  34 + Action: 'RefreshObjectCaches',
  35 + ObjectPath: queryUris,
  36 + ObjectType: objectType
  37 + })
  38 + }
  39 + }
  40 + async _cleanQcloudCache(params) {
  41 + let self = this;
  42 + let qCloudParams = self._getQcloudParams(params);
  43 +
  44 + if (!Object.keys(qCloudParams).length) {
  45 + return;
  46 + }
  47 +
  48 + capi.request(qCloudParams, function(err, res) {
  49 + if (err || res.code) {
  50 + return self._broadcast(`清理腾讯云失败:${res && res.message}`)
  51 + }
  52 +
  53 + self._broadcast(`清理腾讯云提交成功,RefreshTaskId:${res.data.task_id}`)
23 }) 54 })
24 } 55 }
25 async _requestApi(params) { 56 async _requestApi(params) {
@@ -46,12 +77,28 @@ class CdnCache extends Model { @@ -46,12 +77,28 @@ class CdnCache extends Model {
46 json: true 77 json: true
47 }) 78 })
48 .then(function (res) { 79 .then(function (res) {
49 - self._broadcast(`清理提交成功,RefreshTaskId:${res.RefreshTaskId}`) 80 + self._broadcast(`清理阿里云提交成功,RefreshTaskId:${res.RefreshTaskId}`)
50 }) 81 })
51 .catch(function (err) { 82 .catch(function (err) {
52 - self._broadcast(`清理失败:${err.response.body}`) 83 + self._broadcast(`清理阿里云失败:${err.response.body}`)
  84 + });
  85 +
  86 + }
  87 + _getUrlList(str = '') {
  88 + return str.split('\n');
  89 + }
  90 + _getQcloudParams(params) {
  91 + let param = {};
  92 + let type = params.ObjectType === 'File' ? 'urls' : 'dirs';
  93 + let list = this._getUrlList(params.ObjectPath);
  94 +
  95 + param.Action = params.ObjectType === 'File' ? 'RefreshCdnUrl' : 'RefreshCdnDir';
  96 +
  97 + list.forEach((url, index) => {
  98 + param[`${type}.${index}`] = url;
53 }); 99 });
54 100
  101 + return param;
55 } 102 }
56 _getUtcTime(date) { 103 _getUtcTime(date) {
57 let year = date.getUTCFullYear(); 104 let year = date.getUTCFullYear();
@@ -22,6 +22,13 @@ const cdnCache = { @@ -22,6 +22,13 @@ const cdnCache = {
22 }, { 22 }, {
23 name: '目录', 23 name: '目录',
24 typeName: 'Directory', 24 typeName: 'Directory',
  25 + }],
  26 + cloudList: [{
  27 + name: '阿里云',
  28 + cloudName: 'aliyun',
  29 + }, {
  30 + name: '腾讯云',
  31 + cloudName: 'qcloud',
25 }] 32 }]
26 } 33 }
27 await ctx.render('action/cdn_cache', date); 34 await ctx.render('action/cdn_cache', date);
@@ -29,8 +36,9 @@ const cdnCache = { @@ -29,8 +36,9 @@ const cdnCache = {
29 async clear(ctx) { 36 async clear(ctx) {
30 let queryUris = ctx.request.body.query_uri; 37 let queryUris = ctx.request.body.query_uri;
31 let objectType = ctx.request.body.object_type; 38 let objectType = ctx.request.body.object_type;
  39 + let cloud = ctx.request.body.cloud || 'aliyun'; // aliyun or qcloud
32 if (queryUris.trim() && objectType) { 40 if (queryUris.trim() && objectType) {
33 - CdnCache.removeCache(queryUris, objectType); 41 + CdnCache.removeCache(queryUris, objectType, cloud);
34 } 42 }
35 return ctx.body = { 43 return ctx.body = {
36 code: 200 44 code: 200
@@ -45,6 +45,11 @@ @@ -45,6 +45,11 @@
45 <option value="{{typeName}}">{{name}}</option> 45 <option value="{{typeName}}">{{name}}</option>
46 {{/each}} 46 {{/each}}
47 </select> 47 </select>
  48 + <select id="cloud" class="form-control input-sm selcet-auto pull-left mr20">
  49 + {{#each cloudList}}
  50 + <option value="{{cloudName}}">{{name}}</option>
  51 + {{/each}}
  52 + </select>
48 <button class="btn btn-warning btn-clear pull-left">清除</button> 53 <button class="btn btn-warning btn-clear pull-left">清除</button>
49 <div class="result pull-left ml20"></div> 54 <div class="result pull-left ml20"></div>
50 </div> 55 </div>
@@ -58,11 +63,13 @@ @@ -58,11 +63,13 @@
58 $('.btn-clear').click(function() { 63 $('.btn-clear').click(function() {
59 var typeName = $('#objectType').val(); 64 var typeName = $('#objectType').val();
60 var uri = $('#uri').val(); 65 var uri = $('#uri').val();
  66 + var cloud = $('#cloud').val();
61 if (uri && typeName) { 67 if (uri && typeName) {
62 $logs.empty(); 68 $logs.empty();
63 $.post('/cdn_cache/clear', { 69 $.post('/cdn_cache/clear', {
64 query_uri: uri, 70 query_uri: uri,
65 - object_type: typeName 71 + object_type: typeName,
  72 + cloud: cloud
66 }, function(res) { 73 }, function(res) {
67 }); 74 });
68 } 75 }
@@ -54,6 +54,7 @@ @@ -54,6 +54,7 @@
54 "node-zookeeper-client": "^0.2.2", 54 "node-zookeeper-client": "^0.2.2",
55 "nodemailer": "^3.1.8", 55 "nodemailer": "^3.1.8",
56 "npm": "^3.10.9", 56 "npm": "^3.10.9",
  57 + "qcloudapi-sdk": "^0.1.5",
57 "qn": "^1.3.0", 58 "qn": "^1.3.0",
58 "qs": "^6.2.0", 59 "qs": "^6.2.0",
59 "request-promise": "^4.1.1", 60 "request-promise": "^4.1.1",