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