예제 #1
0
        //TODO: have controller be global, or static
        private void fetchAllMDFiles(MetaDataController controller, string relativeRequestPath, string parentID)
        {
            string googleFolderName = "application/vnd.google-apps.folder";
            int    numItemsPerFetch = 1000;

            //over arching list of files in this directory
            IList <Google.Apis.Drive.v3.Data.File> allFiles = new List <Google.Apis.Drive.v3.Data.File>();

            //Each iteration fill the files list.
            IList <Google.Apis.Drive.v3.Data.File> iterationFiles;

            //Execution for each iteration
            FileList exec;

            var googleDriveService = InitializeAPI.googleDriveService;

            FilesResource.ListRequest listRequest = googleDriveService.Files.List();

            string nextPageToken = null;

            Boolean moreFilesExist = true;

            //get all files in this directory
            while (moreFilesExist)
            {
                listRequest          = googleDriveService.Files.List();
                listRequest.Fields   = "nextPageToken, files";
                listRequest.PageSize = numItemsPerFetch; //get 20 things each pass
                listRequest.Q        = "'" + parentID + "' in parents";
                //get the next 10 possible folders
                if (nextPageToken != null)
                {
                    listRequest.PageToken = nextPageToken;
                }
                exec           = listRequest.Execute();
                nextPageToken  = exec.NextPageToken;
                iterationFiles = exec.Files;


                foreach (var cur in iterationFiles)
                {
                    if (cur.Trashed != true)
                    {
                        //not trash
                        allFiles.Add(cur);
                    }
                }
                if (iterationFiles.Count != numItemsPerFetch)
                {
                    //we did not have max items, no more items to fetch
                    moreFilesExist = false;
                }
            }


            //now we have all files/folders in the current directory

            Google.Apis.Drive.v3.Data.File curFile;

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string           curFileSerialized;
            string           cleansedName;
            CommonDescriptor curCD;

            while (allFiles.Count != 0)               //while there are elements
            {
                curFile           = allFiles.First(); //get first file
                curFileSerialized = serializer.Serialize(curFile);
                //pathForFile = controller.getAbsoluteFilePathForAddingMDFile(relativeRequestPath);


                if (curFile.MimeType.Equals(googleFolderName)) //folder
                {
                    //For each folder add the metaDataFolder, the CD, and then recurse.
                    cleansedName = controller.addMetaDataFolder(curFileSerialized, relativeRequestPath, curFile.Name);
                    curCD        = googleCommParser.createCommonDescriptor(relativeRequestPath, curFileSerialized);
                    controller.addCommonDescriptorFile(curCD);
                    fetchAllMDFiles(controller, relativeRequestPath + "\\" + cleansedName, curFile.Id);
                }
                else  //file
                {
                    //For each file add the metadatafile, and the CD.

                    //cleansedName is the 'clean' name of the curFile.Name, don't need to use it cause this terminates.
                    cleansedName = controller.addMetaDataFile(curFileSerialized, relativeRequestPath, curFile.Name);
                    curCD        = googleCommParser.createCommonDescriptor(relativeRequestPath, curFileSerialized);
                    controller.addCommonDescriptorFile(curCD);
                }
                allFiles.RemoveAt(0); //remove this element
            }
        }