【file】整理文件预览的方法

pull/3/head
fengshuonan 2021-01-01 11:09:56 +08:00
parent 5f7fb1f1ad
commit af935b4639
8 changed files with 115 additions and 116 deletions

View File

@ -20,4 +20,14 @@ public interface FileInfoApi {
*/
SysFileInfoResponse getFileInfoWithoutContent(Long fileId);
/**
*
*
* @param fileId id
* @return 访url
* @author fengshuonan
* @date 2020/10/26 10:40
*/
String getFileAuthUrl(Long fileId);
}

View File

@ -38,11 +38,6 @@ public interface FileConstants {
*/
Long DEFAULT_FILE_TIMEOUT_SECONDS = 7200L;
/**
*
*/
String DEFAULT_ATTACHMENT_UPLOAD_METHOD = "local_file_config";
/**
* token
*/
@ -54,9 +49,14 @@ public interface FileConstants {
String FILE_PUBLIC_PREVIEW_URL = "/sysFileInfo/public/preview";
/**
* object
* objectbucket
*/
String DEFAULT_AVATAR_FILE_OBJ_NAME = "10000.png";
String FILE_PREVIEW_BY_OBJECT_NAME = "/sysFileInfo/previewByObjectName";
/**
* id
*/
Long DEFAULT_AVATAR_FILE_ID = 10000L;
/**
*

View File

@ -62,6 +62,7 @@ public class SysFileInfoRequest extends BaseRequest {
/**
*
*/
@NotBlank(message = "fileBucket不能为空", groups = {previewByObjectName.class})
private String fileBucket;
/**
@ -77,6 +78,7 @@ public class SysFileInfoRequest extends BaseRequest {
/**
* bucketid+.
*/
@NotBlank(message = "fileObjectName不能为空", groups = {previewByObjectName.class})
private String fileObjectName;
/**
@ -90,4 +92,10 @@ public class SysFileInfoRequest extends BaseRequest {
public @interface versionBack {
}
/**
* object
*/
public @interface previewByObjectName {
}
}

View File

@ -9,6 +9,7 @@ import cn.stylefeng.roses.kernel.resource.api.annotation.PostResource;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
@ -20,8 +21,7 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import static cn.stylefeng.roses.kernel.file.constants.FileConstants.FILE_PRIVATE_PREVIEW_URL;
import static cn.stylefeng.roses.kernel.file.constants.FileConstants.FILE_PUBLIC_PREVIEW_URL;
import static cn.stylefeng.roses.kernel.file.constants.FileConstants.*;
/**
*
@ -59,6 +59,70 @@ public class SysFileInfoController {
return new SuccessResponseData(fileUploadInfoResult);
}
/**
*
*
* @author fengshuonan
* @date 2020/11/29 11:29
*/
@GetResource(name = "私有文件预览", path = FILE_PRIVATE_PREVIEW_URL)
public void privatePreview(@Validated(SysFileInfoRequest.detail.class) SysFileInfoRequest sysFileInfoRequest) {
HttpServletResponse response = HttpServletUtil.getResponse();
sysFileInfoRequest.setSecretFlag(YesOrNotEnum.Y.getCode());
this.sysFileInfoService.preview(sysFileInfoRequest, response);
}
/**
*
*
* @author majianguo
* @date 2020/12/27 13:17
*/
@GetResource(name = "公有文件预览", path = FILE_PUBLIC_PREVIEW_URL, requiredPermission = false, requiredLogin = false)
public void publicPreview(@Validated(SysFileInfoRequest.detail.class) SysFileInfoRequest sysFileInfoRequest) {
HttpServletResponse response = HttpServletUtil.getResponse();
sysFileInfoRequest.setSecretFlag(YesOrNotEnum.N.getCode());
this.sysFileInfoService.preview(sysFileInfoRequest, response);
}
/**
* bucketobject
*
* @author fengshuonan
* @date 2020/11/29 11:29
*/
@GetResource(name = "文件预览通过bucketName和objectName", path = FILE_PREVIEW_BY_OBJECT_NAME, requiredPermission = false)
public void previewByBucketNameObjectName(@Validated(SysFileInfoRequest.previewByObjectName.class) SysFileInfoRequest sysFileInfoRequest) {
HttpServletResponse response = HttpServletUtil.getResponse();
sysFileInfoService.previewByBucketAndObjName(sysFileInfoRequest, response);
}
/**
*
*
* @author majianguo
* @date 2020/12/27 13:17
*/
@GetResource(name = "私有文件下载", path = "/sysFileInfo/privateDownload")
public void privateDownload(@Validated(SysFileInfoRequest.detail.class) SysFileInfoRequest sysFileInfoRequest) {
HttpServletResponse response = HttpServletUtil.getResponse();
sysFileInfoRequest.setSecretFlag(YesOrNotEnum.Y.getCode());
this.sysFileInfoService.download(sysFileInfoRequest, response);
}
/**
*
*
* @author majianguo
* @date 2020/12/27 13:17
*/
@GetResource(name = "公有文件下载", path = "/sysFileInfo/publicDownload", requiredLogin = false, requiredPermission = false)
public void publicDownload(@Validated(SysFileInfoRequest.detail.class) SysFileInfoRequest sysFileInfoRequest) {
HttpServletResponse response = HttpServletUtil.getResponse();
sysFileInfoRequest.setSecretFlag(YesOrNotEnum.N.getCode());
this.sysFileInfoService.download(sysFileInfoRequest, response);
}
/**
*
* <p>
@ -85,54 +149,6 @@ public class SysFileInfoController {
return new SuccessResponseData(fileUploadInfoResult);
}
/**
*
*
* @author fengshuonan
* @date 2020/11/29 11:29
*/
@GetResource(name = "私有文件预览", path = FILE_PRIVATE_PREVIEW_URL)
public void priPreview(@Validated(SysFileInfoRequest.detail.class) SysFileInfoRequest sysFileInfoRequest, HttpServletResponse response) {
sysFileInfoRequest.setSecretFlag(YesOrNotEnum.Y.getCode());
this.sysFileInfoService.preview(sysFileInfoRequest, response);
}
/**
*
*
* @author majianguo
* @date 2020/12/27 13:17
*/
@GetResource(name = "公有文件预览", path = FILE_PUBLIC_PREVIEW_URL, requiredPermission = false, requiredLogin = false)
public void pubPreview(@Validated(SysFileInfoRequest.detail.class) SysFileInfoRequest sysFileInfoRequest, HttpServletResponse response) {
sysFileInfoRequest.setSecretFlag(YesOrNotEnum.N.getCode());
this.sysFileInfoService.preview(sysFileInfoRequest, response);
}
/**
*
*
* @author majianguo
* @date 2020/12/27 13:17
*/
@GetResource(name = "私有文件下载", path = "/sysFileInfo/privateDownload")
public void privateDownload(@Validated(SysFileInfoRequest.detail.class) SysFileInfoRequest sysFileInfoRequest, HttpServletResponse response) {
sysFileInfoRequest.setSecretFlag(YesOrNotEnum.Y.getCode());
this.sysFileInfoService.download(sysFileInfoRequest, response);
}
/**
*
*
* @author majianguo
* @date 2020/12/27 13:17
*/
@GetResource(name = "公有文件下载", path = "/sysFileInfo/publicDownload", requiredLogin = false, requiredPermission = false)
public void publicDownload(@Validated(SysFileInfoRequest.detail.class) SysFileInfoRequest sysFileInfoRequest, HttpServletResponse response) {
sysFileInfoRequest.setSecretFlag(YesOrNotEnum.N.getCode());
this.sysFileInfoService.download(sysFileInfoRequest, response);
}
/**
* IDS
*
@ -154,7 +170,8 @@ public class SysFileInfoController {
* @date 2020/12/27 13:17
*/
@GetResource(name = "公有打包下载文件", path = "/sysFileInfo/publicPackagingDownload", requiredPermission = false, requiredLogin = false)
public void publicPackagingDownload(@RequestParam(value = "fileIds") String fileIds, HttpServletResponse response) {
public void publicPackagingDownload(@RequestParam(value = "fileIds") String fileIds) {
HttpServletResponse response = HttpServletUtil.getResponse();
this.sysFileInfoService.packagingDownload(fileIds, YesOrNotEnum.N.getCode(), response);
}
@ -165,7 +182,8 @@ public class SysFileInfoController {
* @date 2020/12/27 13:18
*/
@GetResource(name = "私有打包下载文件", path = "/sysFileInfo/privatePackagingDownload")
public void privatePackagingDownload(@RequestParam(value = "fileIds") String fileIds, HttpServletResponse response) {
public void privatePackagingDownload(@RequestParam(value = "fileIds") String fileIds) {
HttpServletResponse response = HttpServletUtil.getResponse();
this.sysFileInfoService.packagingDownload(fileIds, YesOrNotEnum.Y.getCode(), response);
}
@ -217,46 +235,4 @@ public class SysFileInfoController {
return new SuccessResponseData(sysFileInfoService.detail(sysFileInfoRequest));
}
// /**
// * 文件预览
// * <p>
// * 支持通过文件id预览也支持直接通过bucket和obj名称预览
// *
// * @author fengshuonan
// * @date 2020/11/29 11:29
// */
// @GetResource(name = "文件预览", path = FILE_PRIVATE_PREVIEW_URL)
// public void preview(SysFileInfoRequest sysFileInfoRequest) {
// HttpServletResponse response = HttpServletUtil.getResponse();
//
// // 请求参数不能为空
// if (sysFileInfoRequest == null) {
// String userTip = StrUtil.format(FileExceptionEnum.PREVIEW_EMPTY_ERROR.getUserTip(), "");
// throw new FileException(FileExceptionEnum.PREVIEW_EMPTY_ERROR, userTip);
// }
//
// // 文件是-1则返回系统默认头像
// if (DEFAULT_AVATAR_FILE_OBJ_NAME.equals(sysFileInfoRequest.getFileObjectName())) {
// DownloadUtil.renderPreviewFile(response, Base64.decode(FileConfigExpander.getDefaultAvatarBase64()));
// return;
// }
//
// // 文件id不为空则根据文件id预览
// if (ObjectUtil.isNotEmpty(sysFileInfoRequest.getFileId())) {
// sysFileInfoService.previewByFileId(sysFileInfoRequest, response);
// return;
// }
//
// // 文件bucketName和objectName不为空则根据bucket预览
// if (ObjectUtil.isAllNotEmpty(sysFileInfoRequest.getFileBucket(), sysFileInfoRequest.getFileObjectName())) {
// sysFileInfoService.previewByBucketAndObjName(sysFileInfoRequest, response);
// return;
// }
//
// // 提示用户信息不全
// String userTip = StrUtil.format(FileExceptionEnum.PREVIEW_EMPTY_ERROR.getUserTip(), sysFileInfoRequest);
// throw new FileException(FileExceptionEnum.PREVIEW_EMPTY_ERROR, userTip);
// }
}

View File

@ -1,19 +1,23 @@
package cn.stylefeng.roses.kernel.file.modular.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.file.FileInfoApi;
import cn.stylefeng.roses.kernel.file.FileOperatorApi;
import cn.stylefeng.roses.kernel.file.constants.FileConstants;
import cn.stylefeng.roses.kernel.file.enums.FileStatusEnum;
import cn.stylefeng.roses.kernel.file.exception.FileException;
import cn.stylefeng.roses.kernel.file.exception.enums.FileExceptionEnum;
import cn.stylefeng.roses.kernel.file.expander.FileConfigExpander;
import cn.stylefeng.roses.kernel.file.modular.entity.SysFileInfo;
import cn.stylefeng.roses.kernel.file.modular.factory.FileInfoFactory;
import cn.stylefeng.roses.kernel.file.modular.mapper.SysFileInfoMapper;
@ -238,6 +242,12 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
@Override
public void preview(SysFileInfoRequest sysFileInfoRequest, HttpServletResponse response) {
// 如果是默认头像
if (FileConstants.DEFAULT_AVATAR_FILE_ID.equals(sysFileInfoRequest.getFileId())) {
DownloadUtil.renderPreviewFile(response, Base64.decode(FileConfigExpander.getDefaultAvatarBase64()));
return;
}
// 根据文件id获取文件信息结果集
SysFileInfoResponse sysFileInfoResponse = this.getFileInfoResult(sysFileInfoRequest.getFileId());
@ -349,6 +359,15 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
return sysFileInfoResponse;
}
@Override
public String getFileAuthUrl(Long fileId) {
// 获取登录用户的token
String token = LoginContext.me().getToken();
return FileConfigExpander.getServerDeployHost() + FileConstants.FILE_PRIVATE_PREVIEW_URL + "?fileId=" + fileId + "&token=" + token;
}
/**
* servletresponse
*

View File

@ -151,7 +151,6 @@ public class LocalFileOperator implements FileOperatorApi {
// 获取登录用户的token
String token = LoginContext.me().getToken();
// 拼接url = “host” + “预览图片的url” + “?token=xxx”
return FileConfigExpander.getServerDeployHost() + FileConstants.FILE_PRIVATE_PREVIEW_URL + "?fileBucket=" + bucketName + "&fileObjectName=" + key + "&token=" + token;
}

View File

@ -195,7 +195,6 @@ public class MinIoFileOperator implements FileOperatorApi {
// 获取登录用户的token
String token = LoginContext.me().getToken();
// 拼接url = “host” + “预览图片的url” + “?token=xxx”
return FileConfigExpander.getServerDeployHost() + FileConstants.FILE_PRIVATE_PREVIEW_URL + "?fileBucket=" + bucketName + "&fileObjectName=" + key + "&token=" + token;
}

View File

@ -8,9 +8,6 @@ import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.file.FileInfoApi;
import cn.stylefeng.roses.kernel.file.FileOperatorApi;
import cn.stylefeng.roses.kernel.file.expander.FileConfigExpander;
import cn.stylefeng.roses.kernel.file.pojo.response.SysFileInfoResponse;
import cn.stylefeng.roses.kernel.office.api.OfficeExcelApi;
import cn.stylefeng.roses.kernel.office.api.pojo.report.ExcelExportParam;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
@ -89,9 +86,6 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
@Resource
private FileInfoApi fileInfoApi;
@Resource
private FileOperatorApi fileOperatorApi;
@Override
@Transactional(rollbackFor = Exception.class)
public void add(SysUserRequest sysUserRequest) {
@ -363,14 +357,8 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
@Override
public String getUserAvatarUrl(Long fileId) {
// 获取头像文件详细信息
SysFileInfoResponse fileInfoWithoutContent = fileInfoApi.getFileInfoWithoutContent(fileId);
// 获取头像的访问地址
return fileOperatorApi.getFileAuthUrl(
fileInfoWithoutContent.getFileBucket(),
fileInfoWithoutContent.getFileObjectName(),
FileConfigExpander.getDefaultFileTimeoutSeconds() * 1000);
return fileInfoApi.getFileAuthUrl(fileId);
}
@Override