FileSystemRepository.cs 6.4 KB

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