< Summary - Jellyfin

Information
Class: MediaBrowser.Controller.Providers.DirectoryService
Assembly: MediaBrowser.Controller
File(s): /srv/git/jellyfin/MediaBrowser.Controller/Providers/DirectoryService.cs
Line coverage
97%
Covered lines: 37
Uncovered lines: 1
Coverable lines: 38
Total lines: 115
Line coverage: 97.3%
Branch coverage
96%
Covered branches: 25
Total branches: 26
Branch coverage: 96.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetFileSystemEntries(...)100%11100%
GetDirectories(...)100%44100%
GetFiles(...)100%44100%
GetFile(...)100%44100%
GetDirectory(...)100%44100%
GetFileSystemEntry(...)100%66100%
GetFilePaths(...)100%11100%
GetFilePaths(...)75%4.07483.33%
IsAccessible(...)100%11100%

File(s)

/srv/git/jellyfin/MediaBrowser.Controller/Providers/DirectoryService.cs

#LineLine coverage
 1#pragma warning disable CS1591
 2
 3using System;
 4using System.Collections.Concurrent;
 5using System.Collections.Generic;
 6using System.Linq;
 7using MediaBrowser.Model.IO;
 8
 9namespace MediaBrowser.Controller.Providers
 10{
 11    public class DirectoryService : IDirectoryService
 12    {
 13        private readonly IFileSystem _fileSystem;
 14
 21015        private readonly ConcurrentDictionary<string, FileSystemMetadata[]> _cache = new(StringComparer.Ordinal);
 16
 21017        private readonly ConcurrentDictionary<string, FileSystemMetadata> _fileCache = new(StringComparer.Ordinal);
 18
 21019        private readonly ConcurrentDictionary<string, List<string>> _filePathCache = new(StringComparer.Ordinal);
 20
 21        public DirectoryService(IFileSystem fileSystem)
 22        {
 21023            _fileSystem = fileSystem;
 21024        }
 25
 26        public FileSystemMetadata[] GetFileSystemEntries(string path)
 27        {
 29428            return _cache.GetOrAdd(path, static (p, fileSystem) => fileSystem.GetFileSystemEntries(p).ToArray(), _fileSy
 29        }
 30
 31        public List<FileSystemMetadata> GetDirectories(string path)
 32        {
 233            var list = new List<FileSystemMetadata>();
 234            var items = GetFileSystemEntries(path);
 1635            for (var i = 0; i < items.Length; i++)
 36            {
 637                var item = items[i];
 638                if (item.IsDirectory)
 39                {
 340                    list.Add(item);
 41                }
 42            }
 43
 244            return list;
 45        }
 46
 47        public List<FileSystemMetadata> GetFiles(string path)
 48        {
 249            var list = new List<FileSystemMetadata>();
 250            var items = GetFileSystemEntries(path);
 1651            for (var i = 0; i < items.Length; i++)
 52            {
 653                var item = items[i];
 654                if (!item.IsDirectory)
 55                {
 356                    list.Add(item);
 57                }
 58            }
 59
 260            return list;
 61        }
 62
 63        public FileSystemMetadata? GetFile(string path)
 64        {
 4265            var entry = GetFileSystemEntry(path);
 4266            return entry is not null && !entry.IsDirectory ? entry : null;
 67        }
 68
 69        public FileSystemMetadata? GetDirectory(string path)
 70        {
 471            var entry = GetFileSystemEntry(path);
 472            return entry is not null && entry.IsDirectory ? entry : null;
 73        }
 74
 75        public FileSystemMetadata? GetFileSystemEntry(string path)
 76        {
 4677            if (!_fileCache.TryGetValue(path, out var result))
 78            {
 4379                var file = _fileSystem.GetFileSystemInfo(path);
 4380                if (file?.Exists ?? false)
 81                {
 3982                    result = file;
 3983                    _fileCache.TryAdd(path, result);
 84                }
 85            }
 86
 4687            return result;
 88        }
 89
 90        public IReadOnlyList<string> GetFilePaths(string path)
 391            => GetFilePaths(path, false);
 92
 93        public IReadOnlyList<string> GetFilePaths(string path, bool clearCache, bool sort = false)
 94        {
 495            if (clearCache)
 96            {
 197                _filePathCache.TryRemove(path, out _);
 98            }
 99
 4100            var filePaths = _filePathCache.GetOrAdd(path, static (p, fileSystem) => fileSystem.GetFilePaths(p).ToList(),
 101
 4102            if (sort)
 103            {
 0104                filePaths.Sort();
 105            }
 106
 4107            return filePaths;
 108        }
 109
 110        public bool IsAccessible(string path)
 111        {
 40112            return _fileSystem.GetFileSystemEntryPaths(path).Any();
 113        }
 114    }
 115}