PageRenderTime 572ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 1ms

/BlogEngine/DotNetSlave.BusinessLogic/Providers/FileSystemProviders/XmlFileSystemProvider.cs

#
C# | 315 lines | 180 code | 29 blank | 106 comment | 9 complexity | f6a11251a6769bd61df7db8eb1832362 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Web.Hosting;
  6. namespace BlogEngine.Core.Providers
  7. {
  8. /// <summary>
  9. /// XmlBlogProvider Parial class for manageing all FileSystem methods
  10. /// </summary>
  11. public partial class XmlFileSystemProvider : BlogFileSystemProvider
  12. {
  13. #region Properties
  14. /// <summary>
  15. /// gets the absolute file path from a virtual path.
  16. /// </summary>
  17. /// <param name="VirtualPath">the virtual path</param>
  18. /// <returns>the absolute path</returns>
  19. private static string BlogAbsolutePath(string VirtualPath)
  20. {
  21. //VirtualPath = VirtualPath.Trim();
  22. //var fileContainer = string.Concat(Blog.CurrentInstance.StorageLocation.Trim(), "files/").Trim();
  23. //VirtualPath = VirtualPath.ToLower().StartsWith(fileContainer.ToLower()) ? VirtualPath : string.Format("{0}{1}", fileContainer, VirtualPath);
  24. return HostingEnvironment.MapPath(string.IsNullOrWhiteSpace(VirtualPath) ? "~" : VirtualPath);
  25. }
  26. private static string RelativeFilePath(string VirtualPath)
  27. {
  28. VirtualPath = VirtualPath.Replace("//","/").Trim();
  29. var fileContainer = string.Concat(Blog.CurrentInstance.StorageLocation.Trim(), "files").Trim();
  30. if (VirtualPath.ToLower().Contains(fileContainer.ToLower()))
  31. return VirtualPath;
  32. return string.Concat(fileContainer, VirtualPath);
  33. }
  34. #endregion
  35. #region Methods
  36. /// <summary>
  37. /// Clears a file system. This will delete all files and folders recursivly.
  38. /// </summary>
  39. /// <remarks>
  40. /// Handle with care... Possibly an internal method?
  41. /// </remarks>
  42. public override void ClearFileSystem()
  43. {
  44. var root = GetDirectory("");
  45. foreach (var directory in root.Directories)
  46. directory.Delete();
  47. foreach (var file in root.Files)
  48. file.Delete();
  49. }
  50. /// <summary>
  51. /// Creates a directory at a specific path
  52. /// </summary>
  53. /// <param name="VirtualPath">The virtual path to be created</param>
  54. /// <returns>the new Directory object created</returns>
  55. /// <remarks>
  56. /// Virtual path is the path starting from the /files/ containers
  57. /// The entity is created against the current blog id
  58. /// </remarks>
  59. internal override FileSystem.Directory CreateDirectory(string VirtualPath)
  60. {
  61. VirtualPath = RelativeFilePath(VirtualPath);
  62. var aPath = BlogAbsolutePath(VirtualPath);
  63. if (!this.DirectoryExists(VirtualPath))
  64. Directory.CreateDirectory(aPath);
  65. return GetDirectory(VirtualPath);
  66. }
  67. /// <summary>
  68. /// Deletes a spefic directory from a virtual path
  69. /// </summary>
  70. /// <param name="VirtualPath">The path to delete</param>
  71. /// <remarks>
  72. /// Virtual path is the path starting from the /files/ containers
  73. /// The entity is queried against to current blog id
  74. /// </remarks>
  75. public override void DeleteDirectory(string VirtualPath)
  76. {
  77. VirtualPath = RelativeFilePath(VirtualPath);
  78. if (!this.DirectoryExists(VirtualPath))
  79. return;
  80. var aPath = BlogAbsolutePath(VirtualPath);
  81. var sysDir = new DirectoryInfo(aPath);
  82. sysDir.Delete(true);
  83. }
  84. /// <summary>
  85. /// Returns wether or not the specific directory by virtual path exists
  86. /// </summary>
  87. /// <param name="VirtualPath">The virtual path to query</param>
  88. /// <returns>boolean</returns>
  89. public override bool DirectoryExists(string VirtualPath)
  90. {
  91. VirtualPath = VirtualPath.Trim();
  92. var aPath = BlogAbsolutePath(VirtualPath);
  93. return Directory.Exists(aPath);
  94. }
  95. /// <summary>
  96. /// gets a directory by the virtual path
  97. /// </summary>
  98. /// <param name="VirtualPath">the virtual path</param>
  99. /// <returns>the directory object or null for no directory found</returns>
  100. public override FileSystem.Directory GetDirectory(string VirtualPath)
  101. {
  102. return GetDirectory(VirtualPath, true);
  103. }
  104. public override FileSystem.Directory GetDirectory(string VirtualPath, bool CreateNew)
  105. {
  106. VirtualPath = RelativeFilePath(VirtualPath);
  107. var aPath = BlogAbsolutePath(VirtualPath);
  108. var sysDir = new DirectoryInfo(aPath);
  109. if (!sysDir.Exists)
  110. this.CreateDirectory(VirtualPath);
  111. var dir = new FileSystem.Directory();
  112. dir.FullPath = VirtualPath;
  113. dir.Name = sysDir.Name;
  114. dir.IsRoot = VirtualPath == string.Concat(Blog.CurrentInstance.StorageLocation, "files");
  115. dir.LastAccessTime = sysDir.LastAccessTime;
  116. dir.DateModified = sysDir.LastWriteTime;
  117. dir.DateCreated = sysDir.CreationTime;
  118. dir.Id = Guid.NewGuid();
  119. return dir;
  120. }
  121. /// <summary>
  122. /// gets a directory by a basedirectory and a string array of sub path tree
  123. /// </summary>
  124. /// <param name="BaseDirectory">the base directory object</param>
  125. /// <param name="SubPath">the params of sub path</param>
  126. /// <returns>the directory found, or null for no directory found</returns>
  127. public override FileSystem.Directory GetDirectory(FileSystem.Directory BaseDirectory, params string[] SubPath)
  128. {
  129. return GetDirectory(string.Join("/", BaseDirectory.FullPath, SubPath), true);
  130. }
  131. /// <summary>
  132. /// gets a directory by a basedirectory and a string array of sub path tree
  133. /// </summary>
  134. /// <param name="BaseDirectory">the base directory object</param>
  135. /// <param name="CreateNew">if set will create the directory structure</param>
  136. /// <param name="SubPath">the params of sub path</param>
  137. /// <returns>the directory found, or null for no directory found</returns>
  138. public override FileSystem.Directory GetDirectory(FileSystem.Directory BaseDirectory, bool CreateNew, params string[] SubPath)
  139. {
  140. return GetDirectory(string.Join("/", BaseDirectory.FullPath, SubPath), CreateNew);
  141. }
  142. /// <summary>
  143. /// gets all the directories underneath a base directory. Only searches one level.
  144. /// </summary>
  145. /// <param name="BaseDirectory">the base directory</param>
  146. /// <returns>collection of Directory objects</returns>
  147. public override IEnumerable<FileSystem.Directory> GetDirectories(FileSystem.Directory BaseDirectory)
  148. {
  149. var aPath = BlogAbsolutePath(BaseDirectory.FullPath);
  150. var sysDirectory = new DirectoryInfo(aPath);
  151. return sysDirectory.GetDirectories().Select(x => GetDirectory(string.Format("{0}/{1}", BaseDirectory.FullPath, x.Name)));
  152. }
  153. /// <summary>
  154. /// gets all the files in a directory, only searches one level
  155. /// </summary>
  156. /// <param name="BaseDirectory">the base directory</param>
  157. /// <returns>collection of File objects</returns>
  158. public override IEnumerable<FileSystem.File> GetFiles(FileSystem.Directory BaseDirectory)
  159. {
  160. var aPath = BlogAbsolutePath(BaseDirectory.FullPath);
  161. var sysDirectory = new DirectoryInfo(aPath);
  162. return sysDirectory.GetFiles().Select(x => GetFile(string.Format("{0}/{1}", BaseDirectory.FullPath, x.Name)));
  163. }
  164. /// <summary>
  165. /// gets a specific file by virtual path
  166. /// </summary>
  167. /// <param name="VirtualPath">the virtual path of the file</param>
  168. /// <returns></returns>
  169. public override FileSystem.File GetFile(string VirtualPath)
  170. {
  171. VirtualPath = RelativeFilePath(VirtualPath);
  172. var aPath = BlogAbsolutePath(VirtualPath);
  173. var sysFile = new FileInfo(aPath);
  174. if (!sysFile.Exists)
  175. throw new FileNotFoundException("The file at " + VirtualPath + " was not found.");
  176. var file = new FileSystem.File
  177. {
  178. FullPath = VirtualPath,
  179. Name = sysFile.Name,
  180. DateModified = sysFile.LastWriteTime,
  181. DateCreated = sysFile.CreationTime,
  182. Id = VirtualPath.Replace(Blog.CurrentInstance.RootFileStore.FullPath, ""),
  183. LastAccessTime = sysFile.LastAccessTime,
  184. ParentDirectory = GetDirectory(VirtualPath.Substring(0, VirtualPath.LastIndexOf("/"))),
  185. FilePath = VirtualPath.Replace(Blog.CurrentInstance.RootFileStore.FullPath, ""),
  186. FileSize = sysFile.Length,
  187. };
  188. return file;
  189. }
  190. /// <summary>
  191. /// boolean wether a file exists by its virtual path
  192. /// </summary>
  193. /// <param name="VirtualPath">the virtual path</param>
  194. /// <returns>boolean</returns>
  195. public override bool FileExists(string VirtualPath)
  196. {
  197. VirtualPath = RelativeFilePath(VirtualPath);
  198. var aPath = BlogAbsolutePath(VirtualPath);
  199. return File.Exists(aPath);
  200. }
  201. /// <summary>
  202. /// deletes a file by virtual path
  203. /// </summary>
  204. /// <param name="VirtualPath">virtual path</param>
  205. public override void DeleteFile(string VirtualPath)
  206. {
  207. VirtualPath = RelativeFilePath(VirtualPath);
  208. if (!this.DirectoryExists(VirtualPath))
  209. return;
  210. var aPath = BlogAbsolutePath(VirtualPath);
  211. var sysFile = new FileInfo(aPath);
  212. sysFile.Delete();
  213. }
  214. /// <summary>
  215. /// uploads a file to the provider container
  216. /// </summary>
  217. /// <param name="FileBinary">file contents as byte array</param>
  218. /// <param name="FileName">the file name</param>
  219. /// <param name="BaseDirectory">directory object that is the owner</param>
  220. /// <returns>the new file object</returns>
  221. public override FileSystem.File UploadFile(byte[] FileBinary, string FileName, FileSystem.Directory BaseDirectory)
  222. {
  223. return UploadFile(FileBinary, FileName, BaseDirectory, false);
  224. }
  225. /// <summary>
  226. /// uploads a file to the provider container
  227. /// </summary>
  228. /// <param name="FileBinary">the contents of the file as a byte array</param>
  229. /// <param name="FileName">the file name</param>
  230. /// <param name="BaseDirectory">the directory object that is the owner</param>
  231. /// <param name="Overwrite">boolean wether to overwrite the file if it exists.</param>
  232. /// <returns>the new file object</returns>
  233. public override FileSystem.File UploadFile(byte[] FileBinary, string FileName, FileSystem.Directory BaseDirectory, bool Overwrite)
  234. {
  235. var virtualPath = RelativeFilePath(string.Format("{0}/{1}", BaseDirectory.FullPath, FileName));
  236. if (FileExists(virtualPath))
  237. if (Overwrite)
  238. DeleteFile(virtualPath);
  239. else
  240. throw new IOException("File " + virtualPath + " already exists. Unable to upload file.");
  241. var aPath = BlogAbsolutePath(virtualPath);
  242. File.WriteAllBytes(aPath, FileBinary);
  243. return GetFile(virtualPath);
  244. }
  245. /// <summary>
  246. /// gets the file contents via Lazy load, however in the DbProvider the Contents are loaded when the initial object is created to cut down on DbReads
  247. /// </summary>
  248. /// <param name="BaseFile">the baseFile object to fill</param>
  249. /// <returns>the original file object</returns>
  250. internal override FileSystem.File GetFileContents(FileSystem.File BaseFile)
  251. {
  252. var aPath = BlogAbsolutePath(BaseFile.FullPath);
  253. BaseFile.FileContents = FileToByteArray(aPath);
  254. return BaseFile;
  255. }
  256. /// <summary>
  257. /// Converts a file path to a byte array for handler processing
  258. /// </summary>
  259. /// <param name="FilePath">the file path to process</param>
  260. /// <returns>a new binary array</returns>
  261. private static byte[] FileToByteArray(string FilePath)
  262. {
  263. byte[] Buffer = null;
  264. try
  265. {
  266. System.IO.FileStream FileStream = new System.IO.FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
  267. System.IO.BinaryReader BinaryReader = new System.IO.BinaryReader(FileStream);
  268. long TotalBytes = new System.IO.FileInfo(FilePath).Length;
  269. Buffer = BinaryReader.ReadBytes((Int32)TotalBytes);
  270. FileStream.Close();
  271. FileStream.Dispose();
  272. BinaryReader.Close();
  273. }
  274. catch (Exception ex)
  275. {
  276. Utils.Log("File Provider FileToByArray", ex);
  277. }
  278. return Buffer;
  279. }
  280. public override FileSystem.Image ImageThumbnail(string VirtualPath, int MaximumSize)
  281. {
  282. throw new NotImplementedException();
  283. }
  284. #endregion
  285. }
  286. }