-
Notifications
You must be signed in to change notification settings - Fork 5
/
GoogleDriveHelper.cs
221 lines (188 loc) · 8.02 KB
/
GoogleDriveHelper.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System.Threading;
namespace google_drive_sample
{
public class GoogleDriveHelper
{
private const string CredentialPath = "../credentials";
private string[] Scopes = {
DriveService.Scope.Drive,
DriveService.Scope.DriveFile,
DriveService.Scope.DriveMetadata
};
private const string ApplicationName = "google-drive-sample";
private UserCredential _userCredential;
public DriveService _driveService;
public GoogleDriveHelper()
{
GetAuth();
}
public Google.Apis.Drive.v2.Data.File GetFileById(string id)
{
if (id != null)
{
FilesResource.GetRequest file = _driveService.Files.Get(id);
return file.Execute();
}
else return new Google.Apis.Drive.v2.Data.File();
}
public Google.Apis.Drive.v2.Data.File CreateDirectory(string parent_path, string file_name)
{
Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
body.Title = file_name;
body.MimeType = "application/vnd.google-apps.folder";
body.Parents = new List<ParentReference>()
{
new ParentReference() { Id = GetIdByPath(parent_path) }
};
return _driveService.Files.Insert(body).Execute();
}
public Google.Apis.Drive.v2.Data.File CreateDirectory(string path)
{
string file_name = path.Substring(path.LastIndexOf('/'), path.Length);
string parent_path = path.Substring(0, path.LastIndexOf('/'));
Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
body.Title = file_name;
body.MimeType = "application/vnd.google-apps.folder";
body.Parents = new List<ParentReference>()
{
new ParentReference() { Id = GetIdByPath(parent_path) }
};
return _driveService.Files.Insert(body).Execute();
}
public void GetAuth()
{
using (var stream = new FileStream("../../credentials/google_secret.json", System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
var credentials = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(CredentialPath, true));
_userCredential = credentials.Result;
if (credentials.IsCanceled || credentials.IsFaulted)
throw new Exception("cannot connect");
_driveService = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = _userCredential,
ApplicationName = ApplicationName,
});
}
}
public string GetRecursiveParent(string path, IList<ParentReference> parent, int idx)
{
string[] path_list = path.Split('/');
FilesResource.GetRequest parent_req = _driveService.Files.Get(parent[0].Id);
Google.Apis.Drive.v2.Data.File parent_file = parent_req.Execute();
if (parent[0].IsRoot.Value)
return parent[0].Id;
if (parent_file.Title == path_list[idx] && !parent[0].IsRoot.Value)
return GetRecursiveParent(path, parent_file.Parents, idx - 1);
else
return null;
}
public string GetIdByPath(string path)
{
if (path == "/" || path == "")
return "root";
string[] path_list = path.Split('/');
List<Google.Apis.Drive.v2.Data.File> file_search_list = new List<Google.Apis.Drive.v2.Data.File>();
FilesResource.ListRequest req = _driveService.Files.List();
do
{
req.Q = "title='" + path_list.Last<string>() + "'";
FileList file_search = req.Execute();
file_search_list.AddRange(file_search.Items);
} while (!String.IsNullOrEmpty(req.PageToken));
if (file_search_list.Count == 1)
{
return file_search_list.First<Google.Apis.Drive.v2.Data.File>().Id;
}
else
{
int last_idx = path_list.Length - 2;
Google.Apis.Drive.v2.Data.File ret = new Google.Apis.Drive.v2.Data.File();
foreach (Google.Apis.Drive.v2.Data.File f in file_search_list)
{
if (GetRecursiveParent(path, f.Parents, last_idx) != null)
{
ret = f;
break;
}
}
return ret.Id;
}
}
public void GetFile(string file_name)
{
FilesResource.ListRequest req = _driveService.Files.List();
do
{
req.Q = "title='" + file_name + "'";
FileList file_search = req.Execute();
foreach (Google.Apis.Drive.v2.Data.File a in file_search.Items)
{
System.Windows.Forms.MessageBox.Show(a.Id);
}
} while (!String.IsNullOrEmpty(req.PageToken));
}
public List<Google.Apis.Drive.v2.Data.File> GetChildren(string dir_name)
{
List<Google.Apis.Drive.v2.Data.File> result = new List<Google.Apis.Drive.v2.Data.File>();
FilesResource.ListRequest req = _driveService.Files.List();
req.Q = "title='" + dir_name + "'";
FileList children_list = req.Execute();
ChildrenResource.ListRequest child_req = _driveService.Children.List(children_list.Items[0].Id);
ChildList ch = child_req.Execute();
foreach (ChildReference a in ch.Items)
{
FilesResource.GetRequest get_file = _driveService.Files.Get(a.Id);
Google.Apis.Drive.v2.Data.File file_obj = get_file.Execute();
result.Add(file_obj);
}
return result;
}
public List<Google.Apis.Drive.v2.Data.File> GetChildrenById(string id)
{
List<Google.Apis.Drive.v2.Data.File> result = new List<Google.Apis.Drive.v2.Data.File>();
FilesResource.ListRequest req = _driveService.Files.List();
req.Q = "trashed=false";
req.Q += string.Format(" and '{0}' in parents", id);
FileList files = req.Execute();
result = files.Items.ToList<Google.Apis.Drive.v2.Data.File>();
return result;
}
public List<Google.Apis.Drive.v2.Data.File> GetRoot()
{
List<Google.Apis.Drive.v2.Data.File> result = new List<Google.Apis.Drive.v2.Data.File>();
ChildrenResource.ListRequest child_req = _driveService.Children.List("root");
ChildList ch = child_req.Execute();
foreach (ChildReference a in ch.Items)
{
FilesResource.GetRequest get_file = _driveService.Files.Get(a.Id);
Google.Apis.Drive.v2.Data.File file_obj = get_file.Execute();
result.Add(file_obj);
}
return result;
}
public bool IsDirectory(Google.Apis.Drive.v2.Data.File file)
{
if (!file.Copyable.HasValue)
{
return false;
}
return (!file.Copyable.Value && file.MimeType == "application/vnd.google-apps.folder");
}
}
}