< 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: 116
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

0255075100

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%4483.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        // TODO make static and switch to FastConcurrentLru.
 20414        private readonly ConcurrentDictionary<string, FileSystemMetadata[]> _cache = new(StringComparer.Ordinal);
 15
 20416        private readonly ConcurrentDictionary<string, FileSystemMetadata> _fileCache = new(StringComparer.Ordinal);
 17
 20418        private readonly ConcurrentDictionary<string, List<string>> _filePathCache = new(StringComparer.Ordinal);
 19
 20        private readonly IFileSystem _fileSystem;
 21
 22        public DirectoryService(IFileSystem fileSystem)
 23        {
 20424            _fileSystem = fileSystem;
 20425        }
 26
 27        public FileSystemMetadata[] GetFileSystemEntries(string path)
 28        {
 30229            return _cache.GetOrAdd(path, static (p, fileSystem) => fileSystem.GetFileSystemEntries(p).ToArray(), _fileSy
 30        }
 31
 32        public List<FileSystemMetadata> GetDirectories(string path)
 33        {
 234            var list = new List<FileSystemMetadata>();
 235            var items = GetFileSystemEntries(path);
 1636            for (var i = 0; i < items.Length; i++)
 37            {
 638                var item = items[i];
 639                if (item.IsDirectory)
 40                {
 341                    list.Add(item);
 42                }
 43            }
 44
 245            return list;
 46        }
 47
 48        public List<FileSystemMetadata> GetFiles(string path)
 49        {
 250            var list = new List<FileSystemMetadata>();
 251            var items = GetFileSystemEntries(path);
 1652            for (var i = 0; i < items.Length; i++)
 53            {
 654                var item = items[i];
 655                if (!item.IsDirectory)
 56                {
 357                    list.Add(item);
 58                }
 59            }
 60
 261            return list;
 62        }
 63
 64        public FileSystemMetadata? GetFile(string path)
 65        {
 6666            var entry = GetFileSystemEntry(path);
 6667            return entry is not null && !entry.IsDirectory ? entry : null;
 68        }
 69
 70        public FileSystemMetadata? GetDirectory(string path)
 71        {
 472            var entry = GetFileSystemEntry(path);
 473            return entry is not null && entry.IsDirectory ? entry : null;
 74        }
 75
 76        public FileSystemMetadata? GetFileSystemEntry(string path)
 77        {
 7078            if (!_fileCache.TryGetValue(path, out var result))
 79            {
 3780                var file = _fileSystem.GetFileSystemInfo(path);
 3781                if (file?.Exists ?? false)
 82                {
 3383                    result = file;
 3384                    _fileCache.TryAdd(path, result);
 85                }
 86            }
 87
 7088            return result;
 89        }
 90
 91        public IReadOnlyList<string> GetFilePaths(string path)
 392            => GetFilePaths(path, false);
 93
 94        public IReadOnlyList<string> GetFilePaths(string path, bool clearCache, bool sort = false)
 95        {
 496            if (clearCache)
 97            {
 198                _filePathCache.TryRemove(path, out _);
 99            }
 100
 4101            var filePaths = _filePathCache.GetOrAdd(path, static (p, fileSystem) => fileSystem.GetFilePaths(p).ToList(),
 102
 4103            if (sort)
 104            {
 0105                filePaths.Sort();
 106            }
 107
 4108            return filePaths;
 109        }
 110
 111        public bool IsAccessible(string path)
 112        {
 27113            return _fileSystem.GetFileSystemEntryPaths(path).Any();
 114        }
 115    }
 116}