|
1 | 1 | package cn.keking.service.impl; |
2 | 2 |
|
| 3 | +import cn.keking.config.ConfigConstants; |
3 | 4 | import cn.keking.model.FileAttribute; |
| 5 | +import cn.keking.model.ReturnResponse; |
| 6 | +import cn.keking.service.FileHandlerService; |
4 | 7 | import cn.keking.service.FilePreview; |
| 8 | +import cn.keking.utils.DownloadUtils; |
| 9 | +import cn.keking.utils.KkFileUtils; |
| 10 | +import org.apache.commons.codec.binary.Base64; |
5 | 11 | import org.springframework.stereotype.Service; |
6 | 12 | import org.springframework.ui.Model; |
| 13 | +import org.springframework.web.util.HtmlUtils; |
| 14 | + |
| 15 | +import java.io.*; |
| 16 | +import java.nio.charset.StandardCharsets; |
| 17 | +import java.nio.file.Files; |
| 18 | +import java.nio.file.Paths; |
| 19 | + |
7 | 20 |
|
8 | 21 | /** |
9 | 22 | * @author kl (http://kailing.pub) |
|
13 | 26 | @Service |
14 | 27 | public class JsonFilePreviewImpl implements FilePreview { |
15 | 28 |
|
16 | | - private final SimTextFilePreviewImpl simTextFilePreview; |
| 29 | + private final FileHandlerService fileHandlerService; |
| 30 | + private final OtherFilePreviewImpl otherFilePreview; |
17 | 31 |
|
18 | | - public JsonFilePreviewImpl(SimTextFilePreviewImpl simTextFilePreview) { |
19 | | - this.simTextFilePreview = simTextFilePreview; |
| 32 | + public JsonFilePreviewImpl(FileHandlerService fileHandlerService, OtherFilePreviewImpl otherFilePreview) { |
| 33 | + this.fileHandlerService = fileHandlerService; |
| 34 | + this.otherFilePreview = otherFilePreview; |
20 | 35 | } |
21 | 36 |
|
22 | 37 | @Override |
23 | 38 | public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) { |
24 | | - simTextFilePreview.filePreviewHandle(url, model, fileAttribute); |
| 39 | + String fileName = fileAttribute.getName(); |
| 40 | + boolean forceUpdatedCache = fileAttribute.forceUpdatedCache(); |
| 41 | + String filePath = fileAttribute.getOriginFilePath(); |
| 42 | + |
| 43 | + if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(fileName) || !ConfigConstants.isCacheEnabled()) { |
| 44 | + ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName); |
| 45 | + if (response.isFailure()) { |
| 46 | + return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg()); |
| 47 | + } |
| 48 | + filePath = response.getContent(); |
| 49 | + if (ConfigConstants.isCacheEnabled()) { |
| 50 | + fileHandlerService.addConvertedFile(fileName, filePath); |
| 51 | + } |
| 52 | + try { |
| 53 | + String fileData = readJsonFile(filePath, fileName); |
| 54 | + String escapedData = HtmlUtils.htmlEscape(fileData); |
| 55 | + String base64Data = Base64.encodeBase64String(escapedData.getBytes(StandardCharsets.UTF_8)); |
| 56 | + model.addAttribute("textData", base64Data); |
| 57 | + } catch (IOException e) { |
| 58 | + return otherFilePreview.notSupportedFile(model, fileAttribute, e.getLocalizedMessage()); |
| 59 | + } |
| 60 | + return JSON_FILE_PREVIEW_PAGE; |
| 61 | + } |
| 62 | + |
| 63 | + String fileData = null; |
| 64 | + try { |
| 65 | + fileData = HtmlUtils.htmlEscape(readJsonFile(filePath, fileName)); |
| 66 | + } catch (IOException e) { |
| 67 | + e.printStackTrace(); |
| 68 | + } |
| 69 | + String base64Data = Base64.encodeBase64String(fileData.getBytes(StandardCharsets.UTF_8)); |
| 70 | + model.addAttribute("textData", base64Data); |
25 | 71 | return JSON_FILE_PREVIEW_PAGE; |
26 | 72 | } |
| 73 | + |
| 74 | + /** |
| 75 | + * 读取 JSON 文件,强制使用 UTF-8 编码 |
| 76 | + * JSON 标准规定必须使用 UTF-8 编码 |
| 77 | + */ |
| 78 | + private String readJsonFile(String filePath, String fileName) throws IOException { |
| 79 | + File file = new File(filePath); |
| 80 | + if (KkFileUtils.isIllegalFileName(fileName)) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + if (!file.exists() || file.length() == 0) { |
| 84 | + return ""; |
| 85 | + } |
| 86 | + |
| 87 | + // JSON 标准规定使用 UTF-8 编码,不依赖自动检测 |
| 88 | + byte[] bytes = Files.readAllBytes(Paths.get(filePath)); |
| 89 | + return new String(bytes, StandardCharsets.UTF_8); |
| 90 | + } |
27 | 91 | } |
0 commit comments