Example #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);
        }