From bf823e18258a23ffd7ca5330047420ca15f44ac9 Mon Sep 17 00:00:00 2001 From: KharchenkoVV Date: Thu, 19 Aug 2021 16:32:04 +0500 Subject: [PATCH] CS2-55: Moved file saving logic from File controller to service --- AsbCloudApp/Services/IFileService.cs | 4 ++++ AsbCloudInfrastructure/Services/FileService.cs | 17 ++++++++++++++++- AsbCloudWebApi/Controllers/FileController.cs | 9 ++++----- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/AsbCloudApp/Services/IFileService.cs b/AsbCloudApp/Services/IFileService.cs index 9a293d01..4c334401 100644 --- a/AsbCloudApp/Services/IFileService.cs +++ b/AsbCloudApp/Services/IFileService.cs @@ -1,5 +1,6 @@ using AsbCloudApp.Data; using System; +using System.IO; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -13,6 +14,9 @@ namespace AsbCloudApp.Services int idCategory, IEnumerable<(string fileName, int idWell, int idCategory, DateTime date, int idUser)> filesInfo); + Task SaveFile(int idWell, int idCategory, int fileId, + string fileExtension, Stream fileStream); + Task> GetFilesInfoAsync(int idWell, int idCategory, DateTime begin, DateTime end, int skip, int take, CancellationToken token = default); diff --git a/AsbCloudInfrastructure/Services/FileService.cs b/AsbCloudInfrastructure/Services/FileService.cs index 7e560160..b4f60d3e 100644 --- a/AsbCloudInfrastructure/Services/FileService.cs +++ b/AsbCloudInfrastructure/Services/FileService.cs @@ -8,6 +8,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using System.IO; namespace AsbCloudInfrastructure.Services { @@ -29,7 +30,7 @@ namespace AsbCloudInfrastructure.Services foreach (var fileInfo in filesInfo) { - var file = new FileInfo() + var file = new AsbCloudDb.Model.FileInfo() { Name = fileInfo.fileName, IdWell = fileInfo.idWell, @@ -46,6 +47,20 @@ namespace AsbCloudInfrastructure.Services return fileIdsToNames; } + public async Task SaveFile(int idWell, int idCategory, int fileId, + string fileExtension, Stream fileStream) + { + var relativePath = Path.Combine(RootPath, $"{idWell}", + $"{idCategory}", $"{fileId}" + $"{fileExtension}"); + + Directory.CreateDirectory(Path.GetDirectoryName(relativePath)); + + using (var newfileStream = new FileStream(relativePath, FileMode.Create)) + { + await fileStream.CopyToAsync(newfileStream); + } + } + public async Task> GetFilesInfoAsync(int idWell, int idCategory, DateTime begin = default, DateTime end = default, int skip = 0, int take = 32, CancellationToken token = default) diff --git a/AsbCloudWebApi/Controllers/FileController.cs b/AsbCloudWebApi/Controllers/FileController.cs index b8f55f40..c84065c8 100644 --- a/AsbCloudWebApi/Controllers/FileController.cs +++ b/AsbCloudWebApi/Controllers/FileController.cs @@ -54,18 +54,17 @@ namespace AsbCloudWebApi.Controllers var fileNamesAndIds = fileService.SaveFilesPropertiesToDb(idWell, idCategory, fileInfoCollection); + foreach (var file in files) { var fileExtension = Path.GetExtension(file.FileName); var fileId = fileNamesAndIds[file.FileName]; - var relativePath = Path.Combine(fileService.RootPath, $"{idWell}", - $"{idCategory}", $"{fileId}" + $"{fileExtension}"); + var fileStream = file.OpenReadStream(); - Directory.CreateDirectory(Path.GetDirectoryName(relativePath)); - using var fileStream = new FileStream(relativePath, FileMode.Create); - file.CopyTo(fileStream); + await fileService.SaveFile(idWell, idCategory, fileId, + fileExtension, fileStream); } return Ok();