#6385536 Удаление ненужных файлов

This commit is contained in:
ai.astrakhantsev 2022-10-11 08:28:37 +05:00
parent 01e07a56ac
commit dbe5618a56
8 changed files with 263 additions and 74 deletions

View File

@ -1,4 +1,5 @@
using AsbCloudApp.Data;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using System;
using System.Collections.Generic;
@ -7,6 +8,7 @@ using System.Threading.Tasks;
namespace AsbCloudApp.Repositories
{
#nullable enable
/// <summary>
/// Сервис доступа к файлам
/// </summary>
@ -24,19 +26,10 @@ namespace AsbCloudApp.Repositories
/// <summary>
/// Получить список файлов в контейнере
/// </summary>
/// <param name="idWell"></param>
/// <param name="idCategory"></param>
/// <param name="companyName"></param>
/// <param name="fileName"></param>
/// <param name="begin"></param>
/// <param name="end"></param>
/// <param name="skip"></param>
/// <param name="take"></param>
/// <param name="request"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<PaginationContainer<FileInfoDto>> GetInfosAsync(int idWell,
int idCategory, string companyName = default, string fileName = default, DateTime begin = default,
DateTime end = default, int skip = 0, int take = 32, CancellationToken token = default);
Task<PaginationContainer<FileInfoDto>> GetInfosAsync(FileServiceRequest request, CancellationToken token = default);
/// <summary>
/// Пометить файл как удаленный
@ -90,9 +83,10 @@ namespace AsbCloudApp.Repositories
/// <summary>
/// Получение файлов по скважине
/// </summary>
/// <param name="idWell"></param>
/// <param name="request"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<FileInfoDto>> GetInfosByWellIdAsync(int idWell, CancellationToken token);
Task<IEnumerable<FileInfoDto>> GetInfosByWellIdAsync(FileServiceRequest request, CancellationToken token);
}
#nullable disable
}

View File

@ -1,9 +1,12 @@
using System.IO;
using AsbCloudApp.Data;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudApp.Repositories
{
#nullable enable
/// <summary>
/// Репозиторий хранения фалов
/// </summary>
@ -47,5 +50,31 @@ namespace AsbCloudApp.Repositories
/// <param name="fileName"></param>
/// <returns></returns>
bool FileExists(string fullPath, string fileName);
/// <summary>
/// Удаление всех файлов с диска о которых нет информации в базе
/// </summary>
/// <param name="idWell"></param>
/// <param name="idsFiles"></param>
int DeleteFilesNotExistStorage(int idWell, IEnumerable<int> idsFiles);
/// <summary>
/// Вывод списка всех файлов из базы, для которых нет файла на диске
/// </summary>
/// <param name="idWell"></param>
/// <param name="files"></param>
/// <returns></returns>
IEnumerable<FileInfoDto> GetListFilesNotDisc(int idWell, IEnumerable<FileInfoDto> files);
/// <summary>
/// Получение пути к файлу
/// </summary>
/// <param name="idWell"></param>
/// <param name="idCategory"></param>
/// <param name="fileFullName"></param>
/// <param name="fileId"></param>
/// <returns></returns>
string MakeFilePath(int idWell, int idCategory, string fileFullName, int fileId);
}
#nullable disable
}

View File

@ -0,0 +1,60 @@
using System;
using System.ComponentModel.DataAnnotations;
namespace AsbCloudApp.Requests
{
#nullable enable
/// <summary>
/// Параметры запроса для файлового сервиса
/// </summary>
public class FileServiceRequest : RequestBase
{
/// <summary>
/// Идентификатор скважины
/// </summary>
[Required]
public int? IdWell { get; set; }
/// <summary>
/// Идентификатор категории файла
/// </summary>
[Required]
public int? IdCategory { get; set; }
/// <summary>
/// Наименование компании
/// </summary>
public string? CompanyName { get; set; }
/// <summary>
/// Имя файла
/// </summary>
public string? FileName { get; set; }
/// <summary>
/// Дата начала периода
/// </summary>
public DateTime? Begin { get; set; }
/// <summary>
/// Дата окончания периода
/// </summary>
public DateTime? End { get; set; }
/// <summary>
/// Идентификатор файла
/// </summary>
public int? IdFile { get; set; }
/// <summary>
/// Идентификатор отметки
/// </summary>
public int? IdMark { get; set; }
/// <summary>
/// Признак удаления
/// </summary>
public bool? IsDeleted { get; set; }
}
#nullable disable
}

