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-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; }
|
2021-08-31 18:01:26 +05:00
|
|
|
|
|
|
|
|
|
private readonly IQueryable<AsbCloudDb.Model.FileInfo> dbSetConfigured;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
private readonly IAsbCloudDbContext db;
|
|
|
|
|
|
2021-08-31 18:01:26 +05:00
|
|
|
|
public FileService(IAsbCloudDbContext db)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
|
|
|
|
RootPath = "files";
|
|
|
|
|
this.db = db;
|
2021-08-31 18:01:26 +05:00
|
|
|
|
dbSetConfigured = db.Files
|
|
|
|
|
.Include(f => f.Author)
|
|
|
|
|
.ThenInclude(u => u.Company)
|
|
|
|
|
.ThenInclude(c => c.CompanyType);
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-01 15:55:10 +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));
|
|
|
|
|
|
2021-09-23 10:53:48 +05:00
|
|
|
|
using var newfileStream = new FileStream(relativePath, FileMode.Create);
|
|
|
|
|
await fileStream.CopyToAsync(newfileStream, token).ConfigureAwait(false);
|
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)
|
|
|
|
|
{
|
2021-08-31 18:01:26 +05:00
|
|
|
|
var entities = await dbSetConfigured
|
2021-08-29 17:25:16 +05:00
|
|
|
|
.Where(e => e.IdWell == idWell && e.IdCategory == idCategory)
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2021-08-31 18:01:26 +05:00
|
|
|
|
var dtos = entities.Adapt<FileInfoDto>();
|
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 18:01:26 +05:00
|
|
|
|
int idCategory, string companyName = 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-31 18:01:26 +05:00
|
|
|
|
var query = dbSetConfigured
|
2021-08-13 17:26:19 +05:00
|
|
|
|
.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-31 18:01:26 +05:00
|
|
|
|
if (!string.IsNullOrEmpty(companyName))
|
|
|
|
|
query = query
|
|
|
|
|
.Include(file => file.Author)
|
|
|
|
|
.ThenInclude(a => a.Company)
|
|
|
|
|
.Where(e => (e.Author == null) || (e.Author.Company == null) || e.Author.Company.Caption.Contains(companyName));
|
2021-08-20 11:20:24 +05:00
|
|
|
|
|
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 18:01:26 +05:00
|
|
|
|
var dtos = entities.Adapt<FileInfoDto>();
|
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-31 18:01:26 +05:00
|
|
|
|
var entity = await dbSetConfigured
|
2021-08-13 17:26:19 +05:00
|
|
|
|
.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>();
|
|
|
|
|
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;
|
|
|
|
|
|
2021-09-23 10:53:48 +05:00
|
|
|
|
var fileName = GetUrl(fileInfo.Adapt<FileInfoDto>());
|
2021-08-29 17:25:16 +05:00
|
|
|
|
if (File.Exists(fileName))
|
|
|
|
|
File.Delete(fileName);
|
|
|
|
|
|
|
|
|
|
db.Files.Remove(fileInfo);
|
|
|
|
|
return await db.SaveChangesAsync(token).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-23 10:53:48 +05:00
|
|
|
|
public string GetUrl(FileInfoDto fileInfo) =>
|
|
|
|
|
GetUrl(fileInfo.IdWell, fileInfo.IdCategory, fileInfo.Id, Path.GetExtension(fileInfo.Name));
|
|
|
|
|
|
|
|
|
|
public string GetUrl(int idFile)
|
2021-08-29 17:25:16 +05:00
|
|
|
|
{
|
2021-09-23 10:53:48 +05:00
|
|
|
|
var fileInfo = db.Files
|
|
|
|
|
.FirstOrDefault(f => f.Id == idFile);
|
|
|
|
|
|
|
|
|
|
if (fileInfo is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
public string GetUrl(int idWell, int idCategory, int idFile, string dotExtention) =>
|
|
|
|
|
Path.Combine(RootPath, idWell.ToString(), idCategory.ToString(), $"{idFile}{dotExtention}");
|
|
|
|
|
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
|
|
|
|
}
|