-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFunctions.cs
More file actions
executable file
·496 lines (464 loc) · 25.3 KB
/
Functions.cs
File metadata and controls
executable file
·496 lines (464 loc) · 25.3 KB
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
using Google.Apis.Drive.v3;
using Google.Apis.Gmail.v1;
using Google.Apis.Gmail.v1.Data;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace TuckerTech_GABackup_GUI
{
public class exfunctions : Form1
{
Form1 sendtolog = new Form1();
/// <summary>
/// Dec 2016
/// We will stack all our Gmail components up here.
/// </summary>
public static void getUnread(string savelocation)
{
}
public static void CreateSubDir(string savelocation)
{
Console.WriteLine("CREATING SUBDIR: " + savelocation);
string directoryname = (savelocation + "folderlog.txt"); // Folder log created by the folder record f(x)
string filename = (savelocation + ".deltalog.tok"); // The changed file list generated from changed files f(x)
string filedirectory = (savelocation + ".folderlog.log"); // This is the new log file that will be created once the sub dirs are relocated.
var lines = File.ReadLines(directoryname)
.Select(line => line.Split(','))
.Where(item => item.All(part => !string.IsNullOrWhiteSpace(part)))
.ToList();
var result = from a in lines.AsParallel()
from b in lines.AsParallel()
where a[0] == b[2]
select new { a, b, };
StreamWriter finaldir = new StreamWriter(filedirectory);
foreach (var x in result)
{
try
{
if (x.b[4] == "1")
{
Console.Write("Root directory, skipping.\n");
return;
}
try
{
string match1 = string.Join(",", x.a[0]);
string match2 = string.Join(",", x.b[2]);
if (match1 == match2)
{
Directory.Move(savelocation + x.b[3], x.a[1] + x.b[3]);
Console.WriteLine("Moving sub directory: " + x.a[1] + x.b[3] + "\n");
finaldir.WriteLine(x.b[2] + "," + x.a[1] + x.b[3] + "," + x.a[1] + "," + x.b[3] + "," + x.b[0]);
}
else
{
finaldir.Write(x.b[2] + "," + savelocation);
Console.WriteLine("next...");
}
}
catch (IOException ex)
{
Console.Write("Error with directory: " + ex.Message.ToString() + "Directory in question: " + savelocation+x.b[3] + "\nIt's probably safe to say this exists in a parent directory. Let's clean this out.");
Directory.Delete(savelocation + x.b[3]);
}
}
catch (Exception ex)
{
if (ex.Message.ToString().Contains("DirectoryNotFound"))
{
Console.WriteLine("Error: " + ex.Message.ToString() + " Status: Carrying on");
}
}
}
finaldir.Flush();
finaldir.Close();
}
public static void MoveFiles(string savelocation)
{
try
{
string directoryname = (savelocation + "folderlog.txt"); // Folder log created by the folder record f(x)
string filename = (savelocation + ".deltalog.tok"); // The changed file list generated from changed files f(x)
string filedirectory = (savelocation + ".folderlog.log"); // This is the new log file that will be created once the sub dirs are relocated.
string ext = null;
string destfile = null;
string origfile = null;
if (!Directory.Exists(savelocation + "TTO_Backup"))
Directory.CreateDirectory(savelocation + "TTO_Backup");
var filedir = File.ReadLines(directoryname)
.Select(line => line.Split(','))
.Where(item => item.All(part => !string.IsNullOrWhiteSpace(part)))
.ToList();
var filelines = File.ReadLines(filename)
.Select(line => line.Split(','))
.Where(item => item.All(part => !string.IsNullOrWhiteSpace(part)))
.ToList();
var fulldirpath = File.ReadLines(filedirectory)
.Select(line => line.Split(','))
.Where(item => item.All(part => !string.IsNullOrWhiteSpace(part)))
.ToList();
var movefile = from a in filedir.AsParallel() // folderlog.txt (created by folder record earlier)
from b in filelines.AsParallel() // New / changed file .deltalog.tok file.
from c in fulldirpath.AsParallel()
where a[0] == b[3] && b[3] != null || b[3] != "" && c[0] != a[1] // where folderlog.txt's folderID is equal to deltalog.tok's folder ID
select new { a, b, c };
// Move files to the correct folders
Console.WriteLine("Time to move files");
if (!movefile.Any())
Console.Write("No files to move to directories. Linq query is empty!");
if (movefile.Any())
{
foreach (var x in movefile)
{
try
{
switch (x.b[2])
{
case "application/pdf":
ext = ".pdf";
break;
case "application/vnd.google-apps.document":
ext = ".docx";
break;
case "audio/wav":
ext = ".wav";
break;
case "application/vnd.google-apps.spreadsheet":
ext = ".xlsx";
break;
case "application/vnd.google-apps.ritz":
ext = ".xlsx";
break;
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
ext = ".xlsx";
break;
case "application/vnd.google-apps.kix":
ext = ".docx";
break;
case "application/msword":
ext = ".docx";
break;
case "application/vnd.google-apps.presentation":
ext = ".pptx";
break;
case "application/vnd.google-apps.punch":
ext = ".pptx";
break;
case "application/vnd.google-apps.form":
ext = ".docx";
break;
case "application/vnd.google-apps.freebird":
ext = ".docx";
break;
case "application/vnd.google-apps.drawing":
ext = ".ggl";
break;
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
ext = ".docx";
break;
case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
ext = ".pptx";
break;
case "application/vnd.google-apps.drive-sdk":
ext = ".ggl";
break;
case "image/gif":
ext = ".gif";
break;
case "application/vnd.google.drive.ext-type.jpg":
ext = ".jpg";
break;
case "image/jpeg":
ext = ".jpg";
break;
case "application/vnd.google.drive.ext-type.png":
ext = ".png";
break;
case "image/png":
ext = ".png";
break;
case "application/vnd.google.drive.ext-type.gif":
ext = ".gif";
break;
case "text/plain":
ext = ".txt";
break;
case "application/vnd.google-apps.audio":
ext = ".mp3";
break;
case "application/vnd.google-apps.file":
ext = ".docx";
break;
case "application/vnd.google-apps.photo":
ext = ".jpg";
break;
case "application/vnd.google-apps.video":
ext = ".mp4";
break;
case "application/vnd.google-apps.script+json":
ext = ".json";
break;
case "text/html":
ext = ".html";
break;
case "application/rtf":
ext = ".rtf";
break;
case "application/vnd.oasis.opendocument.text":
ext = ".docx";
break;
case "text/csv":
ext = ".csv";
break;
case "application/vnd.google-apps.unknown":
ext = ".ggl";
break;
case "application/vnd.google-apps.drive-sdk.758379822725":
ext = ".ggl";
break;
case "application/vnd.google-apps.map":
ext = ".gglmap";
break;
default:
ext = ".ggl";
break;
}
origfile = Path.Combine(savelocation, x.b[1] + ext);
if (!File.Exists(origfile)) // Dec 13 2016: May come back here w/ an overwrite option and change this so that either overwrite the file or rename it to something like filename_randomguid.
continue;
x.a[1] = x.a[1].Replace('"', '\\');
if (x.b[3] == x.c[4]) // IF the 4th column of deltatok.tok == the 5th column of .folderlog.log
{
destfile = (x.c[1] + "\\" + x.b[1] + ext); // x.c[1] == full directory path in .folderlog.log + filename in deltalog.tok + ext of file
Console.WriteLine("Moving file: " + origfile + " To: " + destfile);
File.Move(origfile, destfile);
continue;
}
}
catch (IOException ex)
{
// Nothing worked? Ugh..
destfile = (savelocation + "TTO_Backup\\" + x.b[1] + ext);
File.Move(origfile, destfile);
Console.WriteLine("Error: " + ex.Message.ToString() + " Status: Carrying on");
}
}
}
}
catch (Exception ex1)
{
Console.WriteLine("Error: " + ex1.Message.ToString() + Environment.NewLine + "Source: " + ex1.Source);
}
}
public static void RecordFolderList(string savedStartPageToken, string pageToken, string user, string savelocation)
{
Console.WriteLine("RECORDFOLDERLIST(): " + savelocation);
if (File.Exists(savelocation + "folderlog.txt"))
File.Delete(savelocation + "folderlog.txt");
FilesResource.ListRequest request1 = null;
StreamWriter folderlog = new StreamWriter(savelocation + "folderlog.txt", true);
StreamWriter logFile = new StreamWriter(savelocation + ".recent.log");
string filename = (savelocation + "folderlog.txt");
request1 = CreateService.BuildService(user).Files.List();
request1.Q = @"mimeType='application/vnd.google-apps.folder'";
request1.Fields = "files";
request1.Spaces = "drive";
request1.PageSize = 500;
var changes = request1.Execute();
foreach (var change in changes.Files)
{
try
{
var subfolderreq = CreateService.BuildService(user).Files.Get(change.Id);
subfolderreq.Fields = "id,name,parents";
var sendreq = subfolderreq.Execute();
List<string> subfolder = new List<string>();
subfolder.Add(sendreq.Name);
string folderid = "";
string updatedfile = change.Name;
string mimetype = change.MimeType;
if (change.Parents != null)
{
foreach (string sub in subfolder)
{
folderid = String.Join(",", change.Parents);
folderid = ("," + folderid + "," + sub.ToString());
string subreplace = sub.Replace(":", "_");
subreplace = string.Join("_", subreplace.Split(Path.GetInvalidFileNameChars()));
Console.WriteLine("Creating directory: " + savelocation+subreplace.ToString());
updatedfile = subreplace.ToString();
Directory.CreateDirectory(savelocation+subreplace.ToString());
}
}
updatedfile = updatedfile.Replace(":", "_");
string dirname = (savelocation + updatedfile + "\\");
//If it's a folder, let's create it now so we can move files into it later.
if (mimetype.Contains("folder"))
{
if (change.Parents == null)
{
folderlog.WriteLine(change.Id + "," + dirname + folderid + "," + "0" + "," + "1");
folderlog.Flush();
Console.WriteLine("Creating directory: " + dirname);
Directory.CreateDirectory(dirname);
}
else
{
folderlog.WriteLine(change.Id + "," + dirname + folderid + "," + "0" + "," + "0");
folderlog.Flush();
Directory.CreateDirectory(dirname);
}
}
}
catch (AggregateException ex)
{
logFile.WriteLine(ex.Message.ToString());
logFile.Flush();
Console.WriteLine("Oops!: " + ex.Message.ToString());
}
}
logFile.Close();
logFile.Dispose();
folderlog.Close();
folderlog.Dispose();
CreateSubDir(savelocation);
}
public static void ChangesFileList(string savedStartPageToken, string pageToken, string user, string savelocation)
{
string bigfiles = ConfigurationManager.AppSettings["bigfiles"];
int intbigfiles = int.Parse(bigfiles);
intbigfiles = ((intbigfiles*1024)*1024);
Form1 sendtolog = new Form1();
sendtolog.TxtLog = "Noting files for download. Please wait.";
int x = 0;
if (File.Exists(savelocation + ".deltalog.tok"))
File.Delete(savelocation + ".deltalog.tok");
var start = CreateService.BuildService(user).Changes.GetStartPageToken().Execute();
using (StreamWriter deltalog = new StreamWriter(savelocation + ".deltalog.tok", true))
{
StreamWriter logFile = new StreamWriter(savelocation + ".recent.log");
while (pageToken != null)
{
var request = CreateService.BuildService(user).Changes.List(pageToken);
request.Fields = "changes,kind,newStartPageToken,nextPageToken";
request.RestrictToMyDrive = false;
request.PageSize = 1000;
request.IncludeRemoved = false;
request.Spaces = "drive";
var changes = request.Execute();
foreach (var change in changes.Changes)
{
try
{
x++;
string folderid = "";
string updatedfile = "";
updatedfile = change.File.Name;
string mimetype = change.File.MimeType;
long? filesize = change.File.Size;
if (change.File.Parents != null)
{
folderid = String.Join(",", change.File.Parents);
}
//Form1.proUserclass.Value = x;
// Record the changed file
// Let's...not backup every file on Earth. Check user options and determine if we want to consume our backup resources to downloading a file that -probably isn't- a normal document (ie: uploaded video/movie)
// Check the bigfiles appsetting. If the file is bigger than X (whatever is set), skip it.
if (bigfiles != "") // Is there input in the "skip files over X MB" textbox.
{
if (filesize >= intbigfiles) // For reference: 104857600 bytes == 100MB
{
//
}
else
{
updatedfile = updatedfile.Replace(",", "_").Replace("\"", "_").Replace("(", "-").Replace(")", "-").Replace("?", "_").Replace("<", "_").Replace(">", "_").Replace(";", "_").Replace("$", "_").Replace("@", "_").Replace("!", "_").Replace("|", "_").Replace(":", "_");
logFile.WriteLine(user + ": New or changed file found: " + change.FileId + " --- " + updatedfile);
updatedfile = string.Join("_", updatedfile.Split(Path.GetInvalidFileNameChars()));
logFile.Flush();
if (updatedfile.Length >= 35)
{
updatedfile = (updatedfile.Substring(0,35) + "...");
}
if (mimetype == "application/vnd.google-apps.folder")
{
//
}
else
{
deltalog.WriteLine(change.FileId + "," + updatedfile + "," + mimetype + "," + folderid);
deltalog.Flush();
}
}
}
else
{
updatedfile = updatedfile.Replace(",", "_").Replace("\"", "_").Replace("(", "-").Replace(")", "-").Replace("?", "_").Replace("<", "_").Replace(">", "_").Replace(";", "_").Replace("$", "_").Replace("@", "_").Replace("!", "_").Replace("|","_").Replace(":","_");
logFile.WriteLine(user + ": New or changed file found: " + change.FileId + " --- " + updatedfile);
logFile.Flush();
if (mimetype == "application/vnd.google-apps.folder")
{
//
}
else
{
deltalog.WriteLine(change.FileId + "," + updatedfile + "," + mimetype + "," + folderid);
deltalog.Flush();
}
}
}
catch (Exception ex)
{
// Google seems to track every file ever created for the user
// it also keeps a record of the fileId. Even if a user
// deletes the file, it's still retained in Google's record
// although the file is no longer available.
// We'll throw an error to the user so the program will continue forth.
if (ex.Message.ToString().Contains("404"))
{
Console.WriteLine(user + ": 404 error. Selected record no longer exists.");
logFile.WriteLine(user + ": 404 error. Selected record no longer exists.");
logFile.Flush();
}
else
{
logFile.WriteLine(user + ": Verbatim error: @F(x)_ChangeFileList" + ex.Message.ToString());
logFile.Flush();
}
}
}
if (changes.NewStartPageToken != null)
// Last page, save this token for the next polling interval
savedStartPageToken = changes.NewStartPageToken;
// Bring our token up to date for next run
pageToken = changes.NextPageToken;
savedStartPageToken = changes.NextPageToken;
//File.WriteAllText(savelocation + ".currenttoken.tok", start.StartPageTokenValue + "," + pageToken);
File.WriteAllText(savelocation + ".currenttoken.tok", start.StartPageTokenValue);
}
deltalog.Close();
logFile.Close();
deltalog.Dispose();
logFile.Dispose();
//Form1.proUserclass.Value = 0;
}
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// exfunctions
//
this.ClientSize = new System.Drawing.Size(814, 667);
this.Name = "exfunctions";
this.Text = "TTO: Google Drive Backup";
this.ResumeLayout(false);
this.PerformLayout();
}
}
}