0

I have made an implementation of File/Folder lookup in Google Drive v3 using their own Google API for .NET.

Code works fine, but to be honest I'm not sure if it's really on a standard-efficient way of doing this.

Logic:

  1. I have to get to each and every folder and download specific files on it.

  2. Structure can be A > B > C > D , basically a folder within a folder within a folder and so on.

  3. I can't use a static-predefined directory schema as a long term solution as it can change anytime the owner wants to modify it, for now, the folders are at least 4 levels deep.

  4. The only way I can navigate to the subfolders is to get its own Google Drive ID and use that to see its contents. It is like you need a KEY first before you can unlock/open the next subfolder.

In short, I can't do LOOKUP on subfolders content the easy way, unless there's someone that can give us better alternatives, I'll be glad to take any criticisms on my aproach and open to all your suggestions.

Thank you.

Update Thank you all for providing links and examples, I believed Recursion solution is the best so far on my current scenario, And also, since this is a heavy IO Operations, I did apply ASYNC operations for downloading files and to the rest as possible, so I made sure to follow the ASYNC ALL THE WAY rule to prevent blocking.

To Call the Recursion Method

var parentID = "<folder id>";
var folderLevel = 0;
var listRequest = service.Files.List();
await MyTask(listRequest, id, count, folderLevel);

This is the Recursion Method, it will search all possible folders from the set root parent id that was defined...

private async Task RecursionTask(ListRequest listRequest, string parentId, int count, int folderLevel)
{
    // This method do the Folder search
    listRequest.Q = $"('{parentId}' in parents) and (mimeType = 'application/vnd.google-apps.folder') and trashed = false";
    listRequest.Fields = "files(id,name)";

    var filesTask = await listRequest.ExecuteAsync();
    var files = filesTask.Files;

    count = files.Count(); // Keep track of recursion flow
    count--;

    // Keep track of how deep recursion is diving on subfolders
    folderLevel++; 

    var tasks = new List<Task>();
    foreach(var file in files)
    {
        tasks.Add(InnerTask(file, listRequest, name, folderLevel)); // Create Array Of Tasks for IMAGE Search
        if (count > 1)  // Loop until I exhausted the value of count
        {
            // Return recursion flow
            await RecursionTask(listRequest, file.Name, file.Id, count, folderLevel);  
        }
    }
    await Task.WhenAll(tasks); // Wait all tasks to finish
}

This is the innerTask that will handle the Downloading of drive files

private async Task InnerTask(File file, ListRequest listRequest, string name,int folderLevel)
{
    // This method do the IMAGE SEARCH 
    listRequest.Q = $"('{file.Id}' in parents) and (mimeType = 'image/jpeg' or mimeType = 'image/png')";
    listRequest.Fields = "files(id,name)";

    var subFiles = await listRequest.ExecuteAsync();
    foreach (var subFile in subFiles.Files)
    {
        // Do Async task for downloading images on Google Drive
    }
}
batuzai04123
  • 379
  • 5
  • 12

0 Answers0