/// <summary> /// Determines the folder to file a document. /// </summary> /// <returns>The parent folder to file a given document.</returns> /// <param name="incomingUpload">Incoming upload.</param> private async Task <string> GetDestinationFolderID(IncomingUpload incomingUpload) { try { // If name is empty file in "No Name" folder. string targetFolderName = string.IsNullOrWhiteSpace(incomingUpload?.Name) ? "No Name" : CleanName(incomingUpload.Name); var searchRequest = DriveAuth.Service.Files.List(); /* * Find files of type "folder" where the name * equals the uploaded name and is within the dropbox folder. */ searchRequest.Q = $"'{DROPBOXFOLDER}' in parents and" + " mimeType = 'application/vnd.google-apps.folder' and " + $"name = '{targetFolderName}'"; // Execute query var searchResult = await searchRequest.ExecuteAsync(); if (searchResult?.Files?.Count != 0) { // Return existing folder if one exists. return(searchResult.Files.FirstOrDefault().Id); } else { // Create a new folder for this name if one doesn't exist. return(await CreateFolder(targetFolderName)); } } catch (Exception) { Console.WriteLine("Error occurred while getting parent folder."); // On error return the root dropbox folder. return(DROPBOXFOLDER); } }
public async Task <Status> UploadMedia([FromForm] IncomingUpload incomingUpload) { try { string destinationFolderID = await GetDestinationFolderID(incomingUpload); var uploadTasks = new List <Task <IUploadProgress> >(); foreach (var incomingFile in incomingUpload.Media) { // Execute upload uploadTasks.Add(UploadFile(incomingFile, destinationFolderID)); } if (!string.IsNullOrWhiteSpace(incomingUpload.Message)) { // Upload message file uploadTasks.Add(UploadFile( $"{DateTime.Now}.txt", "text/plain", incomingUpload.Message.ToStream(), destinationFolderID)); } // Wait for all uploads to complete await Task.WhenAll(uploadTasks); return(new Status { Success = uploadTasks.All(task => task.IsCompletedSuccessfully) }); } catch (Exception ex) { return(new Status { Success = false, Message = ex.Message }); } }