-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiCloudWebService.cs
371 lines (296 loc) · 13.6 KB
/
MultiCloudWebService.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
using System.Web.Services;
using Dropbox.Api;
using System.IO;
using System.Threading.Tasks;
using Dropbox.Api.Files;
using System.Collections.Generic;
using Google.Apis.Drive.v2;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Drive.v2.Data;
using System;
using System.Threading;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Net;
using System.Security.Cryptography;
using System.Text;
/// <summary>
/// Summary description for MultiCloudWebService
/// </summary>
//[WebService(Namespace = "http://tempuri.org/")]
//[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class MultiCloudWebService : WebService
{
//Mediafire API Info
private static string url = "HTTPS://www.mediafire.com/api/1.2/user/get_session_token.php?";
private static string mf_email = "", mf_password = "", mf_app_id = "", mf_api_key = "", mf_signature;
private static string mf_secret_key, mf_ekey, mf_pkey, mf_session, mf_response, mf_time, mf_callsig;
private static string mf_folder_key = "FOLDER_ID"; // Folder to upload files to.
private string sessiontoken, keyforupload;
// END
//Insert Your drop Box Access Token
string dropboxaccesstoken = "";
//send file name
// bytes of files
// dropbox access Token
public async Task<String> UploadDropBox(string filename,byte[] b,string token)
{
try
{
DropboxClient client = new DropboxClient(token);
Stream strm = new System.IO.MemoryStream(b);
await client.Files.UploadAsync("/"+filename, null, false, null, false, strm);
strm.Close();
return "done db";
} catch(Exception ex)
{
return ex.Message + "dropbox";
}
}
public async Task<List<Metadata>> GetDropBoxFilesforweb(string token)
{
DropboxClient client = new DropboxClient(token);
ListFolderResult files = await client.Files.ListFolderAsync("");
List<Metadata> myfiles = (List<Metadata>)files.Entries;
for (int i = myfiles.Count - 1; i >= 0; i--)
{
if (myfiles[i].IsFolder)
{
myfiles.RemoveAt(i);
}
}
return myfiles;
}
public async Task<string> DownloadDropBox(string filename ,string Token)
{
DropboxClient client = new DropboxClient(Token);
var result = await client.Files.DownloadAsync("/" + filename);
byte[] filedata = await result.GetContentAsByteArrayAsync();
FileStream fos = System.IO.File.Create(Server.MapPath("downloads/" + filename));
await fos.WriteAsync(filedata, 0, filedata.Length);
return "";
}
public async Task<String> DownloadDropBox1(string filename,string token)
{
DropboxClient client = new DropboxClient(dropboxaccesstoken);
//var result = await client.Files.DeleteAsync("/" + filename);
var result = await client.Files.DownloadAsync("/" + filename);
byte[] filedata = await result.GetContentAsByteArrayAsync();
FileStream fos = System.IO.File.Create(Server.MapPath("downloads/" + filename));
await fos.WriteAsync(filedata, 0, filedata.Length);
fos.Dispose();
return Server.MapPath("downloaads/" + filedata);
}
public async Task<bool> deletedropbox(string filename, string token)
{
DropboxClient client = new DropboxClient(token);
var result = await client.Files.DeleteAsync("/" + filename);
return result.IsDeleted;
}
//*****************Google Drive API ***********************************//
public void ConnectToGoogleDriveforweb(string filename,string serviceemail,string projectname)
{
string[] scopes = new string[] { DriveService.Scope.Drive }; // Full access
var keyFilePath = Server.MapPath(filename);
var serviceAccountEmail = serviceemail;
var certificate = new X509Certificate2(keyFilePath, "Your Password", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = scopes
}.FromCertificate(certificate));
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = projectname,
});
Session["service"] = service;
}
public List<Google.Apis.Drive.v2.Data.File> GetFileGoogleDrive()
{
MyClass obj = new MyClass();
return obj.GetFiles();
}
public async Task< string> uploadFileGoogledrive(DriveService _service, string _uploadFile, string _parent = "")
{
if (System.IO.File.Exists(_uploadFile))
{
Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
body.Title = System.IO.Path.GetFileName(_uploadFile);
body.Description = "File Name " + DateTime.Now.ToString();
body.MimeType = GetMimeType(_uploadFile);
body.Parents = new List<ParentReference>() { new ParentReference() { Id=_parent} };
// File's content.
byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
try
{
FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, GetMimeType(_uploadFile));
request.Upload();
await Task.Delay(10000);
return "File Uploaded";
}
catch (Exception e)
{
return "An error occurred: " + e.Message;
}
}
else {
return "File does not exist: " + _uploadFile;
}
}
public async Task<string> uploadFileGoogledrivetoweb(DriveService _service,byte[] byteArray, string _uploadFile, string _parent = "")
{
if (true ||System.IO.File.Exists(_uploadFile))
{
Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
body.Title = System.IO.Path.GetFileName(_uploadFile);
body.Description = "File Name " + DateTime.Now.ToString();
body.MimeType = GetMimeType(_uploadFile);
// body.Parents = new List<ParentReference>() { new ParentReference() { Id = _parent } };
// uploadFileGoogledrive(_service, _uploadFile);
// File's content.
byte[] buffer= System.IO.File.ReadAllBytes(_uploadFile);
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
try
{
FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, GetMimeType(_uploadFile));
request.Upload();
await Task.Delay(1000);
return "pass File Uploaded";
}
catch (Exception e)
{
return "fail" + e.Message;
}
}
else
{
return "File does not exist: " + _uploadFile;
}
}
public String DownloadFileGoogleDrive(string downloadurl, string filename,string email)
{
var service = Session["service"] as DriveService;
// service.Files.Delete("abc");
var x = service.HttpClient.GetByteArrayAsync(downloadurl);
byte[] arrBytes = x.Result;
string _saveTo = Server.MapPath("downloads/" + filename);
System.IO.File.WriteAllBytes(_saveTo, arrBytes);
return "downloads/"+ filename;
}
public void DeleteGdrivefile(String id,String email)
{
var service = Session["service"] as DriveService;
service.Files.Delete("abc");
}
private static string GetMimeType(string fileName)
{
string mimeType = "application/unknown";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (regKey != null && regKey.GetValue("Content Type") != null)
mimeType = regKey.GetValue("Content Type").ToString();
return mimeType;
}
public static string Mediafire_GetSignature()
{
// combine email of user and password with our app id and api key to get signature
string data = mf_email + mf_password + mf_app_id + mf_api_key;
// convert them into bytes then encrypt those bytes with SHA1 and remmove '-' from bytes
byte[] bytes = Encoding.UTF8.GetBytes(data);
byte[] hash;
using (SHA1 sha1 = new SHA1Managed())
hash = sha1.ComputeHash(bytes);
string hashString = BitConverter.ToString(hash).Replace("-", "").ToLower();
return hashString;
}
public string[] Mediafire_GetInfo()
{
//get session token
mf_response = Mediafire_GetSessionToken();
//if success is not there
if (!mf_response.ToLower().Contains("<result>success</result>"))
{
// nothing will happen
}
else
{
// if success is found then we will get session , p_key,e_key with substrings
string[] sess = { "<session_token>", "</session_token>" };
string a = "";
mf_session = mf_response.Substring(mf_response.IndexOf(sess[0]) + sess[0].Length, mf_response.IndexOf(sess[1]) - mf_response.IndexOf(sess[0]) - sess[1].Length + 1);
/*string[] secret = { "<secret_key>", "</secret_key>" }; // token_version=2 only
mf_secret_key = mf_response.Substring(mf_response.IndexOf(secret[0]) + secret[0].Length, mf_response.IndexOf(secret[1]) - mf_response.IndexOf(secret[0]) - secret[1].Length + 1);*/
string[] pkey = { "<pkey>", "</pkey>" };
mf_pkey = mf_response.Substring(mf_response.IndexOf(pkey[0]) + pkey[0].Length, mf_response.IndexOf(pkey[1]) - mf_response.IndexOf(pkey[0]) - pkey[1].Length + 1);
string[] ekey = { "<ekey>", "</ekey>" };
mf_ekey = mf_response.Substring(mf_response.IndexOf(ekey[0]) + ekey[0].Length, mf_response.IndexOf(ekey[1]) - mf_response.IndexOf(ekey[0]) - ekey[1].Length + 1);
/*string[] time = { "<time>", "</time>" }; // token_version=2 only
mf_time = mf_response.Substring(mf_response.IndexOf(time[0]) + time[0].Length, mf_response.IndexOf(time[1]) - mf_response.IndexOf(time[0]) - time[1].Length + 1);*/
//store signature and session key into sessions
Session["token"] = mf_session;
Session["signature"] = mf_signature;
return sess;
}
// if nothing then reurn null
if (mf_session == null || /*mf_secret_key == null || */mf_pkey == null || mf_ekey == null/* || mf_time == null*/)
{
}
return null;
}
public string Mediafire_GetSessionToken()
{
// make url with user email and password with your app id and app keyy and create http connection
string posturl = url + "signature=" + mf_signature + "&email=" + mf_email + "&password=" + mf_password + "&application_key=" + mf_api_key + "&application_id=" + mf_app_id + "&token_version=1&response_format=xml";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(posturl);
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(posturl);
request.ContentType = "text/xml; encoding='utf-8'";
request.ContentLength = bytes.Length;
request.Method = "POST";
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
// check wheter response is ok if yes then read all daa from stream
Stream responseStream = response.GetResponseStream();
string responseStr = new StreamReader(responseStream).ReadToEnd();
return responseStr;
}
return null;
}
public List<filesformobile> getfiles()
{
// now get fiiles
List<filesformobile> ls = new List<filesformobile>();
string sign = mf_signature;
String ss = "";
// connection on this url to get details of user with the seesion token
string url = "http://www.mediafire.com/api/1.1/folder/get_content.php?folder_key=&session_token=" + Session["token"].ToString() + "&content_type=files";
WebClient wc = new WebClient();
XmlDocument xml = new XmlDocument();
// response will be in xml
xml.LoadXml(wc.DownloadString(url));
// parse the ml here
XmlNodeList xnList = xml.SelectNodes("/response/folder_content/files/file");
foreach (XmlNode xn in xnList)
{
// get name and file key from xml and add into list
string firstName = xn["filename"].InnerText;
string key = xn["quickkey"].InnerText;
ls.Add(new filesformobile() { url = key, name = firstName });
}
// data table is for design view
return ls;
// adding data into design view
}
}