Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
choucw045 committed Jan 8, 2025
1 parent 67fe3ce commit 0b737db
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,19 @@ public NotebookAiAssistant getAiAssistant(
public ResponseEntity<byte[]> 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("[\\\\/:*?\"<>|]", "_");
}
}
40 changes: 20 additions & 20 deletions backend/src/main/java/com/odde/doughnut/entities/Notebook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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();
}
Expand Down
35 changes: 16 additions & 19 deletions backend/src/test/java/com/odde/doughnut/entities/NotebookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@
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;
import org.springframework.boot.test.context.SpringBootTest;
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
Expand Down Expand Up @@ -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
}
}
}
4 changes: 2 additions & 2 deletions frontend/src/components/notebook/NotebookButtons.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0b737db

Please sign in to comment.