feat: 完成文件模块开发

This commit is contained in:
陈淳
2023-02-23 14:15:24 +08:00
parent 8639372513
commit 272466bbbf
20 changed files with 698 additions and 228 deletions

View File

@@ -0,0 +1,48 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Data.Auditing;
using Yi.Framework.Ddd.Entities;
namespace Yi.Framework.FileManager
{
/// <summary>
/// 文件表
///</summary>
[SugarTable("File")]
public class FileEntity : IEntity<long>,IAuditedObject
{
[SugarColumn(ColumnName = "Id", IsPrimaryKey = true)]
public long Id { get; set; }
/// <summary>
/// 文件类型
///</summary>
[SugarColumn(ColumnName = "FileContentType")]
public string? FileContentType { get; set; }
/// <summary>
/// 文件大小
///</summary>
[SugarColumn(ColumnName = "FileSize")]
public decimal FileSize { get; set; }
/// <summary>
/// 文件名
///</summary>
[SugarColumn(ColumnName = "FileName")]
public string FileName { get; set; }
/// <summary>
/// 文件路径
///</summary>
[SugarColumn(ColumnName = "FilePath")]
public string FilePath { get; set; }
public DateTime CreationTime { get; set; }
public long? CreatorId { get; set; }
public long? LastModifierId { get; set; }
public DateTime? LastModificationTime { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Ddd.Dtos;
namespace Yi.Framework.FileManager
{
public class FileGetListOutputDto:IEntityDto
{
public long Id { get; set; }
}
}

View File

@@ -0,0 +1,158 @@
using Cike.AutoWebApi.Setting;
using Mapster;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.AspNetCore.Extensions;
using Yi.Framework.Core.Const;
using Yi.Framework.Core.Enums;
using Yi.Framework.Core.Helper;
using Yi.Framework.Ddd.Repositories;
using Yi.Framework.Ddd.Services;
using Yi.Framework.Ddd.Services.Abstract;
using Yi.Framework.ThumbnailSharp;
namespace Yi.Framework.FileManager
{
/// <summary>
/// 文件处理
/// </summary>
public class FileService : ApplicationService, IFileService, IAutoApiService
{
private readonly IRepository<FileEntity> _repository;
private readonly ThumbnailSharpManager _thumbnailSharpManager;
private readonly HttpContext _httpContext;
public FileService(IRepository<FileEntity> repository, ThumbnailSharpManager thumbnailSharpManager, IHttpContextAccessor httpContextAccessor
)
{
_repository = repository;
_thumbnailSharpManager = thumbnailSharpManager;
if (httpContextAccessor.HttpContext is null)
{
throw new ApplicationException("HttpContext为空");
}
_httpContext = httpContextAccessor.HttpContext;
}
/// <summary>
/// 下载文件,是否缩略图
/// </summary>
/// <returns></returns>
[Route("/api/file/{code}/{isThumbnail?}")]
public async Task<IActionResult> Get([FromRoute] long code, [FromRoute] bool? isThumbnail)
{
var file = await _repository.GetByIdAsync(code);
if (file is null)
{
return new NotFoundResult();
}
var path = file.FilePath;
//如果为缩略图,需要修改路径
if (isThumbnail is true)
{
path = $"{PathConst.wwwroot}/{FileTypeEnum.Thumbnail}/{file.Id}{Path.GetExtension(file.FileName)}";
}
//路径为: 文件路径/文件id+文件扩展名
if (!File.Exists(path))
{
return new NotFoundResult();
}
var steam = await File.ReadAllBytesAsync(path);
//设置附件下载,下载名称
_httpContext.FileAttachmentHandle(file.FileName);
return new FileContentResult(steam, file.FileContentType ?? @"text/plain");
}
/// <summary>
/// 上传文件
/// </summary>
/// <returns></returns>
public async Task<List<FileGetListOutputDto>> Post([FromForm] IFormFileCollection file)
{
if (file.Count() == 0)
{
throw new ArgumentException("文件上传为空!");
}
//批量插入
List<FileEntity> entities = new();
foreach (var f in file)
{
FileEntity data = new();
data.Id = SnowflakeHelper.NextId;
data.FileSize = ((decimal)f.Length) / 1024;
data.FileName = f.FileName;
data.FileContentType = MimeHelper.GetMimeMapping(f.FileName);
var type = MimeHelper.GetFileType(f.FileName);
//落盘文件文件名为雪花id+自己的扩展名
string filename = data.Id.ToString() + Path.GetExtension(f.FileName);
string typePath = $"{PathConst.wwwroot}/{type}";
if (!Directory.Exists(typePath))
{
Directory.CreateDirectory(typePath);
}
var filePath = Path.Combine(typePath, filename);
data.FilePath = filePath;
//生成文件
using (var stream = new FileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite))
{
await f.CopyToAsync(stream);
//如果是图片类型,还需要生成缩略图,当然,如果图片很小,直接复制过去即可
if (FileTypeEnum.Image.Equals(type))
{
string thumbnailPath = $"{PathConst.wwwroot}/{FileTypeEnum.Thumbnail}";
if (!Directory.Exists(thumbnailPath))
{
Directory.CreateDirectory(thumbnailPath);
}
//保存至缩略图路径
byte[] result = null!;
try
{
result = _thumbnailSharpManager.CreateThumbnailBytes(thumbnailSize: 300, imageStream: stream, imageFormat: Format.Jpeg);
}
catch
{
result = new byte[stream.Length];
stream.Read(result, 0, result.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
}
finally
{
await System.IO.File.WriteAllBytesAsync(Path.Combine(thumbnailPath, filename), result);
}
}
};
entities.Add(data);
}
await _repository.InsertRangeAsync(entities);
return entities.Adapt<List<FileGetListOutputDto>>();
}
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.FileManager
{
public interface IFileService
{
}
}

View File

@@ -4,10 +4,15 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\framework\Yi.Framework.AspNetCore\Yi.Framework.AspNetCore.csproj" />
<ProjectReference Include="..\..\framework\Yi.Framework.Core\Yi.Framework.Core.csproj" />
<ProjectReference Include="..\..\framework\Yi.Framework.Data\Yi.Framework.Data.csproj" />
<ProjectReference Include="..\..\framework\Yi.Framework.Ddd\Yi.Framework.Ddd.csproj" />
<ProjectReference Include="..\Yi.Framework.ThumbnailSharp\Yi.Framework.ThumbnailSharp.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,7 +1,20 @@
namespace Yi.Framework.FileManager
{
public class YiFrameworkFileManagerModule
{
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using StartupModules;
namespace Yi.Framework.FileManager
{
public class YiFrameworkFileManagerModule : IStartupModule
{
public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context)
{
}
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
{
services.AddTransient<FileService>();
services.AddTransient<IFileService,FileService>();
}
}
}