/// <summary> /// 上传单文件 /// </summary> /// <param name="file"></param> /// <param name="config"></param> /// <param name="args"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task <IResponseEntity <FileInfo> > UploadFileAsync(IFormFile file, FileUploadConfig config, object args, CancellationToken cancellationToken = default) { var res = new ResponseEntity <FileInfo>(); if (file == null || file.Length < 1) { return(res.Error("请上传文件!")); } //格式限制 if (!config.ContentType.Contains(file.ContentType)) { return(res.Error("文件格式错误")); } //大小限制 if (!(file.Length <= config.MaxSize)) { return(res.Error("文件过大")); } var fileInfo = new File.FileInfo(file.FileName, file.Length) { UploadPath = config.UploadPath, RequestPath = config.RequestPath }; var dateTimeFormat = config.DateTimeFormat.IsNotNull() ? DateTime.Now.ToString(config.DateTimeFormat) : ""; var format = config.Format.IsNotNull() ? StringHelper.Format(config.Format, args) : ""; fileInfo.RelativePath = Path.Combine(dateTimeFormat, format).ToPath(); if (!Directory.Exists(fileInfo.FileDirectory)) { Directory.CreateDirectory(fileInfo.FileDirectory); } var dataCenterId = _systemConfig.CurrentValue?.DataCenterId ?? 5; var workId = _systemConfig.CurrentValue?.WorkId ?? 20; fileInfo.SaveName = $"{IdHelper.GenSnowflakeId(dataCenterId, workId)}.{fileInfo.Extension}"; await SaveAsync(file, fileInfo.FilePath, cancellationToken); return(res.Ok(fileInfo)); }
public async Task <IResponseEntity <FileInfo> > UploadAvatarAsync(IFormFile file, CancellationToken cancellationToken = default) { var res = new ResponseEntity <FileInfo>(); var config = _systemConfig.CurrentValue.UploadAvatar; if (file == null || file.Length < 1) { return(res.Error("请上传文件!")); } //格式限制 if (!config.ContentType.Contains(file.ContentType)) { return(res.Error("文件格式错误")); } //大小限制 if (!(file.Length <= config.MaxSize)) { return(res.Error("文件过大")); } var fileInfo = new File.FileInfo(file.FileName, file.Length); fileInfo.UploadPath = config.UploadPath; fileInfo.RequestPath = config.RequestPath; fileInfo.RelativePath = _authUser.Tenant.Id; fileInfo.SaveName = $"{_authUser.Id}.{fileInfo.Extension}"; if (!Directory.Exists(fileInfo.FileDirectory)) { Directory.CreateDirectory(fileInfo.FileDirectory); } await SaveAsync(file, fileInfo.FilePath, cancellationToken); return(res.Ok(fileInfo)); }