コード例 #1
0
        private static string getOrCreateCloudSubFolderId(string parentId, string childName)
        {
            var folderSearch = CloudDriveOperations.getChildFolderByName(config, parentId, childName).data;

            if (folderSearch == null || folderSearch.Count == 0)
            {
                return(CloudDriveOperations.createFolder(config, childName, parentId));
            }
            if (folderSearch.Count > 0)
            {
                return(folderSearch.First().id);
            }
            return(String.Empty);
        }
コード例 #2
0
        private static void loadCache()
        {
            CloudDriveListResponse <CloudDriveFolder> allFolders = CloudDriveOperations.getFolders(config, "");

            foreach (CloudDriveFolder x in allFolders.data)
            {
                folderCache.Add(x.id, x, DateTime.Now.AddHours(1));
            }
            Console.WriteLine("Folder cache loaded: {0} items", folderCache.GetCount());
            writeCache("folder_cache.json", allFolders.data);

            CloudDriveListResponse <CloudDriveFile> allFiles =
                CloudDriveOperations.getAllFiles(config);

            // JsonConvert.DeserializeObject<ConfigData>(File.ReadAllText(ConfigurationManager.AppSettings["jsonConfig"]))
            foreach (CloudDriveFile x in allFiles.data)
            {
                fileCache.Add(x.id, x, DateTime.Now.AddHours(1));
            }
            Console.WriteLine("File cache loaded: {0} items", fileCache.GetCount());
            writeCache("file_cache.json", allFiles.data);
        }
コード例 #3
0
        private static void updateSingleFile(String localFilename, Folder cloudParent)
        {
            //rule #1 - avoid uploading if we can.  matching md5s mean the file is already in cloud
            Task <String> getHash = Task.Factory.StartNew(() => { return(getMD5hash(localFilename)); });

            config.updateTokens(() => { configLock.Wait(); File.WriteAllText(ConfigurationManager.AppSettings["jsonConfig"], JsonConvert.SerializeObject(config)); configLock.Release(); });

            CloudDriveFolder actualParent = checkCache <CloudDriveFolder>(cloudParent.cloudId, Task.Factory.StartNew <CloudDriveFolder>(
                                                                              () => { return(CloudDriveOperations.getFolder(config, cloudParent.cloudId)); }
                                                                              ), folderCache);
            String localMd5 = getHash.Result;

            if (memFiles.Any(item => item.parentIds.Contains(cloudParent.cloudId) && item.name == Path.GetFileName(localFilename) && item.md5 == localMd5))
            {
                return;
            }
            CloudDriveListResponse <CloudDriveFile> fileSearch =
                CloudDriveOperations.getFilesByName(config, Path.GetFileName(localFilename));
            List <CloudDriveFile> fileSearchCleaned = new List <CloudDriveFile>();

            if (fileSearch.count > 0)
            {
                fileSearchCleaned = fileSearch.data.Where(x => x.name == Path.GetFileName(localFilename)).ToList <CloudDriveFile>();
            }
            switch (fileSearchCleaned.Count)
            {
            case (0):
                Console.WriteLine("Does not exist in cloud, need to upload {0}", localFilename);
                CloudDriveOperations.uploadFile(config, localFilename, cloudParent.cloudId, true);
                //create the file
                break;

            case (1):
                Console.WriteLine("Exists in cloud, need to compare {0}", localFilename);
                bool md5Match    = (fileSearchCleaned[0].contentProperties.md5 == localMd5);
                bool parentMatch = fileSearchCleaned[0].parents.Contains(cloudParent.cloudId);

                if (md5Match && !parentMatch)
                {
                    //if md5 matches & parent doesnt match, add parent
                    CloudDriveOperations.addNodeParent(config, fileSearchCleaned[0].id, cloudParent.cloudId);
                }
                else if (!md5Match && !parentMatch)
                {
                    //this other file is same name but unrelated, upload force
                    // if possible to *copy* the existing cloud node to an additional name, that would be preferable
                    CloudDriveOperations.uploadFile(config, localFilename, cloudParent.cloudId, true);
                }
                else if (!md5Match && parentMatch)
                {
                    //file has changed, need to update content
                    CloudDriveOperations.uploadFileContent(config, localFilename, fileSearchCleaned[0].id);
                }
                break;

            default:
                //multiple files have the same filename.  look for an Md5 match.
                var matchingMd5 = fileSearchCleaned.Where(x => x.contentProperties.md5 == localMd5).FirstOrDefault();
                if (matchingMd5 == null)
                {
                    CloudDriveOperations.uploadFile(config, localFilename, cloudParent.cloudId, true);
                }
                else if (!matchingMd5.parents.Contains(cloudParent.cloudId))
                {
                    CloudDriveOperations.addNodeParent(config, matchingMd5.id, cloudParent.cloudId);
                }
                break;
            }
        }