CS2-55: Moved file saving logic from File controller to service

This commit is contained in:
KharchenkoVV 2021-08-19 16:32:04 +05:00
parent e45a693c92
commit bf823e1825
3 changed files with 24 additions and 6 deletions

View File

@ -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<PaginationContainer<FileInfoDto>> GetFilesInfoAsync(int idWell,
int idCategory, DateTime begin, DateTime end,
int skip, int take, CancellationToken token = default);

View File

@ -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<PaginationContainer<FileInfoDto>> GetFilesInfoAsync(int idWell,
int idCategory, DateTime begin = default, DateTime end = default,
int skip = 0, int take = 32, CancellationToken token = default)

View File

@ -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();