From 0b737db4c7acc2eb91d5288ad20874f726797853 Mon Sep 17 00:00:00 2001 From: choucw045 Date: Wed, 8 Jan 2025 12:20:03 +0800 Subject: [PATCH] format --- .../controllers/RestNotebookController.java | 21 +++++----- .../com/odde/doughnut/entities/Notebook.java | 40 +++++++++---------- .../odde/doughnut/entities/NotebookTest.java | 35 ++++++++-------- .../components/notebook/NotebookButtons.vue | 4 +- 4 files changed, 49 insertions(+), 51 deletions(-) diff --git a/backend/src/main/java/com/odde/doughnut/controllers/RestNotebookController.java b/backend/src/main/java/com/odde/doughnut/controllers/RestNotebookController.java index 63e9176d0..f5e461e54 100644 --- a/backend/src/main/java/com/odde/doughnut/controllers/RestNotebookController.java +++ b/backend/src/main/java/com/odde/doughnut/controllers/RestNotebookController.java @@ -161,18 +161,19 @@ public NotebookAiAssistant getAiAssistant( public ResponseEntity downloadNotebookForObsidian( @PathVariable("notebook") @Schema(type = "integer") Notebook notebook) throws UnexpectedNoAccessRightException, IOException { - currentUser.assertAuthorization(notebook); - - byte[] zipBytes = notebook.generateObsidianExport(); - - return ResponseEntity.ok() - .header(HttpHeaders.CONTENT_DISPOSITION, - "attachment; filename=\"" + sanitizeFileName(notebook.getTitle()) + "-obsidian.zip\"") - .header(HttpHeaders.CONTENT_TYPE, "application/zip") - .body(zipBytes); + currentUser.assertAuthorization(notebook); + + byte[] zipBytes = notebook.generateObsidianExport(); + + return ResponseEntity.ok() + .header( + HttpHeaders.CONTENT_DISPOSITION, + "attachment; filename=\"" + sanitizeFileName(notebook.getTitle()) + "-obsidian.zip\"") + .header(HttpHeaders.CONTENT_TYPE, "application/zip") + .body(zipBytes); } private String sanitizeFileName(String fileName) { - return fileName.replaceAll("[\\\\/:*?\"<>|]", "_"); + return fileName.replaceAll("[\\\\/:*?\"<>|]", "_"); } } diff --git a/backend/src/main/java/com/odde/doughnut/entities/Notebook.java b/backend/src/main/java/com/odde/doughnut/entities/Notebook.java index fc7ad05f2..b853eae91 100644 --- a/backend/src/main/java/com/odde/doughnut/entities/Notebook.java +++ b/backend/src/main/java/com/odde/doughnut/entities/Notebook.java @@ -9,18 +9,18 @@ import com.odde.doughnut.models.Randomizer; import com.odde.doughnut.services.graphRAG.BareNote; import jakarta.persistence.*; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Comparator; import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; import lombok.Getter; import lombok.Setter; import org.springframework.lang.NonNull; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; @Entity @Table(name = "notebook") @@ -203,22 +203,22 @@ public NotebookAssistant buildOrEditNotebookAssistant( public byte[] generateObsidianExport() throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (ZipOutputStream zos = new ZipOutputStream(baos)) { - for (Note note : getNotes()) { - String fileName = sanitizeFileName(note.getTopicConstructor()) + ".md"; - ZipEntry entry = new ZipEntry(fileName); - zos.putNextEntry(entry); - - // Create markdown content - StringBuilder markdown = new StringBuilder(); - markdown.append("# ").append(note.getTopicConstructor()).append("\n\n"); - if (note.getDetails() != null) { - markdown.append(note.getDetails()).append("\n"); - } - - byte[] content = markdown.toString().getBytes(StandardCharsets.UTF_8); - zos.write(content); - zos.closeEntry(); + for (Note note : getNotes()) { + String fileName = sanitizeFileName(note.getTopicConstructor()) + ".md"; + ZipEntry entry = new ZipEntry(fileName); + zos.putNextEntry(entry); + + // Create markdown content + StringBuilder markdown = new StringBuilder(); + markdown.append("# ").append(note.getTopicConstructor()).append("\n\n"); + if (note.getDetails() != null) { + markdown.append(note.getDetails()).append("\n"); } + + byte[] content = markdown.toString().getBytes(StandardCharsets.UTF_8); + zos.write(content); + zos.closeEntry(); + } } return baos.toByteArray(); } diff --git a/backend/src/test/java/com/odde/doughnut/entities/NotebookTest.java b/backend/src/test/java/com/odde/doughnut/entities/NotebookTest.java index 952f70e4a..0ca3d90f6 100644 --- a/backend/src/test/java/com/odde/doughnut/entities/NotebookTest.java +++ b/backend/src/test/java/com/odde/doughnut/entities/NotebookTest.java @@ -4,6 +4,11 @@ import static org.junit.jupiter.api.Assertions.*; import com.odde.doughnut.testability.MakeMe; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -11,14 +16,6 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.transaction.annotation.Transactional; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; -import java.util.zip.ZipOutputStream; - @SpringBootTest @ActiveProfiles("test") @Transactional @@ -65,17 +62,17 @@ void generateObsidianExportShouldCreateValidZipFile() throws IOException { // Verify zip contents try (ByteArrayInputStream bais = new ByteArrayInputStream(zipBytes); - ZipInputStream zis = new ZipInputStream(bais)) { - - ZipEntry entry; - int fileCount = 0; - while ((entry = zis.getNextEntry()) != null) { - fileCount++; - String content = new String(zis.readAllBytes(), StandardCharsets.UTF_8); - assertTrue(content.startsWith("# ")); - assertTrue(content.contains("Content")); - } - assertEquals(3, fileCount); // Head note + 2 child notes + ZipInputStream zis = new ZipInputStream(bais)) { + + ZipEntry entry; + int fileCount = 0; + while ((entry = zis.getNextEntry()) != null) { + fileCount++; + String content = new String(zis.readAllBytes(), StandardCharsets.UTF_8); + assertTrue(content.startsWith("# ")); + assertTrue(content.contains("Content")); + } + assertEquals(3, fileCount); // Head note + 2 child notes } } } diff --git a/frontend/src/components/notebook/NotebookButtons.vue b/frontend/src/components/notebook/NotebookButtons.vue index cf37deaea..b5051b820 100644 --- a/frontend/src/components/notebook/NotebookButtons.vue +++ b/frontend/src/components/notebook/NotebookButtons.vue @@ -76,8 +76,8 @@ const shareNotebook = async () => { } const exportForObsidian = () => { - const link = document.createElement('a') - link.style.display = 'none' + const link = document.createElement("a") + link.style.display = "none" link.href = `/api/notebooks/${props.notebook.id}/obsidian` link.download = `${props.notebook.title}-obsidian.zip` document.body.appendChild(link)