|
@@ -1,6 +1,10 @@ |
|
|
package com.hz.pm.api.external; |
|
|
package com.hz.pm.api.external; |
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.ArrayUtil; |
|
|
import cn.hutool.core.util.NumberUtil; |
|
|
import cn.hutool.core.util.NumberUtil; |
|
|
|
|
|
import com.hz.pm.api.common.enumeration.FileOrigin; |
|
|
|
|
|
import com.hz.pm.api.common.util.PreviewFileIdEncryptUtil; |
|
|
|
|
|
import com.hz.pm.api.external.model.PreviewFileIdBO; |
|
|
import com.hz.pm.api.external.model.dto.MhFileInfoDTO; |
|
|
import com.hz.pm.api.external.model.dto.MhFileInfoDTO; |
|
|
import com.hz.pm.api.meeting.entity.config.WebProps; |
|
|
import com.hz.pm.api.meeting.entity.config.WebProps; |
|
|
import com.ningdatech.basic.exception.BizException; |
|
|
import com.ningdatech.basic.exception.BizException; |
|
@@ -14,6 +18,7 @@ import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
|
import java.time.Duration; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
@@ -41,9 +46,6 @@ public class FilePreviewClient { |
|
|
|
|
|
|
|
|
private static final String FILE_DOWN_NO_AUTH_URL = "/api/v1/file/preview/anonymous/down/"; |
|
|
private static final String FILE_DOWN_NO_AUTH_URL = "/api/v1/file/preview/anonymous/down/"; |
|
|
|
|
|
|
|
|
private static final String MH_DOWN_FILE_URL = "/api/v1/mh/anonymous/file/download/"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//================================================================================================================== |
|
|
//================================================================================================================== |
|
|
|
|
|
|
|
|
private static final String PREVIEW_PATH_FMT = "%s/public/risen/core/resrc/view/pdf_view.html.do?strMap.remote_url=%s&strMap.ext=%s"; |
|
|
private static final String PREVIEW_PATH_FMT = "%s/public/risen/core/resrc/view/pdf_view.html.do?strMap.remote_url=%s&strMap.ext=%s"; |
|
@@ -52,11 +54,14 @@ public class FilePreviewClient { |
|
|
String serverName = request.getServerName(); |
|
|
String serverName = request.getServerName(); |
|
|
log.info("serverName:{}", serverName); |
|
|
log.info("serverName:{}", serverName); |
|
|
StringBuilder fileDownUrl = new StringBuilder(); |
|
|
StringBuilder fileDownUrl = new StringBuilder(); |
|
|
|
|
|
String filePreviewUrlHost; |
|
|
if (serverName.contains("weixin") || serverName.contains("10.54.38.13")) { |
|
|
if (serverName.contains("weixin") || serverName.contains("10.54.38.13")) { |
|
|
// 浙政钉访问需要转换为互联网可访问的地址 |
|
|
// 浙政钉访问需要转换为互联网可访问的地址 |
|
|
fileDownUrl.append(WebProps.zzdApiUrl); |
|
|
fileDownUrl.append(WebProps.zzdApiUrl); |
|
|
|
|
|
filePreviewUrlHost = mhFilePreviewPublicUrl; |
|
|
} else { |
|
|
} else { |
|
|
fileDownUrl.append(WebProps.apiUrl); |
|
|
fileDownUrl.append(WebProps.apiUrl); |
|
|
|
|
|
filePreviewUrlHost = mhFilePreviewPrivateUrl; |
|
|
} |
|
|
} |
|
|
String fileSuffix; |
|
|
String fileSuffix; |
|
|
// 信产文件ID为字符串、当前系统文件ID为数字 |
|
|
// 信产文件ID为字符串、当前系统文件ID为数字 |
|
@@ -64,18 +69,55 @@ public class FilePreviewClient { |
|
|
List<MhFileInfoDTO> fileInfos = mhFileClient.listFileInfo(fileId); |
|
|
List<MhFileInfoDTO> fileInfos = mhFileClient.listFileInfo(fileId); |
|
|
MhFileInfoDTO mhFileInfo = fileInfos.get(0); |
|
|
MhFileInfoDTO mhFileInfo = fileInfos.get(0); |
|
|
fileSuffix = mhFileInfo.getFileSuffix(); |
|
|
fileSuffix = mhFileInfo.getFileSuffix(); |
|
|
fileDownUrl.append(MH_DOWN_FILE_URL).append(fileId); |
|
|
|
|
|
|
|
|
String tmpFileId = generateFileId(fileId, FileOrigin.MH); |
|
|
|
|
|
fileDownUrl.append(FILE_DOWN_NO_AUTH_URL).append(tmpFileId); |
|
|
} else { |
|
|
} else { |
|
|
fileDownUrl.append(FILE_DOWN_NO_AUTH_URL).append(fileId); |
|
|
|
|
|
|
|
|
String tmpFileId = generateFileId(fileId, FileOrigin.OSS); |
|
|
|
|
|
fileDownUrl.append(FILE_DOWN_NO_AUTH_URL).append(tmpFileId); |
|
|
File file = fileService.getById(fileId); |
|
|
File file = fileService.getById(fileId); |
|
|
fileSuffix = file.getSuffix(); |
|
|
fileSuffix = file.getSuffix(); |
|
|
} |
|
|
} |
|
|
return String.format(PREVIEW_PATH_FMT, mhFilePreviewPrivateUrl, fileDownUrl, fileSuffix); |
|
|
|
|
|
|
|
|
return String.format(PREVIEW_PATH_FMT, filePreviewUrlHost, fileDownUrl, fileSuffix); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static String generateFileId(String fileId, FileOrigin oss) { |
|
|
|
|
|
PreviewFileIdBO previewFileId = PreviewFileIdBO.builder() |
|
|
|
|
|
// 默认过期时间6小时 |
|
|
|
|
|
.expireTime(System.currentTimeMillis() + Duration.ofHours(6).toMillis()) |
|
|
|
|
|
.fileId(fileId) |
|
|
|
|
|
.fileOrigin(oss) |
|
|
|
|
|
.build(); |
|
|
|
|
|
return PreviewFileIdEncryptUtil.encryptFileId(previewFileId.concat()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static PreviewFileIdBO parseFileId(String fileId) { |
|
|
|
|
|
String decryptFileId = PreviewFileIdEncryptUtil.decryptFileId(fileId); |
|
|
|
|
|
String[] split = decryptFileId.split("#"); |
|
|
|
|
|
if (ArrayUtil.isEmpty(split) || split.length != 3) { |
|
|
|
|
|
throw BizException.wrap("文件ID无效"); |
|
|
|
|
|
} |
|
|
|
|
|
return PreviewFileIdBO.builder() |
|
|
|
|
|
.fileId(split[0]) |
|
|
|
|
|
.expireTime(Long.parseLong(split[1])) |
|
|
|
|
|
.fileOrigin(FileOrigin.valueOf(split[2])) |
|
|
|
|
|
.build(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void downloadFile(Long fileId, HttpServletResponse response) { |
|
|
|
|
|
|
|
|
public void downloadFile(String fileId, HttpServletResponse response) { |
|
|
try { |
|
|
try { |
|
|
fileService.download(fileId, response); |
|
|
|
|
|
|
|
|
PreviewFileIdBO previewFileId = parseFileId(fileId); |
|
|
|
|
|
if (previewFileId.getExpireTime() != null |
|
|
|
|
|
&& previewFileId.getExpireTime() > 0 |
|
|
|
|
|
&& System.currentTimeMillis() > previewFileId.getExpireTime()) { |
|
|
|
|
|
throw BizException.wrap("文件地址已过期,请重试"); |
|
|
|
|
|
} |
|
|
|
|
|
if (FileOrigin.MH.equals(previewFileId.getFileOrigin())) { |
|
|
|
|
|
mhFileClient.download(previewFileId.getFileId(), response); |
|
|
|
|
|
} else if (FileOrigin.OSS.equals(previewFileId.getFileOrigin())) { |
|
|
|
|
|
fileService.download(Long.valueOf(previewFileId.getFileId()), response); |
|
|
|
|
|
} else { |
|
|
|
|
|
throw BizException.wrap("文件类型不支持"); |
|
|
|
|
|
} |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
log.info("文件下载失败:{}", fileId, e); |
|
|
log.info("文件下载失败:{}", fileId, e); |
|
|
throw BizException.wrap("文件下载失败:%s", fileId); |
|
|
throw BizException.wrap("文件下载失败:%s", fileId); |
|
|