优化文件存储
This commit is contained in:
parent
b943a516e1
commit
25bead7cab
@ -100,7 +100,7 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
{
|
{
|
||||||
if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空");
|
if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空");
|
||||||
|
|
||||||
SysFile file = await SysFileService.SaveFileLocal(WebHostEnvironment.WebRootPath, fileName, fileDir, HttpContext.GetName(), formFile);
|
SysFile file = await SysFileService.SaveFileToLocal(WebHostEnvironment.WebRootPath, fileName, fileDir, HttpContext.GetName(), formFile);
|
||||||
return SUCCESS(new
|
return SUCCESS(new
|
||||||
{
|
{
|
||||||
url = uploadType == 1 ? file.FileUrl : file.AccessUrl,
|
url = uploadType == 1 ? file.FileUrl : file.AccessUrl,
|
||||||
@ -121,10 +121,9 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
[ActionPermissionFilter(Permission = "common")]
|
[ActionPermissionFilter(Permission = "common")]
|
||||||
public async Task<IActionResult> UploadFileAliyun([FromForm(Name = "file")] IFormFile formFile, string fileName = "", string fileDir = "")
|
public async Task<IActionResult> UploadFileAliyun([FromForm(Name = "file")] IFormFile formFile, string fileName = "", string fileDir = "")
|
||||||
{
|
{
|
||||||
if (fileDir.IsEmpty()) fileDir = "uploads";
|
|
||||||
if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空");
|
if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空");
|
||||||
string fileExt = Path.GetExtension(formFile.FileName);//文件后缀
|
string fileExt = Path.GetExtension(formFile.FileName);//文件后缀
|
||||||
double fileSize = formFile.Length / 1024.0;//文件大小KB
|
double fileSize = Math.Round(formFile.Length / 1024.0, 2);//文件大小KB
|
||||||
string[] NotAllowedFileExtensions = new string[] { ".bat", ".exe", ".jar", ".js" };
|
string[] NotAllowedFileExtensions = new string[] { ".bat", ".exe", ".jar", ".js" };
|
||||||
int MaxContentLength = 15;
|
int MaxContentLength = 15;
|
||||||
if (NotAllowedFileExtensions.Contains(fileExt))
|
if (NotAllowedFileExtensions.Contains(fileExt))
|
||||||
@ -135,26 +134,20 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
{
|
{
|
||||||
return ToResponse(ResultCode.CUSTOM_ERROR, "上传文件过大,不能超过 " + MaxContentLength + " MB");
|
return ToResponse(ResultCode.CUSTOM_ERROR, "上传文件过大,不能超过 " + MaxContentLength + " MB");
|
||||||
}
|
}
|
||||||
|
SysFile file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", fileDir, "", HttpContext.GetName())
|
||||||
(bool, string, string) result = new();
|
|
||||||
await Task.Run(() =>
|
|
||||||
{
|
|
||||||
result = SysFileService.SaveFile(fileDir, formFile, fileName, "");
|
|
||||||
});
|
|
||||||
if (!result.Item1)
|
|
||||||
{
|
|
||||||
return ToResponse(ApiResult.Error("阿里云连接失败"));
|
|
||||||
}
|
|
||||||
long id = await SysFileService.InsertFile(new(formFile.FileName, fileName, fileExt, fileSize + "kb", "", result.Item2, HttpContext.GetName())
|
|
||||||
{
|
{
|
||||||
StoreType = (int)Infrastructure.Enums.StoreType.ALIYUN,
|
StoreType = (int)Infrastructure.Enums.StoreType.ALIYUN,
|
||||||
FileType = formFile.ContentType
|
FileType = formFile.ContentType
|
||||||
});
|
};
|
||||||
|
file = await SysFileService.SaveFileToAliyun(file, formFile);
|
||||||
|
|
||||||
|
if (file.Id <= 0) { return ToResponse(ApiResult.Error("阿里云连接失败")); }
|
||||||
|
|
||||||
return SUCCESS(new
|
return SUCCESS(new
|
||||||
{
|
{
|
||||||
url = result.Item2,
|
url = file.AccessUrl,
|
||||||
fileName = result.Item3,
|
fileName = file.FileName,
|
||||||
fileId = id
|
fileId = file.Id.ToString()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@ -10,15 +10,72 @@ namespace ZR.Model.System.Dto
|
|||||||
public class SysFileDto
|
public class SysFileDto
|
||||||
{
|
{
|
||||||
public long Id { get; set; }
|
public long Id { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 文件原名
|
||||||
|
/// </summary>
|
||||||
|
public string RealName { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 文件类型
|
||||||
|
/// </summary>
|
||||||
|
public string FileType { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 描述 : 存储文件名
|
||||||
|
/// 空值 : true
|
||||||
|
/// </summary>
|
||||||
public string FileName { get; set; }
|
public string FileName { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 描述 : 文件存储地址 eg:/uploads/20220202
|
||||||
|
/// 空值 : true
|
||||||
|
/// </summary>
|
||||||
public string FileUrl { get; set; }
|
public string FileUrl { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 描述 : 仓库位置 eg:/uploads
|
||||||
|
/// 空值 : true
|
||||||
|
/// </summary>
|
||||||
public string StorePath { get; set; }
|
public string StorePath { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 描述 : 文件大小
|
||||||
|
/// 空值 : true
|
||||||
|
/// </summary>
|
||||||
public string FileSize { get; set; }
|
public string FileSize { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 描述 : 文件扩展名
|
||||||
|
/// 空值 : true
|
||||||
|
/// </summary>
|
||||||
public string FileExt { get; set; }
|
public string FileExt { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 描述 : 创建人
|
||||||
|
/// 空值 : true
|
||||||
|
/// </summary>
|
||||||
public string Create_by { get; set; }
|
public string Create_by { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 描述 : 上传时间
|
||||||
|
/// 空值 : true
|
||||||
|
/// </summary>
|
||||||
public DateTime? Create_time { get; set; }
|
public DateTime? Create_time { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 描述 : 存储类型
|
||||||
|
/// 空值 : true
|
||||||
|
/// </summary>
|
||||||
public int? StoreType { get; set; }
|
public int? StoreType { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 描述 : 访问路径
|
||||||
|
/// 空值 : true
|
||||||
|
/// </summary>
|
||||||
public string AccessUrl { get; set; }
|
public string AccessUrl { get; set; }
|
||||||
|
|
||||||
|
public SysFileDto() { }
|
||||||
|
public SysFileDto(string originFileName, string fileName, string ext, string fileSize, string storePath, string accessUrl, string create_by)
|
||||||
|
{
|
||||||
|
StorePath = storePath;
|
||||||
|
RealName = originFileName;
|
||||||
|
FileName = fileName;
|
||||||
|
FileExt = ext;
|
||||||
|
FileSize = fileSize;
|
||||||
|
AccessUrl = accessUrl;
|
||||||
|
Create_by = create_by;
|
||||||
|
Create_time = DateTime.Now;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public class SysFileQueryDto : PagerInfo
|
public class SysFileQueryDto : PagerInfo
|
||||||
{
|
{
|
||||||
|
|||||||
@ -18,7 +18,7 @@ namespace ZR.Model.System
|
|||||||
[SugarColumn(IsPrimaryKey = true)]
|
[SugarColumn(IsPrimaryKey = true)]
|
||||||
public long Id { get; set; }
|
public long Id { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 文件真实名
|
/// 文件原名
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string RealName { get; set; }
|
public string RealName { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -26,17 +26,17 @@ namespace ZR.Model.System
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string FileType { get; set; }
|
public string FileType { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 描述 : 文件名
|
/// 描述 : 存储文件名
|
||||||
/// 空值 : true
|
/// 空值 : true
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string FileName { get; set; }
|
public string FileName { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 描述 : 文件存储地址
|
/// 描述 : 文件存储地址 eg:/uploads/20220202
|
||||||
/// 空值 : true
|
/// 空值 : true
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string FileUrl { get; set; }
|
public string FileUrl { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 描述 : 仓库位置
|
/// 描述 : 仓库位置 eg:/uploads
|
||||||
/// 空值 : true
|
/// 空值 : true
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string StorePath { get; set; }
|
public string StorePath { get; set; }
|
||||||
@ -72,10 +72,10 @@ namespace ZR.Model.System
|
|||||||
public string AccessUrl { get; set; }
|
public string AccessUrl { get; set; }
|
||||||
|
|
||||||
public SysFile() { }
|
public SysFile() { }
|
||||||
public SysFile(string realName, string fileName, string ext, string fileSize, string storePath, string accessUrl,string create_by)
|
public SysFile(string originFileName, string fileName, string ext, string fileSize, string storePath, string accessUrl,string create_by)
|
||||||
{
|
{
|
||||||
StorePath = storePath;
|
StorePath = storePath;
|
||||||
RealName = realName;
|
RealName = originFileName;
|
||||||
FileName = fileName;
|
FileName = fileName;
|
||||||
FileExt = ext;
|
FileExt = ext;
|
||||||
FileSize = fileSize;
|
FileSize = fileSize;
|
||||||
|
|||||||
@ -19,19 +19,21 @@ namespace ZR.Service.System.IService
|
|||||||
/// <param name="rootPath"></param>
|
/// <param name="rootPath"></param>
|
||||||
/// <param name="userName"></param>
|
/// <param name="userName"></param>
|
||||||
/// <returns>文件对象</returns>
|
/// <returns>文件对象</returns>
|
||||||
Task<SysFile> SaveFileLocal(string rootPath, string fileName, string fileDir, string userName, IFormFile formFile);
|
Task<SysFile> SaveFileToLocal(string rootPath, string fileName, string fileDir, string userName, IFormFile formFile);
|
||||||
(bool, string, string) SaveFile(string picdir, IFormFile formFile, string customFileName, string bucketName);
|
|
||||||
|
Task<SysFile> SaveFileToAliyun(SysFile file, IFormFile formFile);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 按时间来创建文件夹
|
/// 按时间来创建文件夹
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="path"></param>
|
/// <param name="path"></param>
|
||||||
|
/// <param name="byTimeStore"></param>
|
||||||
/// <returns>eg: 2020/11/3</returns>
|
/// <returns>eg: 2020/11/3</returns>
|
||||||
string GetdirPath(string path = "");
|
string GetdirPath(string path = "", bool byTimeStore = true);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 取文件名的MD5值(16位)
|
/// 取文件名的MD5值(16位)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="name">文件名,不包括扩展名</param>
|
/// <param name="str">文件名,不包括扩展名</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
string HashFileName(string str = null);
|
string HashFileName(string str = null);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,13 +35,12 @@ namespace ZR.Service.System
|
|||||||
/// 存储本地
|
/// 存储本地
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<SysFile> SaveFileLocal(string rootPath, string fileName, string fileDir, string userName, IFormFile formFile)
|
public async Task<SysFile> SaveFileToLocal(string rootPath, string fileName, string fileDir, string userName, IFormFile formFile)
|
||||||
{
|
{
|
||||||
string fileExt = Path.GetExtension(formFile.FileName);
|
string fileExt = Path.GetExtension(formFile.FileName);
|
||||||
string hashFileName = FileUtil.HashFileName();
|
fileName = (fileName.IsEmpty() ? HashFileName() : fileName) + fileExt;
|
||||||
fileName = (fileName.IsEmpty() ? hashFileName : fileName) + fileExt;
|
|
||||||
fileDir = fileDir.IsEmpty() ? "uploads" : fileDir;
|
fileDir = fileDir.IsEmpty() ? "uploads" : fileDir;
|
||||||
string filePath = FileUtil.GetdirPath(fileDir);
|
string filePath = GetdirPath(fileDir);
|
||||||
string finalFilePath = Path.Combine(rootPath, filePath, fileName);
|
string finalFilePath = Path.Combine(rootPath, filePath, fileName);
|
||||||
double fileSize = Math.Round(formFile.Length / 1024.0, 2);
|
double fileSize = Math.Round(formFile.Length / 1024.0, 2);
|
||||||
|
|
||||||
@ -68,37 +67,39 @@ namespace ZR.Service.System
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 上传文件到阿里云
|
/// 上传文件到阿里云
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="picdir">文件夹</param>
|
/// <param name="file"></param>
|
||||||
/// <param name="formFile"></param>
|
/// <param name="formFile"></param>
|
||||||
/// <param name="customFileName">自定义文件名</param>
|
|
||||||
/// <param name="bucketName">存储桶</param>
|
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public (bool, string, string) SaveFile(string picdir, IFormFile formFile, string customFileName, string bucketName)
|
public async Task<SysFile> SaveFileToAliyun(SysFile file, IFormFile formFile)
|
||||||
{
|
{
|
||||||
// eg: uploads/2020/08/18
|
file.FileName = (file.FileName.IsEmpty() ? HashFileName() : file.FileName) + file.FileExt;
|
||||||
//string dir = GetdirPath(picdir.ToString());
|
file.StorePath = GetdirPath(file.StorePath);
|
||||||
|
string finalPath = Path.Combine(file.StorePath, file.FileName);
|
||||||
|
HttpStatusCode statusCode = AliyunOssHelper.PutObjectFromFile(formFile.OpenReadStream(), finalPath, "");
|
||||||
|
if (statusCode != HttpStatusCode.OK) return file;
|
||||||
|
|
||||||
string tempName = customFileName.IsEmpty() ? HashFileName() : customFileName;
|
file.StorePath = file.StorePath;
|
||||||
string fileExt = Path.GetExtension(formFile.FileName);
|
file.FileUrl = finalPath;
|
||||||
string fileName = tempName + fileExt;
|
file.AccessUrl = string.Concat(domainUrl, "/", file.StorePath.Replace("\\", "/"), "/", file.FileName);
|
||||||
string webUrl = string.Concat(domainUrl, "/", picdir, "/", fileName);
|
file.Id = await InsertFile(file);
|
||||||
|
|
||||||
HttpStatusCode statusCode = AliyunOssHelper.PutObjectFromFile(formFile.OpenReadStream(), Path.Combine(picdir, fileName), bucketName);
|
return file;
|
||||||
|
|
||||||
return (statusCode == HttpStatusCode.OK, webUrl, fileName);
|
|
||||||
}
|
}
|
||||||
public string GetdirPath(string path = "")
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取文件存储目录
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="storePath"></param>
|
||||||
|
/// <param name="byTimeStore">是否按年月日存储</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public string GetdirPath(string storePath = "", bool byTimeStore = true)
|
||||||
{
|
{
|
||||||
DateTime date = DateTime.Now;
|
DateTime date = DateTime.Now;
|
||||||
int year = date.Year;
|
string timeDir = date.ToString("yyyyMMdd");
|
||||||
int month = date.Month;
|
|
||||||
int day = date.Day;
|
|
||||||
//int hour = date.Hour;
|
|
||||||
|
|
||||||
string timeDir = $"{year}/{month}/{day}";// date.ToString("yyyyMM/dd/HH/");
|
if (!string.IsNullOrEmpty(storePath))
|
||||||
if (!string.IsNullOrEmpty(path))
|
|
||||||
{
|
{
|
||||||
timeDir = path + "/" + timeDir;
|
timeDir = Path.Combine(storePath, timeDir);
|
||||||
}
|
}
|
||||||
return timeDir;
|
return timeDir;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -68,11 +68,11 @@
|
|||||||
<pagination class="mt10" background :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
|
<pagination class="mt10" background :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
|
||||||
|
|
||||||
<!-- 添加或修改文件存储对话框 -->
|
<!-- 添加或修改文件存储对话框 -->
|
||||||
<el-dialog :title="title" :lock-scroll="false" :visible.sync="open" width="380px">
|
<el-dialog :title="title" :lock-scroll="false" :visible.sync="open" width="400px">
|
||||||
<el-form ref="form" :model="form" :rules="rules" label-position="left">
|
<el-form ref="form" :model="form" :rules="rules" label-width="90px" label-position="left">
|
||||||
<el-row>
|
<el-row>
|
||||||
<el-col :lg="24">
|
<el-col :lg="24">
|
||||||
<el-form-item label="" prop="storeType">
|
<el-form-item label="存储类型" prop="storeType">
|
||||||
<el-radio-group v-model="form.storeType" placeholder="请选择存储类型">
|
<el-radio-group v-model="form.storeType" placeholder="请选择存储类型">
|
||||||
<el-radio v-for="item in storeTypeOptions" :key="item.dictValue" :label="parseInt(item.dictValue)">
|
<el-radio v-for="item in storeTypeOptions" :key="item.dictValue" :label="parseInt(item.dictValue)">
|
||||||
{{item.dictLabel}}
|
{{item.dictLabel}}
|
||||||
@ -80,13 +80,21 @@
|
|||||||
</el-radio-group>
|
</el-radio-group>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
</el-col>
|
</el-col>
|
||||||
|
<!-- <el-col :lg="24">
|
||||||
|
<el-form-item label="按时间存储" prop="timeStore">
|
||||||
|
<el-radio-group v-model="form.storeType" placeholder="是否按时间(yyyyMMdd)存储">
|
||||||
|
<el-radio :key="1" :label="1">是 </el-radio>
|
||||||
|
<el-radio :key="0" :label="0">否 </el-radio>
|
||||||
|
</el-radio-group>
|
||||||
|
</el-form-item>
|
||||||
|
</el-col> -->
|
||||||
<el-col :lg="24">
|
<el-col :lg="24">
|
||||||
<el-form-item label="" prop="">
|
<el-form-item label="存储文件夹" prop="storePath">
|
||||||
<el-input v-model="form.storePath" placeholder="请输入存储文件夹" clearable="" auto-complete="" />
|
<el-input v-model="form.storePath" placeholder="请输入存储文件夹" clearable="" auto-complete="" />
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
</el-col>
|
</el-col>
|
||||||
<el-col :lg="24">
|
<el-col :lg="24">
|
||||||
<el-form-item label="" prop="fileName">
|
<el-form-item label="自定文件名" prop="fileName">
|
||||||
<el-input v-model="form.fileName" placeholder="请输入文件名" clearable="" />
|
<el-input v-model="form.fileName" placeholder="请输入文件名" clearable="" />
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
</el-col>
|
</el-col>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user