View File

@ -1,5 +1,6 @@
using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using System;
using System.Collections.Generic;
using System.IO;
@ -9,6 +10,7 @@ using System.Threading.Tasks;
namespace AsbCloudApp.Services
{
#nullable enable
/// <summary>
/// Сервис доступа к файлам
/// </summary>
@ -56,7 +58,7 @@ namespace AsbCloudApp.Services
var fileId = await fileRepository.InsertAsync(dto, token)
.ConfigureAwait(false);
string filePath = MakeFilePath(idWell, idCategory, destinationFileName, fileId);
string filePath = fileStorageRepository.MakeFilePath(idWell, idCategory, destinationFileName, fileId);
fileStorageRepository.MoveFile(srcFilePath, filePath);
return await GetInfoAsync(fileId, token);
@ -89,7 +91,7 @@ namespace AsbCloudApp.Services
.ConfigureAwait(false);
//save stream to disk
string filePath = MakeFilePath(idWell, idCategory, fileFullName, fileId);
string filePath = fileStorageRepository.MakeFilePath(idWell, idCategory, fileFullName, fileId);
await fileStorageRepository.CopyFileAsync(filePath, fileStream, token);
return await GetInfoAsync(fileId, token);
@ -215,20 +217,11 @@ namespace AsbCloudApp.Services
/// <summary>
/// Получить список файлов в контейнере
/// </summary>
/// <param name="idWell"></param>
/// <param name="idCategory"></param>
/// <param name="companyName"></param>
/// <param name="fileName"></param>
/// <param name="begin"></param>
/// <param name="end"></param>
/// <param name="skip"></param>
/// <param name="take"></param>
/// <param name="request"></param>
/// <param name="token"></param>
/// <returns></returns>
public async Task<PaginationContainer<FileInfoDto>> GetInfosAsync(int idWell,
int idCategory, string companyName = default, string fileName = default, DateTime begin = default,
DateTime end = default, int skip = 0, int take = 32, CancellationToken token = default)
=> await fileRepository.GetInfosAsync(idWell, idCategory, companyName, fileName, begin, end, skip, take, token)
public async Task<PaginationContainer<FileInfoDto>> GetInfosAsync(FileServiceRequest request, CancellationToken token)
=> await fileRepository.GetInfosAsync(request, token)
.ConfigureAwait(false);
/// <summary>
@ -275,7 +268,7 @@ namespace AsbCloudApp.Services
/// <summary>
/// получить инфо о файле по метке
/// </summary>
/// <param name="idMark"></param>
/// <param name="idsMarks"></param>
/// <param name="token"></param>
/// <returns></returns>
public async Task<int> MarkFileMarkAsDeletedAsync(IEnumerable<int> idsMarks, CancellationToken token)
@ -289,7 +282,7 @@ namespace AsbCloudApp.Services
/// <param name="token"></param>
/// <returns></returns>
public async Task<IEnumerable<FileInfoDto>> GetInfosByWellIdAsync(int idWell, CancellationToken token)
=> await fileRepository.GetInfosByWellIdAsync(idWell, token)
=> await fileRepository.GetInfosByWellIdAsync(new FileServiceRequest { IdWell = idWell, IsDeleted = false }, token)
.ConfigureAwait(false);
/// <summary>
@ -303,10 +296,58 @@ namespace AsbCloudApp.Services
=> await fileRepository.GetInfosByCategoryAsync(idWell, idCategory, token)
.ConfigureAwait(false);
private string MakeFilePath(int idWell, int idCategory, string fileFullName, int fileId)
/// <summary>
/// Удаление всех файлов по скважине помеченных как удаленные
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
public async Task<int> DeleteFilesFromDbMarkedDeletionByIdWell(int idWell, CancellationToken token)
{
return Path.Combine(fileStorageRepository.RootPath, $"{idWell}",
$"{idCategory}", $"{fileId}" + $"{Path.GetExtension(fileFullName)}");
var files = await fileRepository.GetInfosByWellIdAsync(
new FileServiceRequest
{
IdWell = idWell,
IsDeleted = true
},
token);
var result = await DeleteAsync(files.Select(x => x.Id), token);
return result;
}
/// <summary>
/// Удаление всех файлов с диска о которых нет информации в базе
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
public async Task<int> DeleteFilesNotExistStorage(int idWell, CancellationToken token)
{
var files = await fileRepository.GetInfosByWellIdAsync(
new FileServiceRequest
{
IdWell = idWell
},
token);
var result = await Task.FromResult(fileStorageRepository.DeleteFilesNotExistStorage(idWell, files.Select(x => x.Id)));
return result;
}
/// <summary>
/// Вывод списка всех файлов из базы, для которых нет файла на диске
/// </summary>
/// <param name="idWell"></param>
/// <returns></returns>
public async Task<IEnumerable<FileInfoDto>> GetListFilesNotDisc(int idWell, CancellationToken token)
{
var files = await fileRepository.GetInfosByWellIdAsync(
new FileServiceRequest
{
IdWell = idWell
},
token);
var result = fileStorageRepository.GetListFilesNotDisc(idWell, files);
return result;
}
}
#nullable disable
}

