From 430c44ca412cc82267d7909c96bc7dd28a6445ae Mon Sep 17 00:00:00 2001 From: Kathy Date: Wed, 12 Sep 2012 13:06:54 +0000 Subject: [PATCH] --- .../Extension/ROImageExt.cs | 169 ++++++++++++++++-- 1 file changed, 154 insertions(+), 15 deletions(-) diff --git a/PROMS/VEPROMS.CSLA.Library/Extension/ROImageExt.cs b/PROMS/VEPROMS.CSLA.Library/Extension/ROImageExt.cs index 8e955b66..08ab50fe 100644 --- a/PROMS/VEPROMS.CSLA.Library/Extension/ROImageExt.cs +++ b/PROMS/VEPROMS.CSLA.Library/Extension/ROImageExt.cs @@ -6,28 +6,67 @@ using System.Data.SqlClient; using Csla; using Csla.Data; using System.IO; +using System.IO.Compression; using Volian.Base.Library; namespace VEPROMS.CSLA.Library { - // put in for debug - //public partial class ROImage - //{ - // public static int CacheCountPrimaryKey - // { get { return _CacheByPrimaryKey.Count; } } - // public static int CacheCountByRODbID - // { get { return _CacheByRODbID_FileName_DTS.Count; } } - // public static int CacheCountList - // { get { return _CacheList.Count; } } - //} + public delegate void ROImageInfoCompressionEvent(object sender, ROImageInfoCompressionEventArgs args); + public class ROImageInfoCompressionEventArgs + { + private int _Total; + public int Total + { + get { return _Total; } + set { _Total = value; } + } + private int _Current; + public int Current + { + get { return _Current; } + set { _Current = value; } + } + private string _FileName; + public string FileName + { + get { return _FileName; } + set { _FileName = value; } + } + private ROImageCompressionEventType _Type; + public ROImageCompressionEventType Type + { + get { return _Type; } + set { _Type = value; } + } + public ROImageInfoCompressionEventArgs(ROImageCompressionEventType type) + { + _Type = type; + } + public ROImageInfoCompressionEventArgs(int current, string fileName) + { + _Current = current; + _FileName = fileName; + _Type = ROImageCompressionEventType.Update; + } + public ROImageInfoCompressionEventArgs(int total) + { + _Type = ROImageCompressionEventType.Initialize; + _Total = total; + } + } + public enum ROImageCompressionEventType:int {Initialize, Update, Complete}; public partial class ROImageInfo { + public static event ROImageInfoCompressionEvent CompressAllExistingImages; + public static void OnCompressAllExistingImages(object sender, ROImageInfoCompressionEventArgs args) + { + if (CompressAllExistingImages != null) CompressAllExistingImages(sender, args); + } // put in for debug //public static int CacheCountPrimaryKey //{ get { return _CacheByPrimaryKey.Count; } } //public static int CacheCountList //{ get { return _CacheList.Count; } } - public static ROImageInfo GetByROFstID_FileName(int rOFstID, string fileName) { //if (!CanGetObject()) @@ -47,6 +86,105 @@ namespace VEPROMS.CSLA.Library throw new DbCslaException("Error on ROImage.GetByROFstID_FileName", ex); } } + public bool ImagesZipped() + { + FolderInfo topFolder = FolderInfo.Get(1); + FolderConfig fc = topFolder.MyConfig as FolderConfig; + return fc.Images_zipped; + } + public static byte[] Compress(byte[] blob) + { + int size = blob.Length; + using (MemoryStream Source = new MemoryStream(blob)) + { + using (MemoryStream Destination = new MemoryStream()) + { + using (DeflateStream Zip = new DeflateStream(Destination, CompressionMode.Compress, true)) + { + int bytesToRead = 0; + Source.Position = 0; + while (Source.Position < Source.Length) + { + if (Source.Length > int.MaxValue) + bytesToRead = int.MaxValue; + else + bytesToRead = (int)(Source.Length - Source.Position); + + byte[] buf = new byte[bytesToRead]; + Source.Read(buf, 0, bytesToRead); + Zip.Write(buf, 0, bytesToRead); + } + Zip.Close(); + } + return Destination.ToArray(); + } + } + } + public static byte[] Decompress(byte[] blob, int bsize) + { + if (blob == null) return null; + using (MemoryStream inputStream = new MemoryStream(blob)) + { + using (DeflateStream unzip = new DeflateStream(inputStream, CompressionMode.Decompress)) + { + using (BinaryReader reader = new BinaryReader(unzip)) + { + byte[] bytes = new byte[bsize]; + reader.Read(bytes, 0, bsize); + return bytes; + } + } + } + } + public static void FlagImagesZipped() + { + using (Folder folder = Folder.Get(1)) + { + FolderConfig cf = folder.MyConfig as FolderConfig; + cf.Images_zipped = true; + folder.Config = cf.ToString(); ; + folder.Save(); + } + } + // zip all images in the database. It was decided that images would be zipped in September of 2012 + // for a few reasons: 1) database size will be smaller; 2) transfer from server of images will be faster; + // 3) database migration wouldn't use as much memory. + // + public static void ZipImages() + { + FolderInfo fi = FolderInfo.Get(1); + FolderConfig cf = fi.MyConfig as FolderConfig; + if (cf.Images_zipped) return; + + // put the ids in a list and process each separately because if we do it any other way, we run + // into memory issues. + ROImageInfoList rol = ROImageInfoList.Get(); + List imageIds = new List(); + foreach (ROImageInfo roi in rol) imageIds.Add(roi.ImageID); + rol.Dispose(); + OnCompressAllExistingImages(null, new ROImageInfoCompressionEventArgs(imageIds.Count)); + int pcount = 1; + foreach (int id in imageIds) + { + using (ROImage ro = ROImage.Get(id)) + { + string nm = ro.FileName; + OnCompressAllExistingImages(null, new ROImageInfoCompressionEventArgs(pcount++, nm)); + ROImageConfig rc = new ROImageConfig(); + rc.Image_Size = ro.Content.Length.ToString(); + ro.Content = ROImageInfo.Compress(ro.Content); + ro.Config = rc.ToString(); + ro.Save(); + } + } + OnCompressAllExistingImages(null, new ROImageInfoCompressionEventArgs( ROImageCompressionEventType.Complete)); + using (Folder folder = Folder.Get(1)) + { + cf.Images_zipped = true; + folder.Config = cf.ToString(); + folder.Save(); + } + } private void DataPortal_Fetch(ROFstID_FileNameCriteria criteria) { if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] ROImageInfo.DataPortal_Fetch", GetHashCode()); @@ -179,7 +317,10 @@ namespace VEPROMS.CSLA.Library { _MyFile = new FileInfo(string.Format(@"{0}\tmp_{1}{2}", VlnSettings.TemporaryFolder, Unique, MyROImage.FileName)); FileStream fs = _MyFile.Create(); - fs.Write(MyROImage.Content, 0, MyROImage.Content.Length); + ROImageConfig roicfg = new ROImageConfig(MyROImage); + int size = Convert.ToInt32(roicfg.Image_Size); + byte[] tmpb = ROImageInfo.Decompress(MyROImage.Content, size); + fs.Write(tmpb, 0, tmpb.Length); fs.Close(); _MyFile.CreationTimeUtc = MyROImage.DTS; _MyFile.LastWriteTimeUtc = MyROImage.DTS; @@ -202,14 +343,12 @@ namespace VEPROMS.CSLA.Library } public void SaveFile() { - // TODO: Add Try & Catch logic if (_MyROImage == null) return; ROImage roImage = _MyROImage.Get(); FileStream fs = _MyFile.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite); Byte[] buf = new byte[_MyFile.Length]; fs.Read(buf, 0, buf.Length); fs.Close(); - //roImage.FileName; roImage.Content = buf; roImage.UserID = Volian.Base.Library.VlnSettings.UserID; roImage.DTS = _MyFile.LastWriteTimeUtc; @@ -250,7 +389,7 @@ namespace VEPROMS.CSLA.Library try { ROImageInfoList tmp = DataPortal.Fetch(new RODbIDNoDataCriteria(rODbID)); - ROImageInfo.AddList(tmp); + //ROImageInfo.AddList(tmp); tmp.AddEvents(); return tmp; }