DFLayoutEmbeddedImageProperties.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. namespace InABox.Core
  3. {
  4. public class DFLayoutEmbeddedImageProperties : DFLayoutFieldProperties<DFLayoutEmbeddedMediaValue>
  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. if (Guid.TryParse(str, out var id))
  20. {
  21. return Array.Empty<byte>();
  22. }
  23. try
  24. {
  25. return Convert.FromBase64String(str);
  26. }
  27. catch (Exception e)
  28. {
  29. Logger.Send(LogType.Error, "", $"Error in image data; invalid Base-64: {e.Message}");
  30. return null;
  31. }
  32. }
  33. return null;
  34. }
  35. protected override void LoadProperties()
  36. {
  37. base.LoadProperties();
  38. DisableLibrary = GetProperty("DisableLibrary", false);
  39. }
  40. protected override void SaveProperties()
  41. {
  42. base.SaveProperties();
  43. SetProperty("DisableLibrary", DisableLibrary);
  44. }
  45. }
  46. }