实现文件预览的功能

This commit is contained in:
chenxudong 2025-02-13 09:00:10 +08:00
parent bac3da7d8f
commit 1ab139ce48
4 changed files with 68 additions and 1 deletions

View File

@ -132,4 +132,10 @@ public class EdFileInfoController {
public ElectromagneticResult<?> queryChildFolder(@RequestParam String parentId) { public ElectromagneticResult<?> queryChildFolder(@RequestParam String parentId) {
return edFileInfoService.queryChildFolder(parentId); return edFileInfoService.queryChildFolder(parentId);
} }
@RequiredPermission(value = FilePermission.VIEW)
@RequestMapping(value = "preview", method = RequestMethod.GET)
public ResponseEntity<InputStreamResource> preview(@RequestParam String id, HttpServletResponse response) {
return edFileInfoService.preview(id, response);
}
} }

View File

@ -164,4 +164,11 @@ public interface EdFileInfoService {
* @return * @return
*/ */
ElectromagneticResult<?> queryChildFolder(String parentId); ElectromagneticResult<?> queryChildFolder(String parentId);
/**
* 文件预览
* @param id
* @param response
*/
ResponseEntity<InputStreamResource> preview(String id, HttpServletResponse response);
} }

View File

@ -75,12 +75,14 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
private String downloadDataDir = ""; private String downloadDataDir = "";
private String uploadDataDir = ""; private String uploadDataDir = "";
private String tmpDir = "";
@PostConstruct @PostConstruct
public void init() { public void init() {
String osName = System.getProperty("os.name").toLowerCase(); String osName = System.getProperty("os.name").toLowerCase();
uploadDataDir = osName.startsWith("win") ? environment.getProperty("data.upload.windows.tmp.path") : environment.getProperty("data.upload.linux.tmp.path"); uploadDataDir = osName.startsWith("win") ? environment.getProperty("data.upload.windows.tmp.path") : environment.getProperty("data.upload.linux.tmp.path");
downloadDataDir = osName.startsWith("win") ? environment.getProperty("data.download.windows.tmp.path") : environment.getProperty("data.download.linux.tmp.path"); downloadDataDir = osName.startsWith("win") ? environment.getProperty("data.download.windows.tmp.path") : environment.getProperty("data.download.linux.tmp.path");
tmpDir = osName.startsWith("win") ? environment.getProperty("data.windows.tmp.path") : environment.getProperty("data.linux.tmp.path");
} }
/** /**
@ -1195,4 +1197,44 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
return ElectromagneticResultUtil.success(res); return ElectromagneticResultUtil.success(res);
} }
/**
* 文件预览
*
* @param id
* @param response
*/
@Override
public ResponseEntity<InputStreamResource> preview(String id, HttpServletResponse response) {
try {
EdFileInfo fileInfo = this.baseMapper.selectById(id);
Assert.isTrue(Objects.nonNull(fileInfo), "文件不存在");
String fileSysPath = commonService.getFileSysPath(fileInfo.getFilePath());
EleCommonUtil.decryptFile(fileSysPath, SecureUtil.aes(FILE_SEC_PASSWD.getBytes()));
if (Arrays.asList("doc", "docx").contains(fileInfo.getFileType())) {
String pdfTmpPath = tmpDir + File.separator + fileInfo.getFileName() + "_" + IdUtil.fastSimpleUUID() + ".pdf";
OfficeFileUtil.doc2pdf(fileSysPath, pdfTmpPath);
fileSysPath = pdfTmpPath;
}
FileSystemResource fileSystemResource = new FileSystemResource(fileSysPath);
String fileName = fileSystemResource.getFilename();
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
fileName = Base64.encode(fileName.substring(0, fileName.lastIndexOf(".")));
response.setHeader("content-disposition", "attachment;filename=" + fileName);
// 构建响应实体(可以返回<byte[]或Resource返回类型取决body入参类型)
return ResponseEntity
.ok()
.headers(headers)
.contentLength(fileSystemResource.contentLength())
.contentType(MediaType.parseMediaType("application/octet-stream;charset=UTF-8"))
.body(new InputStreamResource(fileSystemResource.getInputStream()));
} catch (Exception e) {
String info = StrFormatter.format("文件预览错误文件id是--->{},错误信息--->{}", id,e.getMessage());
log.error(info, e);
throw new BizException(info);
}
}
} }

View File

@ -57,13 +57,25 @@ public final class EleCommonUtil {
return now.format(formatter); return now.format(formatter);
} }
public static void decryptFile(String filePath, AES aes) {
handleFile(filePath, aes, false);
}
public static void encryptFile(String filePath, AES aes) { public static void encryptFile(String filePath, AES aes) {
handleFile(filePath, aes, true);
}
private static void handleFile(String filePath, AES aes, boolean encrypt) {
String tmpPath = filePath + ".tmp"; String tmpPath = filePath + ".tmp";
try ( try (
InputStream inputStream = Files.newInputStream(Paths.get(filePath)); InputStream inputStream = Files.newInputStream(Paths.get(filePath));
OutputStream outputStream = Files.newOutputStream(Paths.get(tmpPath)); OutputStream outputStream = Files.newOutputStream(Paths.get(tmpPath));
) { ) {
if (encrypt) {
aes.encrypt(inputStream, outputStream, true); aes.encrypt(inputStream, outputStream, true);
} else {
aes.decrypt(inputStream, outputStream, true);
}
} catch (Exception e) { } catch (Exception e) {
String info = "文件加密失败"; String info = "文件加密失败";
log.error(info, e); log.error(info, e);