forked from ddrilling/AsbCloudServer
185 lines
7.6 KiB
C#
185 lines
7.6 KiB
C#
using AsbCloudApp.Data;
|
|
using AsbCloudApp.Exceptions;
|
|
using AsbCloudApp.Services;
|
|
using AsbCloudDb.Model;
|
|
using AsbCloudInfrastructure.Repository;
|
|
using Mapster;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
{
|
|
public class FileService : IFileService
|
|
{
|
|
private readonly IFileRepository fileRepository;
|
|
private readonly IFileStorageRepository fileStorageRepository;
|
|
|
|
public FileService(IFileRepository fileRepository, IFileStorageRepository fileStorageRepository)
|
|
{
|
|
this.fileRepository = fileRepository;
|
|
this.fileStorageRepository = fileStorageRepository;
|
|
}
|
|
|
|
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.GetLengthFile(srcFilePath);
|
|
|
|
//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 = MakeFilePath(idWell, idCategory, destinationFileName, fileId);
|
|
fileStorageRepository.MoveFile(srcFilePath, filePath);
|
|
|
|
return await GetInfoAsync(fileId, token);
|
|
}
|
|
|
|
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
|
|
{
|
|
IdWell = idWell,
|
|
IdAuthor = idUser,
|
|
IdCategory = idCategory,
|
|
Name = Path.GetFileName(fileFullName),
|
|
Size = fileStream?.Length ?? 0
|
|
};
|
|
|
|
var fileId = await fileRepository.InsertAsync(dto, token)
|
|
.ConfigureAwait(false);
|
|
|
|
//save stream to disk
|
|
string filePath = MakeFilePath(idWell, idCategory, fileFullName, fileId);
|
|
await fileStorageRepository.CopyFileAsync(filePath, fileStream, token);
|
|
|
|
return await GetInfoAsync(fileId, token);
|
|
}
|
|
|
|
private string MakeFilePath(int idWell, int idCategory, string fileFullName, int fileId)
|
|
{
|
|
return Path.Combine(fileStorageRepository.RootPath, $"{idWell}",
|
|
$"{idCategory}", $"{fileId}" + $"{Path.GetExtension(fileFullName)}");
|
|
}
|
|
|
|
public async Task<FileInfoDto> GetInfoAsync(int idFile,
|
|
CancellationToken token)
|
|
{
|
|
var dto = await fileRepository.GetOrDefaultAsync(idFile, token).ConfigureAwait(false);
|
|
|
|
var ext = Path.GetExtension(dto.Name);
|
|
|
|
var relativePath = GetUrl(dto.IdWell, dto.IdCategory, dto.Id, ext);
|
|
var fullPath = Path.GetFullPath(relativePath);
|
|
fileStorageRepository.FileExists(fullPath, relativePath);
|
|
|
|
return dto;
|
|
}
|
|
|
|
public Task<int> DeleteAsync(int idFile, CancellationToken token)
|
|
=> DeleteAsync(new int[] { idFile }, token);
|
|
|
|
public async Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token)
|
|
{
|
|
if (ids is null || !ids.Any())
|
|
return 0;
|
|
|
|
var files = await fileRepository.DeleteAsync(ids, token).ConfigureAwait(false);
|
|
|
|
if (files is null || !files.Any())
|
|
return 0;
|
|
|
|
foreach (var file in files)
|
|
{
|
|
var fileName = GetUrl(file.IdWell, file.IdCategory, file.Id, Path.GetExtension(file.Name));
|
|
fileStorageRepository.DeleteFile(fileName);
|
|
}
|
|
|
|
return files.Any() ? 1 : 0;
|
|
}
|
|
|
|
public async Task<string> GetUrl(int idFile)
|
|
{
|
|
var fileInfo = await fileRepository.GetOrDefaultAsync(idFile, CancellationToken.None).ConfigureAwait(false);
|
|
|
|
return GetUrl(fileInfo.IdWell, fileInfo.IdCategory, fileInfo.Id, Path.GetExtension(fileInfo.Name));
|
|
}
|
|
|
|
public string GetUrl(FileInfoDto fileInfo) =>
|
|
GetUrl(fileInfo.IdWell, fileInfo.IdCategory, fileInfo.Id, Path.GetExtension(fileInfo.Name));
|
|
|
|
public string GetUrl(int idWell, int idCategory, int idFile, string dotExtention) =>
|
|
Path.Combine(fileStorageRepository.RootPath, idWell.ToString(), idCategory.ToString(), $"{idFile}{dotExtention}");
|
|
|
|
public Task<int> MarkFileMarkAsDeletedAsync(int idMark,
|
|
CancellationToken token)
|
|
=> fileRepository.MarkFileMarkAsDeletedAsync(new int[] { idMark }, token);
|
|
|
|
public async Task<IEnumerable<FileInfoDto>> GetInfoByIdsAsync(IEnumerable<int> idsFile, CancellationToken token)
|
|
{
|
|
var result = await fileRepository.GetInfoByIdsAsync(idsFile, token).ConfigureAwait(false);
|
|
|
|
foreach (var entity in result)
|
|
{
|
|
|
|
var ext = Path.GetExtension(entity.Name);
|
|
var relativePath = GetUrl(entity.IdWell, entity.IdCategory, entity.Id, ext);
|
|
var fullPath = Path.GetFullPath(relativePath);
|
|
fileStorageRepository.FileExists(fullPath, relativePath);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
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)
|
|
.ConfigureAwait(false);
|
|
|
|
public async Task<int> MarkAsDeletedAsync(int idFile, CancellationToken token = default)
|
|
=> await fileRepository.MarkAsDeletedAsync(idFile, token)
|
|
.ConfigureAwait(false);
|
|
|
|
public async Task<int> CreateFileMarkAsync(FileMarkDto fileMarkDto, int idUser, CancellationToken token)
|
|
=> await fileRepository.CreateFileMarkAsync(fileMarkDto, idUser, token)
|
|
.ConfigureAwait(false);
|
|
|
|
public async Task<FileInfoDto> GetOrDefaultAsync(int id, CancellationToken token)
|
|
=> await fileRepository.GetOrDefaultAsync(id, token)
|
|
.ConfigureAwait(false);
|
|
|
|
public async Task<FileInfoDto> GetByMarkId(int idMark, CancellationToken token)
|
|
=> await fileRepository.GetByMarkId(idMark, token)
|
|
.ConfigureAwait(false);
|
|
|
|
public async Task<int> MarkFileMarkAsDeletedAsync(IEnumerable<int> idsMarks, CancellationToken token)
|
|
=> await fileRepository.MarkFileMarkAsDeletedAsync(idsMarks, token)
|
|
.ConfigureAwait(false);
|
|
|
|
public async Task<IEnumerable<FileInfoDto>> GetInfosByWellIdAsync(int idWell, CancellationToken token)
|
|
=> await fileRepository.GetInfosByWellIdAsync(idWell, token)
|
|
.ConfigureAwait(false);
|
|
|
|
public async Task<IEnumerable<FileInfoDto>> GetInfosByCategoryAsync(int idWell, int idCategory, CancellationToken token)
|
|
=> await fileRepository.GetInfosByCategoryAsync(idWell, idCategory, token)
|
|
.ConfigureAwait(false);
|
|
}
|
|
}
|