valid file extension

This commit is contained in:
eugeniy_ivanov 2023-01-26 15:25:09 +05:00
parent 325c5054fe
commit b7f1962c6f

View File

@ -51,6 +51,8 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
private const int idStateReady = 3; private const int idStateReady = 3;
private const int idStateError = 4; private const int idStateError = 4;
private static readonly string[] filePermittedExt = { ".xlsx", ".xls", ".ods", ".odt", ".doc", ".docx", ".pdf" };
public DrillingProgramService( public DrillingProgramService(
IAsbCloudDbContext context, IAsbCloudDbContext context,
FileService fileService, FileService fileService,
@ -161,13 +163,23 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
await EnqueueMakeProgramWorkAsync(idWell, state, token); await EnqueueMakeProgramWorkAsync(idWell, state, token);
return state; return state;
} }
private bool ValidExtFile(string file)
{
var fileExt = Path.GetExtension(file);
var badExt = Array.Find(filePermittedExt, e => e == fileExt);
if (badExt is null)
return false;
return true;
}
public async Task<int> AddFile(int idWell, int idFileCategory, int idUser, string fileFullName, System.IO.Stream fileStream, CancellationToken token = default) public async Task<int> AddFile(int idWell, int idFileCategory, int idUser, string fileFullName, System.IO.Stream fileStream, CancellationToken token = default)
{ {
if (!ValidExtFile(fileFullName))
throw new FileFormatException($"Файл {fileFullName} - неподдерживаемого формата. Файл не может быть загружен. ");
var part = await context.DrillingProgramParts var part = await context.DrillingProgramParts
.Include(p => p.RelatedUsers) .Include(p => p.RelatedUsers)
.ThenInclude(r => r.User) .ThenInclude(r => r.User)
.FirstOrDefaultAsync(p => p.IdWell == idWell && p.IdFileCategory == idFileCategory, token); .FirstOrDefaultAsync(p => p.IdWell == idWell && p.IdFileCategory == idFileCategory, token);
if (part == null) if (part == null)
throw new ArgumentInvalidException($"DrillingProgramPart id == {idFileCategory} does not exist", nameof(idFileCategory)); throw new ArgumentInvalidException($"DrillingProgramPart id == {idFileCategory} does not exist", nameof(idFileCategory));
@ -465,6 +477,8 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
return part; return part;
} }
private async Task EnqueueMakeProgramWorkAsync(int idWell, DrillingProgramStateDto state, CancellationToken token) private async Task EnqueueMakeProgramWorkAsync(int idWell, DrillingProgramStateDto state, CancellationToken token)
{ {
if (state.IdState == idStateCreating) if (state.IdState == idStateCreating)