Ejemplo n.º 1
0
        ProcessUploadFiles(ICollection <IFormFile> files, string albumName)
        {
            (Album Album, ICollection <UploadImage> FailedFiles)result =
                (Album : null, FailedFiles : new List <UploadImage>());

            result.Album = GetAlbumByName(albumName);
            if (result.Album is null)
            {
                return(result);
            }

            var albumPath = Path.Combine(_uploadImageRootPath, result.Album.Name);

            if (!Directory.Exists(albumPath))
            {
                Directory.CreateDirectory(albumPath);
            }

            var processedFiles = new List <UploadImage>();

            foreach (var file in files)
            {
                var processedFile = new UploadImage(file, file.FileName, _settings.AlbumRootPath, result.Album.Name);

                var processedFilePath = processedFile.GetProcessedFilePath(albumPath);
                CheckFileExistence(albumPath, processedFilePath, processedFile);

                try
                {
                    using (var fileStream = new FileStream(processedFilePath, FileMode.Create, FileAccess.Write))
                    {
                        await file.CopyToAsync(fileStream);
                    }
                    processedFile.MarkAsSucceeded();

                    await _imageProcessService.ResizeByHeightAsync(processedFilePath,
                                                                   processedFile.GetThumbnailFilePath(albumPath),
                                                                   _settings.UploadImageThumbnailHeight);

                    await _imageProcessService.ResizeByHeightAsync(processedFilePath,
                                                                   processedFile.GetIconFilePath(albumPath),
                                                                   _settings.UploadImageIconHeight);

                    processedFiles.Add(processedFile);
                }
                catch (Exception e)
                {
                    _logger.LogError(e, e.Message);
                    processedFile.MarkAsFailed();
                    result.FailedFiles.Add(processedFile);
                }
            }

            result.Album.WithUploadImages(processedFiles);
            _albumRepository.UpdateAlbum(result.Album);

            return(result);
        }
Ejemplo n.º 2
0
        public async Task <ProcessResult <Post> > SaveUploadImagesAsync(Guid postKey, ICollection <IFormFile> files)
        {
            var processResult = _repository.GetPostByKey(postKey);

            if (!processResult.IsOk)
            {
                return(processResult);
            }

            var post       = processResult.Data;
            var postImages = new List <PostImage>();

            foreach (var file in files)
            {
                var postImage = new PostImage
                {
                    Extension = Path.GetExtension(file.FileName)
                }.EnsureValidKey();

                var filePath = GetPostImagePath(postImage.FilePath);
                using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                {
                    await file.CopyToAsync(fileStream);
                }
                var thumbPath = Path.Combine(_uploadImageRootPath, postImage.ThumbPath);
                await _imageProcessService.ResizeByHeightAsync(filePath, thumbPath, _settings.ThumbHeight);

                postImages.Add(postImage);
            }

            post.WithPostImages(postImages);
            processResult = _repository.UpsertPost(post);
            return(processResult);
        }