/projects/PigeonCms.Core/BaseClasses/FilesGallery.cs

http://pigeoncms.googlecode.com/ · C# · 627 lines · 516 code · 56 blank · 55 comment · 43 complexity · bfda0d8323e0b798362fd6af9342b52d MD5 · raw file

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.IO;
  11. using System.Drawing;
  12. using System.Diagnostics;
  13. using System.Collections.Generic;
  14. using System.ComponentModel;
  15. using System.Threading;
  16. using System.Xml;
  17. namespace PigeonCms
  18. {
  19. /// <summary>
  20. /// meta info about generic uploaded file
  21. /// </summary>
  22. public class FileMetaInfo
  23. {
  24. private bool dataLoaded = false;
  25. private Dictionary<string, string> titleTranslations = new Dictionary<string, string>();
  26. private Dictionary<string, string> descriptionTranslations = new Dictionary<string, string>();
  27. public FileMetaInfo(){}
  28. public FileMetaInfo(string fileUrl): this(fileUrl, false){}
  29. public FileMetaInfo(string fileUrl, bool isFolder)
  30. {
  31. this.FileUrl = fileUrl;
  32. this.isFolder = isFolder;
  33. if (!string.IsNullOrEmpty(this.FileUrl))
  34. GetData();
  35. }
  36. private bool isFolder = false;
  37. public bool IsFolder
  38. {
  39. [DebuggerStepThrough()]
  40. get { return isFolder; }
  41. }
  42. public string FileName
  43. {
  44. [DebuggerStepThrough()]
  45. get
  46. {
  47. string res = "";
  48. if (!string.IsNullOrEmpty(fileUrl))
  49. res = VirtualPathUtility.GetFileName(fileUrl);
  50. return res;
  51. }
  52. }
  53. public string FileNameNoExtension
  54. {
  55. [DebuggerStepThrough()]
  56. get
  57. {
  58. int dot = this.FileName.LastIndexOf(".");
  59. return this.FileName.Substring(0, dot);
  60. }
  61. }
  62. private string fileUrl = "";
  63. //absolute path in local website eg:/public/filename.ext
  64. public string FileUrl
  65. {
  66. [DebuggerStepThrough()]
  67. get { return fileUrl; }
  68. [DebuggerStepThrough()]
  69. set { fileUrl = value; }
  70. }
  71. private string fileFullUrl = null;
  72. //absolute path eg:http://www.yoursite.com/public/filename.ext
  73. public string FileFullUrl
  74. {
  75. [DebuggerStepThrough()]
  76. get
  77. {
  78. if (fileFullUrl == null)
  79. {
  80. fileFullUrl = "";
  81. if (!string.IsNullOrEmpty(this.FileUrl))
  82. fileFullUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + this.FileUrl;
  83. }
  84. return fileFullUrl;
  85. }
  86. }
  87. private long fileLength = 0;
  88. public long FileLength
  89. {
  90. [DebuggerStepThrough()]
  91. get { return fileLength; }
  92. [DebuggerStepThrough()]
  93. set { fileLength = value; }
  94. }
  95. /// <summary>
  96. /// file length in B, KB, MB, GB
  97. /// </summary>
  98. public string HumanLength
  99. {
  100. [DebuggerStepThrough()]
  101. get
  102. {
  103. return Utility.GetFileHumanLength(this.FileLength);
  104. }
  105. }
  106. public string FileExtension
  107. {
  108. [DebuggerStepThrough()]
  109. get { return new FileInfo(this.FileName).Extension; }
  110. }
  111. /// <summary>
  112. /// Title in current culture
  113. /// </summary>
  114. public string Title
  115. {
  116. get
  117. {
  118. if (!dataLoaded) GetData();
  119. string res = "";
  120. titleTranslations.TryGetValue(Thread.CurrentThread.CurrentCulture.Name, out res);
  121. if (string.IsNullOrEmpty(res))
  122. titleTranslations.TryGetValue(Config.CultureDefault, out res);
  123. return res;
  124. }
  125. }
  126. /// <summary>
  127. /// Title in different cultures
  128. /// </summary>
  129. public Dictionary<string, string> TitleTranslations
  130. {
  131. [DebuggerStepThrough()]
  132. get { return titleTranslations; }
  133. [DebuggerStepThrough()]
  134. set { titleTranslations = value; }
  135. }
  136. /// <summary>
  137. /// Description in current cultures
  138. /// </summary>
  139. public string Description
  140. {
  141. get
  142. {
  143. if (!dataLoaded) GetData();
  144. string res = "";
  145. descriptionTranslations.TryGetValue(Thread.CurrentThread.CurrentCulture.Name, out res);
  146. if (string.IsNullOrEmpty(res))
  147. descriptionTranslations.TryGetValue(Config.CultureDefault, out res);
  148. return res;
  149. }
  150. }
  151. /// <summary>
  152. /// Description in different cultures
  153. /// </summary>
  154. public Dictionary<string, string> DescriptionTranslations
  155. {
  156. [DebuggerStepThrough()]
  157. get { return descriptionTranslations; }
  158. [DebuggerStepThrough()]
  159. set { descriptionTranslations = value; }
  160. }
  161. /// <summary>
  162. /// save file meta data (title, description, etc.)
  163. /// </summary>
  164. /// <returns></returns>
  165. public bool SaveData()
  166. {
  167. bool res = true;
  168. string filename = HttpContext.Current.Request.MapPath(this.FileUrl);
  169. FileStream fs = new FileStream(filename + ".xml", FileMode.Create);
  170. XmlWriter w = XmlWriter.Create(fs);
  171. w.WriteStartDocument();
  172. w.WriteStartElement("FileInfo");
  173. w.WriteStartElement("TitleTranslations");
  174. foreach (KeyValuePair<string, string> item in Config.CultureList)
  175. {
  176. string currVal = "";
  177. titleTranslations.TryGetValue(item.Key, out currVal);
  178. w.WriteStartElement("item");
  179. w.WriteAttributeString("len", item.Key);
  180. w.WriteCData(currVal);
  181. //w.WriteString(currTitle);
  182. //w.WriteElementString("title", currTitle);
  183. w.WriteEndElement();
  184. }
  185. w.WriteEndElement();
  186. w.WriteStartElement("DescriptionTranslations");
  187. foreach (KeyValuePair<string, string> item in Config.CultureList)
  188. {
  189. string currVal = "";
  190. descriptionTranslations.TryGetValue(item.Key, out currVal);
  191. w.WriteStartElement("item");
  192. w.WriteAttributeString("len", item.Key);
  193. w.WriteCData(currVal);
  194. w.WriteEndElement();
  195. }
  196. w.WriteEndElement();
  197. w.WriteEndElement();
  198. w.WriteEndDocument();
  199. w.Flush();
  200. fs.Close();
  201. return res;
  202. }
  203. /// <summary>
  204. /// get file data (Title, Description, etc.)
  205. /// </summary>
  206. /// <returns></returns>
  207. public bool GetData()
  208. {
  209. bool res = true;
  210. string filename = HttpContext.Current.Request.MapPath(this.FileUrl) + ".xml";
  211. XmlDocument doc = new XmlDocument();
  212. try
  213. {
  214. if (System.IO.File.Exists(filename))
  215. {
  216. doc.Load(filename);
  217. XmlNode rootNode;
  218. rootNode = doc.SelectSingleNode("FileInfo");
  219. if (rootNode == null)
  220. {
  221. //backward compatibility
  222. rootNode = doc.SelectSingleNode("EmbeddedImage");
  223. }
  224. //titles
  225. this.TitleTranslations.Clear();
  226. XmlNodeList titleNodes = rootNode.SelectNodes("TitleTranslations/item");
  227. foreach (XmlNode item in titleNodes)
  228. {
  229. string lenAttr = "";
  230. string currValue = "";
  231. if (item.Attributes["len"] != null)
  232. {
  233. lenAttr = item.Attributes["len"].Value;
  234. }
  235. currValue = item.InnerText;
  236. this.TitleTranslations.Add(lenAttr, currValue);
  237. }
  238. //descriptions
  239. this.DescriptionTranslations.Clear();
  240. XmlNodeList descNodes = rootNode.SelectNodes("DescriptionTranslations/item");
  241. foreach (XmlNode item in descNodes)
  242. {
  243. string lenAttr = "";
  244. string currValue = "";
  245. if (item.Attributes["len"] != null)
  246. {
  247. lenAttr = item.Attributes["len"].Value;
  248. }
  249. currValue = item.InnerText;
  250. this.DescriptionTranslations.Add(lenAttr, currValue);
  251. }
  252. }
  253. }
  254. catch
  255. {
  256. res = false;
  257. }
  258. finally
  259. {
  260. }
  261. this.dataLoaded = res;
  262. return res;
  263. }
  264. }
  265. /// <summary>
  266. /// generic file gallery
  267. /// </summary>
  268. [DataObject()]
  269. public class FilesGallery
  270. {
  271. #region fields
  272. public string TempPath
  273. {
  274. get
  275. {
  276. const string tempPath = "~/public/files/temp/";
  277. string res = "";
  278. if (!string.IsNullOrEmpty(Utility._SessionID()))
  279. {
  280. res = VirtualPathUtility.ToAbsolute(tempPath + Utility._SessionID());
  281. }
  282. return res;
  283. }
  284. }
  285. public string TempPhisicalPath
  286. {
  287. get
  288. {
  289. string res = this.TempPath;
  290. res = HttpContext.Current.Request.MapPath(res);
  291. return res;
  292. }
  293. }
  294. private string searchPattern = "*.*";
  295. /// <summary>
  296. /// search pattern string used by GetAll() method
  297. /// </summary>
  298. public string SearchPattern
  299. {
  300. [DebuggerStepThrough()]
  301. get { return searchPattern; }
  302. [DebuggerStepThrough()]
  303. set
  304. {
  305. if (!string.IsNullOrEmpty(value))
  306. {
  307. searchPattern = value;
  308. }
  309. }
  310. }
  311. private string folderName = "";
  312. public string FolderName
  313. {
  314. [DebuggerStepThrough()]
  315. get { return this.folderName; }
  316. [DebuggerStepThrough()]
  317. set
  318. {
  319. if (!string.IsNullOrEmpty(value))
  320. {
  321. if (!value.EndsWith("/")) value += "/";
  322. this.folderName = value;
  323. }
  324. }
  325. }
  326. private string virtualPath = "~/Public/files/";
  327. /// <summary>
  328. /// virtual base path for files gallery
  329. /// example: "~/Public/files/"
  330. /// </summary>
  331. public string VirtualPath
  332. {
  333. [DebuggerStepThrough()]
  334. get { return this.virtualPath; }
  335. [DebuggerStepThrough()]
  336. set
  337. {
  338. if (!string.IsNullOrEmpty(value))
  339. {
  340. if (!value.EndsWith("/")) value += "/";
  341. this.virtualPath = value;
  342. }
  343. }
  344. }
  345. /// <summary>
  346. /// absolute path
  347. /// </summary>
  348. public string AbsolutePath
  349. {
  350. [DebuggerStepThrough()]
  351. get
  352. {
  353. string res = this.virtualPath;
  354. if (!string.IsNullOrEmpty(this.folderName))
  355. res += this.folderName;
  356. res = VirtualPathUtility.ToAbsolute(res);
  357. return res;
  358. }
  359. }
  360. /// <summary>
  361. /// phisical path of foldername
  362. /// </summary>
  363. protected string PhisicalPath
  364. {
  365. [DebuggerStepThrough()]
  366. get
  367. {
  368. string res = this.virtualPath;
  369. if (!string.IsNullOrEmpty(this.folderName))
  370. res += this.folderName;
  371. res = HttpContext.Current.Request.MapPath(res);
  372. return res;
  373. }
  374. }
  375. #endregion
  376. [DebuggerStepThrough()]
  377. public FilesGallery(){ }
  378. [DebuggerStepThrough()]
  379. public FilesGallery(string virtualPath, string folderName)
  380. {
  381. this.VirtualPath = virtualPath;
  382. this.FolderName = folderName;
  383. }
  384. [DebuggerStepThrough()]
  385. public FilesGallery(string virtualPath, string folderName, string searchPattern)
  386. {
  387. this.VirtualPath = virtualPath;
  388. this.FolderName = folderName;
  389. this.SearchPattern = searchPattern;
  390. }
  391. /// <summary>
  392. /// get list of files filtered by searchPattern
  393. /// </summary>
  394. /// <returns></returns>
  395. [DataObjectMethod(DataObjectMethodType.Select, true)]
  396. public List<FileMetaInfo> GetAll()
  397. {
  398. List<FileMetaInfo> result = new List<FileMetaInfo>();
  399. try
  400. {
  401. DirectoryInfo dir = new DirectoryInfo(this.PhisicalPath);
  402. if (dir.Exists)
  403. {
  404. var folders = dir.GetDirectories(this.SearchPattern);
  405. foreach (DirectoryInfo folder in folders)
  406. {
  407. if (folder.Name.ToLower() != ".svn" && folder.Name.ToLower() != "_vti_cnf")
  408. {
  409. var item = new FileMetaInfo("", true);
  410. fillObject(item, folder);
  411. result.Add(item);
  412. }
  413. }
  414. var files = dir.GetFiles(this.SearchPattern);
  415. foreach (FileInfo file in files)
  416. {
  417. if (file.Extension.ToLower() != ".xml")
  418. {
  419. var item = new FileMetaInfo();
  420. fillObject(item, file);
  421. result.Add(item);
  422. }
  423. }
  424. }
  425. }
  426. finally
  427. {
  428. }
  429. return result;
  430. }
  431. [DataObjectMethod(DataObjectMethodType.Select, false)]
  432. public FileMetaInfo GetByFileName(string fileName)
  433. {
  434. var result = new FileMetaInfo();
  435. try
  436. {
  437. var file = new FileInfo(this.AbsolutePath + fileName);
  438. if (file.Exists)
  439. fillObject(result, file);
  440. }
  441. finally
  442. {
  443. }
  444. return result;
  445. }
  446. public void RenameFile(string sourceFileName, string destFileName)
  447. {
  448. if (sourceFileName == destFileName)
  449. return;
  450. File.Move(Path.Combine(this.PhisicalPath, sourceFileName), Path.Combine(this.PhisicalPath, destFileName));
  451. File.Move(Path.Combine(this.PhisicalPath, sourceFileName + ".xml"), Path.Combine(this.PhisicalPath, destFileName + ".xml"));
  452. }
  453. public bool CreateFolder(string folderName)
  454. {
  455. bool res = false;
  456. try
  457. {
  458. var dir = new DirectoryInfo(this.PhisicalPath);
  459. if (dir.Exists)
  460. {
  461. Directory.CreateDirectory(this.PhisicalPath + folderName);
  462. res = true;
  463. }
  464. }
  465. finally
  466. {
  467. }
  468. return res;
  469. }
  470. [DataObjectMethod(DataObjectMethodType.Delete, true)]
  471. public bool DeleteByFileName(string fileName)
  472. {
  473. return DeleteByFileName(fileName, false);
  474. }
  475. [DataObjectMethod(DataObjectMethodType.Delete, false)]
  476. public bool DeleteByFileName(string fileName, bool isFolder)
  477. {
  478. bool res = false;
  479. try
  480. {
  481. if (isFolder)
  482. {
  483. var dir = new DirectoryInfo(this.PhisicalPath + fileName);
  484. if (dir.Exists)
  485. {
  486. dir.Delete();
  487. res = true;
  488. }
  489. }
  490. else
  491. {
  492. var file = new FileInfo(this.PhisicalPath + fileName);
  493. if (file.Exists)
  494. {
  495. file.Delete();
  496. res = true;
  497. }
  498. }
  499. //delete file with meta info
  500. var metafile = new FileInfo(this.PhisicalPath + fileName + ".xml");
  501. if (metafile.Exists)
  502. metafile.Delete();
  503. }
  504. finally
  505. {
  506. }
  507. return res;
  508. }
  509. [DataObjectMethod(DataObjectMethodType.Delete, true)]
  510. public bool DeleteFolderContent()
  511. {
  512. bool res = false;
  513. try
  514. {
  515. Utility.DeleteFolderContent(this.PhisicalPath);
  516. res = true;
  517. }
  518. finally
  519. {
  520. }
  521. return res;
  522. }
  523. [DataObjectMethod(DataObjectMethodType.Delete, true)]
  524. public bool RemoveSessionTempFolder()
  525. {
  526. bool res = false;
  527. try
  528. {
  529. if (string.IsNullOrEmpty(this.TempPhisicalPath))
  530. return true;
  531. DirectoryInfo dir = new DirectoryInfo(this.TempPhisicalPath);
  532. if (dir.Exists)
  533. dir.Delete(true);
  534. res = true;
  535. }
  536. catch { }
  537. return res;
  538. }
  539. public bool MoveTempFiles()
  540. {
  541. bool res = false;
  542. try
  543. {
  544. if (string.IsNullOrEmpty(this.TempPhisicalPath))
  545. return true;
  546. DirectoryInfo dir = new DirectoryInfo(this.TempPhisicalPath);
  547. if (!dir.Exists)
  548. return true;
  549. Utility.CopyFolder(this.TempPhisicalPath, this.PhisicalPath);
  550. dir.Delete(true);
  551. res = true;
  552. }
  553. catch { }
  554. return res;
  555. }
  556. private void fillObject(FileMetaInfo result, FileInfo item)
  557. {
  558. fillObject(result, item.Name, item.Length);
  559. }
  560. private void fillObject(FileMetaInfo result, DirectoryInfo item)
  561. {
  562. fillObject(result, item.Name, 0);
  563. }
  564. private void fillObject(FileMetaInfo result, string name, long fileLength)
  565. {
  566. //result.FileFullUrl = this.
  567. result.FileUrl = this.AbsolutePath + name;
  568. result.FileLength = fileLength;
  569. }
  570. }
  571. }