Skip to content

Commit

Permalink
feat(back): implement FilePerNote exporting option for md format;
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBestTvarynka committed Dec 13, 2024
1 parent 14d60d6 commit 33c99cb
Showing 1 changed file with 56 additions and 5 deletions.
61 changes: 56 additions & 5 deletions dataans/src-tauri/src/dataans/export/md.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fs::File;
use std::fs::{self, File};
use std::io::{Error as IoError, Write};
use std::path::Path;

Expand All @@ -19,6 +19,45 @@ fn format_time(time: &OffsetDateTime) -> Result<String, IoError> {
Ok(time.format(&format).expect("OffsetDateTime formatting should not fail"))
}

fn write_space_notes_per_file(
space_id: SpaceId,
collection: &Collection<Note<'static>>,
notes_dir: &Path,
) -> Result<(), IoError> {
for note in collection
.find(doc! {
"space_id": space_id.inner().to_string(),
})
.expect("Space notes querying should not fail")
{
let Note {
id,
text,
created_at,
space_id,
files,
} = note.unwrap();

let mut file = File::create(notes_dir.join(format!("{}.md", id.inner())))?;

writeln!(file, "# `{}`\n", id.inner())?;
writeln!(file, "Space Id: `{}`", space_id.inner())?;
writeln!(file, "Created at: {}\n", format_time(created_at.as_ref())?)?;
writeln!(file, "{}\n", text.as_ref())?;

writeln!(file, "## Files\n")?;
for note_file in files {
let NoteFile { id, name, path } = note_file;

writeln!(file, "### {}\n", name)?;
writeln!(file, "Id: {}", id)?;
writeln!(file, "Path: {:?}\n", path)?;
}
}

Ok(())
}

fn write_space_notes(
space_id: SpaceId,
collection: &Collection<Note<'static>>,
Expand Down Expand Up @@ -101,17 +140,29 @@ pub fn export(notes_export_option: &NotesExportOption, backups_dir: &Path, db: &
for space in spaces_collection.find(None).expect("Spaces querying should not fail.") {
let space = space.unwrap();

let space_file_name = backups_dir.join(format!("{}-{}.md", space.name.as_ref(), space.id.inner()));
let mut space_file = File::create(&space_file_name)
.map_err(|err| format!("Cannot create backup file: {:?}. File: {:?}", err, space_file_name))?;
let space_file_path = backups_dir.join(format!("{}-{}.md", space.name.as_ref(), space.id.inner()));
let mut space_file = File::create(&space_file_path)
.map_err(|err| format!("Cannot create backup file: {:?}. File: {:?}", err, space_file_path))?;

write_space(&space, &mut space_file).map_err(|err| format!("Cannot write space: {:?}", err))?;
write_space_notes(space.id, &notes_collection, &mut space_file)
.map_err(|err| format!("Cannot write space notes: {:?}", err))?;
}
}
NotesExportOption::FilePerNote => {
todo!()
let spaces_collection = db.collection::<OwnedSpace>(SPACES_COLLECTION_NAME);
let notes_collection = db.collection::<Note<'static>>(NOTES_COLLECTION_NAME);

for space in spaces_collection.find(None).expect("Spaces querying should not fail.") {
let space = space.unwrap();

let space_dir = backups_dir.join(format!("{}.{}", space.name.as_ref(), space.id.inner()));
fs::create_dir(&space_dir)
.map_err(|err| format!("Cannot create backup dir: {:?}. File: {:?}", err, space_dir))?;

write_space_notes_per_file(space.id, &notes_collection, &space_dir)
.map_err(|err| format!("Cannot write space notes: {:?}", err))?;
}
}
}

Expand Down

0 comments on commit 33c99cb

Please sign in to comment.