DFLayoutEmbeddedImageProperties.cs 1.3 KB

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