feat: preview file
This commit is contained in:
parent
335b8dfce5
commit
965429050c
@ -4,12 +4,15 @@ import com.wmyun.module.infra.controller.admin.file.vo.preview.CheckFileInfoVo;
|
||||
import com.wmyun.module.infra.service.file.FilePreviewService;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.security.PermitAll;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import static com.wmyun.module.infra.framework.file.core.utils.FileTypeUtils.writeAttachment;
|
||||
|
||||
@ -22,6 +25,7 @@ import static com.wmyun.module.infra.framework.file.core.utils.FileTypeUtils.wri
|
||||
@Tag(name = "管理后台 - 文件预览")
|
||||
@RestController
|
||||
@RequestMapping("/infra/file/preview/wopi")
|
||||
@Slf4j
|
||||
public class OfficePreviewController {
|
||||
|
||||
@Autowired
|
||||
@ -41,8 +45,15 @@ public class OfficePreviewController {
|
||||
|
||||
@RequestMapping(value = "/files/{fileId}/contents", method = {RequestMethod.POST, RequestMethod.PUT})
|
||||
@PermitAll
|
||||
public ResponseEntity<?> putFileContents(@PathVariable String fileId, InputStream contentStream) {
|
||||
previewService.saveFile(fileId, contentStream);
|
||||
public ResponseEntity<?> putFileContents(@PathVariable String fileId, InputStream contentStream, HttpServletRequest request) {
|
||||
Enumeration<String> enumeration = request.getHeaderNames();
|
||||
while (enumeration.hasMoreElements()) {
|
||||
String name = enumeration.nextElement();
|
||||
String value = request.getHeader(name);
|
||||
log.info("{} = {}", name, value);
|
||||
}
|
||||
Long size = Long.valueOf(request.getHeader("content-length"));
|
||||
previewService.saveFile(fileId, contentStream, size);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
}
|
||||
|
@ -97,4 +97,8 @@ public class CheckFileInfoVo {
|
||||
@Schema(description = "HOST地址")
|
||||
@JsonProperty("HostViewUrl")
|
||||
private String hostViewUrl;
|
||||
|
||||
@Schema(description = "HOST地址")
|
||||
@JsonProperty("HostEditUrl")
|
||||
private String hostEditUrl;
|
||||
}
|
||||
|
@ -0,0 +1,21 @@
|
||||
package com.wmyun.module.infra.enums.config;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* @Classname ConfigEnum
|
||||
* @Description TODO
|
||||
* @Date 2025/2/27 11:08
|
||||
* @Created by violet
|
||||
*/
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum ConfigEnum {
|
||||
|
||||
WOPI_SERVER_IP("wopi_server_ip_addr"),
|
||||
WOPI_CLIENT("wopi_client_addr");
|
||||
|
||||
private final String key;
|
||||
}
|
@ -1,10 +1,7 @@
|
||||
package com.wmyun.module.infra.service.file;
|
||||
|
||||
import com.wmyun.module.infra.controller.admin.file.vo.preview.CheckFileInfoVo;
|
||||
import com.wmyun.module.infra.controller.admin.file.vo.preview.WopiFileVo;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
@ -19,7 +16,7 @@ public interface FilePreviewService {
|
||||
|
||||
CheckFileInfoVo createPreviewInfo(String fileId);
|
||||
|
||||
void saveFile(String fileId, InputStream content);
|
||||
void saveFile(String fileId, InputStream content, Long size);
|
||||
|
||||
void getFileContent(String fileId, HttpServletResponse response);
|
||||
}
|
||||
|
@ -5,11 +5,13 @@ import com.wmyun.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import com.wmyun.framework.security.core.LoginUser;
|
||||
import com.wmyun.framework.security.core.util.SecurityFrameworkUtils;
|
||||
import com.wmyun.module.infra.controller.admin.file.vo.preview.CheckFileInfoVo;
|
||||
import com.wmyun.module.infra.dal.dataobject.config.ConfigDO;
|
||||
import com.wmyun.module.infra.dal.dataobject.file.FileDO;
|
||||
import com.wmyun.module.infra.dal.mysql.file.FileMapper;
|
||||
import com.wmyun.module.infra.enums.config.ConfigEnum;
|
||||
import com.wmyun.module.infra.framework.file.core.client.FileClient;
|
||||
import com.wmyun.module.infra.framework.file.core.utils.FileTypeUtils;
|
||||
import jakarta.annotation.Resource;
|
||||
import com.wmyun.module.infra.service.config.ConfigService;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -18,6 +20,8 @@ import org.springframework.stereotype.Service;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static com.wmyun.module.infra.framework.file.core.utils.FileTypeUtils.writeAttachment;
|
||||
|
||||
@ -34,9 +38,12 @@ public class FilePreviewServiceImpl implements FilePreviewService {
|
||||
@Autowired
|
||||
private FileMapper fileMapper;
|
||||
|
||||
@Resource
|
||||
@Autowired
|
||||
private FileConfigService fileConfigService;
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
|
||||
@Override
|
||||
public CheckFileInfoVo createPreviewInfo(String fileId) {
|
||||
FileDO file = queryFileInfoByFileId(fileId);
|
||||
@ -44,6 +51,8 @@ public class FilePreviewServiceImpl implements FilePreviewService {
|
||||
return null;
|
||||
}
|
||||
LoginUser user = SecurityFrameworkUtils.getLoginUser();
|
||||
ConfigDO WOPI_SERVER_IP = configService.getConfigByKey(ConfigEnum.WOPI_SERVER_IP.getKey());
|
||||
String hostUrl = WOPI_SERVER_IP.getValue() + "/admin-api/infra/file/preview/wopi/files/" + fileId;
|
||||
return CheckFileInfoVo.builder()
|
||||
.baseFileName(file.getName())
|
||||
.ownerId(file.getUpdater())
|
||||
@ -54,18 +63,19 @@ public class FilePreviewServiceImpl implements FilePreviewService {
|
||||
.userFriendlyName(Optional.ofNullable(user)
|
||||
.map(LoginUser::getInfo)
|
||||
.map(info -> info.get(LoginUser.INFO_KEY_NICKNAME))
|
||||
.orElse(""))
|
||||
.orElse("user_xx"))
|
||||
.version("")
|
||||
.supportsDeleteFile(false)
|
||||
.supportsRename(false)
|
||||
.supportsUpdate(true)
|
||||
.supportsUserInfo(true)
|
||||
.readOnly(true)
|
||||
.userCanRename(false)
|
||||
.userCanWrite(false)
|
||||
.userCanRename(true)
|
||||
.userCanWrite(true)
|
||||
.downloadUrl(file.getUrl())
|
||||
.fileUrl(file.getUrl())
|
||||
.hostViewUrl("")
|
||||
.hostViewUrl(hostUrl)
|
||||
.hostEditUrl(hostUrl)
|
||||
.build();
|
||||
}
|
||||
|
||||
@ -78,11 +88,31 @@ public class FilePreviewServiceImpl implements FilePreviewService {
|
||||
LambdaQueryWrapperX<FileDO> queryWrapperX = new LambdaQueryWrapperX<>();
|
||||
queryWrapperX.eq(FileDO::getId, fileId).eq(FileDO::getDeleted, false);
|
||||
List<FileDO> files = fileMapper.selectList(queryWrapperX);
|
||||
return CollectionUtils.isNotEmpty(files) ? files.get(0) : null;
|
||||
FileDO file = CollectionUtils.isNotEmpty(files) ? files.get(0) : null;
|
||||
|
||||
if (file != null) {
|
||||
ConfigDO config = configService.getConfigByKey(ConfigEnum.WOPI_SERVER_IP.getKey());
|
||||
String wopiServerIp = config.getValue();
|
||||
file.setUrl(replaceWithSelfAddress(file.getUrl(), wopiServerIp));
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换url中ip地址
|
||||
* @param url 文件下载url
|
||||
* @param targetIP 本机ip
|
||||
* @return
|
||||
*/
|
||||
private String replaceWithSelfAddress(String url, String targetIP) {
|
||||
// 正则匹配 http(s):// 后的 IPv4 地址
|
||||
Pattern pattern = Pattern.compile("(https?://)(\\d+\\.\\d+\\.\\d+\\.\\d+)");
|
||||
Matcher matcher = pattern.matcher(url);
|
||||
return matcher.replaceAll("$1" + targetIP);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveFile(String fileId, InputStream content) {
|
||||
public void saveFile(String fileId, InputStream content, Long size) {
|
||||
FileDO existFile = queryFileInfoByFileId(fileId);
|
||||
FileClient client = fileConfigService.getMasterFileClient();
|
||||
if (existFile == null) {
|
||||
@ -93,6 +123,7 @@ public class FilePreviewServiceImpl implements FilePreviewService {
|
||||
try {
|
||||
String url = client.upload(data, existFile.getPath(), type);
|
||||
existFile.setUrl(url);
|
||||
existFile.setSize(Integer.valueOf(size.toString()));
|
||||
fileMapper.updateById(existFile);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
|
Loading…
Reference in New Issue
Block a user