/// <summary> /// 文件夹同步 /// </summary> /// <param name="folder">需要同步的文件夹</param> /// <param name="targetFolderId">OneDrive上的目录id</param> /// <param name="isRecursive">是否递归同步子文件夹,会较慢</param> /// <returns>同步结果</returns> /// <param name="queue">同步队列</param> public async void SyncFolderAsync(IStorageFolder folder, string targetFolderId = OneDriveInfoHelper.RootFolderName, bool isRecursive = false, OneDriveFileSyncQueue queue = null) { if (!OneDriveSession.IsLogged) { return; } var client = OneDriveSession.GetLoggedClient(); var onlineResult = await client.GetItemsInFolderAsync(targetFolderId); if (!onlineResult.IsSuccessful) { return; } if (queue == null) { queue = OneDriveFileSyncPool.CreateQueue(); } var onlineItems = onlineResult.Items; var items = await folder.GetItemsAsync(); foreach (var item in items) { var realItem = (IStorageItem)item; if (realItem.IsOfType(StorageItemTypes.Folder)) { var desiredFolder = (IStorageFolder)realItem; if (isRecursive) { var existedFolder = onlineItems.FirstOrDefault(oitem => oitem.Name.ToUpper() == desiredFolder.Name.ToUpper() && oitem.ItemType.HasFlag(OneDriveItemType.Folder)); if (existedFolder != null) { SyncFolderAsync(desiredFolder, existedFolder.Id, true); } else { var createdFolderResult = await client.CreateFolderInFolderAsync(desiredFolder.Name, targetFolderId); if (createdFolderResult.IsSuccessful) { SyncFolderAsync(desiredFolder, createdFolderResult.Item.Id, true, queue); } } } } else if (realItem.IsOfType(StorageItemTypes.File)) { var desiredItem = (IStorageFile)realItem; var existedItem = onlineItems.FirstOrDefault(oitem => oitem.Name.ToUpper() == desiredItem.Name.ToUpper() && !oitem.ItemType.HasFlag(OneDriveItemType.Folder)); if (existedItem == null) { SyncFile(desiredItem, targetFolderId, desiredItem.Name, queue); } else { OneDriveFileSyncPool.NotifyFileSynced(desiredItem); } } } }
/// <summary> /// 文件同步 /// </summary> /// <param name="file">文件</param> /// <param name="targetFolderId">OneDrive文件夹Id</param> /// <param name="targetFileName">OneDrive目标文件名</param> /// <param name="queue">同步队列</param> public void SyncFile(IStorageFile file, string targetFolderId, string targetFileName, OneDriveFileSyncQueue queue) { if (queue == null) { queue = OneDriveFileSyncPool.CreateQueue(); } queue.Enqueue(new OneDriveFileSyncTask { File = file, FileName = targetFileName, FolderId = targetFolderId }); OneDriveFileSyncPool.NotifyTryStartOneTask(); }