mirror of https://gitee.com/stylefeng/roses
【file】整理文件预览的方法
parent
5f7fb1f1ad
commit
af935b4639
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
|
@ -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名称
|
||||
* 通用文件预览,通过object名称和bucket名称
|
||||
*/
|
||||
String DEFAULT_AVATAR_FILE_OBJ_NAME = "10000.png";
|
||||
String FILE_PREVIEW_BY_OBJECT_NAME = "/sysFileInfo/previewByObjectName";
|
||||
|
||||
/**
|
||||
* 系统默认头像的文件id
|
||||
*/
|
||||
Long DEFAULT_AVATAR_FILE_ID = 10000L;
|
||||
|
||||
/**
|
||||
* 系统默认的头像
|
||||
|
|
|
@ -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 {
|
|||
/**
|
||||
* 存储到bucket中的名称,主键id+.后缀
|
||||
*/
|
||||
@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 {
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用文件预览,通过传bucket名称和object名称
|
||||
*
|
||||
* @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);
|
||||
// }
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染被预览的文件到servlet的response流中
|
||||
*
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue