");
+ if (lines.length == 1 && StrUtil.isBlank(lines[0])) {
+ htmlBuilder.append("
文件内容为空
");
+ } else {
+ for (String line : lines) {
+ if (StrUtil.isBlank(line)) {
+ htmlBuilder.append("
");
+ } else {
+ htmlBuilder.append("
").append(HtmlUtils.htmlEscape(line)).append("
");
+ }
+ }
+ }
+ htmlBuilder.append("
");
+ return new PreviewContent(HTML_CONTENT_TYPE, htmlBuilder.toString().getBytes(StandardCharsets.UTF_8));
+ }
+
+ private String extractDocxText(Path filePath) throws IOException {
+ try (XWPFDocument document = new XWPFDocument(Files.newInputStream(filePath));
+ XWPFWordExtractor extractor = new XWPFWordExtractor(document)) {
+ return extractor.getText();
+ }
+ }
+
+ private String extractDocText(Path filePath) throws IOException {
+ try (HWPFDocument document = new HWPFDocument(Files.newInputStream(filePath));
+ WordExtractor extractor = new WordExtractor(document)) {
+ return extractor.getText();
+ }
+ }
+
+ private String resolveExtension(String fileName) {
+ String normalized = StrUtil.nullToEmpty(fileName).trim().toLowerCase();
+ if (normalized.endsWith(DOCX_EXTENSION)) {
+ return DOCX_EXTENSION;
+ }
+ if (normalized.endsWith(DOC_EXTENSION)) {
+ return DOC_EXTENSION;
+ }
+ if (normalized.endsWith(PDF_EXTENSION)) {
+ return PDF_EXTENSION;
+ }
+ return "";
+ }
+
+ public static class PreviewContent {
+ private final String contentType;
+ private final byte[] content;
+
+ public PreviewContent(String contentType, byte[] content) {
+ this.contentType = contentType;
+ this.content = content;
+ }
+
+ public String getContentType() {
+ return contentType;
+ }
+
+ public byte[] getContent() {
+ return content;
+ }
+ }
+}
diff --git a/comservice/src/test/java/com/njcn/gather/comservice/filepreview/FilePreviewServiceTest.java b/comservice/src/test/java/com/njcn/gather/comservice/filepreview/FilePreviewServiceTest.java
new file mode 100644
index 0000000..6e91276
--- /dev/null
+++ b/comservice/src/test/java/com/njcn/gather/comservice/filepreview/FilePreviewServiceTest.java
@@ -0,0 +1,77 @@
+package com.njcn.gather.comservice.filepreview;
+
+import org.apache.poi.hwpf.HWPFDocument;
+import org.apache.poi.hwpf.usermodel.Range;
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
+import org.junit.Assert;
+import org.junit.Test;
+import org.springframework.http.MediaType;
+
+import java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+public class FilePreviewServiceTest {
+
+ @Test
+ public void previewPdfReturnsInlinePdfContent() throws Exception {
+ Path file = Files.createTempFile("common-preview", ".pdf");
+ byte[] content = "%PDF-1.4".getBytes(StandardCharsets.UTF_8);
+ Files.write(file, content);
+ FilePreviewService service = new FilePreviewService();
+
+ FilePreviewService.PreviewContent previewContent = service.preview(file, "sample.pdf");
+
+ Assert.assertEquals(MediaType.APPLICATION_PDF_VALUE, previewContent.getContentType());
+ Assert.assertArrayEquals(content, previewContent.getContent());
+ }
+
+ @Test
+ public void previewDocxReturnsHtmlContent() throws Exception {
+ Path file = Files.createTempFile("common-preview", ".docx");
+ try (XWPFDocument document = new XWPFDocument();
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
+ document.createParagraph().createRun().setText("第一行");
+ document.createParagraph().createRun().setText("第二行