2022-10-11 08:28:37 +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 System.Collections.Generic;
|
2022-10-06 13:41:46 +05:00
|
|
|
|
using System.IO;
|
2022-10-11 08:28:37 +05:00
|
|
|
|
using System.Linq;
|
2022-10-06 13:41:46 +05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Repository
|
|
|
|
|
{
|
2022-10-11 08:28:37 +05:00
|
|
|
|
#nullable enable
|
2022-10-06 13:41:46 +05:00
|
|
|
|
public class FileStorageRepository : IFileStorageRepository
|
|
|
|
|
{
|
2022-10-11 14:42:04 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Директория хранения файлов
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string RootPath = "files";
|
2022-10-06 13:41:46 +05:00
|
|
|
|
|
|
|
|
|
public FileStorageRepository()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 14:42:04 +05:00
|
|
|
|
public async Task SaveFileAsync(string filePathRec, Stream fileStreamSrc, CancellationToken token)
|
2022-10-06 13:41:46 +05:00
|
|
|
|
{
|
2022-10-11 14:42:04 +05:00
|
|
|
|
CreateDirectory(filePathRec);
|
|
|
|
|
using var newfileStream = new FileStream(filePathRec, FileMode.Create);
|
|
|
|
|
await fileStreamSrc.CopyToAsync(newfileStream, token).ConfigureAwait(false);
|
2022-10-06 13:41:46 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 14:42:04 +05:00
|
|
|
|
public void DeleteFile(IEnumerable<string> filesName)
|
2022-10-06 13:41:46 +05:00
|
|
|
|
{
|
2022-10-11 14:42:04 +05:00
|
|
|
|
foreach (var fileName in filesName)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(fileName))
|
|
|
|
|
File.Delete(fileName);
|
|
|
|
|
}
|
2022-10-06 13:41:46 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 14:42:04 +05:00
|
|
|
|
public long GetFileLength(string srcFilePath)
|
2022-10-06 13:41:46 +05:00
|
|
|
|
{
|
|
|
|
|
var sysFileInfo = new FileInfo(srcFilePath);
|
|
|
|
|
return sysFileInfo.Length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void MoveFile(string srcFilePath, string filePath)
|
|
|
|
|
{
|
|
|
|
|
CreateDirectory(filePath);
|
|
|
|
|
File.Move(srcFilePath, filePath);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 08:28:37 +05:00
|
|
|
|
public string MakeFilePath(int idWell, int idCategory, string fileFullName, int fileId)
|
|
|
|
|
{
|
|
|
|
|
return Path.Combine(RootPath, $"{idWell}",
|
|
|
|
|
$"{idCategory}", $"{fileId}" + $"{Path.GetExtension(fileFullName)}");
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-17 08:58:28 +05:00
|
|
|
|
public int DeleteFilesNotInList(int idWell, IEnumerable<int> idsFilesList)
|
2022-10-11 08:28:37 +05:00
|
|
|
|
{
|
|
|
|
|
var allFilesPath = GetFilesPath(idWell);
|
2022-10-11 14:42:04 +05:00
|
|
|
|
var result = 0;
|
2022-10-11 08:28:37 +05:00
|
|
|
|
|
|
|
|
|
foreach (var filePath in allFilesPath)
|
|
|
|
|
{
|
2022-10-11 14:42:04 +05:00
|
|
|
|
if (int.TryParse(Path.GetFileNameWithoutExtension(filePath), out int idFile)
|
|
|
|
|
|| !idsFilesList.Any(x => x == idFile))
|
2022-10-11 08:28:37 +05:00
|
|
|
|
{
|
|
|
|
|
File.Delete(filePath);
|
2022-10-11 14:42:04 +05:00
|
|
|
|
result++;
|
2022-10-11 08:28:37 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 14:42:04 +05:00
|
|
|
|
return result;
|
2022-10-11 08:28:37 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 14:42:04 +05:00
|
|
|
|
public IEnumerable<FileInfoDto> GetListFilesNotDisc(IEnumerable<FileInfoDto> files)
|
2022-10-11 08:28:37 +05:00
|
|
|
|
{
|
|
|
|
|
var resutl = new List<FileInfoDto>();
|
2022-10-11 14:42:04 +05:00
|
|
|
|
var groupFiles = files.GroupBy(x => x.IdWell);
|
2022-10-11 08:28:37 +05:00
|
|
|
|
|
2022-10-11 14:42:04 +05:00
|
|
|
|
foreach (var itemGroupFiles in groupFiles)
|
|
|
|
|
{
|
|
|
|
|
var idsFilesStorage = GetIdsFiles(itemGroupFiles.Key);
|
|
|
|
|
foreach (var file in files)
|
|
|
|
|
{
|
|
|
|
|
if (!idsFilesStorage.Any(x => x == file.Id))
|
|
|
|
|
resutl.Add(file);
|
|
|
|
|
}
|
2022-10-11 08:28:37 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resutl;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 14:42:04 +05:00
|
|
|
|
public string GetUrl(int idWell, int idCategory, int idFile, string dotExtention) =>
|
|
|
|
|
Path.Combine(RootPath, idWell.ToString(), idCategory.ToString(), $"{idFile}{dotExtention}");
|
|
|
|
|
|
2022-10-11 08:28:37 +05:00
|
|
|
|
private IEnumerable<int> GetIdsFiles(int idWell)
|
|
|
|
|
{
|
|
|
|
|
var result = new List<int>();
|
|
|
|
|
var allFilesPath = GetFilesPath(idWell);
|
|
|
|
|
|
|
|
|
|
foreach (var filePath in allFilesPath)
|
2022-10-17 14:42:47 +05:00
|
|
|
|
if(int.TryParse(Path.GetFileNameWithoutExtension(filePath), out int idFile))
|
|
|
|
|
result.Add(idFile);
|
2022-10-11 08:28:37 +05:00
|
|
|
|
|
|
|
|
|
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)
|
2022-10-06 13:41:46 +05:00
|
|
|
|
{
|
2022-10-17 14:42:47 +05:00
|
|
|
|
var directoryName = Path.GetDirectoryName(filePath)!;
|
|
|
|
|
Directory.CreateDirectory(directoryName);
|
2022-10-06 13:41:46 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-11 08:28:37 +05:00
|
|
|
|
#nullable disable
|
2022-10-06 13:41:46 +05:00
|
|
|
|
}
|