Ejemplo n.º 1
0
        private async Task SynchronizeOnlineFilesFolders()
        {
            var folders = SyncTableManager.GetFolders();

            foreach (var folder in folders)
            {
                try
                {
                    var filesFolders = await DokuFlexService.GetFilesFoldersAsync(_ticket, folder.GroupId, folder.FolderId);

                    foreach (var fileFolder in filesFolders)
                    {
                        if (fileFolder.type == "C")
                        {
                            //Check if file exists in sync table
                            var file = SyncTableManager.GetByFileId(fileFolder.id);

                            if (file != null)
                            {
                                if (fileFolder.modifiedTime > file.ModifiedTime)
                                {
                                    SyncTableManager.ChangeSyncStatusToPending(file.Path);
                                    var command = new DownloadFileCommand(_ticket, folder.GroupId, folder.FolderId, fileFolder, file.Path, _path);
                                    command.ExecuteError += new ExecuteErrorEventHandler(OnExecuteError);
                                    _commandProcessor.AddCommand(command);
                                }
                            }
                            else
                            {
                                var path = string.Format("{0}\\{1}", folder.Path, fileFolder.name);

                                //Add item as pending
                                var item = new SyncTableItem
                                {
                                    Name          = fileFolder.name,
                                    Path          = path,
                                    LastWriteTime = 0,
                                    Type          = "C",
                                    GroupId       = folder.GroupId,
                                    FolderId      = folder.FolderId,
                                    FileId        = string.Empty,
                                    ModifiedTime  = 0,
                                    SyncFolder    = false,
                                    SyncStatus    = SyncTableItemStatus.Pending
                                };

                                SyncTableManager.Add(item);

                                var command = new DownloadFileCommand(_ticket, folder.GroupId, folder.FolderId, fileFolder, path, _path);
                                command.ExecuteError += new ExecuteErrorEventHandler(OnExecuteError);
                                _commandProcessor.AddCommand(command);
                            }
                        }
                        else
                        {
                            //Check if folder exists
                            if (!folders.Any(f => f.FolderId.Equals(fileFolder.id) && f.Type == "F"))
                            {
                                var path    = String.Format("{0}\\{1}", folder.Path, fileFolder.name);
                                var command = new CreateDirectoryCommand(folder.GroupId, fileFolder, path);
                                command.ExecuteError += new ExecuteErrorEventHandler(OnExecuteError);
                                _commandProcessor.AddCommand(command);
                            }
                        }
                    }

                    var files = SyncTableManager.GetFiles(folder.FolderId);

                    foreach (var file in files)
                    {
                        if (!filesFolders.Any(f => f.id.Equals(file.FileId)))
                        {
                            SyncTableManager.ChangeSyncStatusToPending(file.Path);
                            var command = new DeleteFileCommand(file.Path);
                            command.ExecuteError += new ExecuteErrorEventHandler(OnExecuteError);
                            _commandProcessor.AddCommand(command);
                        }
                    }
                }
                catch (Exception ex)
                {
                    //Check is folder doesn't exists
                    if (ex is RestResponseException)
                    {
                        var exception = ex as RestResponseException;

                        //Folder doesn't exists
                        if (exception.ErrorCode == 1)
                        {
                            var command = new DeleteDirectoryCommand(folder.Path);
                            command.ExecuteError += new ExecuteErrorEventHandler(OnExecuteError);
                            _commandProcessor.AddCommand(command);
                        }
                    }
                }
            }
        }