Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ModelFactory.cs 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. },
  28. Share = share,
  29. IsValid = message.IsValid
  30. };
  31. if (model.IsValid)
  32. {
  33. if (message.ExpiryDate != null)
  34. {
  35. model.TimeLeft = message.ExpiryDate - DateTime.UtcNow;
  36. if (message.ExpiryDate <= DateTime.UtcNow)
  37. {
  38. await _messageService.InvalidateMessage(message.Id);
  39. model.IsValid = false;
  40. }
  41. }
  42. else
  43. {
  44. //ONE_TIME sharing: make the message invalid now so that it can't be accessed next time
  45. if (share is null or false) await _messageService.InvalidateMessage(message.Id);
  46. }
  47. }
  48. else
  49. {
  50. Directory.Delete(Path.Combine(_basePath, message.Code.ToString()), true);
  51. await _messageService.Delete(message.Id);
  52. }
  53. }
  54. catch (Exception _)
  55. {
  56. model = new LinkModel { IsValid = false };
  57. }
  58. return model;
  59. }
  60. //public MessageModel PrepareMessageVM(MessageDto message)
  61. //{
  62. // throw new System.NotImplementedException();
  63. //}
  64. }