Ejemplo n.º 1
0
        public async Task <AttachmentResponse> AddMessageAttachmentAsync(AddMessageAttachmentRequest request)
        {
            var message = await UnitOfWork.MessageRepository.GetMessageByIdAsync(request.MessageId);

            Ensure.That(message).WithException(x => new NotFoundException(new ErrorDto(ErrorCode.NotFound, "Message does not exist."))).IsNotNull();
            var messageAttachments = await UnitOfWork.AttachmentRepository.GetMessageAttachmentsAsync(message.Id);

            if (messageAttachments.Count == _attachmentConfiguration.Limit)
            {
                throw new LimitedAttachmentsException(new ErrorDto(ErrorCode.LimmitedAttachmentsError, "Attachments count is limited."));
            }

            var attachment = new Domain.Attachment.Attachment
            {
                Id          = Guid.NewGuid(),
                ContentType = request.ContentType,
                Created     = DateTimeOffset.UtcNow,
                FileName    = Guid.NewGuid() + "." + request.Extension,
                MessageId   = request.MessageId,
                Size        = request.Size
            };

            // Save attachment in database
            await UnitOfWork.AttachmentRepository.AddAttachmentAsync(attachment);

            // Save attachment in blob storage
            await _contentStorage.SaveContentAsync(attachment.FileName, request.Content, _cloudStorageConfiguration.MessageAttachmentsContainer);

            return(attachment.ToAttachmentResponse());
        }
        private async Task <Uri> SavedContentAsync(string fileName)
        {
            Uri    savedUrl;
            string binaryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.RelativeSearchPath ?? "");
            string filePath   = Path.Combine(binaryPath, "Resources", fileName);

            using (var stream = File.Open(filePath, FileMode.Open))
            {
                savedUrl = await _contentStorage.SaveContentAsync(fileName, stream, _containerName);
            }

            return(savedUrl);
        }
Ejemplo n.º 3
0
        public async Task <string> UploadAttachmentAsync(UploadQuestionAttachmentRequest model)
        {
            var question = UnitOfWork.QuestionRepository.GetById(model.QuestionId);

            try
            {
                await DeleteAttachmentAsync(model.QuestionId);
            }
            catch (ContentStorageException ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
            var fileName = Guid.NewGuid() + "." + model.Extention;

            await _contentStorage.SaveContentAsync(fileName, model.Image, PhotoContainerName);

            question.Image = fileName;
            UnitOfWork.QuestionRepository.Update(question);
            await UnitOfWork.SaveChangesAsync();

            return(fileName);
        }