View File

@ -1,5 +1,6 @@
using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore;
@ -12,6 +13,7 @@ using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository
{
#nullable enable
public class FileRepository : IFileRepository
{
private readonly IQueryable<AsbCloudDb.Model.FileInfo> dbSetConfigured;
@ -41,22 +43,23 @@ namespace AsbCloudInfrastructure.Repository
return dtos;
}
public async Task<PaginationContainer<FileInfoDto>> GetInfosAsync(int idWell,
int idCategory, string companyName = default, string fileName = default, DateTime begin = default,
DateTime end = default, int skip = 0, int take = 32, CancellationToken token = default)
public async Task<PaginationContainer<FileInfoDto>> GetInfosAsync(FileServiceRequest request, CancellationToken token = default)
{
var query = dbSetConfigured
.Where(e => e.IdWell == idWell &&
e.IdCategory == idCategory &&
.Where(e => e.IdWell == request.IdWell &&
e.IdCategory == request.IdCategory &&
!e.IsDeleted);
if (!string.IsNullOrEmpty(companyName))
if (request.CompanyName is not null)
query = query.Where(e => (e.Author == null) ||
(e.Author.Company == null) ||
e.Author.Company.Caption.Contains(companyName));
e.Author.Company.Caption.Contains(request.CompanyName));
if (!string.IsNullOrEmpty(fileName))
query = query.Where(e => e.Name.ToLower().Contains(fileName.ToLower()));
if (request.FileName is not null)
query = query.Where(e => e.Name.ToLower().Contains(request.FileName.ToLower()));
var skip = request.Skip ?? 0;
var take = request.Take ?? 32;
var firstFile = await query.FirstOrDefaultAsync(token);
if (firstFile is null)
@ -69,15 +72,15 @@ namespace AsbCloudInfrastructure.Repository
var timezoneOffset = firstFile.Well.Timezone?.Hours ?? 5;
if (begin != default)
if (request.Begin is not null)
{
var beginUtc = begin.ToUtcDateTimeOffset(timezoneOffset);
var beginUtc = request.Begin.Value.ToUtcDateTimeOffset(timezoneOffset);
query = query.Where(e => e.UploadDate >= beginUtc);
}
if (end != default)
if (request.End is not null)
{
var endUtc = end.ToUtcDateTimeOffset(timezoneOffset);
var endUtc = request.End.Value.ToUtcDateTimeOffset(timezoneOffset);
query = query.Where(e => e.UploadDate <= endUtc);
}
@ -201,10 +204,13 @@ namespace AsbCloudInfrastructure.Repository
return await db.SaveChangesAsync(token);
}
public async Task<IEnumerable<FileInfoDto>> GetInfosByWellIdAsync(int idWell, CancellationToken token)
public async Task<IEnumerable<FileInfoDto>> GetInfosByWellIdAsync(FileServiceRequest request, CancellationToken token)
{
var entities = await dbSetConfigured
.Where(e => e.IdWell == idWell && e.IsDeleted == false)
var query = dbSetConfigured.Where(e => e.IdWell == request.IdWell);
if (request.IsDeleted is not null)
query = query.Where(x => x.IsDeleted == request.IsDeleted);
var entities = await query
.AsNoTracking()
.ToListAsync(token)
.ConfigureAwait(false);
@ -302,4 +308,5 @@ namespace AsbCloudInfrastructure.Repository
throw new NotImplementedException();
}
}
#nullable disable
}

View File

