-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
158 lines (132 loc) · 5.29 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
namespace CubeCdnFlush
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml;
using System.Xml.XPath;
class Program
{
enum ReturnCode
{
FILE_NOTFOUND = 0,
CLEAR = 1,
SERVER_ERROR = -1,
MISSING_PARAMETER = 2,
AUTHENTICATION_ERROR = 3,
LOCAL_ERROR = 91,
UNKNOWN = 92,
ACCESS_ERROR = 93
}
static void Main(string[] args)
{
var prms = new Args(args);
if (prms.isParameterMissing())
{
WriteHelp();
return;
}
var files = Directory.GetFiles(prms.LocalPath);
Console.Write("Are you ready? (Yes):");
var isReady = Console.ReadLine();
if (isReady.Equals("Yes"))
{
foreach (var item in files)
{
var currentFile = String.Format("{0}{1}", prms.Prefix, Path.GetFileName(item));
var result = FlushRemoteObject(prms.Username, prms.Password, prms.Vhost, currentFile);
Console.WriteLine("Status: {1}\tFile: {0}", currentFile, result);
}
}
}
static string SendRequest(string username, string password, string vhosts, string filename)
{
var responseText = String.Empty;
string requestData = String.Format("http://mapi.cubecdn.net/clearcache_api.php?username={0}&password={1}&vhost={2}&filename={3}", username, password, vhosts, filename);
try
{
WebClient req = new WebClient();
responseText = req.DownloadString(requestData);
}
catch (WebException ex)
{
Console.Write(ex.Message);
responseText = @"<?xml version=""1.0"" encoding=""UTF-8""?><status>93</status>";
}
catch (Exception ex)
{
Console.Write(ex.Message);
responseText = @"<?xml version=""1.0"" encoding=""UTF-8""?><status>92</status>";
}
return responseText;
}
static string readXml(string xmlText)
{
if (String.IsNullOrEmpty(xmlText))
return String.Empty;
XmlDocument document = new XmlDocument();
document.LoadXml(xmlText);
return document["status"] != null? document["status"].InnerText : String.Empty;
}
static ReturnCode FlushRemoteObject(string username, string password, string vhosts, string filename)
{
ReturnCode currentCode = ReturnCode.UNKNOWN;
var defaultStatus = (int)ReturnCode.UNKNOWN;
var response = SendRequest(username, password, vhosts, filename);
if (int.TryParse(readXml(response), out defaultStatus))
currentCode = (ReturnCode)defaultStatus;
return currentCode;
}
static void WriteHelp()
{
Console.WriteLine("Source: http://www.github.com/maestropanel/flushcdn");
Console.WriteLine("");
Console.WriteLine("Usage:\tflushcdn [username=] [password=] [vhost=] [path=] [prefix=]");
Console.WriteLine("");
Console.WriteLine("Options:");
Console.WriteLine("\tusername\tcubeCDN Username");
Console.WriteLine("\tpassword\tcubeCDN Password");
Console.WriteLine("\tvhost\t\tcubeCDN Service Name or Origin Name");
Console.WriteLine("\tpath\t\tLocal Directory Path");
Console.WriteLine("\tprefix\t\tRemote Directory Name");
Console.WriteLine();
Console.WriteLine("Example:");
Console.WriteLine("flushcdn.exe username=asikome password=p@ssw0rd vhost=mydomain.maestropanel.com path=C:\\packages\\website prefix=/myfile/");
}
private class Args
{
public string Username { get; set; }
public string Password { get; set; }
public string Vhost { get; set; }
public string LocalPath { get; set; }
public string Prefix { get; set; }
private string[] _args;
public Args(string[] args)
{
_args = args;
Username = getArgument("username");
Password = getArgument("password");
Vhost = getArgument("vhost");
LocalPath = getArgument("path");
Prefix = getArgument("prefix");
}
public bool isParameterMissing()
{
return String.IsNullOrEmpty(Username) ||
String.IsNullOrEmpty(Password) ||
String.IsNullOrEmpty(Vhost) ||
String.IsNullOrEmpty(LocalPath);
}
private string getArgument(string name)
{
var arg = String.Empty;
if (_args.Where(m => m.StartsWith(String.Format("{0}=", name))).Any())
arg = _args.FirstOrDefault(m => m.StartsWith(String.Format("{0}=", name))).Split('=').Last();
return arg;
}
}
}
}