新增文件上传分类 不同分类 次数限制不同 文件保存时间不同
This commit is contained in:
parent
25d481d7d6
commit
6d41f52de5
@ -15,6 +15,7 @@ namespace LMS.Repository.DTO
|
||||
public required string FileName { get; set; }
|
||||
public required string ContentType { get; set; }
|
||||
public Dictionary<string, string> Metadata { get; set; } = new();
|
||||
public string? Type { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -10,6 +10,7 @@ using LMS.Tools.HttpTool;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using OneOf.Types;
|
||||
using static LMS.Common.Enums.ResponseCodeEnum;
|
||||
using static LMS.Repository.DTO.FileUploadDto;
|
||||
using static LMS.Repository.FileUpload.FileRequestReturn;
|
||||
@ -88,19 +89,36 @@ namespace LMS.service.Service.FileUploadService
|
||||
{
|
||||
return APIResponseModel<UploadResult>.CreateErrorResponseModel(ResponseCode.ParameterError, "无效的机器ID或未找到关联用户");
|
||||
}
|
||||
|
||||
// 3. 校验当前用户是不是超出了上传限制
|
||||
var userFilesCount = await GetUserUploadToday(userId.Value);
|
||||
if (userFilesCount >= 5)
|
||||
string fileKey;
|
||||
string fileName = $"{Guid.NewGuid().ToString("N")}{Path.GetExtension(request.FileName)}";
|
||||
if (request.Type != "video")
|
||||
{
|
||||
return APIResponseModel<UploadResult>.CreateErrorResponseModel(ResponseCode.ParameterError, "今日上传文件数量已达上限,请明天再试");
|
||||
// 3. 校验当前用户是不是超出了上传限制
|
||||
var userFilesCount = await GetUserUploadToday(userId.Value, request.Type);
|
||||
if (userFilesCount >= 5)
|
||||
{
|
||||
return APIResponseModel<UploadResult>.CreateErrorResponseModel(ResponseCode.ParameterError, "今日上传文件数量已达上限,请明天再试");
|
||||
}
|
||||
fileKey = $"diantu/user/{userId}/{DateTime.Now:yyyyMMdd}/{fileName}";
|
||||
}
|
||||
else
|
||||
{
|
||||
var userFilesCount = await GetUserUploadToday(userId.Value, request.Type);
|
||||
if (userFilesCount >= 50)
|
||||
{
|
||||
return APIResponseModel<UploadResult>.CreateErrorResponseModel(ResponseCode.ParameterError, "今日上传文件数量已达上限,请明天再试");
|
||||
}
|
||||
fileKey = $"upload/user/{userId}/{DateTime.Now:yyyyMMdd}/upload_{fileName}";
|
||||
}
|
||||
|
||||
string fileKey = $"diantu/user/{userId}/{DateTime.Now:yyyyMMdd}/{request.FileName}";
|
||||
|
||||
// 4. 上传到七牛云
|
||||
FileUploads fileUpload = await _qiniuService.UploadFileToQiNiu(fileBytes, userId.Value, request.FileName, fileKey);
|
||||
|
||||
if (request.Type == "video")
|
||||
{
|
||||
fileUpload.Status = "unactive";
|
||||
}
|
||||
// 5. 修改数据库
|
||||
_dbContext.FileUploads.Add(fileUpload);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
@ -279,9 +297,18 @@ namespace LMS.service.Service.FileUploadService
|
||||
return (totalCount, fileUploads);
|
||||
}
|
||||
|
||||
private async Task<int> GetUserUploadToday(long userId)
|
||||
private async Task<int> GetUserUploadToday(long userId, string type)
|
||||
{
|
||||
return await _dbContext.FileUploads
|
||||
var query = _dbContext.FileUploads.AsQueryable();
|
||||
if (type == "video")
|
||||
{
|
||||
query = query.Where(x => x.Status == "unactive");
|
||||
}
|
||||
else
|
||||
{
|
||||
query = query.Where(x => x.Status == "active");
|
||||
}
|
||||
return await query
|
||||
.CountAsync(f => f.UserId == userId && f.CreatedAt.Date == BeijingTimeExtension.GetBeijingTime().Date);
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user