Files
Yi.Admin/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/FileController.cs

207 lines
7.8 KiB
C#
Raw Normal View History

2022-05-03 19:40:13 +08:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
2022-10-12 14:20:03 +08:00
using SqlSugar;
2022-05-03 19:40:13 +08:00
using System;
using System.Collections.Generic;
2022-10-11 16:48:25 +08:00
using System.Globalization;
2022-05-03 19:40:13 +08:00
using System.IO;
using System.Linq;
using System.Threading.Tasks;
2022-10-12 14:20:03 +08:00
using Yi.Framework.Common.Const;
2022-10-11 16:48:25 +08:00
using Yi.Framework.Common.Enum;
2022-10-17 01:10:31 +08:00
using Yi.Framework.Common.Helper;
2022-05-03 19:40:13 +08:00
using Yi.Framework.Common.Models;
2022-10-17 01:10:31 +08:00
using Yi.Framework.Core;
2022-05-03 19:40:13 +08:00
using Yi.Framework.Interface;
2022-10-12 14:20:03 +08:00
using Yi.Framework.Model.Models;
2022-05-03 19:40:13 +08:00
using Yi.Framework.WebCore;
namespace Yi.Framework.ApiMicroservice.Controllers
{
/// <summary>
/// 文件
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class FileController : ControllerBase
{
2022-10-12 14:20:03 +08:00
private IFileService _iFileService;
2022-05-03 19:40:13 +08:00
private readonly IHostEnvironment _env;
2022-10-17 01:10:31 +08:00
private ThumbnailSharpInvoer _thumbnailSharpInvoer;
2022-10-12 14:20:03 +08:00
2022-05-03 19:40:13 +08:00
/// <summary>
2022-10-12 14:20:03 +08:00
/// 文件上传下载
2022-05-03 19:40:13 +08:00
/// </summary>
2022-10-12 14:20:03 +08:00
/// <param name="iFileService"></param>
2022-05-03 19:40:13 +08:00
/// <param name="env"></param>
2022-10-17 01:10:31 +08:00
/// <param name="thumbnailSharpInvoer"></param>
public FileController(IFileService iFileService, IHostEnvironment env, ThumbnailSharpInvoer thumbnailSharpInvoer)
2022-05-03 19:40:13 +08:00
{
2022-10-12 14:20:03 +08:00
_iFileService = iFileService;
2022-05-03 19:40:13 +08:00
_env = env;
2022-10-17 01:10:31 +08:00
_thumbnailSharpInvoer = thumbnailSharpInvoer;
2022-05-03 19:40:13 +08:00
}
/// <summary>
2022-10-17 01:10:31 +08:00
/// 文件下载,只需用文件code即可,可选择是否为缩略图
2022-05-03 19:40:13 +08:00
/// </summary>
2022-10-12 14:20:03 +08:00
/// <param name="code"></param>
2022-10-17 01:10:31 +08:00
/// <param name="isThumbnail"></param>
2022-05-03 19:40:13 +08:00
/// <returns></returns>
2022-10-17 01:10:31 +08:00
[Route("/api/file/{code}/{isThumbnail?}")]
2022-05-03 19:40:13 +08:00
[HttpGet]
2022-10-17 01:10:31 +08:00
public async Task<IActionResult> Get(long code, bool? isThumbnail)
2022-05-03 19:40:13 +08:00
{
2022-10-12 14:20:03 +08:00
var file = await _iFileService._repository.GetByIdAsync(code);
if (file is null)
2022-10-11 16:48:25 +08:00
{
return new NotFoundResult();
}
2022-05-03 19:40:13 +08:00
try
{
2022-10-17 01:10:31 +08:00
//如果为缩略图
if (isThumbnail is true)
{
file.FilePath = PathEnum.Thumbnail.ToString();
}
2022-10-12 14:20:03 +08:00
//路径为: 文件路径/文件id+文件扩展名
2022-10-16 22:51:53 +08:00
var path = Path.Combine($"{PathConst.wwwroot}/{file.FilePath}", file.Id.ToString() + Path.GetExtension(file.FileName));
2022-05-03 19:40:13 +08:00
var stream = System.IO.File.OpenRead(path);
2022-10-12 14:20:03 +08:00
var MimeType = Common.Helper.MimeHelper.GetMimeMapping(file.FileName);
2022-10-16 22:51:53 +08:00
return File(stream, MimeType, file.FileName);
2022-05-03 19:40:13 +08:00
}
catch
{
return new NotFoundResult();
}
}
/// <summary>
2022-10-17 01:10:31 +08:00
/// 多文件上传,type可空默认上传至File文件夹下swagger返回雪花id精度是有问题的同时如果时图片类型还需要进行缩略图制作
2022-05-03 19:40:13 +08:00
/// </summary>
2022-10-12 14:20:03 +08:00
/// <param name="type">文件类型,可空</param>
/// <param name="file">多文件表单</param>
/// <param name="remark">描述</param>
2022-05-03 19:40:13 +08:00
/// <returns></returns>
2022-10-12 14:20:03 +08:00
[Route("/api/file/Upload/{type?}")]
2022-05-03 19:40:13 +08:00
[HttpPost]
2022-10-16 22:51:53 +08:00
public async Task<Result> Upload([FromRoute] string? type, [FromForm] IFormFileCollection file, [FromQuery] string? remark)
2022-05-03 19:40:13 +08:00
{
2022-10-16 22:51:53 +08:00
type = type ?? PathEnum.File.ToString();
type = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(type.ToLower());
if (!Enum.IsDefined(typeof(PathEnum), type))
2022-10-12 14:20:03 +08:00
{
2022-10-16 22:51:53 +08:00
//后续类型可从字典表中获取
return Result.Error("上传失败!文件类型不支持!");
2022-10-12 14:20:03 +08:00
}
2022-10-16 22:51:53 +08:00
2022-10-12 14:20:03 +08:00
if (file.Count() == 0)
2022-10-11 16:48:25 +08:00
{
2022-10-12 14:20:03 +08:00
return Result.Error("未选择文件");
2022-10-11 16:48:25 +08:00
}
2022-10-12 14:20:03 +08:00
//批量插入
List<FileEntity> datas = new();
//返回的codes
List<long> codes = new();
2022-05-03 19:40:13 +08:00
try
{
2022-10-12 14:20:03 +08:00
foreach (var f in file)
2022-05-03 19:40:13 +08:00
{
2022-10-12 14:20:03 +08:00
FileEntity data = new();
data.Id = SnowFlakeSingle.Instance.NextId();
data.FileSize = ((decimal)f.Length) / 1024;
data.FileName = f.FileName;
data.FileType = Common.Helper.MimeHelper.GetMimeMapping(f.FileName);
data.FilePath = type;
data.Remark = remark;
data.IsDeleted = false;
//落盘文件文件名为雪花id+自己的扩展名
string filename = data.Id.ToString() + Path.GetExtension(f.FileName);
2022-10-12 14:43:35 +08:00
string typePath = $"{PathConst.wwwroot}/{type}";
if (!Directory.Exists(typePath))
2022-10-16 22:51:53 +08:00
{
2022-10-12 14:43:35 +08:00
Directory.CreateDirectory(typePath);
}
2022-10-17 01:10:31 +08:00
//生成文件
using (var stream = new FileStream(Path.Combine(typePath, filename), FileMode.CreateNew, FileAccess.ReadWrite))
2022-10-12 14:20:03 +08:00
{
await f.CopyToAsync(stream);
2022-10-17 01:10:31 +08:00
//如果是图片类型,还需要生成缩略图
if (PathEnum.Image.ToString().Equals(type))
{
//保存至缩略图路径
var result = _thumbnailSharpInvoer.CreateThumbnailBytes(thumbnailSize: 300,
imageStream: stream,
imageFormat: Format.Jpeg);
string thumbnailPath = $"{PathConst.wwwroot}/{PathEnum.Thumbnail}";
if (!Directory.Exists(thumbnailPath))
{
Directory.CreateDirectory(thumbnailPath);
}
await System.IO.File.WriteAllBytesAsync(Path.Combine(thumbnailPath, filename), result);
}
};
2022-10-12 14:20:03 +08:00
//将文件信息添加到数据库
datas.Add(data);
codes.Add(data.Id);
2022-05-03 19:40:13 +08:00
}
2022-10-12 14:20:03 +08:00
return Result.Success().SetData(codes).SetStatus(await _iFileService._repository.InsertRangeAsync(datas));
2022-05-03 19:40:13 +08:00
}
catch
{
return Result.Error();
}
}
2022-10-17 01:10:31 +08:00
/// <summary>
/// 一键同步图片到缩略图
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<Result> ThumbnailSync()
{
string typePath = $"{PathConst.wwwroot}/{PathEnum.Image}";
string thumbnailPath = $"{PathConst.wwwroot}/{PathEnum.Thumbnail}";
List<string> fileNames =FileHelper. GetAllFileNames(typePath);
foreach (var filename in fileNames)
{
if (System.IO.File.Exists(Path.Combine(thumbnailPath, filename)))
{
//如果缩略图存在,直接跳过
continue;
}
if (!Directory.Exists(typePath))
{
Directory.CreateDirectory(typePath);
}
using (var stream = new FileStream(Path.Combine(typePath, filename), FileMode.Open, FileAccess.ReadWrite))
{
//保存至缩略图路径
var result = _thumbnailSharpInvoer.CreateThumbnailBytes(thumbnailSize: 300,
imageStream: stream,
imageFormat: Format.Jpeg);
if (!Directory.Exists(thumbnailPath))
{
Directory.CreateDirectory(thumbnailPath);
}
await System.IO.File.WriteAllBytesAsync(Path.Combine(thumbnailPath, filename), result);
};
}
return Result.Success();
}
2022-05-03 19:40:13 +08:00
}
}