2021-08-09 15:41:42 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2022-01-18 11:04:15 +05:00
|
|
|
|
using AsbCloudApp.Exceptions;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
2022-09-28 10:46:12 +05:00
|
|
|
|
using AsbCloudInfrastructure.Repository;
|
2021-08-13 17:26:19 +05:00
|
|
|
|
using Mapster;
|
2021-08-09 15:41:42 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
|
|
|
{
|
|
|
|
|
public class FileService : IFileService
|
|
|
|
|
{
|
|
|
|
|
public string RootPath { get; private set; }
|
2022-09-28 10:46:12 +05:00
|
|
|
|
private readonly IFileRepository fileRepository;
|
2021-08-31 18:01:26 +05:00
|
|
|
|
|
2022-09-28 10:46:12 +05:00
|
|
|
|
public FileService(IFileRepository fileRepository)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
|
|
|
|
RootPath = "files";
|
2022-09-28 10:46:12 +05:00
|
|
|
|
this.fileRepository = fileRepository;
|
2021-11-09 17:36:44 +05:00
|
|
|
|
}
|
2021-10-27 17:00:27 +05:00
|
|
|
|
|
2022-04-11 18:00:34 +05:00
|
|
|
|
public async Task<FileInfoDto> MoveAsync(int idWell, int? idUser, int idCategory,
|
2021-11-09 17:36:44 +05:00
|
|
|
|
string destinationFileName, string srcFilePath, CancellationToken token = default)
|
2021-09-23 11:55:25 +05:00
|
|
|
|
{
|
2021-09-23 14:36:05 +05:00
|
|
|
|
destinationFileName = Path.GetFileName(destinationFileName);
|
|
|
|
|
srcFilePath = Path.GetFullPath(srcFilePath);
|
|
|
|
|
if (!File.Exists(srcFilePath))
|
2022-01-18 11:04:15 +05:00
|
|
|
|
throw new ArgumentInvalidException($"file {srcFilePath} doesn't exist", nameof(srcFilePath));
|
2021-09-23 11:55:25 +05:00
|
|
|
|
|
2021-09-23 14:36:05 +05:00
|
|
|
|
var sysFileInfo = new System.IO.FileInfo(srcFilePath);
|
2021-09-23 11:55:25 +05:00
|
|
|
|
|
|
|
|
|
//save info to db
|
2022-09-28 10:46:12 +05:00
|
|
|
|
var fileId = await fileRepository.AddAsync(idWell, idUser, idCategory, destinationFileName, sysFileInfo.Length, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2021-09-23 11:55:25 +05:00
|
|
|
|
string filePath = MakeFilePath(idWell, idCategory, destinationFileName, fileId);
|
2021-09-23 14:36:05 +05:00
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
|
|
|
|
|
File.Move(srcFilePath, filePath);
|
2021-09-23 11:55:25 +05:00
|
|
|
|
|
2022-09-28 10:46:12 +05:00
|
|
|
|
return await GetInfoAsync(fileId, token);
|
2021-09-23 11:55:25 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-11 18:00:34 +05:00
|
|
|
|
public async Task<FileInfoDto> SaveAsync(int idWell, int? idUser, int idCategory,
|
2021-11-03 15:53:31 +05:00
|
|
|
|
string fileFullName, Stream fileStream, CancellationToken token)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
2021-08-29 17:25:16 +05:00
|
|
|
|
//save info to db
|
2022-09-28 10:46:12 +05:00
|
|
|
|
var fileId = await fileRepository.AddAsync(idWell, idUser, idCategory, Path.GetFileName(fileFullName), fileStream?.Length ?? 0, token)
|
|
|
|
|
.ConfigureAwait(false);
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2021-08-29 17:25:16 +05:00
|
|
|
|
//save stream to disk
|
2021-09-23 11:55:25 +05:00
|
|
|
|
string filePath = MakeFilePath(idWell, idCategory, fileFullName, fileId);
|
2021-08-19 16:32:04 +05:00
|
|
|
|
|
2021-09-23 11:55:25 +05:00
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
|
2021-08-19 16:32:04 +05:00
|
|
|
|
|
2021-09-23 11:55:25 +05:00
|
|
|
|
using var newfileStream = new FileStream(filePath, FileMode.Create);
|
|
|
|
|
await fileStream.CopyToAsync(newfileStream, token).ConfigureAwait(false);
|
2022-01-05 17:50:45 +05:00
|
|
|
|
|
2022-09-28 10:46:12 +05:00
|
|
|
|
return await GetInfoAsync(fileId, token);
|
2021-08-29 17:25:16 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-23 11:55:25 +05:00
|
|
|
|
private string MakeFilePath(int idWell, int idCategory, string fileFullName, int fileId)
|
|
|
|
|
{
|
|
|
|
|
return Path.Combine(RootPath, $"{idWell}",
|
|
|
|
|
$"{idCategory}", $"{fileId}" + $"{Path.GetExtension(fileFullName)}");
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-09 17:36:44 +05:00
|
|
|
|
public async Task<FileInfoDto> GetInfoAsync(int idFile,
|
2021-11-03 15:53:31 +05:00
|
|
|
|
CancellationToken token)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
2022-09-28 10:46:12 +05:00
|
|
|
|
var dto = await fileRepository.GetInfoAsync(idFile, token).ConfigureAwait(false);
|
2022-02-08 10:25:05 +05:00
|
|
|
|
|
2022-09-28 10:46:12 +05:00
|
|
|
|
var ext = Path.GetExtension(dto.Name);
|
2022-02-08 10:25:05 +05:00
|
|
|
|
|
2022-09-28 10:46:12 +05:00
|
|
|
|
var relativePath = GetUrl(dto.IdWell, dto.IdCategory, dto.Id, ext);
|
2022-02-08 10:25:05 +05:00
|
|
|
|
var fullPath = Path.GetFullPath(relativePath);
|
2022-04-11 18:00:34 +05:00
|
|
|
|
if (!File.Exists(fullPath))
|
2022-02-08 10:25:05 +05:00
|
|
|
|
{
|
|
|
|
|
throw new FileNotFoundException("not found", relativePath);
|
|
|
|
|
}
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2021-08-13 17:26:19 +05:00
|
|
|
|
return dto;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
2021-08-19 16:58:26 +05:00
|
|
|
|
|
2022-02-12 11:28:16 +05:00
|
|
|
|
public Task<int> DeleteAsync(int idFile, CancellationToken token)
|
2022-04-11 18:00:34 +05:00
|
|
|
|
=> DeleteAsync(new int[] { idFile }, token);
|
2022-02-12 11:28:16 +05:00
|
|
|
|
|
|
|
|
|
public async Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token)
|
2021-08-29 17:25:16 +05:00
|
|
|
|
{
|
2022-02-12 11:28:16 +05:00
|
|
|
|
if (ids is null || !ids.Any())
|
|
|
|
|
return 0;
|
2021-08-29 17:25:16 +05:00
|
|
|
|
|
2022-09-28 10:46:12 +05:00
|
|
|
|
var files = await fileRepository.DeleteAsync(ids, token).ConfigureAwait(false);
|
2022-02-12 11:28:16 +05:00
|
|
|
|
|
|
|
|
|
if (files is null || !files.Any())
|
2021-08-29 17:25:16 +05:00
|
|
|
|
return 0;
|
|
|
|
|
|
2022-02-12 11:28:16 +05:00
|
|
|
|
foreach (var file in files)
|
|
|
|
|
{
|
|
|
|
|
var fileName = GetUrl(file.IdWell, file.IdCategory, file.Id, Path.GetExtension(file.Name));
|
|
|
|
|
if (File.Exists(fileName))
|
|
|
|
|
File.Delete(fileName);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 10:46:12 +05:00
|
|
|
|
return files.Any() ? 1 : 0;
|
2021-08-29 17:25:16 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 10:46:12 +05:00
|
|
|
|
public async Task<string> GetUrl(int idFile)
|
2021-08-29 17:25:16 +05:00
|
|
|
|
{
|
2022-09-28 10:46:12 +05:00
|
|
|
|
var fileInfo = await fileRepository.GetInfoAsync(idFile, CancellationToken.None).ConfigureAwait(false);
|
2021-09-23 10:53:48 +05:00
|
|
|
|
|
|
|
|
|
return GetUrl(fileInfo.IdWell, fileInfo.IdCategory, fileInfo.Id, Path.GetExtension(fileInfo.Name));
|
2021-08-19 16:58:26 +05:00
|
|
|
|
}
|
2021-09-23 10:53:48 +05:00
|
|
|
|
|
2021-11-09 17:36:44 +05:00
|
|
|
|
public string GetUrl(FileInfoDto fileInfo) =>
|
|
|
|
|
GetUrl(fileInfo.IdWell, fileInfo.IdCategory, fileInfo.Id, Path.GetExtension(fileInfo.Name));
|
|
|
|
|
|
2021-09-23 10:53:48 +05:00
|
|
|
|
public string GetUrl(int idWell, int idCategory, int idFile, string dotExtention) =>
|
|
|
|
|
Path.Combine(RootPath, idWell.ToString(), idCategory.ToString(), $"{idFile}{dotExtention}");
|
2021-11-01 16:41:25 +05:00
|
|
|
|
|
2022-02-12 11:28:16 +05:00
|
|
|
|
public Task<int> MarkFileMarkAsDeletedAsync(int idMark,
|
|
|
|
|
CancellationToken token)
|
2022-09-28 10:46:12 +05:00
|
|
|
|
=> fileRepository.MarkFileMarkAsDeletedAsync(new int[] { idMark }, token);
|
2022-02-12 11:28:16 +05:00
|
|
|
|
|
2022-09-28 10:46:12 +05:00
|
|
|
|
public async Task<IEnumerable<FileInfoDto>> GetInfoByIdsAsync(IEnumerable<int> idsFile, CancellationToken token)
|
2021-10-29 12:47:18 +05:00
|
|
|
|
{
|
2022-09-28 10:46:12 +05:00
|
|
|
|
var result = await fileRepository.GetInfoByIdsAsync(idsFile, token).ConfigureAwait(false);
|
2022-09-05 09:13:45 +05:00
|
|
|
|
|
2022-09-28 10:46:12 +05:00
|
|
|
|
foreach (var entity in result)
|
2022-09-05 09:13:45 +05:00
|
|
|
|
{
|
2022-09-28 10:46:12 +05:00
|
|
|
|
|
2022-09-05 09:13:45 +05:00
|
|
|
|
var ext = Path.GetExtension(entity.Name);
|
|
|
|
|
var relativePath = GetUrl(entity.IdWell, entity.IdCategory, entity.Id, ext);
|
|
|
|
|
var fullPath = Path.GetFullPath(relativePath);
|
|
|
|
|
if (!File.Exists(fullPath))
|
|
|
|
|
{
|
|
|
|
|
throw new FileNotFoundException("not found", relativePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
|
|
|
|
}
|