mirror of
https://gitlab.com/ArkHost/HelixNotes.git
synced 2026-11-21 19:04:56 +01:00
fix: harden note saves, attachment imports, and Markdown handling
- Fix new-note save conflicts and scoped external Markdown access - Preserve attachment URIs and links across saves, imports, and moves - Ignore unused and overridden attachment reference definitions - Improve Markdown parsing, outline handling, and editor performance - Optimize graph, task filtering, tags, WebDAV, and Obsidian imports - Fix note-list metadata wrapping and add regression coverage - Document saving and close-to-tray behavior
This commit is contained in:
1 parent
14b2a3e8a2
commit
f07241dd3a
34 files changed
+3971
-489
No files matched your search
@@ -1,5 +1,38 @@
|
||||
use std::path::Path;
|
||||
use tauri::{AppHandle, Manager, Runtime};
|
||||
use tauri_plugin_fs::FsExt;
|
||||
|
||||
/// Only configured user-selected vaults may provide an external note's vault-relative root.
|
||||
pub fn external_note_root<R: Runtime>(
|
||||
app: &AppHandle<R>,
|
||||
note_path: &Path,
|
||||
) -> Result<std::path::PathBuf, String> {
|
||||
let note = std::fs::canonicalize(note_path).map_err(|error| error.to_string())?;
|
||||
if let Some(state) = app.try_state::<crate::state::AppState>() {
|
||||
let (active, mut roots) = {
|
||||
let config = state.config.lock().map_err(|error| error.to_string())?;
|
||||
(
|
||||
config.active_vault.clone(),
|
||||
config
|
||||
.vaults
|
||||
.iter()
|
||||
.map(|vault| vault.path.clone())
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
};
|
||||
roots.sort_by_key(|root| std::cmp::Reverse(root.len()));
|
||||
for root in active.into_iter().chain(roots) {
|
||||
if let Ok(root) = std::fs::canonicalize(root) {
|
||||
if note.starts_with(&root) {
|
||||
return Ok(root);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
note.parent()
|
||||
.map(Path::to_path_buf)
|
||||
.ok_or_else(|| "External note has no parent directory".into())
|
||||
}
|
||||
|
||||
/// Grants asset-protocol access only to a vault selected by the user.
|
||||
/// `.helixnotes` is added explicitly because Unix glob matching excludes hidden paths.
|
||||
@@ -37,7 +70,23 @@ pub fn allow_external_note_assets<R: Runtime>(
|
||||
.ok_or_else(|| "External note has no parent directory".to_string())?;
|
||||
app.asset_protocol_scope()
|
||||
.allow_directory(parent, true)
|
||||
.map_err(|error| error.to_string())
|
||||
.map_err(|error| error.to_string())?;
|
||||
let root = external_note_root(app, ¬e)?;
|
||||
let attachments = root.join(".helixnotes/attachments");
|
||||
// Never follow a hidden attachment-directory symlink when extending either scope.
|
||||
if std::fs::canonicalize(&attachments)
|
||||
.is_ok_and(|canonical| canonical == attachments && canonical.is_dir())
|
||||
{
|
||||
app.asset_protocol_scope()
|
||||
.allow_directory(&attachments, true)
|
||||
.map_err(|error| error.to_string())?;
|
||||
if let Some(scope) = app.try_fs_scope() {
|
||||
scope
|
||||
.allow_directory(&attachments, true)
|
||||
.map_err(|error| error.to_string())?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -85,6 +134,101 @@ mod tests {
|
||||
fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn external_note_scopes_include_its_attachments_but_not_hidden_private_metadata() {
|
||||
use tauri_plugin_fs::FsExt;
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let parent = root.path().join("external");
|
||||
let attachments = parent.join(".helixnotes/attachments");
|
||||
fs::create_dir_all(&attachments).unwrap();
|
||||
fs::create_dir_all(parent.join(".helixnotes/history")).unwrap();
|
||||
fs::create_dir_all(parent.join(".ssh")).unwrap();
|
||||
let note = parent.join("Note.md");
|
||||
let image = attachments.join("image.png");
|
||||
let history = parent.join(".helixnotes/history/snapshot.md");
|
||||
let private = parent.join(".ssh/private.bin");
|
||||
for file in [¬e, &image, &history, &private] {
|
||||
fs::write(file, b"fixture").unwrap();
|
||||
}
|
||||
let app = tauri::test::mock_builder()
|
||||
.plugin(tauri_plugin_fs::init())
|
||||
.build(tauri::test::mock_context(tauri::test::noop_assets()))
|
||||
.unwrap();
|
||||
app.fs_scope().allow_file(¬e).unwrap();
|
||||
app.fs_scope().allow_directory(&parent, true).unwrap();
|
||||
let before_private = [
|
||||
app.fs_scope().is_allowed(&history),
|
||||
app.fs_scope().is_allowed(&private),
|
||||
];
|
||||
allow_external_note_assets(app.handle(), ¬e).unwrap();
|
||||
assert!(
|
||||
app.fs_scope().is_allowed(&image),
|
||||
"import must be able to read its managed attachments"
|
||||
);
|
||||
assert!(
|
||||
app.asset_protocol_scope().is_allowed(&image),
|
||||
"the viewer must display managed attachments"
|
||||
);
|
||||
for (file, previous) in [&history, &private].into_iter().zip(before_private) {
|
||||
assert_eq!(
|
||||
app.fs_scope().is_allowed(file),
|
||||
previous,
|
||||
"attachment grants must not widen private metadata access"
|
||||
);
|
||||
assert!(!app.asset_protocol_scope().is_allowed(file));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn external_notebook_uses_its_registered_vault_root_for_attachment_scopes() {
|
||||
use tauri_plugin_fs::FsExt;
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let vault = root.path().join("source");
|
||||
fs::create_dir_all(vault.join("Notebook")).unwrap();
|
||||
fs::create_dir_all(vault.join(".helixnotes/attachments")).unwrap();
|
||||
let note = vault.join("Notebook/Note.md");
|
||||
let image = vault.join(".helixnotes/attachments/image.png");
|
||||
fs::write(¬e, b"Note").unwrap();
|
||||
fs::write(&image, b"Image").unwrap();
|
||||
let app = tauri::test::mock_builder()
|
||||
.plugin(tauri_plugin_fs::init())
|
||||
.manage(crate::state::AppState::new(crate::types::AppConfig {
|
||||
vaults: vec![crate::types::VaultConfig {
|
||||
path: vault.to_string_lossy().into_owned(),
|
||||
..Default::default()
|
||||
}],
|
||||
..Default::default()
|
||||
}))
|
||||
.build(tauri::test::mock_context(tauri::test::noop_assets()))
|
||||
.unwrap();
|
||||
allow_external_note_assets(app.handle(), ¬e).unwrap();
|
||||
assert!(app.asset_protocol_scope().is_allowed(&image));
|
||||
assert!(app.fs_scope().is_allowed(&image));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn external_attachment_scope_does_not_follow_hidden_directory_symlinks() {
|
||||
use tauri_plugin_fs::FsExt;
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let parent = root.path().join("external");
|
||||
let private = root.path().join("private");
|
||||
fs::create_dir_all(parent.join(".helixnotes")).unwrap();
|
||||
fs::create_dir(&private).unwrap();
|
||||
let note = parent.join("Note.md");
|
||||
let file = private.join("private.bin");
|
||||
fs::write(¬e, b"Note").unwrap();
|
||||
fs::write(&file, b"private fixture").unwrap();
|
||||
std::os::unix::fs::symlink(&private, parent.join(".helixnotes/attachments")).unwrap();
|
||||
let app = tauri::test::mock_builder()
|
||||
.plugin(tauri_plugin_fs::init())
|
||||
.build(tauri::test::mock_context(tauri::test::noop_assets()))
|
||||
.unwrap();
|
||||
allow_external_note_assets(app.handle(), ¬e).unwrap();
|
||||
assert!(!app.fs_scope().is_allowed(&file));
|
||||
assert!(!app.asset_protocol_scope().is_allowed(&file));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn external_note_scope_allows_relative_assets_only_below_its_directory() {
|
||||
let root = test_directory("external-note-assets");
|
||||
|
||||
+320
-10
@@ -776,15 +776,31 @@ pub async fn get_notes(
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn read_note(
|
||||
pub async fn read_note<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
state: State<'_, AppState>,
|
||||
vault_path: String,
|
||||
path: String,
|
||||
) -> Result<NoteContent, String> {
|
||||
state.validate_vault(&vault_path)?;
|
||||
tauri::async_runtime::spawn_blocking(move || operations::read_note(&vault_path, &path))
|
||||
.await
|
||||
.map_err(|error| error.to_string())?
|
||||
tauri::async_runtime::spawn_blocking(move || {
|
||||
let vault = std::fs::canonicalize(&vault_path).map_err(|error| error.to_string())?;
|
||||
let target = std::fs::canonicalize(&path).map_err(|error| error.to_string())?;
|
||||
if target.starts_with(vault) {
|
||||
// Keep vault/trash validation, even if app metadata was explicitly selected.
|
||||
return operations::read_note(&vault_path, &path);
|
||||
}
|
||||
// OS file-open requests, dialogs and drops grant this scope. Never grant it here.
|
||||
ensure_scoped_path(&app, &target)?;
|
||||
if !target.is_file()
|
||||
|| target.extension().and_then(|extension| extension.to_str()) != Some("md")
|
||||
{
|
||||
return Err("Note path must point to a Markdown file".to_string());
|
||||
}
|
||||
operations::read_note_content(&target, &path)
|
||||
})
|
||||
.await
|
||||
.map_err(|error| error.to_string())?
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -825,6 +841,44 @@ pub async fn save_note(
|
||||
.map_err(|error| error.to_string())?
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn import_note<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
state: State<'_, AppState>,
|
||||
vault_path: String,
|
||||
source_path: String,
|
||||
notebook_relative: Option<String>,
|
||||
expected_raw: String,
|
||||
references: Vec<operations::ExternalNoteReference>,
|
||||
) -> Result<NoteContent, String> {
|
||||
let search = state.vault_snapshot(&vault_path)?;
|
||||
let maintenance = state.vault_maintenance.clone().lock_owned().await;
|
||||
let writes = state.note_save.clone().lock_owned().await;
|
||||
tauri::async_runtime::spawn_blocking(move || {
|
||||
let (_maintenance, _writes) = (maintenance, writes);
|
||||
let source_root = asset_scope::external_note_root(&app, Path::new(&source_path))?;
|
||||
let note = operations::import_note(
|
||||
&vault_path,
|
||||
&source_path,
|
||||
&source_root,
|
||||
notebook_relative.as_deref(),
|
||||
&expected_raw,
|
||||
references,
|
||||
|path| ensure_scoped_path(&app, path),
|
||||
)?;
|
||||
index_note_bg(search, ¬e.path);
|
||||
let event = crate::vault::watcher::changed_entry(
|
||||
PathBuf::from(¬e.path),
|
||||
"create",
|
||||
Path::new(&vault_path),
|
||||
);
|
||||
let _ = app.emit("file-changed", event);
|
||||
Ok(note)
|
||||
})
|
||||
.await
|
||||
.map_err(|error| error.to_string())?
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn duplicate_note(
|
||||
state: State<'_, AppState>,
|
||||
@@ -2977,10 +3031,13 @@ fn open_url_with_system(url: &str) -> Result<(), String> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod external_access_tests {
|
||||
use super::{ensure_readable_path, ensure_writable_path, validate_external_url};
|
||||
use super::{
|
||||
ensure_readable_path, ensure_writable_path, read_note, save_note, validate_external_url,
|
||||
};
|
||||
use crate::state::AppState;
|
||||
use crate::types::AppConfig;
|
||||
use std::fs;
|
||||
use tauri::Manager;
|
||||
use tauri_plugin_fs::FsExt;
|
||||
|
||||
#[test]
|
||||
@@ -3008,6 +3065,250 @@ mod external_access_tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn selected_external_markdown_can_be_viewed_but_not_saved_in_place() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let vault = root.path().join("vault");
|
||||
let vault_path = vault.to_string_lossy().into_owned();
|
||||
crate::vault::operations::ensure_vault_structure(&vault_path).unwrap();
|
||||
let outside = root.path().join("café 雪.md");
|
||||
let raw = "---\ntitle: External note\ncustom: preserved\n---\n# External note\n\n\n";
|
||||
fs::write(&outside, raw).unwrap();
|
||||
let app = tauri::test::mock_builder()
|
||||
.plugin(tauri_plugin_fs::init())
|
||||
.manage(AppState::new(AppConfig {
|
||||
active_vault: Some(vault_path.clone()),
|
||||
..Default::default()
|
||||
}))
|
||||
.build(tauri::test::mock_context(tauri::test::noop_assets()))
|
||||
.unwrap();
|
||||
let path = outside.to_string_lossy().into_owned();
|
||||
|
||||
assert!(read_note(
|
||||
app.handle().clone(),
|
||||
app.state(),
|
||||
vault_path.clone(),
|
||||
path.clone()
|
||||
)
|
||||
.await
|
||||
.is_err());
|
||||
app.fs_scope().allow_file(&outside).unwrap();
|
||||
let note = read_note(
|
||||
app.handle().clone(),
|
||||
app.state(),
|
||||
vault_path.clone(),
|
||||
path.clone(),
|
||||
)
|
||||
.await
|
||||
.expect("an explicitly selected external Markdown file must open in the viewer");
|
||||
assert_eq!(note.path, path);
|
||||
assert_eq!(note.raw, raw);
|
||||
assert_eq!(note.meta.title, "External note");
|
||||
assert!(note.content.contains(""));
|
||||
assert!(save_note(
|
||||
app.state(),
|
||||
vault_path,
|
||||
path,
|
||||
note.meta,
|
||||
"Changed".into(),
|
||||
note.raw
|
||||
)
|
||||
.await
|
||||
.is_err());
|
||||
assert_eq!(fs::read_to_string(&outside).unwrap(), raw);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn external_import_requires_scoped_attachments_and_keeps_its_queued_vault() {
|
||||
use futures::FutureExt;
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let a = root.path().join("A").to_string_lossy().into_owned();
|
||||
let b = root.path().join("B").to_string_lossy().into_owned();
|
||||
crate::vault::operations::ensure_vault_structure(&a).unwrap();
|
||||
crate::vault::operations::ensure_vault_structure(&b).unwrap();
|
||||
let external = root.path().join("external");
|
||||
fs::create_dir(&external).unwrap();
|
||||
let source = external.join("External.md");
|
||||
let image = external.join("photo.png");
|
||||
let raw = "";
|
||||
fs::write(&source, raw).unwrap();
|
||||
fs::write(&image, b"image bytes").unwrap();
|
||||
let app = tauri::test::mock_builder()
|
||||
.plugin(tauri_plugin_fs::init())
|
||||
.manage(AppState::new(AppConfig {
|
||||
active_vault: Some(a.clone()),
|
||||
vaults: vec![
|
||||
crate::types::VaultConfig {
|
||||
path: a.clone(),
|
||||
..Default::default()
|
||||
},
|
||||
crate::types::VaultConfig {
|
||||
path: b.clone(),
|
||||
..Default::default()
|
||||
},
|
||||
],
|
||||
..Default::default()
|
||||
}))
|
||||
.build(tauri::test::mock_context(tauri::test::noop_assets()))
|
||||
.unwrap();
|
||||
app.fs_scope().allow_file(&source).unwrap();
|
||||
let plan = || {
|
||||
vec![crate::vault::operations::ExternalNoteReference {
|
||||
from: raw.find("photo.png").unwrap(),
|
||||
to: raw.find("photo.png").unwrap() + "photo.png".len(),
|
||||
href: "photo.png".into(),
|
||||
html: false,
|
||||
pdf: false,
|
||||
}]
|
||||
};
|
||||
let run = || {
|
||||
super::import_note(
|
||||
app.handle().clone(),
|
||||
app.state(),
|
||||
a.clone(),
|
||||
source.to_string_lossy().into_owned(),
|
||||
None,
|
||||
raw.into(),
|
||||
plan(),
|
||||
)
|
||||
};
|
||||
assert!(run().await.unwrap_err().contains("selected"));
|
||||
assert!(!std::path::Path::new(&a).join("External.md").exists());
|
||||
app.fs_scope().allow_file(&image).unwrap();
|
||||
let state = app.state::<AppState>();
|
||||
let writes = state.note_save.lock().await;
|
||||
let mut importing = Box::pin(run());
|
||||
assert!(importing.as_mut().now_or_never().is_none());
|
||||
state.config.lock().unwrap().active_vault = Some(b.clone());
|
||||
drop(writes);
|
||||
let note = importing.await.unwrap();
|
||||
assert!(std::path::Path::new(¬e.path).starts_with(&a));
|
||||
assert!(!std::path::Path::new(&b).join("External.md").exists());
|
||||
assert_eq!(fs::read_to_string(source).unwrap(), raw);
|
||||
assert_eq!(fs::read(image).unwrap(), b"image bytes");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn imported_external_managed_attachments_use_the_source_vault_with_real_scopes() {
|
||||
use crate::{asset_scope, types::VaultConfig, vault::operations};
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let source_root = root.path().join("source");
|
||||
let destination = root
|
||||
.path()
|
||||
.join("destination")
|
||||
.to_string_lossy()
|
||||
.into_owned();
|
||||
operations::ensure_vault_structure(source_root.to_str().unwrap()).unwrap();
|
||||
operations::ensure_vault_structure(&destination).unwrap();
|
||||
std::fs::create_dir(source_root.join("Notebook")).unwrap();
|
||||
let source = source_root.join("Notebook/Note.md");
|
||||
let image = source_root.join(".helixnotes/attachments/photo%20).png");
|
||||
let href = ".helixnotes/attachments/photo%2520%29.png";
|
||||
let raw = format!("");
|
||||
std::fs::write(&source, &raw).unwrap();
|
||||
std::fs::write(&image, b"source image").unwrap();
|
||||
let app = tauri::test::mock_builder()
|
||||
.plugin(tauri_plugin_fs::init())
|
||||
.manage(AppState::new(AppConfig {
|
||||
active_vault: Some(destination.clone()),
|
||||
vaults: vec![VaultConfig {
|
||||
path: source_root.to_string_lossy().into_owned(),
|
||||
..Default::default()
|
||||
}],
|
||||
..Default::default()
|
||||
}))
|
||||
.build(tauri::test::mock_context(tauri::test::noop_assets()))
|
||||
.unwrap();
|
||||
app.fs_scope().allow_file(&source).unwrap();
|
||||
app.fs_scope()
|
||||
.allow_directory(source.parent().unwrap(), true)
|
||||
.unwrap();
|
||||
asset_scope::allow_external_note_assets(app.handle(), &source).unwrap();
|
||||
let from = raw.find(href).unwrap();
|
||||
let note = super::import_note(
|
||||
app.handle().clone(),
|
||||
app.state(),
|
||||
destination.clone(),
|
||||
source.to_string_lossy().into_owned(),
|
||||
None,
|
||||
raw.clone(),
|
||||
vec![operations::ExternalNoteReference {
|
||||
from,
|
||||
to: from + href.len(),
|
||||
href: href.into(),
|
||||
html: false,
|
||||
pdf: false,
|
||||
}],
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(note.raw.contains(".helixnotes/attachments/"));
|
||||
assert_eq!(std::fs::read_to_string(source).unwrap(), raw);
|
||||
assert_eq!(std::fs::read(image).unwrap(), b"source image");
|
||||
let files: Vec<_> =
|
||||
std::fs::read_dir(std::path::Path::new(&destination).join(".helixnotes/attachments"))
|
||||
.unwrap()
|
||||
.collect();
|
||||
assert_eq!(files.len(), 1);
|
||||
assert_eq!(
|
||||
std::fs::read(files[0].as_ref().unwrap().path()).unwrap(),
|
||||
b"source image"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn external_note_scope_does_not_allow_other_files_or_internal_metadata() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let vault = root.path().join("vault");
|
||||
let vault_path = vault.to_string_lossy().into_owned();
|
||||
crate::vault::operations::ensure_vault_structure(&vault_path).unwrap();
|
||||
let outside = root.path().join("outside.md");
|
||||
let non_markdown = root.path().join("outside.txt");
|
||||
let internal = vault.join(".helixnotes/history/snapshot.md");
|
||||
fs::create_dir_all(internal.parent().unwrap()).unwrap();
|
||||
for path in [&outside, &non_markdown, &internal] {
|
||||
fs::write(path, "Original").unwrap();
|
||||
}
|
||||
let app = tauri::test::mock_builder()
|
||||
.plugin(tauri_plugin_fs::init())
|
||||
.manage(AppState::new(AppConfig {
|
||||
active_vault: Some(vault_path.clone()),
|
||||
..Default::default()
|
||||
}))
|
||||
.build(tauri::test::mock_context(tauri::test::noop_assets()))
|
||||
.unwrap();
|
||||
let read = |path: &std::path::Path| {
|
||||
read_note(
|
||||
app.handle().clone(),
|
||||
app.state(),
|
||||
vault_path.clone(),
|
||||
path.to_string_lossy().into_owned(),
|
||||
)
|
||||
};
|
||||
assert!(read(&outside).await.is_err());
|
||||
for path in [&non_markdown, &internal] {
|
||||
app.fs_scope().allow_file(path).unwrap();
|
||||
assert!(read(path).await.is_err());
|
||||
}
|
||||
app.fs_scope().allow_file(&outside).unwrap();
|
||||
assert!(read_note(
|
||||
app.handle().clone(),
|
||||
app.state(),
|
||||
root.path().to_string_lossy().into_owned(),
|
||||
outside.to_string_lossy().into_owned()
|
||||
)
|
||||
.await
|
||||
.is_err());
|
||||
#[cfg(unix)]
|
||||
{
|
||||
let unselected = root.path().join("unselected.md");
|
||||
let link = vault.join("escape.md");
|
||||
fs::write(&unselected, "Private").unwrap();
|
||||
std::os::unix::fs::symlink(&unselected, &link).unwrap();
|
||||
assert!(read(&link).await.is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_access_requires_the_active_vault_or_an_explicit_user_scope() {
|
||||
let root = std::env::temp_dir().join(format!(
|
||||
@@ -4279,13 +4580,22 @@ mod mutation_order_tests {
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
read_note(app.state(), a.clone(), alpha.path.clone())
|
||||
.await
|
||||
.unwrap()
|
||||
.content,
|
||||
read_note(
|
||||
app.handle().clone(),
|
||||
app.state(),
|
||||
a.clone(),
|
||||
alpha.path.clone()
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
.content,
|
||||
"# Shared\n\nDetached save"
|
||||
);
|
||||
assert!(read_note(app.state(), b.clone(), alpha.path).await.is_err());
|
||||
assert!(
|
||||
read_note(app.handle().clone(), app.state(), b.clone(), alpha.path)
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(std::fs::read(&beta.path).unwrap(), beta_before);
|
||||
}
|
||||
|
||||
|
||||
@@ -187,6 +187,7 @@ pub fn run() {
|
||||
commands::get_notes,
|
||||
commands::read_note,
|
||||
commands::save_note,
|
||||
commands::import_note,
|
||||
commands::create_note,
|
||||
commands::duplicate_note,
|
||||
commands::create_daily_note,
|
||||
|
||||
+247
-6
@@ -409,7 +409,7 @@ impl WebdavClient {
|
||||
"Remote file {relpath} changed during sync; retry sync"
|
||||
));
|
||||
}
|
||||
let bytes = resp.bytes().map_err(|e| e.to_string())?.to_vec();
|
||||
let bytes: Vec<u8> = resp.bytes().map_err(|e| e.to_string())?.into();
|
||||
if !expected_etag.is_empty() && self.etag_for(relpath)? != expected_etag {
|
||||
return Err(format!(
|
||||
"Remote file {relpath} changed during sync; retry sync"
|
||||
@@ -624,10 +624,34 @@ fn parse_multistatus(xml: &str, base_path: &str) -> Result<Vec<RemoteEntry>, Str
|
||||
}
|
||||
Ok(Event::Text(e)) => {
|
||||
if cap != Cap::None {
|
||||
if let Ok(decoded) = e.decode() {
|
||||
if let Ok(text) = quick_xml::escape::unescape(&decoded) {
|
||||
buf.push_str(&text);
|
||||
}
|
||||
let text = e
|
||||
.decode()
|
||||
.map_err(|e| format!("XML text decode error: {e}"))?;
|
||||
buf.push_str(&text);
|
||||
}
|
||||
}
|
||||
Ok(Event::CData(e)) => {
|
||||
if cap != Cap::None {
|
||||
let text = e
|
||||
.decode()
|
||||
.map_err(|e| format!("XML CDATA decode error: {e}"))?;
|
||||
buf.push_str(&text);
|
||||
}
|
||||
}
|
||||
Ok(Event::GeneralRef(e)) => {
|
||||
if cap != Cap::None {
|
||||
if let Some(ch) = e
|
||||
.resolve_char_ref()
|
||||
.map_err(|e| format!("XML reference error: {e}"))?
|
||||
{
|
||||
buf.push(ch);
|
||||
} else {
|
||||
let name = e
|
||||
.decode()
|
||||
.map_err(|e| format!("XML reference decode error: {e}"))?;
|
||||
let text = quick_xml::escape::resolve_xml_entity(&name)
|
||||
.ok_or_else(|| format!("Unknown XML entity: &{name};"))?;
|
||||
buf.push_str(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1332,6 +1356,17 @@ mod tests {
|
||||
}
|
||||
fn webdav_test_client(
|
||||
responses: Vec<(&'static str, &'static str)>,
|
||||
) -> (WebdavClient, std::thread::JoinHandle<Vec<String>>) {
|
||||
webdav_test_client_bytes(
|
||||
responses
|
||||
.into_iter()
|
||||
.map(|(status, body)| (status, body.as_bytes().to_vec()))
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
fn webdav_test_client_bytes(
|
||||
responses: Vec<(&'static str, Vec<u8>)>,
|
||||
) -> (WebdavClient, std::thread::JoinHandle<Vec<String>>) {
|
||||
use std::io::Write;
|
||||
use std::net::TcpListener;
|
||||
@@ -1370,10 +1405,11 @@ mod tests {
|
||||
}
|
||||
requests.push(headers);
|
||||
let response = format!(
|
||||
"HTTP/1.1 {status}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
|
||||
"HTTP/1.1 {status}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
|
||||
body.len()
|
||||
);
|
||||
stream.write_all(response.as_bytes()).unwrap();
|
||||
stream.write_all(&body).unwrap();
|
||||
}
|
||||
requests
|
||||
});
|
||||
@@ -1561,4 +1597,209 @@ mod tests {
|
||||
.contains("no strong ETag"));
|
||||
assert!(requests.join().unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multistatus_preserves_named_and_numeric_references() {
|
||||
let xml = r#"<d:multistatus xmlns:d="DAV:"><d:response>
|
||||
<d:href>/vault/a&b'<>"-é-雪.md</d:href>
|
||||
<d:propstat><d:prop>
|
||||
<d:getetag>"v&'<>雪é"</d:getetag>
|
||||
</d:prop></d:propstat>
|
||||
</d:response></d:multistatus>"#;
|
||||
let entries = parse_multistatus(xml, "/vault").unwrap();
|
||||
assert_eq!(entries.len(), 1);
|
||||
assert_eq!(entries[0].relpath, "a&b'<>\"-é-雪.md");
|
||||
assert_eq!(entries[0].etag, "\"v&'<>雪é\"");
|
||||
assert!(!entries[0].is_dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multistatus_preserves_mixed_cdata_and_strong_etag_policy() {
|
||||
let xml = r#"<multistatus xmlns="DAV:">
|
||||
<response>
|
||||
<href>/vault/<![CDATA[café&]]>雪.md</href>
|
||||
<propstat><prop><getetag>"<![CDATA[v&]]>雪"</getetag></prop></propstat>
|
||||
</response>
|
||||
<response>
|
||||
<href><![CDATA[/vault/folder/]]></href>
|
||||
<propstat><prop><getetag><![CDATA[W/"weak"]]></getetag>
|
||||
<resourcetype><collection/></resourcetype></prop></propstat>
|
||||
</response>
|
||||
</multistatus>"#;
|
||||
let entries = parse_multistatus(xml, "/vault").unwrap();
|
||||
assert_eq!(entries.len(), 2);
|
||||
assert_eq!(entries[0].relpath, "café&雪.md");
|
||||
assert_eq!(entries[0].etag, "\"v&雪\"");
|
||||
assert_eq!(entries[1].relpath, "folder");
|
||||
assert!(entries[1].etag.is_empty());
|
||||
assert!(entries[1].is_dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multistatus_rejects_unknown_and_malformed_captured_references() {
|
||||
for reference in [
|
||||
"&unknown;",
|
||||
"&;",
|
||||
"&#;",
|
||||
"&#xZZ;",
|
||||
"�",
|
||||
"�",
|
||||
"�",
|
||||
"&",
|
||||
"{",
|
||||
] {
|
||||
for field in ["href", "getetag"] {
|
||||
let (href, etag) = if field == "href" {
|
||||
(format!("/vault/bad{reference}.md"), "\"ok\"".to_string())
|
||||
} else {
|
||||
("/vault/bad.md".to_string(), format!("\"bad{reference}\""))
|
||||
};
|
||||
let xml = format!(
|
||||
"<multistatus><response><href>/vault/good.md</href>\
|
||||
<getetag>\"good\"</getetag></response><response>\
|
||||
<href>{href}</href><getetag>{etag}</getetag>\
|
||||
</response></multistatus>"
|
||||
);
|
||||
assert!(
|
||||
parse_multistatus(&xml, "/vault").is_err(),
|
||||
"accepted {reference:?} in {field}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multistatus_does_not_expand_custom_or_external_dtd_entities() {
|
||||
for declaration in [
|
||||
r#"<!ENTITY custom "expanded">"#,
|
||||
r#"<!ENTITY custom SYSTEM "file:///nonexistent-helixnotes-entity">"#,
|
||||
] {
|
||||
let xml = format!(
|
||||
"<!DOCTYPE multistatus [{declaration}]><multistatus><response>\
|
||||
<href>/vault/&custom;.md</href><getetag>\"etag\"</getetag>\
|
||||
</response></multistatus>"
|
||||
);
|
||||
assert!(parse_multistatus(&xml, "/vault")
|
||||
.err()
|
||||
.unwrap()
|
||||
.contains("Unknown XML entity"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_preserves_binary_download_and_rechecks_strong_etag() {
|
||||
let bytes: Vec<u8> = (0..HASH_BUFFER_SIZE * 2 + 137)
|
||||
.map(|i| (i % 256) as u8)
|
||||
.collect();
|
||||
let listing = br#"<d:multistatus xmlns:d="DAV:"><d:response>
|
||||
<d:href>/vault/file.bin</d:href>
|
||||
<d:propstat><d:prop><d:getetag>"stable"</d:getetag></d:prop></d:propstat>
|
||||
</d:response></d:multistatus>"#;
|
||||
let (client, requests) = webdav_test_client_bytes(vec![
|
||||
("200 OK\r\nETag: \"stable\"", bytes.clone()),
|
||||
("207 Multi-Status", listing.to_vec()),
|
||||
]);
|
||||
assert_eq!(client.get("file.bin", "\"stable\"").unwrap(), bytes);
|
||||
let requests = requests.join().unwrap();
|
||||
assert_eq!(requests.len(), 2);
|
||||
assert!(requests[0].starts_with("GET /vault/file.bin "));
|
||||
assert!(requests[0]
|
||||
.to_ascii_lowercase()
|
||||
.contains("if-match: \"stable\""));
|
||||
assert!(requests[1].starts_with("PROPFIND /vault/file.bin "));
|
||||
assert!(requests[1].to_ascii_lowercase().contains("depth: 0"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_rejects_etag_changed_after_download_and_http_failures() {
|
||||
let listing = r#"<multistatus><response><href>/vault/note.md</href>
|
||||
<getetag>"new"</getetag></response></multistatus>"#;
|
||||
let (client, requests) = webdav_test_client(vec![
|
||||
("200 OK\r\nETag: \"old\"", "snapshot"),
|
||||
("207 Multi-Status", listing),
|
||||
]);
|
||||
assert!(client
|
||||
.get("note.md", "\"old\"")
|
||||
.unwrap_err()
|
||||
.contains("retry"));
|
||||
assert_eq!(requests.join().unwrap().len(), 2);
|
||||
|
||||
for (status, expected_error) in [
|
||||
("412 Precondition Failed", "retry"),
|
||||
("404 Not Found", "HTTP 404"),
|
||||
] {
|
||||
let (client, requests) = webdav_test_client(vec![(status, "ignored")]);
|
||||
assert!(client
|
||||
.get("note.md", "\"old\"")
|
||||
.unwrap_err()
|
||||
.contains(expected_error));
|
||||
assert_eq!(requests.join().unwrap().len(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_enforces_whole_body_timeout_for_progressing_chunked_download() {
|
||||
use std::io::Write;
|
||||
use std::net::TcpListener;
|
||||
use std::time::Duration;
|
||||
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let url = format!("http://{}/vault", listener.local_addr().unwrap());
|
||||
let server = std::thread::spawn(move || {
|
||||
let (mut stream, _) = listener.accept().unwrap();
|
||||
stream
|
||||
.set_read_timeout(Some(Duration::from_secs(2)))
|
||||
.unwrap();
|
||||
stream
|
||||
.set_write_timeout(Some(Duration::from_secs(2)))
|
||||
.unwrap();
|
||||
let mut request = Vec::new();
|
||||
let mut buffer = [0; 4096];
|
||||
while !request.windows(4).any(|part| part == b"\r\n\r\n") {
|
||||
let count = stream.read(&mut buffer).unwrap();
|
||||
assert!(count > 0);
|
||||
request.extend_from_slice(&buffer[..count]);
|
||||
}
|
||||
assert!(request.starts_with(b"GET /vault/file.bin "));
|
||||
stream
|
||||
.write_all(
|
||||
b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\
|
||||
Connection: close\r\n\r\n1\r\nx\r\n",
|
||||
)
|
||||
.unwrap();
|
||||
let mut chunks_sent = 1;
|
||||
// Each gap is below the 400ms timeout, but the full body takes 1s.
|
||||
for _ in 0..20 {
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
if stream.write_all(b"1\r\nx\r\n").is_err() {
|
||||
return chunks_sent;
|
||||
}
|
||||
chunks_sent += 1;
|
||||
}
|
||||
let _ = stream.write_all(b"0\r\n\r\n");
|
||||
chunks_sent
|
||||
});
|
||||
let mut client = WebdavClient::new(WebdavConfig {
|
||||
url,
|
||||
username: String::new(),
|
||||
password: String::new(),
|
||||
})
|
||||
.unwrap();
|
||||
client.client = reqwest::blocking::Client::builder()
|
||||
.no_proxy()
|
||||
.timeout(Duration::from_millis(400))
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let result = client.get("file.bin", "");
|
||||
let chunks_sent = server.join().unwrap();
|
||||
assert!(
|
||||
chunks_sent > 1,
|
||||
"server must make progress during the download"
|
||||
);
|
||||
assert!(
|
||||
result.is_err(),
|
||||
"accepted a body exceeding the total timeout"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -883,30 +883,79 @@ fn pathdiff(target: &str, base: &str) -> Result<String, ()> {
|
||||
}
|
||||
|
||||
fn percent_decode(s: &str) -> String {
|
||||
let mut result = String::with_capacity(s.len());
|
||||
let mut chars = s.bytes();
|
||||
while let Some(b) = chars.next() {
|
||||
if b == b'%' {
|
||||
let hi = chars.next();
|
||||
let lo = chars.next();
|
||||
if let (Some(h), Some(l)) = (hi, lo) {
|
||||
if let Ok(byte) = u8::from_str_radix(&format!("{}{}", h as char, l as char), 16) {
|
||||
result.push(byte as char);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
result.push('%');
|
||||
} else {
|
||||
result.push(b as char);
|
||||
}
|
||||
}
|
||||
result
|
||||
// Keep the complete original reference if escaped bytes are not valid UTF-8.
|
||||
urlencoding::decode(s)
|
||||
.map(|decoded| decoded.into_owned())
|
||||
.unwrap_or_else(|_| s.to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn percent_decode_preserves_unicode_plus_and_malformed_escapes() {
|
||||
for (input, expected) in [
|
||||
("café 雪+.png", "café 雪+.png"),
|
||||
("caf%C3%A9%20%E9%9B%AA+.png", "café 雪+.png"),
|
||||
("bad%GG%20name.png", "bad%GG name.png"),
|
||||
("tail%", "tail%"),
|
||||
("tail%2", "tail%2"),
|
||||
("雪%FF%20+%GG", "雪%FF%20+%GG"),
|
||||
("%E9%9B", "%E9%9B"),
|
||||
] {
|
||||
assert_eq!(percent_decode(input), expected, "input {input:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_rewrites_unicode_encoded_and_malformed_attachment_references() {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let root = vault.path();
|
||||
let note_dir = root.join("笔记");
|
||||
std::fs::create_dir(¬e_dir).unwrap();
|
||||
let attachments = [
|
||||
"café 雪+.png",
|
||||
"bad%GG.png",
|
||||
"tail%.png",
|
||||
"tail%2.png",
|
||||
"bytes%FF%20.png",
|
||||
];
|
||||
for name in attachments {
|
||||
std::fs::write(note_dir.join(name), name.as_bytes()).unwrap();
|
||||
}
|
||||
let note_path = note_dir.join("Entry.md");
|
||||
std::fs::write(
|
||||
¬e_path,
|
||||
"# Entry\n\n\n\
|
||||
[encoded](caf%C3%A9%20%E9%9B%AA+.png#preview)\n\
|
||||
\n\n\
|
||||
[short](tail%2.png)\n[invalid UTF-8](bytes%FF%20.png)\n",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let result = import(root.to_str().unwrap()).unwrap();
|
||||
assert_eq!(result.attachments_moved, attachments.len() as u64);
|
||||
let note = std::fs::read_to_string(¬e_path).unwrap();
|
||||
for reference in [
|
||||
"",
|
||||
"[encoded](../.helixnotes/attachments/笔记/café%20雪+.png#preview)",
|
||||
"",
|
||||
"",
|
||||
"[short](../.helixnotes/attachments/笔记/tail%2.png)",
|
||||
"[invalid UTF-8](../.helixnotes/attachments/笔记/bytes%FF%20.png)",
|
||||
] {
|
||||
assert!(note.contains(reference), "missing {reference:?} in {note}");
|
||||
}
|
||||
for name in attachments {
|
||||
assert!(!note_dir.join(name).exists());
|
||||
assert_eq!(
|
||||
std::fs::read(root.join(".helixnotes/attachments/笔记").join(name)).unwrap(),
|
||||
name.as_bytes()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_normalize_tags_comma_string() {
|
||||
let mapping: serde_yaml::Mapping = serde_yaml::from_str("tags: \"a, b, c\"").unwrap();
|
||||
|
||||
@@ -2,3 +2,6 @@ pub mod frontmatter;
|
||||
pub mod import;
|
||||
pub mod operations;
|
||||
pub mod watcher;
|
||||
|
||||
#[cfg(test)]
|
||||
mod note_import_tests;
|
||||
@@ -0,0 +1,493 @@
|
||||
use super::operations::{
|
||||
ensure_vault_structure, import_note as import_from_root, ExternalNoteReference,
|
||||
};
|
||||
|
||||
fn import_note(
|
||||
vault: &str,
|
||||
source: &str,
|
||||
notebook: Option<&str>,
|
||||
raw: &str,
|
||||
references: Vec<ExternalNoteReference>,
|
||||
authorize: impl Fn(&Path) -> Result<(), String>,
|
||||
) -> Result<crate::types::NoteContent, String> {
|
||||
import_from_root(
|
||||
vault,
|
||||
source,
|
||||
Path::new(source).parent().unwrap(),
|
||||
notebook,
|
||||
raw,
|
||||
references,
|
||||
authorize,
|
||||
)
|
||||
}
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
fn reference(raw: &str, original: &str, href: &str, html: bool) -> ExternalNoteReference {
|
||||
let from = raw.find(original).unwrap();
|
||||
ExternalNoteReference {
|
||||
from,
|
||||
to: from + original.len(),
|
||||
href: href.into(),
|
||||
html,
|
||||
pdf: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn fixture(raw: &str) -> (tempfile::TempDir, String, PathBuf) {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let vault = root.path().join("vault").to_string_lossy().into_owned();
|
||||
ensure_vault_structure(&vault).unwrap();
|
||||
let external = root.path().join("external");
|
||||
fs::create_dir_all(external.join("images")).unwrap();
|
||||
let source = external.join("External.md");
|
||||
fs::write(&source, raw).unwrap();
|
||||
(root, vault, source)
|
||||
}
|
||||
|
||||
fn attachments(vault: &str) -> Vec<PathBuf> {
|
||||
fs::read_dir(Path::new(vault).join(".helixnotes/attachments"))
|
||||
.unwrap()
|
||||
.map(|entry| entry.unwrap().path())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn imports_relative_files_once_preserving_originals_frontmatter_and_literal_content() {
|
||||
let raw = "---\r\ntitle: Portable\r\ncustom: 雪 📝\r\n---\r\n\r\n\r\n<a href=\"images/report.pdf#page=2&mode=fit\">PDF</a>\r\n\r\n~~~md\r\n\r\n~~~\r\n";
|
||||
for notebook in [None, Some("Projects/Nested")] {
|
||||
let (_root, vault, source) = fixture(raw);
|
||||
let parent = source.parent().unwrap();
|
||||
fs::write(parent.join("images/café 雪.png"), b"image bytes").unwrap();
|
||||
fs::write(parent.join("images/report.pdf"), b"pdf bytes").unwrap();
|
||||
fs::create_dir_all(Path::new(&vault).join("Projects/Nested")).unwrap();
|
||||
let references = vec![
|
||||
reference(raw, "images/café%20雪.png", "images/café%20雪.png", false),
|
||||
reference(
|
||||
raw,
|
||||
"images/./café%20雪.png",
|
||||
"images/./café%20雪.png",
|
||||
false,
|
||||
),
|
||||
reference(
|
||||
raw,
|
||||
"images/report.pdf#page=2&mode=fit",
|
||||
"images/report.pdf#page=2&mode=fit",
|
||||
true,
|
||||
),
|
||||
];
|
||||
let note = import_note(
|
||||
&vault,
|
||||
source.to_str().unwrap(),
|
||||
notebook,
|
||||
raw,
|
||||
references,
|
||||
|path| {
|
||||
if path.starts_with(parent) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("Not selected".into())
|
||||
}
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
assert!(note
|
||||
.raw
|
||||
.starts_with("---\r\ntitle: Portable\r\ncustom: 雪 📝\r\n---\r\n"));
|
||||
assert!(note.raw.contains(""));
|
||||
assert!(note.raw.contains("#page=2&mode=fit"));
|
||||
assert_eq!(fs::read_to_string(&source).unwrap(), raw);
|
||||
assert_eq!(fs::read_to_string(¬e.path).unwrap(), note.raw);
|
||||
let files = attachments(&vault);
|
||||
assert_eq!(
|
||||
files.len(),
|
||||
2,
|
||||
"aliases of one file share one portable copy"
|
||||
);
|
||||
for file in files {
|
||||
let filename = urlencoding::encode(file.file_name().unwrap().to_str().unwrap());
|
||||
let relative = format!(".helixnotes/attachments/{filename}");
|
||||
assert!(note.raw.contains(&relative));
|
||||
assert!(Path::new(&vault)
|
||||
.join(urlencoding::decode(&relative).unwrap().as_ref())
|
||||
.is_file());
|
||||
}
|
||||
assert_eq!(
|
||||
fs::read(parent.join("images/café 雪.png")).unwrap(),
|
||||
b"image bytes"
|
||||
);
|
||||
assert_eq!(
|
||||
fs::read(parent.join("images/report.pdf")).unwrap(),
|
||||
b"pdf bytes"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn imported_pdf_embeds_keep_the_vault_root_path_convention_in_nested_notebooks() {
|
||||
let raw = "<div data-pdf-src=\"images/report.pdf\" data-pdf-name=\"Report\"></div>";
|
||||
let (_root, vault, source) = fixture(raw);
|
||||
fs::write(source.parent().unwrap().join("images/report.pdf"), b"pdf").unwrap();
|
||||
fs::create_dir_all(Path::new(&vault).join("Projects/Nested")).unwrap();
|
||||
let mut pdf = reference(raw, "images/report.pdf", "images/report.pdf", true);
|
||||
pdf.pdf = true;
|
||||
let note = import_note(
|
||||
&vault,
|
||||
source.to_str().unwrap(),
|
||||
Some("Projects/Nested"),
|
||||
raw,
|
||||
vec![pdf],
|
||||
|_| Ok(()),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(note.raw.contains("data-pdf-src=\".helixnotes/attachments/"));
|
||||
assert!(note.raw.contains("data-pdf-name=\"Report\""));
|
||||
assert_eq!(attachments(&vault).len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn imported_document_links_remain_managed_and_stable_after_note_and_notebook_moves() {
|
||||
let raw = "[Report](images/report.pdf#page=2)";
|
||||
let (_root, vault, source) = fixture(raw);
|
||||
fs::write(source.parent().unwrap().join("images/report.pdf"), b"pdf").unwrap();
|
||||
fs::create_dir_all(Path::new(&vault).join("Projects/Nested")).unwrap();
|
||||
fs::create_dir_all(Path::new(&vault).join("Archive")).unwrap();
|
||||
let note = import_note(
|
||||
&vault,
|
||||
source.to_str().unwrap(),
|
||||
Some("Projects/Nested"),
|
||||
raw,
|
||||
vec![reference(
|
||||
raw,
|
||||
"images/report.pdf#page=2",
|
||||
"images/report.pdf#page=2",
|
||||
false,
|
||||
)],
|
||||
|_| Ok(()),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(
|
||||
note.raw.starts_with("[Report](.helixnotes/attachments/"),
|
||||
"managed links must not depend on notebook depth"
|
||||
);
|
||||
let href = note
|
||||
.raw
|
||||
.strip_prefix("[Report](")
|
||||
.unwrap()
|
||||
.strip_suffix(')')
|
||||
.unwrap();
|
||||
let attachment = Path::new(&vault).join(
|
||||
urlencoding::decode(href.split('#').next().unwrap())
|
||||
.unwrap()
|
||||
.as_ref(),
|
||||
);
|
||||
assert_eq!(fs::read(attachment).unwrap(), b"pdf");
|
||||
let moved = super::operations::move_note(&vault, ¬e.path, &vault).unwrap();
|
||||
assert_eq!(fs::read_to_string(moved).unwrap(), note.raw);
|
||||
// Imported managed paths remain stable when their containing notebook is moved too.
|
||||
let second = import_note(
|
||||
&vault,
|
||||
source.to_str().unwrap(),
|
||||
Some("Projects/Nested"),
|
||||
raw,
|
||||
vec![reference(
|
||||
raw,
|
||||
"images/report.pdf#page=2",
|
||||
"images/report.pdf#page=2",
|
||||
false,
|
||||
)],
|
||||
|_| Ok(()),
|
||||
)
|
||||
.unwrap();
|
||||
super::operations::move_notebook(
|
||||
&vault,
|
||||
Path::new(&vault).join("Projects").to_str().unwrap(),
|
||||
Path::new(&vault).join("Archive").to_str().unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
fs::read_to_string(Path::new(&vault).join("Archive/Projects/Nested/External.md")).unwrap(),
|
||||
second.raw
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_does_not_replace_existing_note_or_same_named_attachment() {
|
||||
let raw = "";
|
||||
let (_root, vault, source) = fixture(raw);
|
||||
fs::write(
|
||||
source.parent().unwrap().join("images/photo.png"),
|
||||
b"new image",
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(Path::new(&vault).join("External.md"), b"keep note").unwrap();
|
||||
fs::write(
|
||||
Path::new(&vault).join(".helixnotes/attachments/photo.png"),
|
||||
b"keep image",
|
||||
)
|
||||
.unwrap();
|
||||
let note = import_note(
|
||||
&vault,
|
||||
source.to_str().unwrap(),
|
||||
None,
|
||||
raw,
|
||||
vec![reference(
|
||||
raw,
|
||||
"images/photo.png",
|
||||
"images/photo.png",
|
||||
false,
|
||||
)],
|
||||
|_| Ok(()),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(note.path.ends_with("External (2).md"));
|
||||
assert_eq!(
|
||||
fs::read(Path::new(&vault).join("External.md")).unwrap(),
|
||||
b"keep note"
|
||||
);
|
||||
assert_eq!(
|
||||
fs::read(Path::new(&vault).join(".helixnotes/attachments/photo.png")).unwrap(),
|
||||
b"keep image"
|
||||
);
|
||||
assert_eq!(attachments(&vault).len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stale_sources_missing_files_and_denied_attachments_publish_nothing() {
|
||||
let raw = "";
|
||||
let (_root, vault, source) = fixture(raw);
|
||||
let plan = || {
|
||||
vec![reference(
|
||||
raw,
|
||||
"images/photo.png",
|
||||
"images/photo.png",
|
||||
false,
|
||||
)]
|
||||
};
|
||||
assert!(import_note(
|
||||
&vault,
|
||||
source.to_str().unwrap(),
|
||||
None,
|
||||
"stale",
|
||||
plan(),
|
||||
|_| Ok(())
|
||||
)
|
||||
.is_err());
|
||||
assert!(
|
||||
import_note(&vault, source.to_str().unwrap(), None, raw, plan(), |_| Ok(
|
||||
()
|
||||
))
|
||||
.is_err()
|
||||
);
|
||||
fs::write(source.parent().unwrap().join("images/photo.png"), b"image").unwrap();
|
||||
let denied = import_note(
|
||||
&vault,
|
||||
source.to_str().unwrap(),
|
||||
None,
|
||||
raw,
|
||||
plan(),
|
||||
|path| {
|
||||
if path == source {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("Attachment not selected".into())
|
||||
}
|
||||
},
|
||||
);
|
||||
assert!(denied.unwrap_err().contains("not selected"));
|
||||
assert!(attachments(&vault).is_empty());
|
||||
assert!(!Path::new(&vault).join("External.md").exists());
|
||||
assert_eq!(fs::read_to_string(&source).unwrap(), raw);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_destinations_ranges_and_absolute_references_are_rejected() {
|
||||
let raw = "雪 ";
|
||||
let (_root, vault, source) = fixture(raw);
|
||||
fs::write(source.parent().unwrap().join("images/photo.png"), b"image").unwrap();
|
||||
assert!(import_note(
|
||||
&vault,
|
||||
source.to_str().unwrap(),
|
||||
Some("../escape"),
|
||||
raw,
|
||||
vec![],
|
||||
|_| Ok(())
|
||||
)
|
||||
.is_err());
|
||||
for href in [
|
||||
"/private.png",
|
||||
"%2Fprivate.png",
|
||||
"C:/private.png",
|
||||
"https://example.com/file.png",
|
||||
"Related.md",
|
||||
] {
|
||||
assert!(import_note(
|
||||
&vault,
|
||||
source.to_str().unwrap(),
|
||||
None,
|
||||
raw,
|
||||
vec![reference(raw, "images/photo.png", href, false)],
|
||||
|_| Ok(())
|
||||
)
|
||||
.is_err());
|
||||
}
|
||||
assert!(import_note(
|
||||
&vault,
|
||||
source.to_str().unwrap(),
|
||||
None,
|
||||
raw,
|
||||
vec![ExternalNoteReference {
|
||||
from: 1,
|
||||
to: 3,
|
||||
href: "images/photo.png".into(),
|
||||
html: false,
|
||||
pdf: false,
|
||||
}],
|
||||
|_| Ok(())
|
||||
)
|
||||
.is_err());
|
||||
let plan = vec![
|
||||
reference(raw, "images/photo.png", "images/photo.png", false),
|
||||
reference(raw, "images/photo.png", "images/photo.png", false),
|
||||
];
|
||||
assert!(import_note(
|
||||
&vault,
|
||||
source.to_str().unwrap(),
|
||||
None,
|
||||
raw,
|
||||
plan,
|
||||
|_| Ok(())
|
||||
)
|
||||
.is_err());
|
||||
assert!(attachments(&vault).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_note_publication_removes_only_its_new_attachments() {
|
||||
let raw = "";
|
||||
let (_root, vault, source) = fixture(raw);
|
||||
fs::write(source.parent().unwrap().join("images/photo.png"), b"image").unwrap();
|
||||
fs::write(Path::new(&vault).join("External.md"), b"keep").unwrap();
|
||||
for number in 2..=100 {
|
||||
fs::write(
|
||||
Path::new(&vault).join(format!("External ({number}).md")),
|
||||
b"keep",
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
let original_asset = Path::new(&vault).join(".helixnotes/attachments/original.png");
|
||||
fs::write(&original_asset, b"keep attachment").unwrap();
|
||||
assert!(import_note(
|
||||
&vault,
|
||||
source.to_str().unwrap(),
|
||||
None,
|
||||
raw,
|
||||
vec![reference(
|
||||
raw,
|
||||
"images/photo.png",
|
||||
"images/photo.png",
|
||||
false
|
||||
)],
|
||||
|_| Ok(())
|
||||
)
|
||||
.is_err());
|
||||
assert_eq!(attachments(&vault), vec![original_asset]);
|
||||
assert_eq!(
|
||||
fs::read(Path::new(&vault).join("External.md")).unwrap(),
|
||||
b"keep"
|
||||
);
|
||||
assert_eq!(fs::read_to_string(&source).unwrap(), raw);
|
||||
assert!(!fs::read_dir(&vault).unwrap().any(|entry| entry
|
||||
.unwrap()
|
||||
.file_name()
|
||||
.to_string_lossy()
|
||||
.starts_with(".helixnotes-import-")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn note_content_cannot_import_parent_files_or_hidden_secrets_from_a_broad_scope() {
|
||||
let raw = "[File](asset.bin)";
|
||||
let (root, vault, source) = fixture(raw);
|
||||
let parent = source.parent().unwrap();
|
||||
for name in [
|
||||
".ssh/key.bin",
|
||||
".helixnotes/history/private.bin",
|
||||
".helixnotes/attachments/photo.bin",
|
||||
] {
|
||||
let file = parent.join(name);
|
||||
fs::create_dir_all(file.parent().unwrap()).unwrap();
|
||||
fs::write(file, b"private fixture").unwrap();
|
||||
}
|
||||
fs::write(root.path().join("private.bin"), b"private outside fixture").unwrap();
|
||||
for href in [
|
||||
"../private.bin",
|
||||
".ssh/key.bin",
|
||||
".helixnotes/history/private.bin",
|
||||
] {
|
||||
assert!(import_note(
|
||||
&vault,
|
||||
source.to_str().unwrap(),
|
||||
None,
|
||||
raw,
|
||||
vec![reference(raw, "asset.bin", href, false)],
|
||||
|_| Ok(())
|
||||
)
|
||||
.is_err());
|
||||
assert!(attachments(&vault).is_empty());
|
||||
assert!(!Path::new(&vault).join("External.md").exists());
|
||||
}
|
||||
let note = import_note(
|
||||
&vault,
|
||||
source.to_str().unwrap(),
|
||||
None,
|
||||
raw,
|
||||
vec![reference(
|
||||
raw,
|
||||
"asset.bin",
|
||||
".helixnotes/attachments/photo.bin",
|
||||
false,
|
||||
)],
|
||||
|_| Ok(()),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(note.raw.contains(".helixnotes/attachments/"));
|
||||
assert_eq!(attachments(&vault).len(), 1);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn import_rejects_attachment_and_notebook_destinations_that_follow_symlinks() {
|
||||
let raw = "";
|
||||
let (root, vault, source) = fixture(raw);
|
||||
fs::write(source.parent().unwrap().join("images/photo.png"), b"image").unwrap();
|
||||
let outside = root.path().join("outside");
|
||||
fs::create_dir(&outside).unwrap();
|
||||
std::os::unix::fs::symlink(&outside, Path::new(&vault).join("Notebook")).unwrap();
|
||||
assert!(import_note(
|
||||
&vault,
|
||||
source.to_str().unwrap(),
|
||||
Some("Notebook"),
|
||||
raw,
|
||||
vec![],
|
||||
|_| Ok(())
|
||||
)
|
||||
.is_err());
|
||||
let directory = Path::new(&vault).join(".helixnotes/attachments");
|
||||
fs::remove_dir(&directory).unwrap();
|
||||
std::os::unix::fs::symlink(&outside, &directory).unwrap();
|
||||
assert!(import_note(
|
||||
&vault,
|
||||
source.to_str().unwrap(),
|
||||
None,
|
||||
raw,
|
||||
vec![reference(
|
||||
raw,
|
||||
"images/photo.png",
|
||||
"images/photo.png",
|
||||
false
|
||||
)],
|
||||
|_| Ok(())
|
||||
)
|
||||
.is_err());
|
||||
assert!(fs::read_dir(outside).unwrap().next().is_none());
|
||||
}
|
||||
@@ -714,7 +714,10 @@ pub fn read_vault_note(vault_path: &str, path: &str) -> Result<NoteContent, Stri
|
||||
read_note_content(&validated, path)
|
||||
}
|
||||
|
||||
fn read_note_content(validated: &Path, reported_path: &str) -> Result<NoteContent, String> {
|
||||
pub(crate) fn read_note_content(
|
||||
validated: &Path,
|
||||
reported_path: &str,
|
||||
) -> Result<NoteContent, String> {
|
||||
let p = validated;
|
||||
let raw = fs::read_to_string(p).map_err(|e| e.to_string())?;
|
||||
let filename = p
|
||||
@@ -1663,13 +1666,11 @@ pub fn get_all_tags(vault_path: &str) -> Result<Vec<(String, usize)>, String> {
|
||||
let hn_dir = helixnotes_dir(vault_path);
|
||||
let md_files: Vec<PathBuf> = WalkDir::new(vault_path)
|
||||
.into_iter()
|
||||
.filter_entry(|entry| entry.depth() == 0 || entry.path() != hn_dir.as_path())
|
||||
.filter_map(|e| e.ok())
|
||||
.filter(|e| {
|
||||
let p = e.path();
|
||||
p.is_file()
|
||||
&& p.extension().and_then(|x| x.to_str()) == Some("md")
|
||||
&& !is_hidden(p)
|
||||
&& !p.starts_with(&hn_dir)
|
||||
p.is_file() && p.extension().and_then(|x| x.to_str()) == Some("md") && !is_hidden(p)
|
||||
})
|
||||
.map(|e| e.path().to_path_buf())
|
||||
.collect();
|
||||
@@ -1734,6 +1735,256 @@ pub fn save_attachment(vault_path: &str, name: &str, data: &[u8]) -> Result<Stri
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Deserialize)]
|
||||
pub struct ExternalNoteReference {
|
||||
pub from: usize,
|
||||
pub to: usize,
|
||||
pub href: String,
|
||||
pub html: bool,
|
||||
#[serde(default)]
|
||||
pub pdf: bool,
|
||||
}
|
||||
|
||||
/// Import an accepted Markdown snapshot and its scoped relative attachments without overwriting files.
|
||||
pub fn import_note(
|
||||
vault_path: &str,
|
||||
source_path: &str,
|
||||
source_root: &Path,
|
||||
notebook_relative: Option<&str>,
|
||||
expected_raw: &str,
|
||||
mut references: Vec<ExternalNoteReference>,
|
||||
authorize: impl Fn(&Path) -> Result<(), String>,
|
||||
) -> Result<NoteContent, String> {
|
||||
let source = canonicalize_path(Path::new(source_path), "source note")?;
|
||||
authorize(&source)?;
|
||||
if !source.is_file() || source.extension().and_then(|ext| ext.to_str()) != Some("md") {
|
||||
return Err("Import source must be a Markdown file".into());
|
||||
}
|
||||
let source_root = canonicalize_path(source_root, "source attachment root")?;
|
||||
if !source.starts_with(&source_root) {
|
||||
return Err("Source attachment root does not own the note".into());
|
||||
}
|
||||
let source_attachments = source_root.join(".helixnotes/attachments");
|
||||
let raw = fs::read_to_string(&source).map_err(|error| error.to_string())?;
|
||||
if raw != expected_raw {
|
||||
return Err("The source note changed on disk. Reopen it before importing.".into());
|
||||
}
|
||||
let relative = notebook_relative.map(safe_relative_path).transpose()?;
|
||||
let directory = Path::new(vault_path).join(relative.unwrap_or(Path::new("")));
|
||||
ensure_vault_content_dir(vault_path, &directory)?;
|
||||
let vault = canonicalize_path(Path::new(vault_path), "vault path")?;
|
||||
let canonical_directory = canonicalize_path(&directory, "import destination")?;
|
||||
if canonical_directory != vault.join(relative.unwrap_or(Path::new(""))) {
|
||||
return Err("Import destination must not use symbolic links".into());
|
||||
}
|
||||
let attachments = helixnotes_dir(vault_path).join("attachments");
|
||||
references.sort_by_key(|reference| reference.from);
|
||||
let mut previous = 0;
|
||||
let mut targets = Vec::new();
|
||||
// Validate every range and permission before publishing any attachment.
|
||||
for reference in &references {
|
||||
if reference.from < previous
|
||||
|| reference.from >= reference.to
|
||||
|| raw.get(reference.from..reference.to).is_none()
|
||||
{
|
||||
return Err("Invalid attachment reference range".into());
|
||||
}
|
||||
previous = reference.to;
|
||||
let (path, suffix) = reference
|
||||
.href
|
||||
.find(['#', '?'])
|
||||
.map(|index| reference.href.split_at(index))
|
||||
.unwrap_or((&reference.href, ""));
|
||||
let decoded = urlencoding::decode(path)
|
||||
.map_err(|error| error.to_string())?
|
||||
.replace('\\', "/");
|
||||
let path = Path::new(&decoded);
|
||||
if decoded.is_empty()
|
||||
|| decoded.contains(':')
|
||||
|| path.is_absolute()
|
||||
|| path
|
||||
.extension()
|
||||
.and_then(|ext| ext.to_str())
|
||||
.is_some_and(|ext| ext.eq_ignore_ascii_case("md"))
|
||||
{
|
||||
return Err("Import attachments must be relative non-Markdown files".into());
|
||||
}
|
||||
let base = if decoded.starts_with(".helixnotes/attachments/") || reference.pdf {
|
||||
source_root.as_path()
|
||||
} else {
|
||||
source.parent().ok_or("Source note has no parent")?
|
||||
};
|
||||
let target = canonicalize_path(&base.join(path), "attachment")?;
|
||||
authorize(&target)?;
|
||||
let relative_target = if let Ok(relative) = target.strip_prefix(&source_attachments) {
|
||||
relative
|
||||
} else {
|
||||
target.strip_prefix(source.parent().ok_or("Source note has no parent")?)
|
||||
.map_err(|_| "Attachments must stay inside the source note's folder or managed attachment root")?
|
||||
};
|
||||
let components: Vec<_> = relative_target.components().collect();
|
||||
let helix_attachment = components
|
||||
.first()
|
||||
.is_some_and(|part| part.as_os_str() == ".helixnotes")
|
||||
&& components
|
||||
.get(1)
|
||||
.is_some_and(|part| part.as_os_str() == "attachments");
|
||||
if components.iter().enumerate().any(|(index, part)| {
|
||||
part.as_os_str().to_string_lossy().starts_with('.') && !(helix_attachment && index == 0)
|
||||
}) {
|
||||
return Err("Hidden metadata is not imported as an attachment".into());
|
||||
}
|
||||
if !target.is_file() {
|
||||
return Err("Attachment reference must point to a file".into());
|
||||
}
|
||||
targets.push((target, suffix.to_string()));
|
||||
}
|
||||
if !references.is_empty() {
|
||||
fs::create_dir_all(&attachments).map_err(|error| error.to_string())?;
|
||||
if canonicalize_path(&attachments, "attachment destination")?
|
||||
!= vault.join(".helixnotes/attachments")
|
||||
{
|
||||
return Err("Attachment destination must not use symbolic links".into());
|
||||
}
|
||||
}
|
||||
let mut created = Vec::new();
|
||||
let result = (|| {
|
||||
let mut copied = std::collections::HashMap::<PathBuf, String>::new();
|
||||
let mut rewritten = String::with_capacity(raw.len());
|
||||
let mut previous = 0;
|
||||
for (reference, (target, suffix)) in references.iter().zip(targets) {
|
||||
let relative = if let Some(relative) = copied.get(&target) {
|
||||
relative.clone()
|
||||
} else {
|
||||
let name = sanitize_filename(
|
||||
&target
|
||||
.file_name()
|
||||
.ok_or("Attachment has no filename")?
|
||||
.to_string_lossy(),
|
||||
);
|
||||
let mut temporary = tempfile::Builder::new()
|
||||
.prefix(".helixnotes-import-")
|
||||
.tempfile_in(&attachments)
|
||||
.map_err(|error| error.to_string())?;
|
||||
let mut file = fs::File::open(&target).map_err(|error| error.to_string())?;
|
||||
let before = file.metadata().map_err(|error| error.to_string())?;
|
||||
let bytes = std::io::copy(&mut file, temporary.as_file_mut())
|
||||
.map_err(|error| error.to_string())?;
|
||||
let after = file.metadata().map_err(|error| error.to_string())?;
|
||||
if bytes != before.len()
|
||||
|| before.len() != after.len()
|
||||
|| before.modified().ok() != after.modified().ok()
|
||||
{
|
||||
return Err("An attachment changed while importing. Retry the import.".into());
|
||||
}
|
||||
temporary
|
||||
.as_file()
|
||||
.sync_all()
|
||||
.map_err(|error| error.to_string())?;
|
||||
let filename = loop {
|
||||
let filename = format!("{}_{}", Uuid::new_v4(), name);
|
||||
let destination = attachments.join(&filename);
|
||||
match temporary.persist_noclobber(&destination) {
|
||||
Ok(_) => {
|
||||
created.push(destination);
|
||||
break filename;
|
||||
}
|
||||
Err(error) if error.error.kind() == std::io::ErrorKind::AlreadyExists => {
|
||||
temporary = error.file
|
||||
}
|
||||
Err(error) => return Err(error.error.to_string()),
|
||||
}
|
||||
};
|
||||
let relative =
|
||||
format!(".helixnotes/attachments/{}", urlencoding::encode(&filename));
|
||||
copied.insert(target, relative.clone());
|
||||
relative
|
||||
};
|
||||
// Use the same stable vault-root convention as normal attachment insertion/saving.
|
||||
let mut href = format!("{relative}{suffix}");
|
||||
if reference.html {
|
||||
href = href
|
||||
.replace('&', "&")
|
||||
.replace('"', """)
|
||||
.replace('\'', "'")
|
||||
.replace('<', "<")
|
||||
.replace('>', ">");
|
||||
} else {
|
||||
href = href
|
||||
.replace(' ', "%20")
|
||||
.replace('"', "%22")
|
||||
.replace('<', "%3C")
|
||||
.replace('>', "%3E");
|
||||
}
|
||||
rewritten.push_str(&raw[previous..reference.from]);
|
||||
rewritten.push_str(&href);
|
||||
previous = reference.to;
|
||||
}
|
||||
rewritten.push_str(&raw[previous..]);
|
||||
let filename = source.file_name().ok_or("Source note has no filename")?;
|
||||
let mut temporary = tempfile::Builder::new()
|
||||
.prefix(".helixnotes-import-")
|
||||
.tempfile_in(&directory)
|
||||
.map_err(|error| error.to_string())?;
|
||||
temporary
|
||||
.write_all(rewritten.as_bytes())
|
||||
.map_err(|error| error.to_string())?;
|
||||
temporary
|
||||
.as_file()
|
||||
.sync_all()
|
||||
.map_err(|error| error.to_string())?;
|
||||
let mut destination = directory.join(filename);
|
||||
let mut suffix = 2;
|
||||
loop {
|
||||
match temporary.persist_noclobber(&destination) {
|
||||
Ok(_) => break,
|
||||
Err(error)
|
||||
if error.error.kind() == std::io::ErrorKind::AlreadyExists && suffix <= 100 =>
|
||||
{
|
||||
temporary = error.file;
|
||||
destination = directory.join(format!(
|
||||
"{} ({suffix}).md",
|
||||
source.file_stem().unwrap_or_default().to_string_lossy()
|
||||
));
|
||||
suffix += 1;
|
||||
}
|
||||
Err(error) => return Err(error.error.to_string()),
|
||||
}
|
||||
}
|
||||
let (meta, content) = frontmatter::parse_note(
|
||||
&rewritten,
|
||||
&destination
|
||||
.file_name()
|
||||
.unwrap_or_default()
|
||||
.to_string_lossy(),
|
||||
);
|
||||
Ok(NoteContent {
|
||||
path: destination.to_string_lossy().into_owned(),
|
||||
meta,
|
||||
content,
|
||||
raw: rewritten,
|
||||
})
|
||||
})();
|
||||
if let Err(error) = result {
|
||||
let mut failed_cleanup = Vec::new();
|
||||
for path in created {
|
||||
if let Err(cleanup) = fs::remove_file(&path) {
|
||||
if cleanup.kind() != std::io::ErrorKind::NotFound {
|
||||
failed_cleanup.push(format!("{}: {cleanup}", path.display()));
|
||||
}
|
||||
}
|
||||
}
|
||||
if !failed_cleanup.is_empty() {
|
||||
return Err(format!(
|
||||
"{error}; could not remove unused attachments: {}",
|
||||
failed_cleanup.join("; ")
|
||||
));
|
||||
}
|
||||
return Err(error);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
pub(crate) fn normalize_notebook_icon_key(path: &str) -> String {
|
||||
path.replace('\\', "/")
|
||||
}
|
||||
@@ -2200,6 +2451,71 @@ mod tests {
|
||||
use std::fs;
|
||||
use std::io::Read;
|
||||
|
||||
#[test]
|
||||
fn all_tags_excludes_only_root_metadata_subtree_and_hidden_filenames() {
|
||||
let parent = tempfile::tempdir().unwrap();
|
||||
let root = parent.path().join(".helixnotes");
|
||||
let fixtures = [
|
||||
("Root.md", "root"),
|
||||
(".helixnotes/history/Stored.md", "excluded"),
|
||||
("Notebook/.helixnotes/Nested.md", "nested"),
|
||||
(".helixnotes-other/Visible.md", "similar"),
|
||||
(".obsidian/Visible.md", "obsidian"),
|
||||
("assets/Visible.md", "assets"),
|
||||
(".other/Visible.md", "other"),
|
||||
("Notebook/.Hidden.md", "hidden"),
|
||||
("Notebook/Upper.MD", "uppercase"),
|
||||
("Notebook/other.txt", "text"),
|
||||
];
|
||||
for (relative, tag) in fixtures {
|
||||
let path = root.join(relative);
|
||||
fs::create_dir_all(path.parent().unwrap()).unwrap();
|
||||
fs::write(path, format!("---\ntags: [{tag}]\n---\n# Note")).unwrap();
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
super::get_all_tags(root.to_str().unwrap()).unwrap(),
|
||||
vec![
|
||||
("assets".to_string(), 1),
|
||||
("nested".to_string(), 1),
|
||||
("obsidian".to_string(), 1),
|
||||
("other".to_string(), 1),
|
||||
("root".to_string(), 1),
|
||||
("similar".to_string(), 1),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn all_tags_keeps_file_symlinks_without_following_directory_symlinks() {
|
||||
use std::os::unix::fs::symlink;
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let outside = tempfile::tempdir().unwrap();
|
||||
fs::write(
|
||||
outside.path().join("External.md"),
|
||||
"---\ntags: [linked]\n---\n# Note",
|
||||
)
|
||||
.unwrap();
|
||||
symlink(
|
||||
outside.path().join("External.md"),
|
||||
vault.path().join("Linked.md"),
|
||||
)
|
||||
.unwrap();
|
||||
symlink(outside.path(), vault.path().join("Directory")).unwrap();
|
||||
fs::create_dir(vault.path().join(".helixnotes")).unwrap();
|
||||
symlink(
|
||||
outside.path().join("External.md"),
|
||||
vault.path().join(".helixnotes/Excluded.md"),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
super::get_all_tags(vault.path().to_str().unwrap()).unwrap(),
|
||||
vec![("linked".to_string(), 1)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn same_named_attachment_writes_never_replace_each_other() {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
|
||||
Reference in new issue
Block a user