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
|
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.IO;
namespace MediaBrowser.Controller.Providers
{
public class DirectoryService : IDirectoryService
{
private readonly ILogger _logger;
private readonly IFileSystem _fileSystem;
private readonly ConcurrentDictionary<string, FileSystemMetadata[]> _cache =
new ConcurrentDictionary<string, FileSystemMetadata[]>(StringComparer.OrdinalIgnoreCase);
private readonly ConcurrentDictionary<string, FileSystemMetadata> _fileCache =
new ConcurrentDictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase);
public DirectoryService(ILogger logger, IFileSystem fileSystem)
{
_logger = logger;
_fileSystem = fileSystem;
}
public DirectoryService(IFileSystem fileSystem)
: this(new NullLogger(), fileSystem)
{
}
public FileSystemMetadata[] GetFileSystemEntries(string path)
{
return GetFileSystemEntries(path, false);
}
private FileSystemMetadata[] GetFileSystemEntries(string path, bool clearCache)
{
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentNullException("path");
}
FileSystemMetadata[] entries;
if (clearCache)
{
FileSystemMetadata[] removed;
_cache.TryRemove(path, out removed);
}
if (!_cache.TryGetValue(path, out entries))
{
//_logger.Debug("Getting files for " + path);
try
{
// using EnumerateFileSystemInfos doesn't handle reparse points (symlinks)
entries = _fileSystem.GetFileSystemEntries(path).ToArray();
}
catch (IOException)
{
entries = new FileSystemMetadata[] { };
}
_cache.TryAdd(path, entries);
}
return entries;
}
public List<FileSystemMetadata> GetFiles(string path)
{
return GetFiles(path, false);
}
public List<FileSystemMetadata> GetFiles(string path, bool clearCache)
{
var list = new List<FileSystemMetadata>();
var items = GetFileSystemEntries(path, clearCache);
foreach (var item in items)
{
if (!item.IsDirectory)
{
list.Add(item);
}
}
return list;
}
public FileSystemMetadata GetFile(string path)
{
FileSystemMetadata file;
if (!_fileCache.TryGetValue(path, out file))
{
file = _fileSystem.GetFileInfo(path);
if (file != null && file.Exists)
{
_fileCache.TryAdd(path, file);
}
else
{
return null;
}
}
return file;
//return _fileSystem.GetFileInfo(path);
}
}
}
|