83 internal IRESTClient DriveRESTClient {
get;
private set; }
85 internal GPALFile LocalFile {
get;
private set; }
86 internal string FileId {
get;
private set; }
87 internal string FileName {
get;
private set; }
89 internal GoogleDrive()
92 DriveRESTClient =
GPAL.
RESTClient.WithAPIBase(Config.DriveApiBase).ToGPALObject();
97 Credentials = credentials;
119 private string GetToken()
121 Credentials.FetchAccessToken(out
string token);
125 private string ResolveFileId()
127 if (
false ==
string.IsNullOrEmpty(FileId))
130 if (
string.IsNullOrEmpty(FileName))
133 var query = $
"name='{FileName}' and trashed=false";
134 var response = DriveRESTClient
135 .WithEndpoint($
"{Config.FilesEndpoint}?q={Uri.EscapeDataString(query)}&fields=files(id)")
136 .WithHeader(Config.AuthorizationHeader, $
"{Config.Bearer} {GetToken()}")
137 .Execute<DriveFilesResponse>();
139 return response?.Files?.FirstOrDefault()?.Id;
144 if (LocalFile ==
null || LocalFile.Filenames.Count == 0)
146 GPAL.
PublishSimpleEvent(GPALEventType.ERROR,
"No local file specified for upload",
null, GPALObjectType.None);
150 string filePath = LocalFile.Filenames.First();
153 string existingId = ResolveFileId();
154 if (existingId !=
null)
155 GPAL.
PublishSimpleEvent(GPALEventType.WARNING, $
"A file named [{fileName}] already exists in Drive (id: [{existingId}]). Drive will create a duplicate.",
null, GPALObjectType.None);
160 var content =
new MultipartFormDataContent();
161 content.Add(
new StringContent($
"{{ \"name\": \"{fileName}\" }}"),
"metadata",
"application/json");
162 content.Add(
new ByteArrayContent(File.ReadAllBytes(filePath)),
"file", fileName);
164 var response = DriveRESTClient
165 .WithEndpoint($
"{Config.UploadEndpoint}{Config.UploadType}")
166 .WithParameters(content)
167 .WithHeader(Config.AuthorizationHeader, $
"{Config.Bearer} {GetToken()}")
168 .Execute<DriveFileResponse>();
170 if (response ==
null)
172 GPAL.
PublishSimpleEvent(GPALEventType.ERROR, $
"Failed to upload file [{filePath}]",
null, GPALObjectType.None);
176 FileId = response.Id;
181 GPAL.
PublishSimpleEvent(GPALEventType.EXCEPTION, $
"Failed to upload file [{filePath}]",
null, GPALObjectType.None, ex);
189 var resolvedId = ResolveFileId();
190 if (
string.IsNullOrEmpty(resolvedId))
192 GPAL.
PublishSimpleEvent(GPALEventType.ERROR,
"No file specified for download",
null, GPALObjectType.None);
196 if (destination ==
null || destination.
Filenames.Count == 0)
198 GPAL.
PublishSimpleEvent(GPALEventType.ERROR,
"No destination file specified for download",
null, GPALObjectType.None);
202 string destinationPath = destination.
Filenames.First();
205 var bytes = DriveRESTClient
206 .WithEndpoint($
"{Config.FilesEndpoint}/{resolvedId}?alt=media")
207 .WithHeader(Config.AuthorizationHeader, $
"{Config.Bearer} {GetToken()}")
212 GPAL.
PublishSimpleEvent(GPALEventType.INFO, $
"Saving [{bytes.Length}] bytes of drive file [{resolvedId}] to [{destinationPath}].",
null, GPALObjectType.None);
213 File.WriteAllBytes(destinationPath, bytes);
216 GPAL.
PublishSimpleEvent(GPALEventType.ERROR, $
"Failed to download file [{resolvedId}]",
null, GPALObjectType.None);
220 GPAL.
PublishSimpleEvent(GPALEventType.EXCEPTION, $
"Failed to download file [{resolvedId}]",
null, GPALObjectType.None, ex);
228 var resolvedId = ResolveFileId();
229 if (
string.IsNullOrEmpty(resolvedId))
231 GPAL.
PublishSimpleEvent(GPALEventType.ERROR,
"No file specified for deletion",
null, GPALObjectType.None);
235 var response = DriveRESTClient
236 .WithEndpoint($
"{Config.FilesEndpoint}/{resolvedId}")
237 .WithHttpMethod(
"DELETE")
238 .WithHeader(Config.AuthorizationHeader, $
"{Config.Bearer} {GetToken()}")
241 if (response ==
null)
242 GPAL.
PublishSimpleEvent(GPALEventType.ERROR, $
"Failed to delete file [{resolvedId}]",
null, GPALObjectType.None);
250 var resolvedId = ResolveFileId();
251 if (
string.IsNullOrEmpty(resolvedId))
257 if (
string.IsNullOrEmpty(newName))
259 GPAL.
PublishSimpleEvent(GPALEventType.ERROR,
"No new file name specified for rename",
null, GPALObjectType.None);
265 var response = DriveRESTClient
266 .WithEndpoint($
"{Config.FilesEndpoint}/{resolvedId}")
267 .WithParameters(
new { name = newName })
268 .WithHeader(Config.AuthorizationHeader, $
"{Config.Bearer} {GetToken()}")
269 .WithHttpMethod(
"PATCH")
270 .Execute<DriveFileResponse>();
272 if (response ==
null)
273 GPAL.
PublishSimpleEvent(GPALEventType.ERROR, $
"Failed to rename file [{resolvedId}] to [{newName}]",
null, GPALObjectType.None);
277 GPAL.
PublishSimpleEvent(GPALEventType.EXCEPTION, $
"Failed to rename file [{resolvedId}]",
null, GPALObjectType.None, ex);
285 var resolvedId = ResolveFileId();
286 if (
string.IsNullOrEmpty(resolvedId))
292 if (
string.IsNullOrEmpty(folderId))
294 GPAL.
PublishSimpleEvent(GPALEventType.ERROR,
"No folder ID specified for move",
null, GPALObjectType.None);
300 var getResponse = DriveRESTClient
301 .WithEndpoint($
"{Config.FilesEndpoint}/{resolvedId}?fields=parents")
302 .WithHeader(Config.AuthorizationHeader, $
"{Config.Bearer} {GetToken()}")
303 .Execute<DriveFileResponse>();
305 var currentParents = getResponse?.Parents ??
new List<string>();
307 var response = DriveRESTClient
308 .WithEndpoint($
"{Config.FilesEndpoint}/{resolvedId}?addParents={folderId}&removeParents={string.Join(",
", currentParents)}")
309 .WithHeader(Config.AuthorizationHeader, $
"{Config.Bearer} {GetToken()}")
310 .WithHttpMethod(
"PATCH")
311 .Execute<DriveFileResponse>();
313 if (response ==
null)
314 GPAL.
PublishSimpleEvent(GPALEventType.ERROR, $
"Failed to move file [{resolvedId}] to folder [{folderId}]",
null, GPALObjectType.None);
318 GPAL.
PublishSimpleEvent(GPALEventType.EXCEPTION, $
"Failed to move file [{resolvedId}]",
null, GPALObjectType.None, ex);
326 fileIds =
GPAL.GridForType<
string>();
328 if (
string.IsNullOrEmpty(FileName))
330 GPAL.
PublishSimpleEvent(GPALEventType.ERROR,
"No file name specified for getting IDs",
null, GPALObjectType.None);
336 var query = $
"name='{FileName}' and trashed=false";
337 var response = DriveRESTClient
338 .WithEndpoint($
"{Config.FilesEndpoint}?q={Uri.EscapeDataString(query)}&fields=files({Config.QueryFields})")
339 .WithHeader(Config.AuthorizationHeader, $
"{Config.Bearer} {GetToken()}")
340 .Execute<DriveFilesResponse>();
342 if (response?.Files !=
null)
343 foreach (var file
in response.Files)
344 fileIds.AddRow(
new List<string> { file.Id });
346 GPAL.
PublishSimpleEvent(GPALEventType.ERROR, $
"Failed to get IDs for file name [{FileName}]",
null, GPALObjectType.None);
350 GPAL.
PublishSimpleEvent(GPALEventType.EXCEPTION, $
"Failed to get IDs for file name [{FileName}]",
null, GPALObjectType.None, ex);
358 files =
GPAL.GridForType<
string>();
362 var response = DriveRESTClient
363 .WithEndpoint($
"{Config.FilesEndpoint}?fields=files({Config.QueryFields})")
364 .WithHeader(Config.AuthorizationHeader, $
"{Config.Bearer} {GetToken()}")
365 .Execute<DriveFilesResponse>();
367 if (response?.Files !=
null)
369 foreach (var file
in response.Files)
371 files.AddRow(
new List<string> { file.Id, file.Name, file.MimeType,
string.Join(
",", file.Parents ??
new List<string>()) });