| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- using SecureSharing.Business.Interfaces;
- using SecureSharing.Models;
-
- namespace SecureSharing.Infrastructure;
-
- public sealed class ModelFactory : IModelFactory
- {
- private readonly IMessageService _messageService;
- private readonly string _basePath;
-
- public ModelFactory(IMessageService messageService, IWebHostEnvironment webHostEnvironment)
- {
- _basePath = Path.Combine(webHostEnvironment.WebRootPath.Split('/')[0], "files");
- _messageService = messageService;
- }
-
- public async Task<LinkModel> PrepareLinkVM(Guid code, bool? share)
- {
- //share is true when the link is created
-
- LinkModel model = null;
- try
- {
- var message = await _messageService.GetByCode(code);
-
- model = new LinkModel
- {
- MessageModel = new MessageModel
- {
- Code = code,
- Text = message.Text,
- FileNames = message.FileNames.Select(x => x.Name).ToList()
- },
- Share = share,
- IsValid = message.IsValid
- };
-
- if (model.IsValid)
- {
- if (message.ExpiryDate != null)
- {
- model.TimeLeft = message.ExpiryDate - DateTime.UtcNow;
- if (message.ExpiryDate <= DateTime.UtcNow)
- {
- await _messageService.InvalidateMessage(message.Id);
- model.IsValid = false;
- }
- }
- else
- {
- //ONE_TIME sharing: make the message invalid now so that it can't be accessed next time
- if (share is null or false) await _messageService.InvalidateMessage(message.Id);
- }
- }
- else
- {
- Directory.Delete(Path.Combine(_basePath, message.Code.ToString()), true);
- await _messageService.Delete(message.Id);
- }
- }
- catch (Exception _)
- {
- model = new LinkModel { IsValid = false };
- }
-
- return model;
- }
-
- //public MessageModel PrepareMessageVM(MessageDto message)
- //{
- // throw new System.NotImplementedException();
- //}
- }
|