Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ModelFactory.cs 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using SecureSharing.Business.Interfaces;
  2. using SecureSharing.Models;
  3. namespace SecureSharing.Infrastructure;
  4. public sealed class ModelFactory : IModelFactory
  5. {
  6. private readonly IMessageService _messageService;
  7. private readonly string _basePath;
  8. public ModelFactory(IMessageService messageService, IWebHostEnvironment webHostEnvironment)
  9. {
  10. _basePath = Path.Combine(webHostEnvironment.WebRootPath.Split('/')[0], "files");
  11. _messageService = messageService;
  12. }
  13. public async Task<LinkModel> PrepareLinkVM(Guid code, bool? share)
  14. {
  15. //share is true when the link is created
  16. LinkModel model = null;
  17. try
  18. {
  19. var message = await _messageService.GetByCode(code);
  20. model = new LinkModel
  21. {
  22. MessageModel = new MessageModel
  23. {
  24. Code = code,
  25. Text = message.Text,
  26. FileNames = message.FileNames.Select(x => x.Name).ToList(),
  27. Anonymous = message.Anonymous
  28. },
  29. Share = share,
  30. IsValid = message.IsValid
  31. };
  32. if (model.IsValid)
  33. {
  34. if (message.ExpiryDate != null)
  35. {
  36. model.TimeLeft = message.ExpiryDate - DateTime.UtcNow;
  37. if (message.ExpiryDate <= DateTime.UtcNow)
  38. {
  39. await _messageService.InvalidateMessage(message.Id);
  40. model.IsValid = false;
  41. }
  42. }
  43. else
  44. {
  45. //ONE_TIME sharing: make the message invalid now so that it can't be accessed next time
  46. if (share is null or false) await _messageService.InvalidateMessage(message.Id);
  47. }
  48. }
  49. else
  50. {
  51. Directory.Delete(Path.Combine(_basePath, message.Code.ToString()), true);
  52. await _messageService.Delete(message.Id);
  53. }
  54. }
  55. catch (Exception _)
  56. {
  57. model = new LinkModel { IsValid = false };
  58. }
  59. return model;
  60. }
  61. public async Task<LinkModel> PrepareLinkAnonymous(Guid code, bool? share)
  62. {
  63. var model = await PrepareLinkVM(code, share);
  64. return model.MessageModel.Anonymous ? model : new LinkModel { IsValid = false };
  65. }
  66. //public MessageModel PrepareMessageVM(MessageDto message)
  67. //{
  68. // throw new System.NotImplementedException();
  69. //}
  70. }