FileSystemRepository.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using MediaBrowser.Common.Extensions;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.IO;
  5. using System.Threading.Tasks;
  6. namespace MediaBrowser.Common.IO
  7. {
  8. /// <summary>
  9. /// This is a wrapper for storing large numbers of files within a directory on a file system.
  10. /// Simply pass a filename into GetResourcePath and it will return a full path location of where the file should be stored.
  11. /// </summary>
  12. public class FileSystemRepository : IDisposable
  13. {
  14. /// <summary>
  15. /// Contains the list of subfolders under the main directory
  16. /// The directory entry is created when the item is first added to the dictionary
  17. /// </summary>
  18. private readonly ConcurrentDictionary<string, string> _subFolderPaths = new ConcurrentDictionary<string, string>();
  19. /// <summary>
  20. /// The _file locks
  21. /// </summary>
  22. private readonly NamedLock _fileLocks = new NamedLock();
  23. /// <summary>
  24. /// Gets or sets the path.
  25. /// </summary>
  26. /// <value>The path.</value>
  27. protected string Path { get; set; }
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="FileSystemRepository" /> class.
  30. /// </summary>
  31. /// <param name="path">The path.</param>
  32. /// <exception cref="System.ArgumentNullException"></exception>
  33. public FileSystemRepository(string path)
  34. {
  35. if (string.IsNullOrEmpty(path))
  36. {
  37. throw new ArgumentNullException();
  38. }
  39. Path = path;
  40. Initialize();
  41. }
  42. /// <summary>
  43. /// Initializes this instance.
  44. /// </summary>
  45. protected void Initialize()
  46. {
  47. if (!Directory.Exists(Path))
  48. {
  49. Directory.CreateDirectory(Path);
  50. }
  51. }
  52. /// <summary>
  53. /// Gets the full path of where a resource should be stored within the repository
  54. /// </summary>
  55. /// <param name="uniqueName">Name of the unique.</param>
  56. /// <param name="fileExtension">The file extension.</param>
  57. /// <returns>System.String.</returns>
  58. /// <exception cref="System.ArgumentNullException"></exception>
  59. public string GetResourcePath(string uniqueName, string fileExtension)
  60. {
  61. if (string.IsNullOrEmpty(uniqueName))
  62. {
  63. throw new ArgumentNullException();
  64. }
  65. if (string.IsNullOrEmpty(fileExtension))
  66. {
  67. throw new ArgumentNullException();
  68. }
  69. var filename = uniqueName.GetMD5() + fileExtension;
  70. return GetResourcePath(filename);
  71. }
  72. /// <summary>
  73. /// Gets the full path of where a file should be stored within the repository
  74. /// </summary>
  75. /// <param name="filename">The filename.</param>
  76. /// <returns>System.String.</returns>
  77. /// <exception cref="System.ArgumentNullException"></exception>
  78. public string GetResourcePath(string filename)
  79. {
  80. if (string.IsNullOrEmpty(filename))
  81. {
  82. throw new ArgumentNullException();
  83. }
  84. return GetInternalResourcePath(filename);
  85. }
  86. /// <summary>
  87. /// Takes a filename and returns the full path of where it should be stored
  88. /// </summary>
  89. /// <param name="filename">The filename.</param>
  90. /// <returns>System.String.</returns>
  91. private string GetInternalResourcePath(string filename)
  92. {
  93. var prefix = filename.Substring(0, 1);
  94. var folder = _subFolderPaths.GetOrAdd(prefix, GetCachePath);
  95. return System.IO.Path.Combine(folder, filename);
  96. }
  97. /// <summary>
  98. /// Creates a subfolder under the image cache directory and returns the full path
  99. /// </summary>
  100. /// <param name="prefix">The prefix.</param>
  101. /// <returns>System.String.</returns>
  102. private string GetCachePath(string prefix)
  103. {
  104. var path = System.IO.Path.Combine(Path, prefix);
  105. if (!Directory.Exists(path))
  106. {
  107. Directory.CreateDirectory(path);
  108. }
  109. return path;
  110. }
  111. /// <summary>
  112. /// Determines if a resource is present in the repository
  113. /// </summary>
  114. /// <param name="uniqueName">Name of the unique.</param>
  115. /// <param name="fileExtension">The file extension.</param>
  116. /// <returns><c>true</c> if the specified unique name contains resource; otherwise, <c>false</c>.</returns>
  117. public bool ContainsResource(string uniqueName, string fileExtension)
  118. {
  119. return ContainsFilePath(GetResourcePath(uniqueName, fileExtension));
  120. }
  121. /// <summary>
  122. /// Determines if a file with a given name is present in the repository
  123. /// </summary>
  124. /// <param name="filename">The filename.</param>
  125. /// <returns><c>true</c> if the specified filename contains filename; otherwise, <c>false</c>.</returns>
  126. /// <exception cref="System.ArgumentNullException"></exception>
  127. public bool ContainsFilename(string filename)
  128. {
  129. if (string.IsNullOrEmpty(filename))
  130. {
  131. throw new ArgumentNullException();
  132. }
  133. return ContainsFilePath(GetInternalResourcePath(filename));
  134. }
  135. /// <summary>
  136. /// Determines if a file is present in the repository
  137. /// </summary>
  138. /// <param name="path">The path.</param>
  139. /// <returns><c>true</c> if [contains file path] [the specified path]; otherwise, <c>false</c>.</returns>
  140. /// <exception cref="System.ArgumentNullException"></exception>
  141. public bool ContainsFilePath(string path)
  142. {
  143. if (string.IsNullOrEmpty(path))
  144. {
  145. throw new ArgumentNullException();
  146. }
  147. return File.Exists(path);
  148. }
  149. /// <summary>
  150. /// Waits for lock.
  151. /// </summary>
  152. /// <param name="resourcePath">The resource path.</param>
  153. public Task WaitForLockAsync(string resourcePath)
  154. {
  155. return _fileLocks.WaitAsync(resourcePath);
  156. }
  157. /// <summary>
  158. /// Releases the lock.
  159. /// </summary>
  160. /// <param name="resourcePath">The resource path.</param>
  161. public void ReleaseLock(string resourcePath)
  162. {
  163. _fileLocks.Release(resourcePath);
  164. }
  165. /// <summary>
  166. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  167. /// </summary>
  168. public void Dispose()
  169. {
  170. Dispose(true);
  171. }
  172. /// <summary>
  173. /// Releases unmanaged and - optionally - managed resources.
  174. /// </summary>
  175. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  176. protected virtual void Dispose(bool dispose)
  177. {
  178. if (dispose)
  179. {
  180. _fileLocks.Dispose();
  181. }
  182. }
  183. }
  184. }