@ -1,11 +1,17 @@
using AsbCloudApp.Exceptions;
using AsbCloudApp.Data;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FileInfo = System.IO.FileInfo;
namespace AsbCloudInfrastructure.Repository
{
#nullable enable
public class FileStorageRepository : IFileStorageRepository
{
public string RootPath { get; private set; }
@ -51,9 +57,68 @@ namespace AsbCloudInfrastructure.Repository
return true;
}
private void CreateDirectory(string filePath)
public string MakeFilePath(int idWell, int idCategory, string fileFullName, int fileId)
{
return Path.Combine(RootPath, $"{idWell}",
$"{idCategory}", $"{fileId}" + $"{Path.GetExtension(fileFullName)}");
}
public int DeleteFilesNotExistStorage(int idWell, IEnumerable<int> idsFiles)
{
var allFilesPath = GetFilesPath(idWell);
var resutl = 0;
foreach (var filePath in allFilesPath)
{
var idFile = Path.GetFileNameWithoutExtension(filePath);
if (!idsFiles.Any(x => x.ToString() == idFile))
{
File.Delete(filePath);
resutl++;
}
}
return resutl;
}
public IEnumerable<FileInfoDto> GetListFilesNotDisc(int idWell, IEnumerable<FileInfoDto> files)
{
var resutl = new List<FileInfoDto>();
var idsFilesStorage = GetIdsFiles(idWell);
foreach (var file in files)
{
if (!idsFilesStorage.Any(x => x == file.Id))
resutl.Add(file);
}
return resutl;
}
private IEnumerable<int> GetIdsFiles(int idWell)
{
var result = new List<int>();
var allFilesPath = GetFilesPath(idWell);
foreach (var filePath in allFilesPath)
{
var idFileStr = Path.GetFileNameWithoutExtension(filePath);
result.Add(Convert.ToInt32(idFileStr));
}
return result;
}
private IEnumerable<string> GetFilesPath(int idWell)
{
var path = Path.Combine(RootPath, $"{idWell}");
return Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
}
private static void CreateDirectory(string filePath)
{
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
}
}
#nullable disable
}

View File

@ -1,5 +1,6 @@
using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Repository;
@ -115,9 +116,9 @@ namespace AsbCloudWebApi.Tests.ServicesTests
return Task.FromResult(result);
});
repositoryMock.Setup(x => x.GetInfosByWellIdAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
.Returns((int idWell, CancellationToken token) => {
var data = Files.Where(x => x.IdWell == idWell);
repositoryMock.Setup(x => x.GetInfosByWellIdAsync(It.IsAny<FileServiceRequest>(), It.IsAny<CancellationToken>()))
.Returns((FileServiceRequest request, CancellationToken token) => {
var data = Files.Where(x => x.IdWell == request.IdWell);
return Task.FromResult(data);
});

View File

@ -1,4 +1,5 @@
using AsbCloudApp.Data;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
@ -12,6 +13,7 @@ using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
#nullable enable
/// <summary>
/// Хранение файлов
/// </summary>
@ -70,38 +72,27 @@ namespace AsbCloudWebApi.Controllers
/// <summary>
/// Возвращает информацию о файлах для скважины в выбраной категории
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="idCategory">id категории файла</param>
/// <param name="companyName">id компаний для фильтрации возвращаемых файлов</param>
/// <param name="fileName">часть имени файла для поиска</param>
/// <param name="begin">дата начала</param>
/// <param name="end">дата окончания</param>
/// <param name="skip">для пагинации кол-во записей пропустить</param>
/// <param name="take">для пагинации кол-во записей взять </param>
/// <param name="request"> </param>
/// <param name="token"> Токен отмены задачи </param>
/// <returns>Список информации о файлах в этой категории</returns>
[HttpGet]
[Route("/api/files")]
[Permission]
[ProducesResponseType(typeof(PaginationContainer<FileInfoDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetFilesInfoAsync(
[FromRoute] int idWell,
int idCategory = default,
string companyName = default,
string fileName = default,
DateTime begin = default,
DateTime end = default,
int skip = 0,
int take = 32,
[FromQuery] FileServiceRequest request,
CancellationToken token = default)
{
int? idCompany = User.GetCompanyId();
if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false))
if (request.IdWell is null || request.IdCategory is null || idCompany is null)
return Forbid();
var filesInfo = await fileService.GetInfosAsync(idWell, idCategory,
companyName, fileName, begin, end, skip, take, token).ConfigureAwait(false);
if (!await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value,
request.IdWell.Value, token).ConfigureAwait(false))
return Forbid();
var filesInfo = await fileService.GetInfosAsync(request, token).ConfigureAwait(false);
return Ok(filesInfo);
}
@ -254,4 +245,5 @@ namespace AsbCloudWebApi.Controllers
}
}
}
#nullable disable
}