2021-08-09 15:41:42 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
2021-08-31 12:29:27 +05:00
|
|
|
|
using AsbCloudInfrastructure.Services.Cache;
|
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; }
|
|
|
|
|
private readonly IAsbCloudDbContext db;
|
2021-08-31 12:29:27 +05:00
|
|
|
|
private readonly CacheTable<Company> cacheCompanies;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2021-08-31 12:29:27 +05:00
|
|
|
|
public FileService(IAsbCloudDbContext db, Cache.CacheDb cacheDb)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
|
|
|
|
RootPath = "files";
|
|
|
|
|
this.db = db;
|
2021-08-31 12:29:27 +05:00
|
|
|
|
cacheCompanies = cacheDb.GetCachedTable<AsbCloudDb.Model.Company>((DbContext)db);
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-29 17:25:16 +05:00
|
|
|
|
public async Task<FileInfoDto> SaveAsync(int idWell, int idUser, int idCategory, string fileFullName, Stream fileStream, CancellationToken token = default)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
2021-08-29 17:25:16 +05:00
|
|
|
|
//save info to db
|
|
|
|
|
var fileInfo = new AsbCloudDb.Model.FileInfo()
|
2021-08-09 15:41:42 +05:00
|
|
|
|
{
|
2021-08-29 17:25:16 +05:00
|
|
|
|
IdWell = idWell,
|
|
|
|
|
IdAuthor = idUser,
|
|
|
|
|
IdCategory = idCategory,
|
|
|
|
|
Name = Path.GetFileName(fileFullName),
|
|
|
|
|
UploadDate = DateTime.Now,
|
|
|
|
|
IsDeleted = false,
|
2021-08-31 12:29:27 +05:00
|
|
|
|
Size = fileStream.Length,
|
2021-08-29 17:25:16 +05:00
|
|
|
|
};
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2021-08-29 17:25:16 +05:00
|
|
|
|
var entry = db.Files.Add(fileInfo);
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
var fileId = entry.Entity.Id;
|
|
|
|
|
//save stream to disk
|
2021-08-19 16:32:04 +05:00
|
|
|
|
var relativePath = Path.Combine(RootPath, $"{idWell}",
|
2021-08-29 17:25:16 +05:00
|
|
|
|
$"{idCategory}", $"{fileId}" + $"{Path.GetExtension(fileFullName)}");
|
2021-08-19 16:32:04 +05:00
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(relativePath));
|
|
|
|
|
|
|
|
|
|
using (var newfileStream = new FileStream(relativePath, FileMode.Create))
|
|
|
|
|
{
|
|
|
|
|
await fileStream.CopyToAsync(newfileStream);
|
|
|
|
|
}
|
2021-08-29 17:25:16 +05:00
|
|
|
|
|
|
|
|
|
var dto = entry.Entity.Adapt<FileInfoDto>();
|
|
|
|
|
return dto;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<FileInfoDto>> GetInfosByCategoryAsync(int idWell,
|
|
|
|
|
int idCategory, CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
var entities = await db.Files
|
|
|
|
|
.Include(f => f.Author)
|
|
|
|
|
.Where(e => e.IdWell == idWell && e.IdCategory == idCategory)
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2021-08-31 12:29:27 +05:00
|
|
|
|
var dtos = entities.Adapt<FileInfoDto, AsbCloudDb.Model.FileInfo>(async (d,s) => {
|
|
|
|
|
d.Author = s.Author?.Adapt<UserDto>();
|
|
|
|
|
var company = await cacheCompanies.FirstOrDefaultAsync(c => c.Id == s.Author?.IdCompany, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
d.Company = company?.Caption;
|
2021-08-29 17:25:16 +05:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return dtos;
|
2021-08-19 16:32:04 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-29 17:25:16 +05:00
|
|
|
|
public async Task<PaginationContainer<FileInfoDto>> GetInfosAsync(int idWell,
|
2021-08-31 09:59:23 +05:00
|
|
|
|
int idCategory, IEnumerable<int> companies = default, string fileName = default, DateTime begin = default,
|
2021-08-20 11:20:24 +05:00
|
|
|
|
DateTime end = default, int skip = 0, int take = 32, CancellationToken token = default)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
2021-08-13 17:26:19 +05:00
|
|
|
|
var query = db.Files
|
|
|
|
|
.Include(f => f.Author)
|
|
|
|
|
.Where(e => e.IdWell == idWell &&
|
|
|
|
|
e.IdCategory == idCategory);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
2021-08-13 17:26:19 +05:00
|
|
|
|
query = query.Where(e => !e.IsDeleted);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
2021-08-20 11:20:24 +05:00
|
|
|
|
if (companies.Any())
|
|
|
|
|
query.Include(file => file.Author).ThenInclude(a => a.Company)
|
|
|
|
|
.Where(e => companies.Contains(e.Author.Company.Id));
|
|
|
|
|
|
2021-08-31 09:59:23 +05:00
|
|
|
|
if (!string.IsNullOrEmpty(fileName))
|
2021-08-31 12:29:27 +05:00
|
|
|
|
query = query.Where(e => e.Name.ToLower().Contains(fileName.ToLower()));
|
2021-08-31 09:59:23 +05:00
|
|
|
|
|
2021-07-26 11:54:50 +05:00
|
|
|
|
if (begin != default)
|
2021-08-13 17:26:19 +05:00
|
|
|
|
query = query.Where(e => e.UploadDate >= begin);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
|
|
|
|
if (end != default)
|
2021-08-13 17:26:19 +05:00
|
|
|
|
query = query.Where(e => e.UploadDate <= end);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
2021-08-13 17:26:19 +05:00
|
|
|
|
var count = await query.CountAsync(token).ConfigureAwait(false);
|
|
|
|
|
|
2021-08-24 10:59:10 +05:00
|
|
|
|
var result = new PaginationContainer<FileInfoDto>(count)
|
|
|
|
|
{
|
|
|
|
|
Skip = skip,
|
2021-08-13 17:26:19 +05:00
|
|
|
|
Take = take,
|
|
|
|
|
Count = count,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (count <= skip)
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
query = query.OrderBy(e => e.UploadDate);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
|
|
|
|
if (skip > 0)
|
2021-08-13 17:26:19 +05:00
|
|
|
|
query = query.Skip(skip);
|
|
|
|
|
query = query.Take(take);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
2021-08-13 17:26:19 +05:00
|
|
|
|
var entities = await query
|
|
|
|
|
.Take(take).AsNoTracking().ToListAsync(token)
|
2021-08-11 17:26:02 +05:00
|
|
|
|
.ConfigureAwait(false);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
2021-08-31 12:29:27 +05:00
|
|
|
|
var dtos = entities.Adapt<FileInfoDto, AsbCloudDb.Model.FileInfo>(async (d, s) =>
|
2021-07-26 11:54:50 +05:00
|
|
|
|
{
|
2021-08-31 12:29:27 +05:00
|
|
|
|
d.Author = s.Author?.Adapt<UserDto>();
|
|
|
|
|
var company = await cacheCompanies.FirstOrDefaultAsync(c => c.Id == s.Author?.IdCompany, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
d.Company = company?.Caption;
|
|
|
|
|
});
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
2021-08-31 12:29:27 +05:00
|
|
|
|
result.Items.AddRange(dtos);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
return result;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-29 17:25:16 +05:00
|
|
|
|
public async Task<FileInfoDto> GetInfoAsync(int fileId,
|
2021-08-11 17:26:02 +05:00
|
|
|
|
CancellationToken token = default)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
2021-08-13 17:26:19 +05:00
|
|
|
|
var entity = await db.Files
|
|
|
|
|
.Include(f => f.Author)
|
|
|
|
|
.AsNoTracking()
|
2021-08-11 17:26:02 +05:00
|
|
|
|
.FirstOrDefaultAsync(f => f.Id == fileId, token)
|
|
|
|
|
.ConfigureAwait(false);
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2021-08-13 17:26:19 +05:00
|
|
|
|
if (entity is null)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
return null;
|
|
|
|
|
|
2021-08-13 17:26:19 +05:00
|
|
|
|
var dto = entity.Adapt<FileInfoDto>();
|
2021-08-31 12:29:27 +05:00
|
|
|
|
dto.Author = entity.Author?.Adapt<UserDto>();
|
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
|
|
|
|
|
2021-08-29 17:25:16 +05:00
|
|
|
|
public async Task<int> MarkAsDeletedAsync(int idFile,
|
2021-08-19 16:58:26 +05:00
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
2021-08-29 17:25:16 +05:00
|
|
|
|
var fileInfo = await db.Files.FirstOrDefaultAsync(f => f.Id == idFile, token).ConfigureAwait(false);
|
2021-08-19 16:58:26 +05:00
|
|
|
|
|
|
|
|
|
if (fileInfo is null)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
fileInfo.IsDeleted = true;
|
|
|
|
|
|
2021-08-29 17:25:16 +05:00
|
|
|
|
return await db.SaveChangesAsync(token).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> DeletedAsync(int idFile, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var fileInfo = await db.Files
|
|
|
|
|
.FirstOrDefaultAsync(f => f.Id == idFile, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (fileInfo is null)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
var fileName = GetFileName(fileInfo.Adapt<FileInfoDto>());
|
|
|
|
|
if (File.Exists(fileName))
|
|
|
|
|
File.Delete(fileName);
|
|
|
|
|
|
|
|
|
|
db.Files.Remove(fileInfo);
|
|
|
|
|
return await db.SaveChangesAsync(token).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetFileName(FileInfoDto fileInfo)
|
|
|
|
|
{
|
2021-08-31 09:52:32 +05:00
|
|
|
|
var fileName = $"{fileInfo.Id}{Path.GetExtension(fileInfo.Name)}";
|
2021-08-29 17:25:16 +05:00
|
|
|
|
fileName = Path.Combine(RootPath, fileInfo.IdWell.ToString(), fileInfo.IdCategory.ToString(), fileName);
|
|
|
|
|
fileName = Path.GetFullPath(fileName);
|
|
|
|
|
return fileName;
|
2021-08-19 16:58:26 +05:00
|
|
|
|
}
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
|
|
|
|
}
|