【7.0.4】【file】更新一个非鉴权获取文件url的方法

pull/22/head
fengshuonan 2021-06-10 13:46:28 +08:00
parent 352fb63a2b
commit 548f2a844c
10 changed files with 60 additions and 6 deletions

View File

@ -167,6 +167,17 @@ public interface FileOperatorApi {
*/
String getFileAuthUrl(String bucketName, String key, Long timeoutMillis);
/**
*
*
* @param bucketName
* @param key
* @return 访url
* @author fengshuonan
* @date 2021/6/10 12:03
*/
String getFileUnAuthUrl(String bucketName, String key);
/**
*
*

View File

@ -116,7 +116,12 @@ public enum FileExceptionEnum implements AbstractExceptionEnum {
/**
* 访
*/
FILE_DENIED_ACCESS(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + FileConstants.FILE_EXCEPTION_STEP_CODE + "16", "文件不允许被访问,文件加密等级不符合");
FILE_DENIED_ACCESS(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + FileConstants.FILE_EXCEPTION_STEP_CODE + "16", "文件不允许被访问,文件加密等级不符合"),
/**
* 访
*/
FILE_PERMISSION_DENIED(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + FileConstants.FILE_EXCEPTION_STEP_CODE + "17", "文件不允许被访问,请登录后访问");
/**
*

View File

@ -132,7 +132,7 @@ public class SysFileInfoController {
* @author fengshuonan
* @date 2020/11/29 11:29
*/
@GetResource(name = "文件预览通过bucketName和objectName", path = FileConstants.FILE_PREVIEW_BY_OBJECT_NAME, requiredPermission = false)
@GetResource(name = "文件预览通过bucketName和objectName", path = FileConstants.FILE_PREVIEW_BY_OBJECT_NAME, requiredPermission = false, requiredLogin = false)
public void previewByBucketNameObjectName(@Validated(SysFileInfoRequest.previewByObjectName.class) SysFileInfoRequest sysFileInfoRequest) {
HttpServletResponse response = HttpServletUtil.getResponse();
sysFileInfoService.previewByBucketAndObjName(sysFileInfoRequest, response);

View File

@ -336,6 +336,17 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
return;
}
// 判断文件是否需要鉴权需要鉴权的需要带token访问
LambdaQueryWrapper<SysFileInfo> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SysFileInfo::getFileObjectName, sysFileInfoRequest.getFileObjectName());
wrapper.eq(SysFileInfo::getSecretFlag, YesOrNotEnum.Y.getCode());
int count = this.count(wrapper);
if (count > 0) {
if (!LoginContext.me().hasLogin()) {
throw new FileException(FileExceptionEnum.FILE_PERMISSION_DENIED);
}
}
// 获取文件字节码
byte[] fileBytes;
try {

View File

@ -29,6 +29,7 @@ import cn.stylefeng.roses.kernel.file.api.FileOperatorApi;
import cn.stylefeng.roses.kernel.file.api.enums.BucketAuthEnum;
import cn.stylefeng.roses.kernel.file.api.exception.FileException;
import cn.stylefeng.roses.kernel.file.api.exception.enums.FileExceptionEnum;
import cn.stylefeng.roses.kernel.file.api.expander.FileConfigExpander;
import cn.stylefeng.roses.kernel.file.api.pojo.props.AliyunOssProperties;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
@ -197,6 +198,11 @@ public class AliyunFileOperator implements FileOperatorApi {
}
}
@Override
public String getFileUnAuthUrl(String bucketName, String key) {
return this.getFileAuthUrl(bucketName, key, FileConfigExpander.getDefaultFileTimeoutSeconds() * 1000);
}
@Override
public void deleteFile(String bucketName, String key) {
ossClient.deleteObject(bucketName, key);

View File

@ -178,6 +178,14 @@ public class LocalFileOperator implements FileOperatorApi {
return FileConfigExpander.getServerDeployHost() + contextPath + FileConstants.FILE_PREVIEW_BY_OBJECT_NAME + "?fileBucket=" + bucketName + "&fileObjectName=" + key + "&token=" + token;
}
@Override
public String getFileUnAuthUrl(String bucketName, String key) {
// 获取context-path
String contextPath = HttpServletUtil.getRequest().getContextPath();
return FileConfigExpander.getServerDeployHost() + contextPath + FileConstants.FILE_PREVIEW_BY_OBJECT_NAME + "?fileBucket=" + bucketName + "&fileObjectName=" + key;
}
@Override
public void deleteFile(String bucketName, String key) {

View File

@ -214,6 +214,14 @@ public class MinIoFileOperator implements FileOperatorApi {
}
@Override
public String getFileUnAuthUrl(String bucketName, String key) {
// 获取context-path
String contextPath = HttpServletUtil.getRequest().getContextPath();
return FileConfigExpander.getServerDeployHost() + contextPath + FileConstants.FILE_PREVIEW_BY_OBJECT_NAME + "?fileBucket=" + bucketName + "&fileObjectName=" + key;
}
@Override
public void deleteFile(String bucketName, String key) {
try {

View File

@ -30,6 +30,7 @@ import cn.stylefeng.roses.kernel.file.api.FileOperatorApi;
import cn.stylefeng.roses.kernel.file.api.enums.BucketAuthEnum;
import cn.stylefeng.roses.kernel.file.api.exception.FileException;
import cn.stylefeng.roses.kernel.file.api.exception.enums.FileExceptionEnum;
import cn.stylefeng.roses.kernel.file.api.expander.FileConfigExpander;
import cn.stylefeng.roses.kernel.file.api.pojo.props.TenCosProperties;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
@ -250,6 +251,11 @@ public class TenFileOperator implements FileOperatorApi {
return url.toString();
}
@Override
public String getFileUnAuthUrl(String bucketName, String key) {
return this.getFileAuthUrl(bucketName, key, FileConfigExpander.getDefaultFileTimeoutSeconds() * 1000);
}
@Override
public void deleteFile(String bucketName, String key) {
cosClient.deleteObject(bucketName, key);

View File

@ -104,7 +104,7 @@ public class CustomerFactory {
loginUser.setSimpleUserInfo(simpleUserInfo);
// 设置用户头像url
String fileAuthUrl = fileOperatorApi.getFileAuthUrl(CustomerConfigExpander.getCustomerBucket(), customer.getAvatarObjectName(), CustomerConfigExpander.getCustomerBucketExpiredSeconds());
String fileAuthUrl = fileOperatorApi.getFileUnAuthUrl(CustomerConfigExpander.getCustomerBucket(), customer.getAvatarObjectName());
loginUser.setAvatarUrl(fileAuthUrl);
return loginUser;

View File

@ -289,10 +289,9 @@ public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> i
BeanUtil.copyProperties(customer, result);
// 获取头像的url
String fileAuthUrl = fileOperatorApi.getFileAuthUrl(
String fileAuthUrl = fileOperatorApi.getFileUnAuthUrl(
CustomerConfigExpander.getCustomerBucket(),
customer.getAvatarObjectName(),
CustomerConfigExpander.getCustomerBucketExpiredSeconds());
customer.getAvatarObjectName());
result.setAvatarObjectUrl(fileAuthUrl);
// 放入缓存用户信息