2024-07-04 11:02:45 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2022-10-06 14:37:03 +05:00
|
|
|
|
using AsbCloudApp.Repositories;
|
2022-10-11 08:28:37 +05:00
|
|
|
|
using AsbCloudApp.Requests;
|
2021-08-09 15:41:42 +05:00
|
|
|
|
using System.Collections.Generic;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
using System.IO;
|
2021-08-09 15:41:42 +05:00
|
|
|
|
using System.Linq;
|
2021-08-11 16:54:42 +05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
namespace AsbCloudApp.Services;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сервис доступа к файлам
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class FileService
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
|
private readonly IFileRepository fileRepository;
|
|
|
|
|
private readonly IFileStorageRepository fileStorageRepository;
|
|
|
|
|
|
2022-10-06 14:37:03 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сервис доступа к файлам
|
|
|
|
|
/// </summary>
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <param name="fileRepository"></param>
|
|
|
|
|
/// <param name="fileStorageRepository"></param>
|
|
|
|
|
public FileService(IFileRepository fileRepository, IFileStorageRepository fileStorageRepository)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
|
this.fileRepository = fileRepository;
|
|
|
|
|
this.fileStorageRepository = fileStorageRepository;
|
|
|
|
|
}
|
2021-10-27 17:00:27 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// переместить файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell"></param>
|
|
|
|
|
/// <param name="idUser"></param>
|
|
|
|
|
/// <param name="idCategory"></param>
|
|
|
|
|
/// <param name="destinationFileName"></param>
|
|
|
|
|
/// <param name="srcFilePath"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<FileInfoDto?> MoveAsync(int idWell, int? idUser, int idCategory,
|
|
|
|
|
string destinationFileName, string srcFilePath, CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
destinationFileName = Path.GetFileName(destinationFileName);
|
|
|
|
|
srcFilePath = Path.GetFullPath(srcFilePath);
|
|
|
|
|
var fileSize = fileStorageRepository.GetFileLength(srcFilePath);
|
2021-09-23 11:55:25 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
//save info to db
|
|
|
|
|
var dto = new FileInfoDto {
|
|
|
|
|
IdWell = idWell,
|
|
|
|
|
IdAuthor = idUser,
|
|
|
|
|
IdCategory = idCategory,
|
|
|
|
|
Name = destinationFileName,
|
|
|
|
|
Size = fileSize
|
|
|
|
|
};
|
|
|
|
|
var fileId = await fileRepository.InsertAsync(dto, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
string filePath = fileStorageRepository.MakeFilePath(idWell, idCategory, destinationFileName, fileId);
|
|
|
|
|
fileStorageRepository.MoveFile(srcFilePath, filePath);
|
|
|
|
|
|
|
|
|
|
return await GetOrDefaultAsync(fileId, token);
|
|
|
|
|
}
|
2021-09-23 11:55:25 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сохранить файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell"></param>
|
|
|
|
|
/// <param name="idUser"></param>
|
|
|
|
|
/// <param name="idCategory"></param>
|
|
|
|
|
/// <param name="fileFullName"></param>
|
|
|
|
|
/// <param name="fileStream"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<FileInfoDto> SaveAsync(int idWell, int? idUser, int idCategory,
|
|
|
|
|
string fileFullName, Stream fileStream, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
//save info to db
|
|
|
|
|
var dto = new FileInfoDto
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
2024-08-19 10:01:07 +05:00
|
|
|
|
IdWell = idWell,
|
|
|
|
|
IdAuthor = idUser,
|
|
|
|
|
IdCategory = idCategory,
|
|
|
|
|
Name = Path.GetFileName(fileFullName),
|
|
|
|
|
Size = fileStream.Length
|
|
|
|
|
};
|
2022-09-30 10:49:40 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
var fileId = await fileRepository.InsertAsync(dto, token)
|
|
|
|
|
.ConfigureAwait(false);
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
//save stream to disk
|
|
|
|
|
string filePath = fileStorageRepository.MakeFilePath(idWell, idCategory, fileFullName, fileId);
|
|
|
|
|
await fileStorageRepository.SaveFileAsync(filePath, fileStream, token);
|
2022-01-05 17:50:45 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
return (await GetOrDefaultAsync(fileId, token))!;
|
|
|
|
|
}
|
2021-08-19 16:58:26 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// удалить файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idFile"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public Task<int> DeleteAsync(int idFile, CancellationToken token)
|
|
|
|
|
=> DeleteAsync(new int[] { idFile }, token);
|
2022-02-12 11:28:16 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// удалить файлы
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ids"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
if (ids is null || !ids.Any())
|
|
|
|
|
return 0;
|
2021-08-29 17:25:16 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
var files = await fileRepository.DeleteAsync(ids, token).ConfigureAwait(false);
|
2022-02-12 11:28:16 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
if (files is null || !files.Any())
|
|
|
|
|
return 0;
|
2021-08-29 17:25:16 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
var filesName = files.Select(x => GetUrl(x.IdWell, x.IdCategory, x.Id, Path.GetExtension(x.Name)));
|
|
|
|
|
fileStorageRepository.DeleteFiles(filesName);
|
2022-02-12 11:28:16 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
return files.Any() ? 1 : 0;
|
|
|
|
|
}
|
2021-08-29 17:25:16 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// получить путь для скачивания
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="fileInfo"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public string GetUrl(FileInfoDto fileInfo) =>
|
|
|
|
|
GetUrl(fileInfo.IdWell, fileInfo.IdCategory, fileInfo.Id, Path.GetExtension(fileInfo.Name));
|
2021-11-09 17:36:44 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// получить путь для скачивания
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell"></param>
|
|
|
|
|
/// <param name="idCategory"></param>
|
|
|
|
|
/// <param name="idFile"></param>
|
|
|
|
|
/// <param name="dotExtention"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public string GetUrl(int idWell, int idCategory, int idFile, string dotExtention) =>
|
|
|
|
|
fileStorageRepository.GetFilePath(idWell, idCategory, idFile, dotExtention);
|
2021-11-01 16:41:25 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// пометить метку файла как удаленную
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idMark"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public Task<int> MarkFileMarkAsDeletedAsync(int idMark,
|
|
|
|
|
CancellationToken token)
|
|
|
|
|
=> fileRepository.MarkFileMarkAsDeletedAsync(new int[] { idMark }, token);
|
2022-02-12 11:28:16 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Инфо о файле
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idsFile"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<IEnumerable<FileInfoDto>> GetInfoByIdsAsync(IEnumerable<int> idsFile, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var result = await fileRepository.GetInfoByIdsAsync(idsFile, token).ConfigureAwait(false);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2022-09-30 10:49:40 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получить файлы определенной категории
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public Task<IEnumerable<FileInfoDto>> GetInfosAsync(FileRequest request, CancellationToken token)
|
|
|
|
|
=> fileRepository.GetInfosAsync(request, token);
|
2022-10-17 14:42:47 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получить список файлов в контейнере
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public Task<PaginationContainer<FileInfoDto>> GetInfosPaginatedAsync(FileRequest request, CancellationToken token)
|
|
|
|
|
=> fileRepository.GetInfosPaginatedAsync(request, token);
|
2022-09-30 10:49:40 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Пометить файл как удаленный
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idFile"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public Task<int> MarkAsDeletedAsync(int idFile, CancellationToken token = default)
|
|
|
|
|
=> fileRepository.MarkAsDeletedAsync(idFile, token);
|
2022-09-30 10:49:40 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// добавить метку на файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="fileMarkDto"></param>
|
|
|
|
|
/// <param name="idUser"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public Task<int> CreateFileMarkAsync(FileMarkDto fileMarkDto, int idUser, CancellationToken token)
|
|
|
|
|
=> fileRepository.CreateFileMarkAsync(fileMarkDto, idUser, token);
|
2022-09-30 10:49:40 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получить запись по id
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public Task<FileInfoDto?> GetOrDefaultAsync(int id, CancellationToken token)
|
|
|
|
|
=> fileRepository.GetOrDefaultAsync(id, token);
|
2022-09-30 11:05:09 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// получить инфо о файле по метке
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idMark"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public Task<FileInfoDto> GetByMarkId(int idMark, CancellationToken token)
|
|
|
|
|
=> fileRepository.GetByMarkId(idMark, token);
|
2022-09-30 13:34:50 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// получить инфо о файле по метке
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idsMarks"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public Task<int> MarkFileMarkAsDeletedAsync(IEnumerable<int> idsMarks, CancellationToken token)
|
|
|
|
|
=> fileRepository.MarkFileMarkAsDeletedAsync(idsMarks, token);
|
2022-10-06 14:37:03 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление всех файлов по скважине помеченных как удаленные
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<int> DeleteFilesFromDbMarkedDeletionByIdWell(int idWell, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var files = await fileRepository.GetInfosAsync(
|
|
|
|
|
new FileRequest
|
|
|
|
|
{
|
|
|
|
|
IdWell = idWell,
|
|
|
|
|
IsDeleted = true
|
|
|
|
|
},
|
|
|
|
|
token);
|
|
|
|
|
var result = await DeleteAsync(files.Select(x => x.Id), token);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2022-10-11 08:28:37 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление всех файлов с диска о которых нет информации в базе
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
public async Task<int> DeleteFilesNotExistStorage(int idWell, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var files = await fileRepository.GetInfosAsync(
|
|
|
|
|
new FileRequest
|
|
|
|
|
{
|
|
|
|
|
IdWell = idWell
|
|
|
|
|
},
|
|
|
|
|
token);
|
|
|
|
|
var result = await Task.FromResult(fileStorageRepository.DeleteFilesNotInList(idWell, files.Select(x => x.Id)));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2022-10-11 08:28:37 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Вывод списка всех файлов из базы, для которых нет файла на диске
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<IEnumerable<FileInfoDto>> GetListFilesNotDisc(int idWell, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var files = await fileRepository.GetInfosAsync(
|
|
|
|
|
new FileRequest
|
|
|
|
|
{
|
|
|
|
|
IdWell = idWell
|
|
|
|
|
},
|
|
|
|
|
token);
|
|
|
|
|
var result = fileStorageRepository.GetListFilesNotDisc(files);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2022-10-17 08:58:28 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получить файловый поток по идентификатору файла
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="fileInfo"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public Stream GetFileStream(FileInfoDto fileInfo)
|
|
|
|
|
{
|
|
|
|
|
var relativePath = GetUrl(fileInfo);
|
|
|
|
|
var fileStream = new FileStream(Path.GetFullPath(relativePath), FileMode.Open);
|
2022-10-17 08:58:28 +05:00
|
|
|
|
|
2024-08-19 10:01:07 +05:00
|
|
|
|
return fileStream;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
|
|
|
|
}
|