94 internal IRESTClient DriveRESTClient {
get;
private set; }
96 internal GPALFile LocalFile {
get;
private set; }
97 internal string FileId {
get;
private set; }
98 internal string FileName {
get;
private set; }
100 internal GoogleDrive()
103 DriveRESTClient =
GPAL.
RESTClient.WithAPIBase(Config.DriveApiBase).ToGPALObject();
108 Credentials = credentials;
130 private string GetToken()
132 Credentials.FetchAccessToken(out
string token);
136 private string ResolveFileId()
138 if (
false ==
string.IsNullOrEmpty(FileId))
141 if (
string.IsNullOrEmpty(FileName))
144 var query = $
"name='{FileName}' and trashed=false";
145 var response = DriveRESTClient
146 .WithEndpoint($
"{Config.FilesEndpoint}?q={Uri.EscapeDataString(query)}&fields=files(id)")
147 .WithHeader(Config.AuthorizationHeader, $
"{Config.Bearer} {GetToken()}")
148 .Execute<DriveFilesResponse>();
150 return response?.Files?.FirstOrDefault()?.Id;
155 if (LocalFile ==
null || LocalFile.Filenames.Count == 0)
157 GPAL.
PublishSimpleEvent(GPALEventType.ERROR,
"No local file specified for upload",
null, GPALObjectType.None);
161 string filePath = LocalFile.Filenames.First();
164 string existingId = ResolveFileId();
165 if (existingId !=
null)
166 GPAL.
PublishSimpleEvent(GPALEventType.WARNING, $
"A file named [{fileName}] already exists in Drive (id: [{existingId}]). Drive will create a duplicate.",
null, GPALObjectType.None);
171 var content =
new MultipartFormDataContent();
172 content.Add(
new StringContent($
"{{ \"name\": \"{fileName}\" }}"),
"metadata",
"application/json");
173 content.Add(
new ByteArrayContent(File.ReadAllBytes(filePath)),
"file", fileName);
175 var response = DriveRESTClient
176 .WithEndpoint($
"{Config.UploadEndpoint}{Config.UploadType}")
177 .WithParameters(content)
178 .WithHeader(Config.AuthorizationHeader, $
"{Config.Bearer} {GetToken()}")
179 .Execute<DriveFileResponse>();
181 if (response ==
null)
183 GPAL.
PublishSimpleEvent(GPALEventType.ERROR, $
"Failed to upload file [{filePath}]",
null, GPALObjectType.None);
187 FileId = response.Id;
192 GPAL.
PublishSimpleEvent(GPALEventType.EXCEPTION, $
"Failed to upload file [{filePath}]",
null, GPALObjectType.None, ex);
200 var resolvedId = ResolveFileId();
201 if (
string.IsNullOrEmpty(resolvedId))
203 GPAL.
PublishSimpleEvent(GPALEventType.ERROR,
"No file specified for download",
null, GPALObjectType.None);
207 if (destination ==
null || destination.
Filenames.Count == 0)
209 GPAL.
PublishSimpleEvent(GPALEventType.ERROR,
"No destination file specified for download",
null, GPALObjectType.None);
213 string destinationPath = destination.
Filenames.First();
216 var bytes = DriveRESTClient
217 .WithEndpoint($
"{Config.FilesEndpoint}/{resolvedId}?alt=media")
218 .WithHeader(Config.AuthorizationHeader, $
"{Config.Bearer} {GetToken()}")
223 GPAL.
PublishSimpleEvent(GPALEventType.INFO, $
"Saving [{bytes.Length}] bytes of drive file [{resolvedId}] to [{destinationPath}].",
null, GPALObjectType.None);
224 File.WriteAllBytes(destinationPath, bytes);
227 GPAL.
PublishSimpleEvent(GPALEventType.ERROR, $
"Failed to download file [{resolvedId}]",
null, GPALObjectType.None);
231 GPAL.
PublishSimpleEvent(GPALEventType.EXCEPTION, $
"Failed to download file [{resolvedId}]",
null, GPALObjectType.None, ex);
239 var resolvedId = ResolveFileId();
240 if (
string.IsNullOrEmpty(resolvedId))
242 GPAL.
PublishSimpleEvent(GPALEventType.ERROR,
"No file specified for deletion",
null, GPALObjectType.None);
246 var response = DriveRESTClient
247 .WithEndpoint($
"{Config.FilesEndpoint}/{resolvedId}")
248 .WithHttpMethod(
"DELETE")
249 .WithHeader(Config.AuthorizationHeader, $
"{Config.Bearer} {GetToken()}")
252 if (response ==
null)
253 GPAL.
PublishSimpleEvent(GPALEventType.ERROR, $
"Failed to delete file [{resolvedId}]",
null, GPALObjectType.None);
261 var resolvedId = ResolveFileId();
262 if (
string.IsNullOrEmpty(resolvedId))
268 if (
string.IsNullOrEmpty(newName))
270 GPAL.
PublishSimpleEvent(GPALEventType.ERROR,
"No new file name specified for rename",
null, GPALObjectType.None);
276 var response = DriveRESTClient
277 .WithEndpoint($
"{Config.FilesEndpoint}/{resolvedId}")
278 .WithParameters(
new { name = newName })
279 .WithHeader(Config.AuthorizationHeader, $
"{Config.Bearer} {GetToken()}")
280 .WithHttpMethod(
"PATCH")
281 .Execute<DriveFileResponse>();
283 if (response ==
null)
284 GPAL.
PublishSimpleEvent(GPALEventType.ERROR, $
"Failed to rename file [{resolvedId}] to [{newName}]",
null, GPALObjectType.None);
288 GPAL.
PublishSimpleEvent(GPALEventType.EXCEPTION, $
"Failed to rename file [{resolvedId}]",
null, GPALObjectType.None, ex);
296 var resolvedId = ResolveFileId();
297 if (
string.IsNullOrEmpty(resolvedId))
303 if (
string.IsNullOrEmpty(folderId))
305 GPAL.
PublishSimpleEvent(GPALEventType.ERROR,
"No folder ID specified for move",
null, GPALObjectType.None);
311 var getResponse = DriveRESTClient
312 .WithEndpoint($
"{Config.FilesEndpoint}/{resolvedId}?fields=parents")
313 .WithHeader(Config.AuthorizationHeader, $
"{Config.Bearer} {GetToken()}")
314 .Execute<DriveFileResponse>();
316 var currentParents = getResponse?.Parents ??
new List<string>();
318 var response = DriveRESTClient
319 .WithEndpoint($
"{Config.FilesEndpoint}/{resolvedId}?addParents={folderId}&removeParents={string.Join(",
", currentParents)}")
320 .WithHeader(Config.AuthorizationHeader, $
"{Config.Bearer} {GetToken()}")
321 .WithHttpMethod(
"PATCH")
322 .Execute<DriveFileResponse>();
324 if (response ==
null)
325 GPAL.
PublishSimpleEvent(GPALEventType.ERROR, $
"Failed to move file [{resolvedId}] to folder [{folderId}]",
null, GPALObjectType.None);
329 GPAL.
PublishSimpleEvent(GPALEventType.EXCEPTION, $
"Failed to move file [{resolvedId}]",
null, GPALObjectType.None, ex);
337 fileIds =
GPAL.GridForType<
string>();
339 if (
string.IsNullOrEmpty(FileName))
341 GPAL.
PublishSimpleEvent(GPALEventType.ERROR,
"No file name specified for getting IDs",
null, GPALObjectType.None);
347 var query = $
"name='{FileName}' and trashed=false";
348 var response = DriveRESTClient
349 .WithEndpoint($
"{Config.FilesEndpoint}?q={Uri.EscapeDataString(query)}&fields=files({Config.QueryFields})")
350 .WithHeader(Config.AuthorizationHeader, $
"{Config.Bearer} {GetToken()}")
351 .Execute<DriveFilesResponse>();
353 if (response?.Files !=
null)
354 foreach (var file
in response.Files)
355 fileIds.AddRow(
new List<string> { file.Id });
357 GPAL.
PublishSimpleEvent(GPALEventType.ERROR, $
"Failed to get IDs for file name [{FileName}]",
null, GPALObjectType.None);
361 GPAL.
PublishSimpleEvent(GPALEventType.EXCEPTION, $
"Failed to get IDs for file name [{FileName}]",
null, GPALObjectType.None, ex);
369 files =
GPAL.GridForType<
string>();
373 var response = DriveRESTClient
374 .WithEndpoint($
"{Config.FilesEndpoint}?fields=files({Config.QueryFields})")
375 .WithHeader(Config.AuthorizationHeader, $
"{Config.Bearer} {GetToken()}")
376 .Execute<DriveFilesResponse>();
378 if (response?.Files !=
null)
380 foreach (var file
in response.Files)
382 files.AddRow(
new List<string> { file.Id, file.Name, file.MimeType,
string.Join(
",", file.Parents ??
new List<string>()) });