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;
namespace AI.Platform.Service;
[ApiGroup(ApiGroupNames.System)]
public class FileService : BaseService
{
///
///
///
private readonly IHttpContextAccessor _contextAccessor;
public FileService(IHttpContextAccessor contextAccessor)
{
_contextAccessor = contextAccessor;
}
///
/// 上传图片
///
///
///
[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, AllowAnonymous]
public async Task UploadMedia(IFormFile file)
{
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}";
// 获取当前日期
var currentDate = DateTime.Now;
// 构建目录路径
var directoryPath = Path.Combine("wwwroot", "madia", 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/{currentDate:yyyy}/{currentDate:MM}/{currentDate:dd}/{fileName}";
//return relativePath;
return new MediaFileUploadOutput()
{
isSuccess = true,
msg = "上传成功",
fileName = file.FileName,
guidName = fileName,
savePath = filePath,
extension = fileExtension
};
}
}