private async Task <bool> ValidateMediaAttachment(AttachmentVm mediaAttachment) { using (MessengerDbContext context = contextFactory.Create()) { if (mediaAttachment.Payload is string) { return(await context.FilesInfo .AnyAsync(file => file.Id == (string)mediaAttachment.Payload && file.Deleted == false).ConfigureAwait(false)); } else if (mediaAttachment.Payload is FileInfoVm fileInfo) { return(await context.FilesInfo .AnyAsync(file => file.Id == fileInfo.FileId && file.Deleted == false).ConfigureAwait(false)); } if (mediaAttachment.Payload is JObject jsonObject) { var fileMessage = jsonObject.ToObject <FileInfoVm>(); return(!string.IsNullOrEmpty(fileMessage.FileId)); } else { return(false); } } }
public IActionResult NewAttachment(IFormFile file, AttachmentVm model) { if (file is null || !ModelState.IsValid) { return(BadRequest()); } model.FileName = file.FileName; model.FileType = file.ContentType; var entity = AttachmentVm.MapToEntityModel(model); using var memoryStream = new MemoryStream(); file.CopyTo(memoryStream); IValidationResult upload; try { upload = _attachmentBlProvider.UploadAttachment(memoryStream, entity); } catch (Exception e) { return(Conflict(e.ToString())); } if (!upload.IsValid) { return(Conflict()); // ModelState.AddModelError("File error", upload.Message); } return(RedirectToAction("Index", "Attachment")); }
private async Task <bool> ValidateVoiceMessageAttachmentAsync(AttachmentVm attachment) { using (MessengerDbContext context = contextFactory.Create()) { if (attachment.Payload is string) { var existingFile = await context.FilesInfo .FirstOrDefaultAsync(file => file.Id == (string)attachment.Payload && file.Deleted == false).ConfigureAwait(false); return(existingFile?.Size <= 1024 * 1024 * 2); } else if (attachment.Payload is FileInfoVm fileInfo) { var existingFile = await context.FilesInfo .FirstOrDefaultAsync(file => file.Id == fileInfo.FileId && file.Deleted == false).ConfigureAwait(false); return(existingFile?.Size <= 1024 * 1024 * 2); } if (attachment.Payload is JObject jsonObject) { var fileMessage = jsonObject.ToObject <FileInfoVm>(); return(!string.IsNullOrEmpty(fileMessage.FileId) && fileMessage.Size.GetValueOrDefault() <= 1024 * 1024 * 2); } else { return(false); } } }
public async Task <ActionResult <Attachment> > Attachment(AttachmentVm vm) { if (vm is null) { return(BadRequest()); } var attachment = new Attachment() { InvoiceId = vm.InvoiceId, File = new SWENAR.Models.File() { Name = vm.File.FileName, ContentType = vm.File.ContentType, FileData = new FileData() { Data = FileHelper.ReadFully(vm.File.OpenReadStream()) } } }; _db.Attachments.Add(attachment); if (await _db.SaveChangesAsync() > 0) { return(CreatedAtAction(nameof(Attachment), new { id = attachment.Id }, new Attachment() { Id = attachment.Id })); } return(StatusCode(500)); }
public static AttachmentDto GetAttachment(AttachmentVm attachment) { if (attachment.Payload == null) { return(null); } object payload = attachment.Payload; AttachmentDto attachmentDto = new AttachmentDto { Type = attachment.Type, Hash = attachment.Hash ?? Array.Empty <byte>() }; if (payload is string strPayload) { attachmentDto.Payload = strPayload; } else if (payload is PollVm pollPayload) { attachmentDto.Payload = ObjectSerializer.ObjectToJson(new PollAttachmentInformation { ConversationId = pollPayload.ConversationId.GetValueOrDefault(), ConversationType = pollPayload.ConversationType.GetValueOrDefault(), PollId = pollPayload.PollId.GetValueOrDefault() }); } else { attachmentDto.Payload = ObjectSerializer.ObjectToJson(attachment.Payload); } return(attachmentDto); }
public IActionResult DeleteAttachment(AttachmentVm model) { var entityModel = AttachmentVm.MapToEntityModel(model); _attachmentBlProvider.DeleteAttachment(entityModel); return(RedirectToAction("Index", "Attachment")); }
private async Task DownloadPollAttachmentAsync(AttachmentVm attachment, NodeConnection connection) { if (attachment.Payload is PollVm pollVm) { PollDto pollDto = await _nodeRequestSender.GetPollInformationAsync( pollVm.ConversationId.Value, pollVm.ConversationType.Value, pollVm.PollId.Value, connection).ConfigureAwait(false); await _pollsService.SavePollAsync(pollDto).ConfigureAwait(false); } }
public IActionResult NewAttachment() { ViewBag.Customers = _settingsBlProvider.GetCustomers() .Select(t => new SelectModel() { label = t.Name, value = t.Id }).ToList(); var deliveryVm = new AttachmentVm(); return(PartialView("_NewAttachment", deliveryVm)); }
private bool ValidateKeyAttachment(AttachmentVm attachment) { if (attachment.Payload is string strKey) { if (ObjectSerializer.TryDeserializeJson <KeyExchangeMessageVm>(strKey, out var keyMessage)) { return(!keyMessage.EncryptedData.IsNullOrEmpty()); } } if (attachment.Payload is JObject jsonObject) { var keyMessage = jsonObject.ToObject <KeyExchangeMessageVm>(); return(!keyMessage.EncryptedData.IsNullOrEmpty()); } if (attachment.Payload is KeyExchangeMessageVm keyExchangeMessage) { return(!keyExchangeMessage.EncryptedData.IsNullOrEmpty()); } return(false); }
private async Task <bool> HandleAndValidatePollAttachmentAsync(AttachmentVm attachment, MessageVm message) { try { PollVm pollAttachment; if (attachment.Payload.GetType() == typeof(string)) { pollAttachment = ObjectSerializer.JsonToObject <PollVm>((string)attachment.Payload); } else { pollAttachment = ObjectSerializer.JsonToObject <PollVm>(ObjectSerializer.ObjectToJson(attachment.Payload)); } bool isValid = !string.IsNullOrWhiteSpace(pollAttachment.Title) && pollAttachment.Title.Length < 100 && pollAttachment.PollOptions != null && pollAttachment.PollOptions.All(opt => !string.IsNullOrWhiteSpace(opt.Description)); if (isValid) { pollAttachment = await PollConverter.InitPollConversationAsync(pollAttachment, message).ConfigureAwait(false); if (pollAttachment.PollId == null || pollAttachment.PollId == Guid.Empty) { pollAttachment.PollId = RandomExtensions.NextGuid(); } attachment.Payload = pollAttachment; await _pollsService.SavePollAsync(PollConverter.GetPollDto(pollAttachment, message.SenderId.GetValueOrDefault())).ConfigureAwait(false); return(true); } return(false); } catch (Exception ex) { Logger.WriteLog(ex); return(false); } }
private async Task DownloadAttachmentAsync(AttachmentVm attachment, NodeConnection connection) { switch (attachment.Type) { case AttachmentType.Audio: case AttachmentType.File: case AttachmentType.Picture: case AttachmentType.Video: case AttachmentType.VoiceMessage: case AttachmentType.VideoMessage: { await DownloadMediaAttachmentAsync(attachment, connection).ConfigureAwait(false); } break; case AttachmentType.Poll: { await DownloadPollAttachmentAsync(attachment, connection).ConfigureAwait(false); } break; } }
private async Task DownloadMediaAttachmentAsync(AttachmentVm attachment, NodeConnection connection) { FileInfoVm fileInfoPayload = (FileInfoVm)attachment.Payload; if (fileInfoPayload == null) { return; } string fileId = fileInfoPayload.FileId; FileInfo fileInfo = await _filesService.GetFileInfoAsync(fileId).ConfigureAwait(false); if (fileInfo != null && string.IsNullOrWhiteSpace(fileInfo.Url)) { try { await _nodeRequestSender.DownloadFileNodeRequestAsync(fileId, connection).ConfigureAwait(false); } catch (DownloadFileException ex) { Logger.WriteLog(ex); } } }
private async Task <bool> ValidateForwardedMessagesAttachment(AttachmentVm forwardedMessagesAttachment, long?userId, bool anotherNodeMessage) { ForwardedMessagesInfo fMessagesInfo = null; if (forwardedMessagesAttachment.Payload is string) { fMessagesInfo = ObjectSerializer.JsonToObject <ForwardedMessagesInfo>(forwardedMessagesAttachment.Payload.ToString()); } else if (forwardedMessagesAttachment.Payload is List <MessageVm> messages) { if (!messages.Any()) { return(true); } var message = messages.FirstOrDefault(); if (anotherNodeMessage) { await _createMessagesService.SaveForwardedMessagesAsync(MessageConverter.GetMessagesDto(messages)).ConfigureAwait(false); forwardedMessagesAttachment.Payload = new ForwardedMessagesInfo( messages.Select(opt => opt.GlobalId.Value), message.ConversationType == ConversationType.Dialog ? (await _loadDialogsService.GetDialogsIdByUsersIdPairAsync(message.SenderId.Value, message.ReceiverId.Value).ConfigureAwait(false)).FirstOrDefault() : message.ConversationId, message.ConversationType); return(true); } switch (message.ConversationType) { case ConversationType.Dialog: { var dialogsId = await _loadDialogsService.GetDialogsIdByUsersIdPairAsync(message.SenderId.Value, message.ReceiverId.Value).ConfigureAwait(false); if (dialogsId.Any()) { fMessagesInfo = new ForwardedMessagesInfo(messages.Select(opt => opt.GlobalId.Value), dialogsId[0], ConversationType.Dialog); } else { fMessagesInfo = new ForwardedMessagesInfo(messages.Select(opt => opt.GlobalId.Value), null, ConversationType.Dialog); } } break; case ConversationType.Chat: { var chat = await _loadChatsService.GetChatByIdAsync(message.ConversationId.GetValueOrDefault()).ConfigureAwait(false); if (chat.Type == ChatType.Private) { return(false); } } break; case ConversationType.Channel: fMessagesInfo = new ForwardedMessagesInfo(messages.Select(opt => opt.GlobalId.Value), message.ConversationId.GetValueOrDefault(), message.ConversationType); break; } } else { fMessagesInfo = ObjectSerializer.JsonToObject <ForwardedMessagesInfo>(ObjectSerializer.ObjectToJson(forwardedMessagesAttachment.Payload)); } if (fMessagesInfo == null) { return(false); } return(await _loadMessagesService.CanUserGetMessageAsync(fMessagesInfo.ConversationType, fMessagesInfo.ConversationId, userId).ConfigureAwait(false)); }
public IActionResult DeleteAttachment(int attachmentId) { var customerVm = AttachmentVm.MapToViewModel(_attachmentBlProvider.GetSingle(attachmentId)); return(PartialView("_DeleteAttachment", customerVm)); }