private static void Validate(IFileProviderFactory fileProviderFactory, GetSortedFoldersDefinition parameters) { if (parameters.Limit < 0) { throw new ValidationPublicException("Negative value can't be used for a limit"); } if (!fileProviderFactory.Exists(parameters.Path)) { throw new ObjectNotFoundPublicException("Path not found"); } }
public Task <IEnumerable <FolderInfo> > Handle(GetSortedFoldersDefinition parameters, CancellationToken cancellationToken = default(CancellationToken)) { Validate(_fileProviderFactory, parameters); if (parameters.Limit == 0) { return(Task.FromResult(Enumerable.Empty <FolderInfo>())); } if (_memoryCache.TryGetValue(parameters.Path, out var cacheResult)) { return(Task.FromResult((IEnumerable <FolderInfo>)cacheResult)); } var result = GetFolderInfosInternal(parameters, _fileProviderFactory, cancellationToken); _memoryCache.Set(parameters.Path, result, TimeSpan.FromMinutes(5)); return(Task.FromResult(result.AsEnumerable())); }
private static FolderInfo[] GetFolderInfosInternal(GetSortedFoldersDefinition parameters, IFileProviderFactory fileProviderFactory, CancellationToken cancellationToken) { var fileProvider = fileProviderFactory.GetFileProvider(parameters.Path); var rootContents = fileProvider.GetDirectoryContents(string.Empty); var topDirs = rootContents.Where(fi => fi.IsDirectory); var resultList = new List <FolderInfo>(); foreach (var topDir in topDirs) { var size = GetFolderSize(fileProvider, topDir, cancellationToken); var folderInfo = new FolderInfo { Name = topDir.Name, Size = size }; resultList.Add(folderInfo); } var orderedFolders = resultList.OrderByDescending(f => f.Size); var result = parameters.Limit.HasValue ? orderedFolders.Take(parameters.Limit.Value) : orderedFolders; return(result.ToArray()); }