DFLayoutEmbeddedImageProperties.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. namespace InABox.Core
  3. {
  4. public class DFLayoutEmbeddedImageProperties : DFLayoutFieldProperties<byte[]>
  5. {
  6. [CheckBoxEditor]
  7. [EditorSequence(1)]
  8. public bool DisableLibrary { get; set; }
  9. public override string FormatValue(object? value)
  10. {
  11. return value != null ? "Yes" : "";
  12. }
  13. public override object? ParseValue(object? value)
  14. {
  15. if (value is byte[])
  16. return value;
  17. if (value is string str)
  18. {
  19. try
  20. {
  21. var tuple = Serialization.Deserialize<Tuple<Guid, byte[]>>(str, true);
  22. if ((tuple != null) && (tuple.Item1 != Guid.Empty))
  23. return DigitalFormDocumentFactory.LoadDocument(tuple.Item1);
  24. return null;
  25. }
  26. catch
  27. {
  28. }
  29. if (Guid.TryParse(str, out var id))
  30. {
  31. return Array.Empty<byte>();
  32. }
  33. try
  34. {
  35. return Convert.FromBase64String(str);
  36. }
  37. catch (Exception e)
  38. {
  39. Logger.Send(LogType.Error, "", $"Error in image data; invalid Base-64: {e.Message}");
  40. return null;
  41. }
  42. }
  43. return null;
  44. }
  45. protected override void LoadProperties()
  46. {
  47. base.LoadProperties();
  48. DisableLibrary = GetProperty("DisableLibrary", false);
  49. }
  50. protected override void SaveProperties()
  51. {
  52. base.SaveProperties();
  53. SetProperty("DisableLibrary", DisableLibrary);
  54. }
  55. }
  56. }