aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.MediaEncoding/BdInfo/BdInfoDirectoryInfo.cs
blob: dc20a6d6319d3d9a519de7e6b781b8e2f75e4c1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.IO;
using System.Linq;
using BDInfo.IO;
using MediaBrowser.Model.IO;

namespace MediaBrowser.MediaEncoding.BdInfo;

/// <summary>
/// Class BdInfoDirectoryInfo.
/// </summary>
public class BdInfoDirectoryInfo : IDirectoryInfo
{
    private readonly IFileSystem _fileSystem;

    private readonly FileSystemMetadata _impl;

    /// <summary>
    /// Initializes a new instance of the <see cref="BdInfoDirectoryInfo" /> class.
    /// </summary>
    /// <param name="fileSystem">The filesystem.</param>
    /// <param name="path">The path.</param>
    public BdInfoDirectoryInfo(IFileSystem fileSystem, string path)
    {
        _fileSystem = fileSystem;
        _impl = _fileSystem.GetDirectoryInfo(path);
    }

    private BdInfoDirectoryInfo(IFileSystem fileSystem, FileSystemMetadata impl)
    {
        _fileSystem = fileSystem;
        _impl = impl;
    }

    /// <summary>
    /// Gets the name.
    /// </summary>
    public string Name => _impl.Name;

    /// <summary>
    /// Gets the full name.
    /// </summary>
    public string FullName => _impl.FullName;

    /// <summary>
    /// Gets the parent directory information.
    /// </summary>
    public IDirectoryInfo? Parent
    {
        get
        {
            var parentFolder = Path.GetDirectoryName(_impl.FullName);
            if (parentFolder is not null)
            {
                return new BdInfoDirectoryInfo(_fileSystem, parentFolder);
            }

            return null;
        }
    }

    private static bool IsHidden(ReadOnlySpan<char> name) => name.StartsWith('.');

    /// <summary>
    /// Gets the directories.
    /// </summary>
    /// <returns>An array with all directories.</returns>
    public IDirectoryInfo[] GetDirectories()
    {
        return _fileSystem.GetDirectories(_impl.FullName)
            .Where(d => !IsHidden(d.Name))
            .Select(x => new BdInfoDirectoryInfo(_fileSystem, x))
            .ToArray();
    }

    /// <summary>
    /// Gets the files.
    /// </summary>
    /// <returns>All files of the directory.</returns>
    public IFileInfo[] GetFiles()
    {
        return _fileSystem.GetFiles(_impl.FullName)
            .Where(d => !IsHidden(d.Name))
            .Select(x => new BdInfoFileInfo(x))
            .ToArray();
    }

    /// <summary>
    /// Gets the files matching a pattern.
    /// </summary>
    /// <param name="searchPattern">The search pattern.</param>
    /// <returns>All files of the directory matching the search pattern.</returns>
    public IFileInfo[] GetFiles(string searchPattern)
    {
        return _fileSystem.GetFiles(_impl.FullName, new[] { searchPattern }, false, false)
            .Where(d => !IsHidden(d.Name))
            .Select(x => new BdInfoFileInfo(x))
            .ToArray();
    }

    /// <summary>
    /// Gets the files matching a pattern and search options.
    /// </summary>
    /// <param name="searchPattern">The search pattern.</param>
    /// <param name="searchOption">The search option.</param>
    /// <returns>All files of the directory matching the search pattern and options.</returns>
    public IFileInfo[] GetFiles(string searchPattern, SearchOption searchOption)
    {
        return _fileSystem.GetFiles(
                _impl.FullName,
                new[] { searchPattern },
                false,
                searchOption == SearchOption.AllDirectories)
            .Where(d => !IsHidden(d.Name))
            .Select(x => new BdInfoFileInfo(x))
            .ToArray();
    }

    /// <summary>
    /// Gets the bdinfo of a file system path.
    /// </summary>
    /// <param name="fs">The file system.</param>
    /// <param name="path">The path.</param>
    /// <returns>The BD directory information of the path on the file system.</returns>
    public static IDirectoryInfo FromFileSystemPath(IFileSystem fs, string path)
    {
        return new BdInfoDirectoryInfo(fs, path);
    }
}