Exemplo n.º 1
0
 internal static void NotifyTryStartOneTask()
 {
     if (OneDriveSession.IsLogged)
     {
         var client          = OneDriveSession.GetLoggedClient();
         var queueEnumerator = _pool.GetEnumerator();
         if (_pool.Any())
         {
             while (queueEnumerator.MoveNext())
             {
                 if (queueEnumerator.Current.Any())
                 {
                     var task = queueEnumerator.Current.Dequeue();
                     task.RunTaskAsync(client, succeeded =>
                     {
                         if (succeeded)
                         {
                             NotifyFileSynced(task.File);
                         }
                         else
                         {
                             //失败,则重新加入队列
                             queueEnumerator.Current.Enqueue(task);
                         }
                         NotifyTryStartOneTask();
                     });
                     return;
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
        /// <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);
                    }
                }
            }
        }