Esempio n. 1
0
 public async Task UploadOrOverwriteImage(BlogPostImage image, IFormFile file)
 {
     using (var memoryStream = new MemoryStream())
     {
         file.OpenReadStream().CopyTo(memoryStream);
         var azureContainerName = GetContainerNameByImageType(image.ImageType).ToString();
         await UploadOrOverwriteFile(azureContainerName, image.Id.ToString(), memoryStream.ToArray());
     }
 }
        public async Task Execute(UploadBlogPostCommand command)
        {
            var postFile   = command.Files.FirstOrDefault(f => f.FileName.EndsWith(".md"));
            var imageFiles = command.Files.Where(f => !f.FileName.EndsWith(".md"));

            var postFileContent          = Encoding.UTF8.GetString(postFile.Data, 0, postFile.Data.Length);
            var processingMetadataResult = _postMetadataResolver.Resolve(postFileContent);
            var metadata = processingMetadataResult.Result;

            var date   = metadata.PublishedDate ?? DateTime.Now;
            var postId = metadata.Id;
            var tags   = _tagMapper.Map(metadata.Categories?.ToArray());

            var blogPost = new BlogPost
            {
                Id             = postId,
                Title          = metadata.Title,
                Content        = postFileContent,
                Header         = metadata.Header,
                PublishingDate = date,
                Status         = (metadata.PostType == BlogPostMetadata.PostTypeEnum.Article) ?
                                 BlogStatusTypeEnum.Active :
                                 BlogStatusTypeEnum.Page,
                Images = new List <BlogPostImage>(),
                Tags   = tags
            };

            foreach (var imageFile in imageFiles)
            {
                var fileName      = Path.GetFileNameWithoutExtension(imageFile.FileName);
                var blogPostImage = new BlogPostImage
                {
                    Id   = fileName,
                    Data = imageFile.Data
                };

                blogPost.Images.Add(blogPostImage);
            }

            var existingPost = await _blogRepository.GetBlogPostWithNoImages(postId);

            var isPostExists = existingPost != null;

            if (isPostExists)
            {
                await _blogRepository.DeleteBlogPost(postId);
            }

            await _blogRepository.Add(blogPost);
        }