123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Web;
- using System.Web.Hosting;
- using iBemsDataService.Model;
- namespace iBemsDataService.Util
- {
- public class FileCategoryManager : IDisposable
- {
- private static FileCategoryManager _fileCategoryManager = new FileCategoryManager();
- private string _filePath;
- private iBemsEntities _db = new iBemsEntities();
- private Dictionary<string , string> _fileCategories = new Dictionary<string , string>();
- private Nullable<DateTime> _updatedDateTime = null;
- private FileCategoryManager()
- {
- _filePath = HostingEnvironment.MapPath( "~/App_Data/" );
- }
- public static FileCategoryManager GetInstance()
- {
- _fileCategoryManager.LoadFileCategories();
- return _fileCategoryManager;
- }
- public void LoadFileCategories()
- {
- var now = DateTime.Now;
- if ( _updatedDateTime != null && now.Subtract( (DateTime)_updatedDateTime ).TotalMinutes < 5 )
- {
- return;
- }
- var query = from data in _db.CmFileCategory
- select data;
- _fileCategories.Clear();
- foreach( var c in query )
- {
- string id = MakeFileCategoryId( c.FileCategoryId );
- string path = GetFileCategoryPath( c.Name );
- if( Directory.Exists( path ) == false )
- {
- Directory.CreateDirectory( path );
- }
- _fileCategories.Add( id , c.Name );
- }
- _updatedDateTime = DateTime.Now;
- }
- private string MakeFileCategoryId( int categoryId )
- {
- return categoryId.ToString();
- //var sb = new StringBuilder();
- //sb.Append( siteId );
- //sb.Append( "#" );
- //sb.Append( categoryId );
- //return sb.ToString();
- }
- public string GetFileCategoryPath( string fileCategoryName )
- {
- var sb = new StringBuilder();
- sb.Append( _filePath );
- sb.Append( "files/" );
- sb.Append( fileCategoryName );
- return sb.ToString();
- }
- public string GetFilePath( CmFile file )
- {
- var sb = new StringBuilder();
- sb.Append( GetFileCategoryPath( GetFileCategoryName( file.SiteId , file.FileCategoryId ) ) );
- sb.Append( "/" );
- sb.Append( file.FileId );
- return sb.ToString();
- }
- public string GetFileCategoryName( int siteId , int fileCategoryId )
- {
- string id = MakeFileCategoryId( fileCategoryId );
- if( _fileCategories.ContainsKey( id ) == false )
- {
- return null;
- }
- return _fileCategories[id];
- }
- public void Dispose()
- {
- _db.Dispose();
- }
- }
- }
|