forked from ddrilling/AsbCloudServer
CS2-55: Moved file saving logic from File controller to service
This commit is contained in:
parent
e45a693c92
commit
bf823e1825
@ -1,5 +1,6 @@
|
|||||||
using AsbCloudApp.Data;
|
using AsbCloudApp.Data;
|
||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -13,6 +14,9 @@ namespace AsbCloudApp.Services
|
|||||||
int idCategory, IEnumerable<(string fileName, int idWell, int idCategory,
|
int idCategory, IEnumerable<(string fileName, int idWell, int idCategory,
|
||||||
DateTime date, int idUser)> filesInfo);
|
DateTime date, int idUser)> filesInfo);
|
||||||
|
|
||||||
|
Task SaveFile(int idWell, int idCategory, int fileId,
|
||||||
|
string fileExtension, Stream fileStream);
|
||||||
|
|
||||||
Task<PaginationContainer<FileInfoDto>> GetFilesInfoAsync(int idWell,
|
Task<PaginationContainer<FileInfoDto>> GetFilesInfoAsync(int idWell,
|
||||||
int idCategory, DateTime begin, DateTime end,
|
int idCategory, DateTime begin, DateTime end,
|
||||||
int skip, int take, CancellationToken token = default);
|
int skip, int take, CancellationToken token = default);
|
||||||
|
@ -8,6 +8,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
namespace AsbCloudInfrastructure.Services
|
namespace AsbCloudInfrastructure.Services
|
||||||
{
|
{
|
||||||
@ -29,7 +30,7 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
|
|
||||||
foreach (var fileInfo in filesInfo)
|
foreach (var fileInfo in filesInfo)
|
||||||
{
|
{
|
||||||
var file = new FileInfo()
|
var file = new AsbCloudDb.Model.FileInfo()
|
||||||
{
|
{
|
||||||
Name = fileInfo.fileName,
|
Name = fileInfo.fileName,
|
||||||
IdWell = fileInfo.idWell,
|
IdWell = fileInfo.idWell,
|
||||||
@ -46,6 +47,20 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
return fileIdsToNames;
|
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,
|
public async Task<PaginationContainer<FileInfoDto>> GetFilesInfoAsync(int idWell,
|
||||||
int idCategory, DateTime begin = default, DateTime end = default,
|
int idCategory, DateTime begin = default, DateTime end = default,
|
||||||
int skip = 0, int take = 32, CancellationToken token = default)
|
int skip = 0, int take = 32, CancellationToken token = default)
|
||||||
|
@ -54,18 +54,17 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
var fileNamesAndIds = fileService.SaveFilesPropertiesToDb(idWell,
|
var fileNamesAndIds = fileService.SaveFilesPropertiesToDb(idWell,
|
||||||
idCategory, fileInfoCollection);
|
idCategory, fileInfoCollection);
|
||||||
|
|
||||||
|
|
||||||
foreach (var file in files)
|
foreach (var file in files)
|
||||||
{
|
{
|
||||||
var fileExtension = Path.GetExtension(file.FileName);
|
var fileExtension = Path.GetExtension(file.FileName);
|
||||||
|
|
||||||
var fileId = fileNamesAndIds[file.FileName];
|
var fileId = fileNamesAndIds[file.FileName];
|
||||||
|
|
||||||
var relativePath = Path.Combine(fileService.RootPath, $"{idWell}",
|
var fileStream = file.OpenReadStream();
|
||||||
$"{idCategory}", $"{fileId}" + $"{fileExtension}");
|
|
||||||
|
|
||||||
Directory.CreateDirectory(Path.GetDirectoryName(relativePath));
|
await fileService.SaveFile(idWell, idCategory, fileId,
|
||||||
using var fileStream = new FileStream(relativePath, FileMode.Create);
|
fileExtension, fileStream);
|
||||||
file.CopyTo(fileStream);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok();
|
return Ok();
|
||||||
|
Loading…
Reference in New Issue
Block a user