mirror of
https://github.com/narrrl/narl-workspace.git
synced 2026-11-18 18:35:04 +01:00
The data directory is the whole diary, and until now keeping a copy of it was someone's manual job. The server can now register itself in Proton Drive as a device — its own sync root, next to the desktop clients — and keep that folder equal to DIARY_DATA_DIR, encrypted client-side by the same Rust SDK the Linux client uses. One interactive `narl-diary proton-login`, then nothing prompts again. The stored session carries the tokens, the mailbox password (the key chain is rebuilt from it on every start) and the account key salts, which have to be captured at login: `core/v4/keys/salts` needs the `locked` scope, and only a password-login token has it. Refresh tokens are single-use, so every rotation is written back before it can be needed. It lives as a 0600 file next to the database, on the same volume as the data it protects; `--features keyring` puts it in the OS keyring instead, for hosts that have a Secret Service. The mirror is one-way and change-driven: writes mark the diary dirty and it is mirrored once the writing stops, with an interval as the backstop — an hour of writing is one backup, not sixty. `diary.db` goes up as a VACUUM INTO snapshot, so it is a real point in time rather than a file copied out from under a writer, and as a new revision each time, so Proton keeps the older ones. Uploads are immutable, so they are written once and skipped by size afterwards. Deletions do not propagate unless asked: a backup that forgets on command is one accident away from being no backup at all. Identification follows Proton's third-party rules — an honest `external-drive-narl_diary@<version>-alpha`, the disclosure printed before any credential is asked for, and remembered folder uids instead of walking the tree every run. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01D5Gk8FPkRXu1e6Nj2BCRE1
124 lines
3.1 KiB
Rust
124 lines
3.1 KiB
Rust
use axum::{
|
|
extract::{Path, State},
|
|
response::Response,
|
|
Json,
|
|
};
|
|
use base64::{engine::general_purpose::URL_SAFE_NO_PAD as B64, Engine};
|
|
use rand::RngCore;
|
|
use serde::Serialize;
|
|
use serde_json::json;
|
|
use sqlx::Row;
|
|
|
|
use crate::{
|
|
auth::Session,
|
|
error::{AppError, AppResult},
|
|
routes::media,
|
|
state::AppState,
|
|
};
|
|
|
|
#[derive(Serialize)]
|
|
pub struct SharedEntry {
|
|
pub title: String,
|
|
pub body: String,
|
|
pub created_at: i64,
|
|
pub updated_at: i64,
|
|
pub token: String,
|
|
}
|
|
|
|
fn new_token() -> String {
|
|
let mut bytes = [0u8; 24];
|
|
rand::thread_rng().fill_bytes(&mut bytes);
|
|
B64.encode(bytes)
|
|
}
|
|
|
|
pub async fn enable(
|
|
_: Session,
|
|
State(state): State<AppState>,
|
|
Path(id): Path<i64>,
|
|
) -> AppResult<Json<serde_json::Value>> {
|
|
let existing: Option<Option<String>> = sqlx::query("SELECT share_token FROM entries WHERE id = ?1")
|
|
.bind(id)
|
|
.fetch_optional(&state.db)
|
|
.await?
|
|
.map(|row| row.get("share_token"));
|
|
|
|
let token = match existing {
|
|
None => return Err(AppError::NotFound),
|
|
Some(Some(token)) => token,
|
|
Some(None) => {
|
|
let token = new_token();
|
|
sqlx::query("UPDATE entries SET share_token = ?1 WHERE id = ?2")
|
|
.bind(&token)
|
|
.bind(id)
|
|
.execute(&state.db)
|
|
.await?;
|
|
token
|
|
}
|
|
};
|
|
|
|
state.backup.signal();
|
|
Ok(Json(json!({ "token": token, "path": format!("/s/{token}") })))
|
|
}
|
|
|
|
pub async fn disable(
|
|
_: Session,
|
|
State(state): State<AppState>,
|
|
Path(id): Path<i64>,
|
|
) -> AppResult<Json<serde_json::Value>> {
|
|
let affected = sqlx::query("UPDATE entries SET share_token = NULL WHERE id = ?1")
|
|
.bind(id)
|
|
.execute(&state.db)
|
|
.await?
|
|
.rows_affected();
|
|
if affected == 0 {
|
|
return Err(AppError::NotFound);
|
|
}
|
|
state.backup.signal();
|
|
Ok(Json(json!({ "ok": true })))
|
|
}
|
|
|
|
/// Public: no session required, the unguessable token is the credential.
|
|
pub async fn read(
|
|
State(state): State<AppState>,
|
|
Path(token): Path<String>,
|
|
) -> AppResult<Json<SharedEntry>> {
|
|
let row = sqlx::query(
|
|
"SELECT title, body, created_at, updated_at FROM entries WHERE share_token = ?1",
|
|
)
|
|
.bind(&token)
|
|
.fetch_optional(&state.db)
|
|
.await?
|
|
.ok_or(AppError::NotFound)?;
|
|
|
|
Ok(Json(SharedEntry {
|
|
title: row.get("title"),
|
|
body: row.get("body"),
|
|
created_at: row.get("created_at"),
|
|
updated_at: row.get("updated_at"),
|
|
token,
|
|
}))
|
|
}
|
|
|
|
/// Public: serves a media file only while it is embedded in a shared entry.
|
|
pub async fn serve_media(
|
|
State(state): State<AppState>,
|
|
Path((token, id)): Path<(String, String)>,
|
|
) -> AppResult<Response> {
|
|
let allowed = sqlx::query(
|
|
"SELECT 1 AS ok
|
|
FROM entry_media em
|
|
JOIN entries e ON e.id = em.entry_id
|
|
WHERE em.media_id = ?1 AND e.share_token = ?2",
|
|
)
|
|
.bind(&id)
|
|
.bind(&token)
|
|
.fetch_optional(&state.db)
|
|
.await?
|
|
.is_some();
|
|
|
|
if !allowed {
|
|
return Err(AppError::NotFound);
|
|
}
|
|
media::stream_media(&state, &id).await
|
|
}
|