39a9be0f17c8bffc0e5a2fd6fa259cd781c39635.svn-base 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Web;
  7. using System.Web.Hosting;
  8. using iBemsDataService.Model;
  9. namespace iBemsDataService.Util
  10. {
  11. public class FileCategoryManager : IDisposable
  12. {
  13. private static FileCategoryManager _fileCategoryManager = new FileCategoryManager();
  14. private string _filePath;
  15. private iBemsEntities _db = new iBemsEntities();
  16. private Dictionary<string , string> _fileCategories = new Dictionary<string , string>();
  17. private Nullable<DateTime> _updatedDateTime = null;
  18. private FileCategoryManager()
  19. {
  20. _filePath = HostingEnvironment.MapPath( "~/App_Data/" );
  21. }
  22. public static FileCategoryManager GetInstance()
  23. {
  24. _fileCategoryManager.LoadFileCategories();
  25. return _fileCategoryManager;
  26. }
  27. public void LoadFileCategories()
  28. {
  29. var now = DateTime.Now;
  30. if ( _updatedDateTime != null && now.Subtract( (DateTime)_updatedDateTime ).TotalMinutes < 5 )
  31. {
  32. return;
  33. }
  34. var query = from data in _db.CmFileCategory
  35. select data;
  36. _fileCategories.Clear();
  37. foreach( var c in query )
  38. {
  39. string id = MakeFileCategoryId( c.FileCategoryId );
  40. string path = GetFileCategoryPath( c.Name );
  41. if( Directory.Exists( path ) == false )
  42. {
  43. Directory.CreateDirectory( path );
  44. }
  45. _fileCategories.Add( id , c.Name );
  46. }
  47. _updatedDateTime = DateTime.Now;
  48. }
  49. private string MakeFileCategoryId( int categoryId )
  50. {
  51. return categoryId.ToString();
  52. //var sb = new StringBuilder();
  53. //sb.Append( siteId );
  54. //sb.Append( "#" );
  55. //sb.Append( categoryId );
  56. //return sb.ToString();
  57. }
  58. public string GetFileCategoryPath( string fileCategoryName )
  59. {
  60. var sb = new StringBuilder();
  61. sb.Append( _filePath );
  62. sb.Append( "files/" );
  63. sb.Append( fileCategoryName );
  64. return sb.ToString();
  65. }
  66. public string GetFilePath( CmFile file )
  67. {
  68. var sb = new StringBuilder();
  69. sb.Append( GetFileCategoryPath( GetFileCategoryName( file.SiteId , file.FileCategoryId ) ) );
  70. sb.Append( "/" );
  71. sb.Append( file.FileId );
  72. return sb.ToString();
  73. }
  74. public string GetFileCategoryName( int siteId , int fileCategoryId )
  75. {
  76. string id = MakeFileCategoryId( fileCategoryId );
  77. if( _fileCategories.ContainsKey( id ) == false )
  78. {
  79. return null;
  80. }
  81. return _fileCategories[id];
  82. }
  83. public void Dispose()
  84. {
  85. _db.Dispose();
  86. }
  87. }
  88. }