mirror of https://gitee.com/stylefeng/roses
【7.0.4】【file】更新一个非鉴权获取文件url的方法
parent
352fb63a2b
commit
548f2a844c
|
@ -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);
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
|
|
|
@ -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", "文件不允许被访问,请登录后访问");
|
||||
|
||||
/**
|
||||
* 错误编码
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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) {
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
||||
// 放入缓存用户信息
|
||||
|
|
Loading…
Reference in New Issue