public async Task AddMessageAttachmentAsync(AddMessageAttachmentRequest request) { try { var member = await _memberService.GetMemberSummaryBySaasUserIdAsync(request.SaasUserId); var message = await _messageService.GetMessageByIdAsync(request.MessageId); if (message.Sender.Id != member.Id) { throw new Exception(String.Format(LanguageResources.Msg_AccessPermission, message.Id)); } var attachmentsCount = await _messageService.GetMessageAttachmentsCount(message.Id); if (attachmentsCount == 10) { throw new Exception(LanguageResources.Msg_LimitedAttachments); } await _messageService.AddMessageAttachmentAsync(request); await _messageNotificationHub.OnAddMessageAttachment(member, message); } catch (ServiceException ex) { if (ex.Errors.Any(x => x.Description == "Message does not exist.")) { _logger.Event(PropertyNames.EventId).With.Message("Exception: Message does not exist. MessageId: {messageId}", request.MessageId).Exception(ex).AsError(); throw new Exception(String.Format(LanguageResources.Msg_NotFound, request.MessageId)); } } }
public async Task <IActionResult> AddMessageAttachmentAsync(Guid messageId, IFormCollection model) { var userId = GetCurrentUserId(); if (model?.Files == null) { return(BadRequest(new ErrorDto(ErrorCode.NotFound, "There is not photo"))); } var file = model.Files.Count == 0 ? null : model.Files.First(); if (file == null) { return(BadRequest(new ErrorDto(ErrorCode.NotFound, "There is no files in the request"))); } var type = AttachmentsUtils.GetExtentionFromMimeType(file.ContentType); if (type != "jpg" && type != "png" && type != "mp4") { return(BadRequest(new Exception("Only jpg, png, mp4 formats are supported."))); } if (file.Length > 0) { using (var stream = file.OpenReadStream()) { await _messageService.AddMessageAttachmentAsync(new AddMessageAttachmentRequest(userId, messageId, stream, type, file.ContentType, file.Length)); } } return(Ok()); }
public async Task <AttachmentResponse> AddMessageAttachmentAsync(AddMessageAttachmentRequest request) { var attachmentResponse = await _messageService.AddMessageAttachmentAsync(request); var message = await _messageService.GetMessageByIdAsync(request.MessageId); await _messageNotificationService.OnAddMessageAttachment(message.ChannelId); return(attachmentResponse); }