using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Text; using System.Text.RegularExpressions; using System.Web; using Aliyun.OSS.Util; using Common.Enum; using Common.Util; using Entity.Base; using Entity.DbModel.System.SysBaseObject; using Entity.Dto.Req; using Furion.VirtualFileServer; using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.Configuration; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using OnceMi.AspNetCore.OSS; using Repository.System; using Service.Mgr; using SqlSugar; using Yitter.IdGenerator; namespace Service.System { [Scope("SingleInstance")] public class SysFileServices : BaseServices { private readonly SysFileRepository _sysFileRep; private readonly SysUserRepository _sysUserRep; private readonly SysFileMgr _sysFileMgr; private readonly string _imageType = ".jpg.png.bmp.gif.tif"; public SysFileServices( SysUserRepository sysUserRepository, SysFileRepository sysFileRep, SysFileMgr sysFileMgr) { _sysUserRep = sysUserRepository; _sysFileRep = sysFileRep; _sysFileMgr = sysFileMgr; } /// /// 上传头像 🔖 /// /// /// [DisplayName("上传头像")] public async Task UploadAvatar([Required] IFormFile file, string path) { var sysFile = await _sysFileMgr.HandleUploadFile(file, path, _imageType); var user = _sysUserRep.QueryByClause(u => u.Id == UserManager.UserId); await _sysUserRep.UpdateAsync(u => new SysUser() { Avatar = sysFile.Url }, u => u.Id == user.Id); return sysFile; } /// /// 获取文件分页列表 🔖 /// /// /// [DisplayName("获取文件分页列表")] public async Task> Page(PageFileReq input) { RefAsync total = 0; var items = await _sysFileRep.QueryPageAsync( !string.IsNullOrWhiteSpace(input.FileName), u => u.FileName.Contains(input.FileName.Trim()), !string.IsNullOrWhiteSpace(input.StartTime.ToString()) && !string.IsNullOrWhiteSpace(input.EndTime.ToString()), u => u.CreateTime >= input.StartTime && u.CreateTime <= input.EndTime, u => u.CreateTime, input.Page, input.PageSize, total); return SqlSugarPagedExtensions.CreateSqlSugarPagedList(items, total, input.Page, input.PageSize); } /// /// 上传文件 🔖 /// /// /// /// [DisplayName("上传文件")] public async Task UploadFile([Required] IFormFile file, [FromQuery] string? path) { return await _sysFileMgr.HandleUploadFile(file, path); } /// /// 上传文件Base64 /// /// /// /// /// /// private async Task UploadFileFromBase64(string strBase64, string fileName, string contentType, string? path) { byte[] fileData = Convert.FromBase64String(strBase64); var ms = new MemoryStream(); ms.Write(fileData); ms.Seek(0, SeekOrigin.Begin); if (string.IsNullOrEmpty(fileName)) fileName = $"{YitIdHelper.NextId()}.jpg"; if (string.IsNullOrEmpty(contentType)) contentType = "image/jpg"; IFormFile formFile = new FormFile(ms, 0, fileData.Length, "file", fileName) { Headers = new HeaderDictionary(), ContentType = contentType }; return await UploadFile(formFile, path); } /// /// 上传文件Base64 🔖 /// /// /// [DisplayName("上传文件Base64")] [HttpPost] public async Task UploadFileFromBase64(UploadFileFromBase64Req input) { return await UploadFileFromBase64(input.FileDataBase64, input.FileName, input.ContentType, input.Path); } /// /// 上传多文件 🔖 /// /// /// [DisplayName("上传多文件")] public async Task> UploadFiles([Required] List files) { var filelist = new List(); foreach (var file in files) { filelist.Add(await UploadFile(file, "")); } return filelist; } ///// ///// 根据文件Id或Url下载 🔖 ///// ///// ///// //[DisplayName("根据文件Id或Url下载")] //public async Task DownloadFile(FileReq input) //{ // var file = input.Id > 0 ? await GetFile(input) : await _sysFileRep.QueryByClauseAsync(u => u.Url == input.Url); // var fileName = HttpUtility.UrlEncode(file.FileName, Encoding.GetEncoding("UTF-8")); // if (_OSSProviderOptions.IsEnable) // { // var filePath = string.Concat(file.FilePath, "/", file.Id.ToString() + file.Suffix); // var stream = await (await _OSSService.PresignedGetObjectAsync(file.BucketName.ToString(), filePath, 5)).GetAsStreamAsync(); // return new FileStreamResult(stream.Stream, "application/octet-stream") { FileDownloadName = fileName + file.Suffix }; // } // else // { // var filePath = Path.Combine(file.FilePath, file.Id.ToString() + file.Suffix); // var path = Path.Combine(App.WebHostEnvironment.WebRootPath, filePath); // return new FileStreamResult(new FileStream(path, FileMode.Open), "application/octet-stream") { FileDownloadName = fileName + file.Suffix }; // } //} /// /// 更新文件 🔖 /// /// /// [ApiDescriptionSettings(Name = "Update"), HttpPost] [DisplayName("更新文件")] public async Task UpdateFile(FileReq input) { string result = ""; var isExist = await _sysFileRep.QueryByClauseAsync(u => u.Id == input.Id); if (isExist == null) result = "文件不存在"; bool updateResult = await _sysFileRep.UpdateAsync(u => new SysFile() { FileName = input.FileName }, u => u.Id == input.Id); if (updateResult) { result = "更新文件成功"; } return result; } /// /// 获取文件 /// /// /// private async Task GetFile([FromQuery] FileReq input) { var file = await _sysFileRep.QueryByClauseAsync(u => u.Id == input.Id); if (file == null) { //文件不存在 return new SysFile(); } return file; } } }