1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- using Admin.Core.Common.Attributes;
- using Admin.Core.Common.Configs;
- using Admin.Core.Common.Output;
- using Microsoft.AspNetCore.Http;
- using System;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using FileInfo = Admin.Core.Common.Files.FileInfo;
- namespace Admin.Core.Common.Helpers
- {
- /// <summary>
- /// 文件上传帮助类
- /// </summary>
- [SingleInstance]
- public class UploadHelper
- {
- /// <summary>
- /// 上传单文件
- /// </summary>
- /// <param name="file"></param>
- /// <param name="config"></param>
- /// <param name="args"></param>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- public async Task<IResponseOutput<FileInfo>> UploadAsync(IFormFile file, FileUploadConfig config, object args, CancellationToken cancellationToken = default)
- {
- var res = new ResponseOutput<FileInfo>();
- if (file == null || file.Length < 1)
- {
- return res.NotOk("请上传文件!");
- }
- //格式限制
- if (!config.ContentType.Contains(file.ContentType))
- {
- return res.NotOk("文件格式错误");
- }
- //大小限制
- if (!(file.Length <= config.MaxSize))
- {
- return res.NotOk("文件过大");
- }
- var fileInfo = new FileInfo(file.FileName, file.Length)
- {
- UploadPath = config.UploadPath,
- RequestPath = config.RequestPath
- };
- var dateTimeFormat = config.DateTimeFormat.NotNull() ? DateTime.Now.ToString(config.DateTimeFormat) : "";
- var format = config.Format.NotNull() ? StringHelper.Format(config.Format, args) : "";
- fileInfo.RelativePath = Path.Combine(dateTimeFormat, format).ToPath();
- if (!Directory.Exists(fileInfo.FileDirectory))
- {
- Directory.CreateDirectory(fileInfo.FileDirectory);
- }
- fileInfo.SaveName = $"{IdWorkerHelper.GenId64()}.{fileInfo.Extension}";
- await SaveAsync(file, fileInfo.FilePath, cancellationToken);
- return res.Ok(fileInfo);
- }
- /// <summary>
- /// 保存文件
- /// </summary>
- /// <param name="file"></param>
- /// <param name="filePath"></param>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- public async Task SaveAsync(IFormFile file, string filePath, CancellationToken cancellationToken = default)
- {
- using (var stream = new FileStream(filePath, FileMode.Create))
- {
- await file.CopyToAsync(stream, cancellationToken);
- }
- }
- }
- }
|