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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
using MediaBrowser.Common.Extensions;
using System;
using System.Collections.Concurrent;
using System.IO;
namespace MediaBrowser.Common.IO
{
/// <summary>
/// This is a wrapper for storing large numbers of files within a directory on a file system.
/// Simply pass a filename into GetResourcePath and it will return a full path location of where the file should be stored.
/// </summary>
public class FileSystemRepository
{
/// <summary>
/// Contains the list of subfolders under the main directory
/// The directory entry is created when the item is first added to the dictionary
/// </summary>
private readonly ConcurrentDictionary<string, string> _subFolderPaths = new ConcurrentDictionary<string, string>();
/// <summary>
/// Gets or sets the path.
/// </summary>
/// <value>The path.</value>
protected string Path { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="FileSystemRepository" /> class.
/// </summary>
/// <param name="path">The path.</param>
/// <exception cref="System.ArgumentNullException"></exception>
public FileSystemRepository(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException();
}
Path = path;
Initialize();
}
/// <summary>
/// Initializes this instance.
/// </summary>
protected void Initialize()
{
if (!Directory.Exists(Path))
{
Directory.CreateDirectory(Path);
}
}
/// <summary>
/// Gets the full path of where a resource should be stored within the repository
/// </summary>
/// <param name="uniqueName">Name of the unique.</param>
/// <param name="fileExtension">The file extension.</param>
/// <returns>System.String.</returns>
/// <exception cref="System.ArgumentNullException"></exception>
public string GetResourcePath(string uniqueName, string fileExtension)
{
if (string.IsNullOrEmpty(uniqueName))
{
throw new ArgumentNullException();
}
if (string.IsNullOrEmpty(fileExtension))
{
throw new ArgumentNullException();
}
var filename = uniqueName.GetMD5() + fileExtension;
return GetResourcePath(filename);
}
/// <summary>
/// Gets the full path of where a file should be stored within the repository
/// </summary>
/// <param name="filename">The filename.</param>
/// <returns>System.String.</returns>
/// <exception cref="System.ArgumentNullException"></exception>
public string GetResourcePath(string filename)
{
if (string.IsNullOrEmpty(filename))
{
throw new ArgumentNullException();
}
return GetInternalResourcePath(filename);
}
/// <summary>
/// Takes a filename and returns the full path of where it should be stored
/// </summary>
/// <param name="filename">The filename.</param>
/// <returns>System.String.</returns>
private string GetInternalResourcePath(string filename)
{
var prefix = filename.Substring(0, 1);
var folder = _subFolderPaths.GetOrAdd(prefix, GetCachePath);
return System.IO.Path.Combine(folder, filename);
}
/// <summary>
/// Creates a subfolder under the image cache directory and returns the full path
/// </summary>
/// <param name="prefix">The prefix.</param>
/// <returns>System.String.</returns>
private string GetCachePath(string prefix)
{
var path = System.IO.Path.Combine(Path, prefix);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
/// <summary>
/// Determines if a resource is present in the repository
/// </summary>
/// <param name="uniqueName">Name of the unique.</param>
/// <param name="fileExtension">The file extension.</param>
/// <returns><c>true</c> if the specified unique name contains resource; otherwise, <c>false</c>.</returns>
public bool ContainsResource(string uniqueName, string fileExtension)
{
return ContainsFilePath(GetResourcePath(uniqueName, fileExtension));
}
/// <summary>
/// Determines if a file with a given name is present in the repository
/// </summary>
/// <param name="filename">The filename.</param>
/// <returns><c>true</c> if the specified filename contains filename; otherwise, <c>false</c>.</returns>
/// <exception cref="System.ArgumentNullException"></exception>
public bool ContainsFilename(string filename)
{
if (string.IsNullOrEmpty(filename))
{
throw new ArgumentNullException();
}
return ContainsFilePath(GetInternalResourcePath(filename));
}
/// <summary>
/// Determines if a file is present in the repository
/// </summary>
/// <param name="path">The path.</param>
/// <returns><c>true</c> if [contains file path] [the specified path]; otherwise, <c>false</c>.</returns>
/// <exception cref="System.ArgumentNullException"></exception>
public bool ContainsFilePath(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException();
}
return File.Exists(path);
}
}
}
|