😎优化文件上传模块

This commit is contained in:
zuohuaijun 2024-11-03 19:33:02 +08:00
parent d5f9a60f47
commit 073201d774
9 changed files with 105 additions and 1987 deletions

View File

@ -32,6 +32,10 @@ public class PageFileInput : BasePageInput
public DateTime? EndTime { get; set; } public DateTime? EndTime { get; set; }
} }
public class FileInput : BaseIdInput
{
}
public class DeleteFileInput : BaseIdInput public class DeleteFileInput : BaseIdInput
{ {
} }

View File

@ -57,17 +57,6 @@ public class SysFileService : IDynamicApiController, ITransient
.ToPagedListAsync(input.Page, input.PageSize); .ToPagedListAsync(input.Page, input.PageSize);
} }
/// <summary>
/// 上传文件 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[DisplayName("上传文件")]
public async Task<SysFile> UploadFile([FromForm] UploadFileInput input)
{
return await HandleUploadFile(input);
}
/// <summary> /// <summary>
/// 上传文件Base64 🔖 /// 上传文件Base64 🔖
/// </summary> /// </summary>
@ -122,7 +111,7 @@ public class SysFileService : IDynamicApiController, ITransient
[DisplayName("根据文件Id或Url下载")] [DisplayName("根据文件Id或Url下载")]
public async Task<IActionResult> DownloadFile(SysFile input) public async Task<IActionResult> DownloadFile(SysFile input)
{ {
var file = input.Id > 0 ? await GetFile(input) : await _sysFileRep.CopyNew().GetFirstAsync(u => u.Url == input.Url); var file = input.Id > 0 ? await GetFile(input.Id) : await _sysFileRep.CopyNew().GetFirstAsync(u => u.Url == input.Url);
var fileName = HttpUtility.UrlEncode(file.FileName, Encoding.GetEncoding("UTF-8")); var fileName = HttpUtility.UrlEncode(file.FileName, Encoding.GetEncoding("UTF-8"));
var filePath = Path.Combine(file.FilePath, file.Id.ToString() + file.Suffix); var filePath = Path.Combine(file.FilePath, file.Id.ToString() + file.Suffix);
@ -149,12 +138,12 @@ public class SysFileService : IDynamicApiController, ITransient
/// <summary> /// <summary>
/// 文件预览 🔖 /// 文件预览 🔖
/// </summary> /// </summary>
/// <param name="Id"></param> /// <param name="id"></param>
/// <returns></returns> /// <returns></returns>
[DisplayName("文件预览")] [DisplayName("文件预览")]
public async Task<IActionResult> GetPreview([FromRoute] long Id) public async Task<IActionResult> GetPreview([FromRoute] long id)
{ {
var file = await GetFile(new SysFile { Id = Id }); var file = await GetFile(id);
//var fileName = HttpUtility.UrlEncode(file.FileName, Encoding.GetEncoding("UTF-8")); //var fileName = HttpUtility.UrlEncode(file.FileName, Encoding.GetEncoding("UTF-8"));
var filePath = Path.Combine(file.FilePath, file.Id.ToString() + file.Suffix); var filePath = Path.Combine(file.FilePath, file.Id.ToString() + file.Suffix);
@ -276,27 +265,28 @@ public class SysFileService : IDynamicApiController, ITransient
var isExist = await _sysFileRep.IsAnyAsync(u => u.Id == input.Id); var isExist = await _sysFileRep.IsAnyAsync(u => u.Id == input.Id);
if (!isExist) throw Oops.Oh(ErrorCodeEnum.D8000); if (!isExist) throw Oops.Oh(ErrorCodeEnum.D8000);
await _sysFileRep.UpdateAsync(u => new SysFile() { FileName = input.FileName, FileType = input.FileType, IsPublic = input.IsPublic }, u => u.Id == input.Id); await _sysFileRep.UpdateAsync(u => input.Adapt<SysFile>(), u => u.Id == input.Id);
} }
/// <summary> /// <summary>
/// 获取文件 🔖 /// 获取文件 🔖
/// </summary> /// </summary>
/// <param name="input"></param> /// <param name="id"></param>
/// <returns></returns> /// <returns></returns>
[DisplayName("获取文件")] [DisplayName("获取文件")]
public async Task<SysFile> GetFile([FromQuery] SysFile input) public async Task<SysFile> GetFile([FromQuery] long id)
{ {
var file = await _sysFileRep.CopyNew().GetByIdAsync(input.Id); var file = await _sysFileRep.CopyNew().GetByIdAsync(id);
return file ?? throw Oops.Oh(ErrorCodeEnum.D8000); return file ?? throw Oops.Oh(ErrorCodeEnum.D8000);
} }
/// <summary> /// <summary>
/// 上传文件 /// 上传文件 🔖
/// </summary> /// </summary>
/// <param name="input"></param> /// <param name="input"></param>
/// <returns></returns> /// <returns></returns>
private async Task<SysFile> HandleUploadFile(UploadFileInput input) [DisplayName("上传文件")]
public async Task<SysFile> UploadFile([FromForm] UploadFileInput input)
{ {
if (input.File == null) throw Oops.Oh(ErrorCodeEnum.D8000); if (input.File == null) throw Oops.Oh(ErrorCodeEnum.D8000);
@ -422,7 +412,7 @@ public class SysFileService : IDynamicApiController, ITransient
[DisplayName("上传头像")] [DisplayName("上传头像")]
public async Task<SysFile> UploadAvatar([Required] IFormFile file) public async Task<SysFile> UploadAvatar([Required] IFormFile file)
{ {
var sysFile = await HandleUploadFile(new UploadFileInput { File = file, AllowSuffix = _imageType, SavePath = "upload/avatar" }); var sysFile = await UploadFile(new UploadFileInput { File = file, AllowSuffix = _imageType, SavePath = "upload/avatar" });
var sysUserRep = _sysFileRep.ChangeRepository<SqlSugarRepository<SysUser>>(); var sysUserRep = _sysFileRep.ChangeRepository<SqlSugarRepository<SysUser>>();
var user = await sysUserRep.GetByIdAsync(_userManager.UserId); var user = await sysUserRep.GetByIdAsync(_userManager.UserId);
@ -444,7 +434,7 @@ public class SysFileService : IDynamicApiController, ITransient
[DisplayName("上传电子签名")] [DisplayName("上传电子签名")]
public async Task<SysFile> UploadSignature([Required] IFormFile file) public async Task<SysFile> UploadSignature([Required] IFormFile file)
{ {
var sysFile = await HandleUploadFile(new UploadFileInput { File = file, AllowSuffix = _imageType, SavePath = "upload/signature" }); var sysFile = await UploadFile(new UploadFileInput { File = file, AllowSuffix = _imageType, SavePath = "upload/signature" });
var sysUserRep = _sysFileRep.ChangeRepository<SqlSugarRepository<SysUser>>(); var sysUserRep = _sysFileRep.ChangeRepository<SqlSugarRepository<SysUser>>();
var user = await sysUserRep.GetByIdAsync(_userManager.UserId); var user = await sysUserRep.GetByIdAsync(_userManager.UserId);

File diff suppressed because one or more lines are too long

View File

@ -82,30 +82,6 @@ export interface PageCodeGenInput {
*/ */
descStr?: string | null; descStr?: string | null;
/**
*
*
* @type {string}
* @memberof PageCodeGenInput
*/
tableName?: string | null;
/**
*
*
* @type {string}
* @memberof PageCodeGenInput
*/
busName?: string | null;
/**
*
*
* @type {string}
* @memberof PageCodeGenInput
*/
nameSpace?: string | null;
/** /**
* *
* *
@ -114,30 +90,6 @@ export interface PageCodeGenInput {
*/ */
authorName?: string | null; authorName?: string | null;
/**
*
*
* @type {string}
* @memberof PageCodeGenInput
*/
generateType?: string | null;
/**
*
*
* @type {boolean}
* @memberof PageCodeGenInput
*/
generateMenu?: boolean;
/**
* 使 Api Service
*
* @type {boolean}
* @memberof PageCodeGenInput
*/
isApiService?: boolean;
/** /**
* *
* *
@ -186,6 +138,38 @@ export interface PageCodeGenInput {
*/ */
connectionString?: string | null; connectionString?: string | null;
/**
*
*
* @type {string}
* @memberof PageCodeGenInput
*/
generateType?: string | null;
/**
*
*
* @type {string}
* @memberof PageCodeGenInput
*/
tableName?: string | null;
/**
*
*
* @type {string}
* @memberof PageCodeGenInput
*/
nameSpace?: string | null;
/**
*
*
* @type {string}
* @memberof PageCodeGenInput
*/
busName?: string | null;
/** /**
* *
* *
@ -202,6 +186,14 @@ export interface PageCodeGenInput {
*/ */
menuApplication?: string | null; menuApplication?: string | null;
/**
*
*
* @type {boolean}
* @memberof PageCodeGenInput
*/
generateMenu?: boolean;
/** /**
* *
* *
@ -241,4 +233,12 @@ export interface PageCodeGenInput {
* @memberof PageCodeGenInput * @memberof PageCodeGenInput
*/ */
printName?: string | null; printName?: string | null;
/**
* 使 Api Service
*
* @type {boolean}
* @memberof PageCodeGenInput
*/
isApiService?: boolean;
} }

View File

@ -85,6 +85,14 @@ export interface SysOAuthUser {
*/ */
isDelete?: boolean; isDelete?: boolean;
/**
*
*
* @type {string}
* @memberof SysOAuthUser
*/
email?: string | null;
/** /**
* Id * Id
* *
@ -147,14 +155,6 @@ export interface SysOAuthUser {
*/ */
avatar?: string | null; avatar?: string | null;
/**
*
*
* @type {string}
* @memberof SysOAuthUser
*/
email?: string | null;
/** /**
* *
* *

View File

@ -29,14 +29,6 @@ export interface SysRegion {
*/ */
id?: number; id?: number;
/**
*
*
* @type {string}
* @memberof SysRegion
*/
name: string;
/** /**
* Id * Id
* *
@ -45,6 +37,14 @@ export interface SysRegion {
*/ */
pid?: number; pid?: number;
/**
*
*
* @type {string}
* @memberof SysRegion
*/
name: string;
/** /**
* *
* *

View File

@ -93,14 +93,6 @@ export interface SysSchedule {
*/ */
tenantId?: number | null; tenantId?: number | null;
/**
*
*
* @type {string}
* @memberof SysSchedule
*/
content: string;
/** /**
* Id * Id
* *
@ -133,6 +125,14 @@ export interface SysSchedule {
*/ */
endTime?: string | null; endTime?: string | null;
/**
*
*
* @type {string}
* @memberof SysSchedule
*/
content: string;
/** /**
* @type {FinishStatusEnum} * @type {FinishStatusEnum}
* @memberof SysSchedule * @memberof SysSchedule

View File

@ -49,7 +49,7 @@
</el-row> </el-row>
<el-row> <el-row>
<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20"> <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
<el-form-item label="所属ID" prop="fileName"> <el-form-item label="所属Id" prop="fileName">
<el-input v-model="state.ruleForm.belongId" placeholder="所属ID" clearable /> <el-input v-model="state.ruleForm.belongId" placeholder="所属ID" clearable />
</el-form-item> </el-form-item>
</el-col> </el-col>
@ -71,7 +71,7 @@ import { ElMessage } from 'element-plus';
import { getAPI } from '/@/utils/axios-utils'; import { getAPI } from '/@/utils/axios-utils';
import { SysFileApi } from '/@/api-services/api'; import { SysFileApi } from '/@/api-services/api';
import { FileInput } from '/@/api-services/models'; import { SysFile } from '/@/api-services/models';
const props = defineProps({ const props = defineProps({
title: String, title: String,
@ -81,7 +81,7 @@ const emits = defineEmits(['handleQuery']);
const ruleFormRef = ref(); const ruleFormRef = ref();
const state = reactive({ const state = reactive({
isShowDialog: false, isShowDialog: false,
ruleForm: {} as FileInput, ruleForm: {} as SysFile,
}); });
// //

View File

@ -92,7 +92,7 @@
<el-option label="归档文件" value="归档文件" /> <el-option label="归档文件" value="归档文件" />
</el-select> </el-select>
是否公开 是否公开
<el-radio-group v-model="state.isPublic"> <el-radio-group v-model="state.isPublic" style="margin-bottom: 10px">
<el-radio :value="false"></el-radio> <el-radio :value="false"></el-radio>
<el-radio :value="true"></el-radio> <el-radio :value="true"></el-radio>
</el-radio-group> </el-radio-group>