-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.rs
More file actions
625 lines (564 loc) · 22.4 KB
/
Copy pathexport.rs
File metadata and controls
625 lines (564 loc) · 22.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2024-2026 TripleACS Pty Ltd t/a 2pi Software
//! Workspace export and import as `.zip` archives.
use std::collections::HashSet;
use std::io::{Cursor, Read, Seek, Write};
use std::path::Path;
use ed25519_dalek;
use serde::{Deserialize, Serialize};
use zip::write::SimpleFileOptions;
use zip::AesMode;
use zip::{ZipArchive, ZipWriter};
use crate::core::attachment::AttachmentMeta;
use crate::core::note::Note;
use crate::core::timestamp::UnixSecs;
use crate::core::user_script;
use crate::core::workspace::Workspace;
use crate::get_or_create_seed_device_id;
use crate::Storage;
/// The current Krillnotes app version, read from Cargo.toml at compile time.
pub const APP_VERSION: &str = env!("CARGO_PKG_VERSION");
/// Top-level JSON structure in `notes.json`.
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExportNotes {
pub version: u32,
pub app_version: String,
pub notes: Vec<Note>,
}
/// One entry in `scripts/scripts.json`.
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ScriptManifestEntry {
pub filename: String,
pub load_order: i32,
pub enabled: bool,
#[serde(default)]
pub category: Option<String>,
}
/// The `scripts/scripts.json` manifest.
#[derive(Debug, Serialize, Deserialize)]
pub struct ScriptManifest {
pub scripts: Vec<ScriptManifestEntry>,
}
/// Top-level JSON structure in `workspace.json`.
///
/// This is written on export and read back on import to carry authorship,
/// licensing, and gallery-discovery metadata for template distribution.
/// All fields except `version` are optional so old archives without them
/// deserialise successfully (all missing fields default to `None` / empty).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WorkspaceMetadata {
pub version: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub author_name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub author_org: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub homepage_url: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub license: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub license_url: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub language: Option<String>,
/// Workspace-level taxonomy tags for gallery discovery (distinct from per-note tags).
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub tags: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub owner_pubkey: Option<String>,
}
/// Result returned after reading an export archive's metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ImportResult {
pub app_version: String,
pub note_count: usize,
pub script_count: usize,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub metadata: Option<WorkspaceMetadata>,
}
/// Errors specific to export/import operations.
#[derive(Debug, thiserror::Error)]
pub enum ExportError {
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("Zip error: {0}")]
Zip(#[from] zip::result::ZipError),
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("Invalid export format: {0}")]
InvalidFormat(String),
#[error("Database error: {0}")]
Database(String),
#[error("Archive is password-protected; provide a password to decrypt")]
EncryptedArchive,
#[error("Incorrect password")]
InvalidPassword,
}
/// Converts a script name into a safe filename stem.
pub fn slugify_script_name(name: &str) -> String {
let slug: String = name
.to_lowercase()
.chars()
.map(|c| if c.is_alphanumeric() { c } else { '-' })
.collect();
let slug: String = slug
.split('-')
.filter(|s| !s.is_empty())
.collect::<Vec<_>>()
.join("-");
if slug.is_empty() {
"script".to_string()
} else {
slug
}
}
/// Opens a named entry and reads all its bytes, decrypting with `password` if provided.
/// Returns `ExportError::InvalidPassword` if the password is wrong (detected via MAC verification).
/// Returns `ExportError::InvalidFormat` if the entry doesn't exist.
fn read_entry<R: Read + Seek>(
archive: &mut ZipArchive<R>,
name: &str,
password: Option<&str>,
) -> Result<Cursor<Vec<u8>>, ExportError> {
let mut content = Vec::new();
if let Some(pwd) = password {
let mut file = archive
.by_name_decrypt(name, pwd.as_bytes())
.map_err(|e| match e {
zip::result::ZipError::FileNotFound => {
ExportError::InvalidFormat(format!("Missing '{name}' in archive"))
}
zip::result::ZipError::InvalidPassword => ExportError::InvalidPassword,
other => ExportError::Zip(other),
})?;
file.read_to_end(&mut content).map_err(|e| {
if matches!(
e.kind(),
std::io::ErrorKind::InvalidData | std::io::ErrorKind::InvalidInput
) {
ExportError::InvalidFormat(format!(
"Archive entry '{name}' is corrupted (integrity check failed)"
))
} else {
ExportError::InvalidPassword
}
})?;
} else {
let mut file = archive
.by_name(name)
.map_err(|_| ExportError::InvalidFormat(format!("Missing '{name}' in archive")))?;
file.read_to_end(&mut content).map_err(|e| {
if matches!(
e.kind(),
std::io::ErrorKind::InvalidData | std::io::ErrorKind::InvalidInput
) {
ExportError::InvalidFormat(format!(
"Archive entry '{name}' is corrupted (integrity check failed)"
))
} else {
ExportError::Io(e)
}
})?;
}
Ok(Cursor::new(content))
}
/// Like `read_entry` but returns `None` instead of an error when the entry is absent or unreadable.
fn try_read_entry<R: Read + Seek>(
archive: &mut ZipArchive<R>,
name: &str,
password: Option<&str>,
) -> Option<Cursor<Vec<u8>>> {
let mut content = Vec::new();
if let Some(pwd) = password {
let mut file = archive.by_name_decrypt(name, pwd.as_bytes()).ok()?;
file.read_to_end(&mut content).ok()?;
} else {
let mut file = archive.by_name(name).ok()?;
file.read_to_end(&mut content).ok()?;
}
Some(Cursor::new(content))
}
/// Exports the workspace contents as a zip archive.
///
/// The archive contains:
/// - `notes.json` -- all notes with format version and app version
/// - `scripts/scripts.json` -- script metadata (filename, load_order, enabled)
/// - `scripts/<name>.rhai` -- each user script's source code
///
/// The `operations` table and `workspace_meta` are excluded.
pub fn export_workspace<W: Write + Seek>(
workspace: &Workspace,
writer: W,
password: Option<&str>,
) -> Result<(), ExportError> {
let notes = workspace
.list_all_notes()
.map_err(|e| ExportError::Database(e.to_string()))?;
let scripts = workspace
.list_user_scripts()
.map_err(|e| ExportError::Database(e.to_string()))?;
let mut zip = ZipWriter::new(writer);
let options = match password {
Some(pwd) => SimpleFileOptions::default()
.compression_method(zip::CompressionMethod::Deflated)
.with_aes_encryption(AesMode::Aes256, pwd),
None => SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated),
};
// Write notes.json — strip identity fields so the archive is identity-neutral
let notes = notes
.into_iter()
.map(|mut n| {
n.created_by = String::new();
n.modified_by = String::new();
n
})
.collect();
let export_notes = ExportNotes {
version: 1,
app_version: APP_VERSION.to_string(),
notes,
};
zip.start_file("notes.json", options)?;
serde_json::to_writer_pretty(&mut zip, &export_notes)?;
// Build script manifest and write .rhai files
let mut manifest_entries = Vec::new();
let mut used_filenames: HashSet<String> = HashSet::new();
for script in &scripts {
let base = slugify_script_name(&script.name);
let mut filename = format!("{base}.rhai");
// Deduplicate filenames with numeric suffix
let mut counter = 1u32;
while used_filenames.contains(&filename) {
counter += 1;
filename = format!("{base}-{counter}.rhai");
}
used_filenames.insert(filename.clone());
manifest_entries.push(ScriptManifestEntry {
filename: filename.clone(),
load_order: script.load_order,
enabled: script.enabled,
category: Some(script.category.clone()),
});
zip.start_file(format!("scripts/{filename}"), options)?;
zip.write_all(script.source_code.as_bytes())?;
}
// Write scripts/scripts.json
let manifest = ScriptManifest {
scripts: manifest_entries,
};
zip.start_file("scripts/scripts.json", options)?;
serde_json::to_writer_pretty(&mut zip, &manifest)?;
// Write workspace.json (workspace metadata)
let mut ws_meta = workspace
.get_workspace_metadata()
.map_err(|e| ExportError::Database(e.to_string()))?;
ws_meta.version = 1;
ws_meta.owner_pubkey = None;
zip.start_file("workspace.json", options)?;
serde_json::to_writer_pretty(&mut zip, &ws_meta)?;
// Write attachments
let all_attachments = workspace
.list_all_attachments()
.map_err(|e| ExportError::Database(e.to_string()))?;
if !all_attachments.is_empty() {
// Write attachments.json manifest
zip.start_file("attachments.json", options)?;
serde_json::to_writer(&mut zip, &all_attachments)
.map_err(|e| ExportError::Database(e.to_string()))?;
// Write each attachment file (plaintext — zip password protects them at rest)
for meta in &all_attachments {
let plaintext = workspace
.get_attachment_bytes(&meta.id)
.map_err(|e| ExportError::Database(e.to_string()))?;
zip.start_file(
format!("attachments/{}/{}", meta.id, meta.filename),
options,
)?;
zip.write_all(&plaintext)?;
}
}
zip.finish()?;
Ok(())
}
/// Reads the metadata from an export archive without creating a workspace.
///
/// Opens the zip, parses `notes.json` to extract the note count and app version,
/// and optionally reads `scripts/scripts.json` for the script count.
///
/// # Errors
///
/// Returns [`ExportError::EncryptedArchive`] if the archive is encrypted and no
/// password is provided. Returns [`ExportError::InvalidPassword`] if the password
/// is wrong. Returns [`ExportError::InvalidFormat`] if the format version is not
/// `1` or `notes.json` is missing. Returns other `ExportError` variants for I/O,
/// zip, or JSON failures.
pub fn peek_import<R: Read + Seek>(
reader: R,
password: Option<&str>,
) -> Result<ImportResult, ExportError> {
let mut archive = ZipArchive::new(reader)?;
// Detect encryption before trying to read data.
// by_index_raw reads metadata without decrypting, so .encrypted() is safe to call
// without a password.
{
let index = archive.index_for_name("notes.json").ok_or_else(|| {
ExportError::InvalidFormat("Missing notes.json in archive".to_string())
})?;
let check = archive.by_index_raw(index).map_err(ExportError::Zip)?;
if check.encrypted() && password.is_none() {
return Err(ExportError::EncryptedArchive);
}
}
let notes_cursor = read_entry(&mut archive, "notes.json", password)?;
let export_notes: ExportNotes = serde_json::from_reader(notes_cursor)?;
if export_notes.version != 1 {
return Err(ExportError::InvalidFormat(format!(
"Unsupported export format version: {}",
export_notes.version
)));
}
let script_count = match try_read_entry(&mut archive, "scripts/scripts.json", password) {
Some(manifest_cursor) => {
let manifest: ScriptManifest = serde_json::from_reader(manifest_cursor)?;
manifest.scripts.len()
}
None => 0,
};
let metadata: Option<WorkspaceMetadata> =
try_read_entry(&mut archive, "workspace.json", password)
.and_then(|cursor| serde_json::from_reader(cursor).ok());
Ok(ImportResult {
app_version: export_notes.app_version,
note_count: export_notes.notes.len(),
script_count,
metadata,
})
}
/// Imports an export archive into a new workspace database.
///
/// Creates a fresh database at `db_path` using [`Storage::create`], then bulk-inserts
/// all notes (preserving original IDs, parent relationships, and positions) and all
/// scripts (with new UUIDs, preserving source code, load order, and enabled state).
///
/// Does **not** create a root note — the exported notes already contain one.
///
/// # Errors
///
/// Returns [`ExportError::InvalidFormat`] if `notes.json` is missing or the format
/// version is not `1`. Returns [`ExportError::Database`] for any storage or SQL
/// failure. Returns other `ExportError` variants for I/O, zip, or JSON errors.
pub fn import_workspace<R: Read + Seek>(
reader: R,
db_path: &Path,
zip_password: Option<&str>,
workspace_password: &str,
identity_uuid: &str,
signing_key: ed25519_dalek::SigningKey,
data_dir: &Path,
) -> Result<ImportResult, ExportError> {
let mut archive = ZipArchive::new(reader)?;
// Detect encryption (same pattern as peek_import)
{
let index = archive.index_for_name("notes.json").ok_or_else(|| {
ExportError::InvalidFormat("Missing notes.json in archive".to_string())
})?;
let check = archive.by_index_raw(index).map_err(ExportError::Zip)?;
if check.encrypted() && zip_password.is_none() {
return Err(ExportError::EncryptedArchive);
}
}
let notes_cursor = read_entry(&mut archive, "notes.json", zip_password)?;
let export_notes: ExportNotes = serde_json::from_reader(notes_cursor)?;
// Validate format version
if export_notes.version != 1 {
return Err(ExportError::InvalidFormat(format!(
"Unsupported export format version: {}",
export_notes.version
)));
}
// Read script manifest and source files
let manifest = match try_read_entry(&mut archive, "scripts/scripts.json", zip_password) {
Some(manifest_cursor) => {
let m: ScriptManifest = serde_json::from_reader(manifest_cursor)?;
Some(m)
}
None => None,
};
// Read each .rhai script source from the archive
let mut script_sources: Vec<(String, i32, bool, String)> = Vec::new(); // (source_code, load_order, enabled, category)
if let Some(ref manifest) = manifest {
for entry in &manifest.scripts {
let path = format!("scripts/{}", entry.filename);
let mut rhai_cursor = read_entry(&mut archive, &path, zip_password).map_err(|e| {
ExportError::InvalidFormat(format!(
"Script file '{}' referenced in manifest but missing from archive: {}",
path, e
))
})?;
let mut source = String::new();
rhai_cursor.read_to_string(&mut source)?;
let category = entry
.category
.clone()
.unwrap_or_else(|| "schema".to_string());
script_sources.push((source, entry.load_order, entry.enabled, category));
}
}
// Create the database
let mut storage = Storage::create(db_path, workspace_password)
.map_err(|e| ExportError::Database(e.to_string()))?;
// Insert workspace metadata
let device_id =
get_or_create_seed_device_id(data_dir).map_err(|e| ExportError::Database(e.to_string()))?;
storage
.connection()
.execute(
"INSERT INTO workspace_meta (key, value) VALUES (?, ?)",
["device_id", &device_id],
)
.map_err(|e| ExportError::Database(e.to_string()))?;
storage
.connection()
.execute(
"INSERT INTO workspace_meta (key, value) VALUES (?, ?)",
["identity_uuid", identity_uuid],
)
.map_err(|e| ExportError::Database(e.to_string()))?;
// Bulk-insert notes in a transaction.
// Defer foreign-key checks so child notes can be inserted before their parents.
{
storage
.connection()
.execute_batch("PRAGMA defer_foreign_keys = ON;")
.map_err(|e| ExportError::Database(e.to_string()))?;
let tx = storage
.connection_mut()
.transaction()
.map_err(|e| ExportError::Database(e.to_string()))?;
for note in &export_notes.notes {
let fields_json = serde_json::to_string(¬e.fields)?;
tx.execute(
"INSERT INTO notes (id, title, schema, parent_id, position, created_at, modified_at, created_by, modified_by, fields_json, is_expanded, is_checked)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
rusqlite::params![
note.id,
note.title,
note.schema,
note.parent_id,
note.position,
note.created_at,
note.modified_at,
note.created_by,
note.modified_by,
fields_json,
note.is_expanded,
note.is_checked,
],
)
.map_err(|e| ExportError::Database(e.to_string()))?;
// Restore tags for this note.
for tag in ¬e.tags {
tx.execute(
"INSERT OR IGNORE INTO note_tags (note_id, tag) VALUES (?, ?)",
rusqlite::params![note.id, tag],
)
.map_err(|e| ExportError::Database(e.to_string()))?;
}
}
tx.commit()
.map_err(|e| ExportError::Database(e.to_string()))?;
}
// Bulk-insert scripts in a transaction
let script_count = script_sources.len();
if !script_sources.is_empty() {
let tx = storage
.connection_mut()
.transaction()
.map_err(|e| ExportError::Database(e.to_string()))?;
let now = UnixSecs::now();
for (source_code, load_order, enabled, category) in &script_sources {
let id = uuid::Uuid::new_v4().to_string();
let fm = user_script::parse_front_matter(source_code);
tx.execute(
"INSERT INTO user_scripts (id, name, description, source_code, load_order, enabled, created_at, modified_at, category)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
rusqlite::params![id, fm.name, fm.description, source_code, load_order, enabled, now, now, category],
)
.map_err(|e| ExportError::Database(e.to_string()))?;
}
tx.commit()
.map_err(|e| ExportError::Database(e.to_string()))?;
}
// Read workspace metadata before dropping storage (archive must still be alive).
let workspace_metadata: Option<WorkspaceMetadata> =
try_read_entry(&mut archive, "workspace.json", zip_password)
.and_then(|cursor| serde_json::from_reader(cursor).ok());
// Drop storage before opening via Workspace::open (avoids double-locking the file).
drop(storage);
// Rebuild the note_links index from the imported fields_json data.
let mut workspace = Workspace::open(
db_path,
workspace_password,
identity_uuid,
signing_key,
Box::new(crate::core::permission::AllowAllGate::new("krillnotes/1")),
None,
data_dir,
)
.map_err(|e| ExportError::Database(e.to_string()))?;
workspace
.rebuild_note_links_index()
.map_err(|e| ExportError::Database(e.to_string()))?;
// Restore attachments if the archive contains them.
// Use try_read_entry so the borrow on archive is fully released before
// we call by_name again for each individual attachment file.
if let Some(att_cursor) = try_read_entry(&mut archive, "attachments.json", zip_password) {
let att_json = String::from_utf8(att_cursor.into_inner()).unwrap_or_default();
let attachment_metas: Vec<AttachmentMeta> =
serde_json::from_str(&att_json).unwrap_or_default();
for meta in attachment_metas {
let zip_path = format!("attachments/{}/{}", meta.id, meta.filename);
if let Some(file_cursor) = try_read_entry(&mut archive, &zip_path, zip_password) {
let plaintext = file_cursor.into_inner();
let _ = workspace.attach_file_with_id(
&meta.id,
&meta.note_id,
&meta.filename,
meta.mime_type.as_deref(),
&plaintext,
);
}
}
}
// Restore workspace metadata if the archive contained it.
if let Some(ref meta) = workspace_metadata {
workspace
.set_workspace_metadata(meta)
.map_err(|e| ExportError::Database(e.to_string()))?;
}
// Stamp the importer's identity as author of all imported notes.
workspace
.connection()
.execute(
"UPDATE notes SET created_by = ?, modified_by = ?",
[workspace.identity_pubkey(), workspace.identity_pubkey()],
)
.map_err(|e| ExportError::Database(e.to_string()))?;
Ok(ImportResult {
app_version: export_notes.app_version,
note_count: export_notes.notes.len(),
script_count,
metadata: workspace_metadata,
})
}
#[cfg(test)]
#[path = "export_tests.rs"]
mod tests;