-
Notifications
You must be signed in to change notification settings - Fork 2
/
DriveActivityHelper.cs
139 lines (126 loc) · 5.78 KB
/
DriveActivityHelper.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
using Google.Apis.DriveActivity.v2;
using Google.Apis.DriveActivity.v2.Data;
using Google.Apis.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace GoogleDriveOrphanFixer
{
class DriveActivityHelper
{
DriveHelper drive;
DriveActivityService service;
public DriveActivityHelper(DriveHelper drive)
{
this.drive = drive;
// Create Google Drive Activity API service.
service = new DriveActivityService(new BaseClientService.Initializer()
{
HttpClientInitializer = drive.Credential,
ApplicationName = drive.ApplicationName,
});
}
public List<DriveItem> FindOrphanParent(List<string> orphans)
{
List<DriveItem> reparentedItems = new List<DriveItem>(orphans.Count);
for (int i = 0; i < orphans.Count; i++)
{
Console.Write("\rFinding parents of orphan {0} of {1}... ", i, orphans.Count);
try
{
reparentedItems.Add(FindOrphanParent(orphans[i]));
}
catch (Google.GoogleApiException e)
{
Console.WriteLine("ERROR: Could not retrieve activity data for orphan! " + e.Message);
}
catch (FileNotFoundException)
{
Console.Write("No activity found!");
}
catch (InvalidOperationException)
{
Console.Write("Not an orphan.");
}
}
Console.WriteLine();
return reparentedItems;
}
public DriveItem FindOrphanParent(string orphan_id)
{
QueryDriveActivityResponse response;
QueryDriveActivityRequest requestData = new QueryDriveActivityRequest();
requestData.ItemName = orphan_id;
requestData.PageSize = drive.PageSize;
do
{
ActivityResource.QueryRequest queryRequest = service.Activity.Query(requestData);
response = queryRequest.Execute();
IList<Google.Apis.DriveActivity.v2.Data.DriveActivity> activities = response.Activities;
if (activities != null && activities.Count > 0)
{
foreach (var activity in activities)
{
var move = activity.PrimaryActionDetail.Move;
// check that the item was moved from a standard folder to the MY_DRIVE_ROOT
if (move == null || move.RemovedParents == null || move.AddedParents == null) continue;
if (move.RemovedParents.Count > 1 || move.AddedParents.Count > 1)
{
Console.WriteLine("WARNING: Item has more than one removed or added parent. Only the first entries are used. This is untested.");
}
var removed = move.RemovedParents[0];
var added = move.AddedParents[0];
if (removed.DriveItem.DriveFolder == null || added.DriveItem.DriveFolder == null) continue;
// Item must be moved from STANDARD to ROOT folder NOT by the owner (i.e. current user)
if (removed.DriveItem.DriveFolder.Type == "STANDARD_FOLDER" &&
added.DriveItem.DriveFolder.Type == "MY_DRIVE_ROOT" &&
activity.Actors.Any(c => c.User.KnownUser.IsCurrentUser == null)
)
{
DriveItem item = new DriveItem
{
id = activity.Targets[0].DriveItem.Name,
title = activity.Targets[0].DriveItem.Title,
parent_id = activity.PrimaryActionDetail.Move.RemovedParents[0].DriveItem.Name,
parent_title = activity.PrimaryActionDetail.Move.RemovedParents[0].DriveItem.Title,
isFile = activity.Targets[0].DriveItem.DriveFile != null
};
return item;
}
else
{
// the folder was not moved from STANDARD -> ROOT or by owner himself
throw new InvalidOperationException("Not an orphan.");
}
}
}
else
{
break;
}
// set next request to get next page
requestData.PageToken = response.NextPageToken;
} while (response.NextPageToken != null && response.NextPageToken != "");
throw new FileNotFoundException("No Activity found!");
}
public void GetSingleActivity(string item, bool getAllInfoInsideFolder = false)
{
QueryDriveActivityResponse response;
QueryDriveActivityRequest requestData = new QueryDriveActivityRequest();
if (getAllInfoInsideFolder)
{
requestData.AncestorName = item; // gets all activity inside the folder
}
else
{
requestData.ItemName = item; // gets all activity from the item
}
requestData.PageSize = drive.PageSize;
ActivityResource.QueryRequest queryRequest = service.Activity.Query(requestData);
response = queryRequest.Execute();
// List activities.
IList<Google.Apis.DriveActivity.v2.Data.DriveActivity> activities = response.Activities;
}
}
}