EntityDocumentStore.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using Comal.Classes;
  2. using Comal.Stores;
  3. using InABox.Core;
  4. using System;
  5. using System.Linq;
  6. using System.IO;
  7. using Syncfusion.Pdf.Parsing;
  8. using System.Drawing;
  9. using System.Drawing.Imaging;
  10. namespace PRSStores
  11. {
  12. public abstract class EntityDocumentStore<TEntityDocument, TEntity, TEntityLink> : BaseStore<TEntityDocument>
  13. where TEntityDocument : EntityDocument<TEntityLink>, new()
  14. where TEntityLink : EntityLink<TEntity>, new()
  15. where TEntity : Entity, new()
  16. {
  17. protected override void BeforeSave(TEntityDocument entity)
  18. {
  19. SaveThumbnail(entity); //current size for saved thumbnails is 256*256
  20. base.BeforeSave(entity);
  21. }
  22. private void SaveThumbnail(TEntityDocument entity)
  23. {
  24. var stopwatch = new System.Diagnostics.Stopwatch();
  25. stopwatch.Start();
  26. if (entity.DocumentLink.ID == Guid.Empty)
  27. return;
  28. CoreTable table = Provider.Query<Document>(new Filter<Document>(x => x.ID).IsEqualTo(entity.DocumentLink.ID),
  29. new Columns<Document>(x => x.Data, x => x.FileName));
  30. if (table.Rows.Count == 0)
  31. return;
  32. Document doc = table.Rows.FirstOrDefault().ToObject<Document>();
  33. if (!doc.FileName.ToLower().EndsWith("pdf"))
  34. return;
  35. PdfLoadedDocument loadeddoc = new PdfLoadedDocument(doc.Data);
  36. Bitmap image = loadeddoc.ExportAsImage(0, new SizeF(256, 256), true);
  37. MemoryStream stream = new MemoryStream();
  38. image.Save(stream, ImageFormat.Jpeg);
  39. entity.Thumbnail = stream.ToArray();
  40. stopwatch.Stop();
  41. Logger.Send(LogType.Information, UserID, "Time taken to compress thumbnail " + stopwatch.ElapsedMilliseconds + " milliseconds");
  42. }
  43. }
  44. //In order to have BeforeSave active for a specific EntityDocument, it has to be concretized as below
  45. public class JobDocumentSetSetMileStoneFileStore : EntityDocumentStore<JobDocumentSetMileStoneFile, JobDocumentSetMileStone, JobDocumentSetMileStoneLink>
  46. {
  47. }
  48. public class MeetingItemDocumentStore : EntityDocumentStore<MeetingItemDocument, MeetingItem, MeetingItemLink>
  49. {
  50. }
  51. }