Compare commits
2 Commits
5d773f7ef3
...
b048fc6f02
| Author | SHA1 | Date |
|---|---|---|
|
|
b048fc6f02 | |
|
|
1ab139ce48 |
|
|
@ -132,4 +132,10 @@ public class EdFileInfoController {
|
|||
public ElectromagneticResult<?> queryChildFolder(@RequestParam String 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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -164,4 +164,11 @@ public interface EdFileInfoService {
|
|||
* @return
|
||||
*/
|
||||
ElectromagneticResult<?> queryChildFolder(String parentId);
|
||||
|
||||
/**
|
||||
* 文件预览
|
||||
* @param id
|
||||
* @param response
|
||||
*/
|
||||
ResponseEntity<InputStreamResource> preview(String id, HttpServletResponse response);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -75,12 +75,14 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
|
|||
|
||||
private String downloadDataDir = "";
|
||||
private String uploadDataDir = "";
|
||||
private String tmpDir = "";
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
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");
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件预览
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -57,13 +57,25 @@ public final class EleCommonUtil {
|
|||
return now.format(formatter);
|
||||
}
|
||||
|
||||
public static void decryptFile(String filePath, AES aes) {
|
||||
handleFile(filePath, aes, false);
|
||||
}
|
||||
|
||||
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";
|
||||
try (
|
||||
InputStream inputStream = Files.newInputStream(Paths.get(filePath));
|
||||
OutputStream outputStream = Files.newOutputStream(Paths.get(tmpPath));
|
||||
) {
|
||||
if (encrypt) {
|
||||
aes.encrypt(inputStream, outputStream, true);
|
||||
} else {
|
||||
aes.decrypt(inputStream, outputStream, true);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
String info = "文件加密失败";
|
||||
log.error(info, e);
|
||||
|
|
|
|||
Loading…
Reference in New Issue