using Comal.Classes; using Comal.Stores; using InABox.Core; using System; using System.Linq; using System.IO; using Syncfusion.Pdf.Parsing; using System.Drawing; using System.Drawing.Imaging; namespace PRSStores { public abstract class EntityDocumentStore : BaseStore where TEntityDocument : EntityDocument, new() where TEntityLink : EntityLink, new() where TEntity : Entity, new() { protected override void BeforeSave(TEntityDocument entity) { SaveThumbnail(entity); //current size for saved thumbnails is 256*256 base.BeforeSave(entity); } private void SaveThumbnail(TEntityDocument entity) { var stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); if (entity.DocumentLink.ID == Guid.Empty) return; CoreTable table = Provider.Query(new Filter(x => x.ID).IsEqualTo(entity.DocumentLink.ID), new Columns(x => x.Data, x => x.FileName)); if (table.Rows.Count == 0) return; Document doc = table.Rows.FirstOrDefault().ToObject(); if (!doc.FileName.ToLower().EndsWith("pdf")) return; PdfLoadedDocument loadeddoc = new PdfLoadedDocument(doc.Data); Bitmap image = loadeddoc.ExportAsImage(0, new SizeF(256, 256), true); MemoryStream stream = new MemoryStream(); image.Save(stream, ImageFormat.Jpeg); entity.Thumbnail = stream.ToArray(); stopwatch.Stop(); Logger.Send(LogType.Information, UserID, "Time taken to compress thumbnail " + stopwatch.ElapsedMilliseconds + " milliseconds"); } } //In order to have BeforeSave active for a specific EntityDocument, it has to be concretized as below public class JobDocumentSetSetMileStoneFileStore : EntityDocumentStore { } public class MeetingItemDocumentStore : EntityDocumentStore { } }