private async Task <WsItemsReaderEngine> GetItemsOrItem(WsItemPath path, bool onlyOne, bool useCreatedFileResolver) { if (path == null) { throw new ArgumentNullException(nameof(path)); } CheckConnected(); string fileSearch = path is WsFilePath ? path.Name : string.Empty; WsItemsReaderEngine readerEngine = await PostFormDataWithLoginRetry(async() => { FormUrlEncodedContent formContent = CreateFormContent(new[] { new KeyValuePair <string, string>("path", path.Folder.FullPath), new KeyValuePair <string, string>("private", path.Folder.IsPrivate ? "1" : "0"), //new KeyValuePair<string, string>("sort_by", "name"), //new KeyValuePair<string, string>("sort_order", "asc"), //new KeyValuePair<string, string>("sort_order", "asc"), new KeyValuePair <string, string>("limit", onlyOne ? "1" : "99999999"), //new KeyValuePair<string, string>("offset", "0") new KeyValuePair <string, string>("search", fileSearch), }); HttpResponseMessage response = await _httpClient.PostFormData(API_FILES_URI, formContent); return(await WsItemsReaderEngine.Create(this, response, path.Folder, useCreatedFileResolver)); }); CheckResultStatus(readerEngine); return(readerEngine); }
public async Task <WsFile> FindFile(WsFilePath filePath) { using (WsItemsReaderEngine filesResult = await GetItem(filePath, true)) { return(filesResult.GetItems().OfType <WsFile>().Where(f => f.PathInfo.Equals(filePath)).SingleOrDefault()); }; }
public async Task <WsFolder> FindFolder(WsFolderPath folderPath) { if (folderPath.FullPath == "/") { return(folderPath.IsPrivate ? PrivateRootFolder : PublicRootFolder); } using (WsItemsReaderEngine itemsResult = await GetItems(folderPath.Parent, false, false)) { foreach (WsItem item in itemsResult.GetItems()) { if (item is WsFolder folder) { if (folder.PathInfo.Equals(folderPath)) { return(folder); } } else { break; } } }; return(null); }
public static async Task <WsItemsReaderEngine> Create(WsApiClient apiClient, HttpResponseMessage responseMessage, WsFolderPath folderPath, bool useCreatedFileResolver) { WsItemsReaderEngine engine = new WsItemsReaderEngine(apiClient, responseMessage, folderPath, useCreatedFileResolver); XmlDocument xml = new XmlDocument(); xml.Load(await engine._responseMessage.Content.ReadAsStreamAsync()); engine._xmlReader = new XmlNodeReader(xml.DocumentElement); if (engine._xmlReader.Read() && engine._xmlReader.Name == ROOT_ELEMENT_NAME && engine._xmlReader.Read() && engine._xmlReader.Name == "status") { engine.Status = engine._xmlReader.ReadElementContentAsString(); if (engine.Status != ResultStatus.OK) { if (engine._xmlReader.Name == "code") { engine.ErrorCode = engine._xmlReader.ReadElementContentAsString(); } engine.Dispose(); } } else { engine.Status = "Xml format error."; engine.Dispose(); } return(engine); }
private IEnumerable <WsFile> GetAllFilesRecursive(WsItemsReaderEngine engine, int depth, int currentDepth) { List <WsFolderPath> folderPaths = new List <WsFolderPath>(); foreach (WsItem item in engine.GetItems()) { if (item is WsFile file) { yield return(file); } else if (item is WsFolder folder) { folderPaths.Add(folder.PathInfo); } } currentDepth++; if (currentDepth <= depth) { foreach (WsFolderPath folderPath in folderPaths) { using (WsItemsReaderEngine childEngine = _apiClient.GetItems(folderPath, false, true).Result) { _childEngine = childEngine; foreach (WsFile file in GetAllFilesRecursive(childEngine, depth, currentDepth)) { yield return(file); } _childEngine = null; } } } }
public void Dispose() { _disposed = true; _getItemsInvoked = true; _xmlReader?.Dispose(); _xmlReader = null; _responseMessage?.Dispose(); _responseMessage = null; _childEngine?.Dispose(); _childEngine = null; }
private async Task ContinuosResolve(CancellationToken cancellationToken) { int delay = 1000; bool firstCall = true; while (cancellationToken.IsCancellationRequested == false && (_dic.Count > 0 || firstCall)) { firstCall = false; try { await Task.Delay(delay, cancellationToken); WsFile[] maxFiveWaitingFilesOrNull = _dic.Count > 5 ? new WsFile[] { null } : _dic.Values.ToArray(); string[] allWaitingFilesIdents = _dic.Values.Select(f => f.Ident).ToArray(); foreach (WsFile waitingFileOrNull in maxFiveWaitingFilesOrNull) { if (cancellationToken.IsCancellationRequested) { return; } using (WsItemsReaderEngine reader = await _apiClient.GetFolderItemsWithoutCreatedFilesInProgress(_folderPath, waitingFileOrNull?.PathInfo.Name ?? "")) { if (cancellationToken.IsCancellationRequested) { return; } foreach (WsFile foundedFile in reader.GetItems().OfType <WsFile>().Where(f => f.Ident == waitingFileOrNull?.Ident || allWaitingFilesIdents.Contains(f.Ident))) { if (cancellationToken.IsCancellationRequested) { return; } if (RemoveFileAndFolderIfEmpty(foundedFile)) { CleanContinuosResolve(); return; } } } } } catch (Exception) { } if (delay < (1000 * 2 * 2)) { delay *= 2; } } }
public async Task <WsFilesReader> GetFolderAllFilesRecursive(WsFolderPath folderPath, int depth) { WsItemsReaderEngine filesResult = await GetItems(folderPath, false, true); return(new WsFilesReader(filesResult, depth)); }
public async Task <WsItemsReader> GetFolderItems(WsFolderPath folderPath) { WsItemsReaderEngine filesResult = await GetItems(folderPath, false, true); return(new WsItemsReader(filesResult)); }
internal WsFilesReader(WsItemsReaderEngine readerEngine, int depth) : base(readerEngine) { _readerEngine = readerEngine; _depth = depth; }
internal WsItemsReader(WsItemsReaderEngine readerEngine) { _readerEngine = readerEngine; }