using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; using AI.Platform.Service.Common; using AI.Platform.Core.Entity; using Microsoft.AspNetCore.Components; using AI.Platform.Core; using static AI.Platform.Core.Entity.PublicEnum; using System.Net; using System; using AI.Platform.Service.Output; using Microsoft.AspNetCore.Http.HttpResults; using AI.Platform.Core.Entity.Site; namespace AI.Platform.Service; [ApiGroup(ApiGroupNames.System)] public class FileService : BaseService { /// /// /// private readonly IHttpContextAccessor _contextAccessor; private readonly SqlSugarRepository _siteReponsitory; public FileService(IHttpContextAccessor contextAccessor, SqlSugarRepository siteReponsitory) { _contextAccessor = contextAccessor; _siteReponsitory = siteReponsitory; } /// /// 上传图片 /// /// /// [HttpPost, AllowAnonymous] public async Task UploadImage(string classification = "default") { var host = _contextAccessor.GetHost(); var file = _contextAccessor.HttpContext.Request.Form.Files[0]; if (file.Length <= 0) { return ""; } using var memoryStream = new MemoryStream(); file.CopyTo(memoryStream); var fileBytes = memoryStream.ToArray(); // 获取文件扩展名 var fileExtension = Path.GetExtension(file.FileName); // 生成文件名 var id = Guid.NewGuid().ToString().ToUpper().Replace("-", ""); var fileName = $"{id}{fileExtension}"; // 获取当前日期 var currentDate = DateTime.Now; // 构建目录路径 var directoryPath = Path.Combine("wwwroot", "image", classification, currentDate.ToString("yyyy"), currentDate.ToString("MM"), currentDate.ToString("dd")); // 创建目录 if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } // 构建文件路径 var filePath = Path.Combine(directoryPath, fileName); // 保存文件 await System.IO.File.WriteAllBytesAsync(filePath, fileBytes); // 构建返回的相对路径 var relativePath = $"/image/{classification}/{currentDate:yyyy}/{currentDate:MM}/{currentDate:dd}/{fileName}"; return relativePath; } /// /// 上传多媒体文件 /// /// /// [HttpPost] //[RequestSizeLimit(307_200_000)] public async Task UploadMedia(IFormFile file,long siteId) { if (file == null || file.Length <= 0) { return new MediaFileUploadOutput() { isSuccess = false, msg = "上传文件为空" }; } using var memoryStream = new MemoryStream(); file.CopyTo(memoryStream); var fileBytes = memoryStream.ToArray(); // 获取文件扩展名 var fileExtension = Path.GetExtension(file.FileName); // 生成文件名 var id = Guid.NewGuid().ToString().ToUpper().Replace("-", ""); var fileName = $"{id}_{file.FileName}"; string siteName = "未知站点"; SiteEntity siteEntity = await _siteReponsitory.AsQueryable().Where(it => it.Id == siteId).FirstAsync(); if(siteEntity != null) { siteName = siteEntity.Name; } // 构建目录路径 var directoryPath = Path.Combine("wwwroot", "madia", siteName); // 创建目录 if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } // 构建文件路径 var filePath = Path.Combine(directoryPath, fileName); // 保存文件 await System.IO.File.WriteAllBytesAsync(filePath, fileBytes); //// 构建返回的相对路径 //var relativePath = $"/image/{currentDate:yyyy}/{currentDate:MM}/{currentDate:dd}/{fileName}"; //return relativePath; return new MediaFileUploadOutput() { isSuccess = true, msg = "上传成功", fileName = file.FileName, guidName = fileName, savePath = filePath, extension = fileExtension }; } }