You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.8 KiB
95 lines
2.8 KiB
using Mapster;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Wood.Data.Repository;
|
|
using Wood.Entity.SystemManage;
|
|
using Wood.Service.BaseService;
|
|
using Wood.Service.SystemManage.Dto;
|
|
using Wood.Service.SystemManage.Manager;
|
|
using Wood.Util;
|
|
|
|
namespace Wood.Service.SystemManage
|
|
{
|
|
/// <summary>
|
|
/// 上传文件
|
|
/// </summary>
|
|
public class FileService : ApiService
|
|
{
|
|
private readonly FileManager _fileManager;
|
|
|
|
public FileService(FileManager fileManager)
|
|
{
|
|
_fileManager = fileManager;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 上传文件
|
|
/// </summary>
|
|
/// <param name="file"></param>
|
|
/// <param name="bucket">存储的仓储 默认:local 本地存储</param>
|
|
/// <returns></returns>
|
|
public async Task<FileDto> Upload(IFormFile file, [FromQuery]string? bucket="local")
|
|
{
|
|
if (file == null || file.Length == 0)
|
|
throw Oops.Oh("没有可以保存的文件!");
|
|
|
|
var folder = Path.Combine(GlobalContext.HostingEnvironment!.WebRootPath, "uploads", DateTime.Now.ToString("yyyy_MM_dd"));
|
|
if (!Directory.Exists(folder))
|
|
Directory.CreateDirectory(folder);
|
|
var ext = Path.GetExtension(file.FileName);
|
|
var uniqueFileName = $"{IdGeneratorHelper.Instance.GetGuid("N")}{ext}";
|
|
var filePath = Path.Combine(folder, uniqueFileName);
|
|
|
|
using (var stream = new FileStream(filePath, FileMode.Create))
|
|
await file.CopyToAsync(stream);
|
|
|
|
FileEntity fileEntity = new FileEntity()
|
|
{
|
|
BucketName = bucket,
|
|
Provider = "",
|
|
FileName = file.FileName,
|
|
FilePath = Path.GetRelativePath(GlobalContext.HostingEnvironment!.WebRootPath, filePath),
|
|
SizeKb =(file.Length / 1024.0F),
|
|
SizeInfo = "",
|
|
Suffix = ext,
|
|
};
|
|
|
|
await _fileManager.AsRepository().InsertAsync(fileEntity);
|
|
|
|
return fileEntity.Adapt<FileDto>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据code 获取文件列表
|
|
/// </summary>
|
|
/// <param name="code"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<FileDto>> GetFilesByCode(string code)
|
|
{
|
|
if (string.IsNullOrEmpty(code))
|
|
return new List<FileDto>();
|
|
|
|
var files= await _fileManager.GetByCode(code);
|
|
|
|
var dtos= files.Adapt<List<FileDto>>();
|
|
|
|
return dtos;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据code 获取文件列表
|
|
/// </summary>
|
|
/// <param name="code"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<string>> GetFilePathsByCode(string code)
|
|
{
|
|
return await _fileManager.GetFilePathsByCode(code);
|
|
}
|
|
}
|
|
}
|
|
|