PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/FileSystem/File.cs

#
C# | 401 lines | 222 code | 43 blank | 136 comment | 9 complexity | bb0a2ecf38a0b581b7387d6e917da8ea MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. using System;
  2. using System.Linq;
  3. using System.IO;
  4. using System.Web;
  5. using BlogEngine.Core.Providers;
  6. namespace BlogEngine.Core.FileSystem
  7. {
  8. /// <summary>
  9. /// The file is a virtual file that is loaded into memory.
  10. /// </summary>
  11. /// <remarks>
  12. /// Not to be confused with System.IO.File
  13. /// </remarks>
  14. public partial class File : BusinessBase<File, string>, IComparable<File>
  15. {
  16. #region Constants and Fields
  17. /// <summary>
  18. /// the binary of the file
  19. /// </summary>
  20. private byte[] contents;
  21. /// <summary>
  22. /// the file size, in raw long format
  23. /// </summary>
  24. private long fileSize;
  25. /// <summary>
  26. /// the parent directory of the file
  27. /// </summary>
  28. private Directory parentDirectory;
  29. /// <summary>
  30. /// the name of the file without the path
  31. /// </summary>
  32. private string name;
  33. /// <summary>
  34. /// the full path of the file, internal field only, use file path for external calls. reduces security concerns
  35. /// while outside of the buisness layer
  36. /// </summary>
  37. internal string fullpath;
  38. /// <summary>
  39. /// the relative file path to the blog file container
  40. /// </summary>
  41. private string filepath;
  42. /// <summary>
  43. /// list of valid image extensions
  44. /// </summary>
  45. private string[] ImageExtensnios = { ".jpg", ".png", ".jpeg", ".tiff", ".gif", ".bmp" };
  46. #endregion
  47. #region Properties
  48. /// <summary>
  49. /// Gets the File Contents, Lazy loaded
  50. /// </summary>
  51. /// <remarks>
  52. /// set accessor set to internal
  53. /// </remarks>
  54. public byte[] FileContents
  55. {
  56. get
  57. {
  58. if (this.contents == null)
  59. FileContents = BlogService.GetFileContents(this).FileContents;
  60. return this.contents;
  61. }
  62. internal set
  63. {
  64. base.SetValue("FileContents", value, ref this.contents);
  65. }
  66. }
  67. /// <summary>
  68. /// gets the file size, in raw long
  69. /// </summary>
  70. /// <remarks>
  71. /// set accessor set to internal
  72. /// </remarks>
  73. public long FileSize
  74. {
  75. get
  76. {
  77. return this.fileSize;
  78. }
  79. internal set
  80. {
  81. base.SetValue("FileSize", value, ref this.fileSize);
  82. }
  83. }
  84. /// <summary>
  85. /// gets the parent directory, uses Lazy loading
  86. /// </summary>
  87. /// <remarks>
  88. /// set accessor set to internal
  89. /// </remarks>
  90. public Directory ParentDirectory
  91. {
  92. get
  93. {
  94. if (this.parentDirectory == null)
  95. {
  96. var parent = this.FullPath.Contains("/") ? this.FullPath.Substring(0,this.FullPath.LastIndexOf("/")) : this.FullPath;
  97. ParentDirectory = BlogService.GetDirectory(parent);
  98. }
  99. return this.parentDirectory;
  100. }
  101. internal set
  102. {
  103. base.SetValue("ParentDirectory", value, ref this.parentDirectory);
  104. }
  105. }
  106. /// <summary>
  107. /// gets the file name
  108. /// </summary>
  109. /// <remarks>
  110. /// set accessor set to the internal
  111. /// </remarks>
  112. public string Name
  113. {
  114. get
  115. {
  116. return this.name;
  117. }
  118. internal set
  119. {
  120. base.SetValue("Name", value, ref this.name);
  121. }
  122. }
  123. /// <summary>
  124. /// gets the full path, internale set. To change the path use rename methods
  125. /// </summary>
  126. internal string FullPath
  127. {
  128. get
  129. {
  130. return this.fullpath;
  131. }
  132. set
  133. {
  134. base.SetValue("FullPath", value, ref this.fullpath);
  135. }
  136. }
  137. /// <summary>
  138. /// gets the relative path to the file
  139. /// </summary>
  140. /// <remarks>
  141. /// set accessor set to internal
  142. /// </remarks>
  143. public string FilePath
  144. {
  145. get
  146. {
  147. return this.filepath;
  148. }
  149. internal set
  150. {
  151. base.SetValue("FilePath", value, ref this.filepath);
  152. }
  153. }
  154. /// <summary>
  155. /// gets the file extension
  156. /// </summary>
  157. public string Extension
  158. {
  159. get
  160. {
  161. return Path.GetExtension(Name);
  162. }
  163. }
  164. /// <summary>
  165. /// gets the safe file name, URL encoded version of this.FilePath
  166. /// </summary>
  167. public string SafeFilePath
  168. {
  169. get
  170. {
  171. return HttpUtility.UrlEncode(this.FilePath);
  172. }
  173. }
  174. /// <summary>
  175. /// gets the file size in string formated
  176. /// </summary>
  177. public string FileSizeFormat
  178. {
  179. get
  180. {
  181. return SizeFormat(this.FileSize, "N");
  182. }
  183. }
  184. /// <summary>
  185. /// gets the full file description of name and filesize (formated)
  186. /// </summary>
  187. public string FileDescription
  188. {
  189. get
  190. {
  191. return string.Format("{0} ({1})", this.Name, SizeFormat(this.FileSize, "N"));
  192. }
  193. }
  194. /// <summary>
  195. /// gets the full download path to the file, using the file handler
  196. /// </summary>
  197. public string FileDownloadPath
  198. {
  199. get
  200. {
  201. return string.Format("{0}FILES{1}.axdx", Utils.RelativeWebRoot, this.SafeFilePath);
  202. }
  203. }
  204. /// <summary>
  205. /// gets the last access time for the file
  206. /// </summary>
  207. /// <remarks>
  208. /// set accessor set to internal
  209. /// </remarks>
  210. public DateTime LastAccessTime { get; internal set; }
  211. /// <summary>
  212. /// valdidates if this object is an image
  213. /// </summary>
  214. public bool IsImage
  215. {
  216. get
  217. {
  218. return ImageExtensnios.Any(x => x.ToLower() == this.Extension.ToLower());
  219. }
  220. }
  221. /// <summary>
  222. /// converts the object to the Image object and disposes of the original object. An exception will be thrown if the image is not of a file type.
  223. /// </summary>
  224. /// <remarks>
  225. /// always compare the IsImage flag first before attempting a direct call to AsImage
  226. /// </remarks>
  227. public Image AsImage
  228. {
  229. get
  230. {
  231. var img = new Image(this);
  232. this.Dispose();
  233. return img;
  234. }
  235. }
  236. #endregion
  237. #region Methods
  238. /// <summary>
  239. /// deletes the current file object from the storage container
  240. /// </summary>
  241. public void Delete()
  242. {
  243. BlogService.DeleteFile(this.FullPath);
  244. this.Dispose();
  245. }
  246. /// <summary>
  247. /// renames the current file
  248. /// </summary>
  249. /// <param name="NewName">the new name of the file</param>
  250. /// <returns>the new file objbect</returns>
  251. /// <remarks>
  252. /// methods performs an upload, then deletes itself. The result will dispose of the original object
  253. /// </remarks>
  254. public File Rename(string NewName)
  255. {
  256. var newFile = BlogService.UploadFile(this.FileContents, NewName, this.ParentDirectory);
  257. this.Delete();
  258. this.Dispose();
  259. return newFile;
  260. }
  261. /// <summary>
  262. /// moves the file to a new directory & new name
  263. /// </summary>
  264. /// <param name="NewName">the new name</param>
  265. /// <param name="NewBaseDirectory">the new directory</param>
  266. /// <returns>
  267. /// the new file object
  268. /// </returns>
  269. /// <remarks>
  270. /// methods perfoms an upload, the deletes iteself. The result will dispose of the original object
  271. /// </remarks>
  272. public File MoveFile(string NewName, Directory NewBaseDirectory)
  273. {
  274. var newFile = BlogService.UploadFile(this.FileContents, NewName, NewBaseDirectory, true);
  275. this.Delete();
  276. this.Dispose();
  277. return newFile;
  278. }
  279. /// <summary>
  280. /// copys a file from one directory to another
  281. /// </summary>
  282. /// <param name="NewBaseDirectory">the new directory</param>
  283. /// <returns>the new file object</returns>
  284. /// <remarks>
  285. /// both object will be maintained after the copy.
  286. /// </remarks>
  287. public File CopyFile(Directory NewBaseDirectory)
  288. {
  289. return BlogService.UploadFile(this.FileContents, this.Name, NewBaseDirectory, true);
  290. }
  291. /// <summary>
  292. /// copys a file from one directory to another with a new name
  293. /// </summary>
  294. /// <param name="NewName">the new name</param>
  295. /// <param name="NewBaseDirectory">the new directory</param>
  296. /// <returns>the new file object</returns>
  297. /// <remarks>
  298. /// both object will be maintained after the copy.
  299. /// </remarks>
  300. public File CopyFile(string NewName, Directory NewBaseDirectory)
  301. {
  302. return BlogService.UploadFile(this.FileContents, NewName, NewBaseDirectory, true);
  303. }
  304. #region Unimplemented Methods
  305. protected override void ValidationRules()
  306. {
  307. throw new NotImplementedException();
  308. }
  309. protected override void DataUpdate()
  310. {
  311. throw new NotImplementedException();
  312. }
  313. protected override File DataSelect(string id)
  314. {
  315. throw new NotImplementedException();
  316. }
  317. protected override void DataInsert()
  318. {
  319. throw new NotImplementedException();
  320. }
  321. protected override void DataDelete()
  322. {
  323. throw new NotImplementedException();
  324. }
  325. public int CompareTo(File other)
  326. {
  327. throw new NotImplementedException();
  328. }
  329. #endregion
  330. /// <summary>
  331. /// formats a file to a predetermided size
  332. /// </summary>
  333. /// <param name="size">the file size as a float</param>
  334. /// <param name="formatString">the numeric format string</param>
  335. /// <returns>the formated string</returns>
  336. private static string SizeFormat(float size, string formatString)
  337. {
  338. if (size < 1024)
  339. {
  340. return string.Format("{0} bytes", size.ToString(formatString));
  341. }
  342. if (size < Math.Pow(1024, 2))
  343. {
  344. return string.Format("{0} kb", (size / 1024).ToString(formatString));
  345. }
  346. if (size < Math.Pow(1024, 3))
  347. {
  348. return string.Format("{0} mb", (size / Math.Pow(1024, 2)).ToString(formatString));
  349. }
  350. if (size < Math.Pow(1024, 4))
  351. {
  352. return string.Format("{0} gb", (size / Math.Pow(1024, 3)).ToString(formatString));
  353. }
  354. return size.ToString(formatString);
  355. }
  356. #endregion
  357. }
  358. }