new finctions and fixes

This commit is contained in:
Mateusz Gruszczyński
2026-07-30 00:12:48 +02:00
parent 9ca055de5f
commit 2274cf57c9
31 changed files with 1348 additions and 505 deletions
+37 -9
View File
@@ -101,14 +101,37 @@ pub async fn upload_pad_file(
let mime = mime_guess::from_path(&stored)
.first_or_octet_stream()
.to_string();
let cache_control = format!("public, max-age={}", state.file_cache_max_age_seconds);
let cache_control = crate::cache::cache_control(state.file_cache_max_age_seconds);
state
.storage
.put(&key, bytes.clone().into(), &mime, &cache_control)
.await
.map_err(|_| ApiError::internal("Failed to save the file"))?;
db::register_pad_file(&state.db, pad.id, &stored, &url, &mime, bytes.len() as i64).await?;
Ok(Json(serde_json::json!({"name": stored, "url": url})))
Ok(Json(serde_json::json!({"name": stored, "url": url, "mime_type": mime})))
}
pub(super) fn content_references_file(content: &str, filename: &str, url: &str) -> bool {
if content.contains(url) {
return true;
}
for marker in ["[file=", "[image=", "[img="] {
let mut remaining = content;
while let Some(index) = remaining.find(marker) {
let after = &remaining[index + marker.len()..];
let end = after
.find(|character| character == ',' || character == ']')
.unwrap_or(after.len());
if after[..end].trim() == filename {
return true;
}
if end >= after.len() {
break;
}
remaining = &after[end + 1..];
}
}
false
}
pub async fn pad_files(
@@ -127,7 +150,7 @@ pub async fn pad_files(
.await?;
let mut files = db::list_pad_files(&state.db, pad.id).await?;
for file in &mut files {
let attached = pad.content.contains(&file.url);
let attached = content_references_file(&pad.content, &file.filename, &file.url);
if attached != file.is_attached {
db::set_pad_file_attached(&state.db, file.id, attached).await?;
file.is_attached = attached;
@@ -265,14 +288,14 @@ pub async fn upload_note_file(
let mime = mime_guess::from_path(&stored)
.first_or_octet_stream()
.to_string();
let cache_control = format!("public, max-age={}", state.file_cache_max_age_seconds);
let cache_control = crate::cache::cache_control(state.file_cache_max_age_seconds);
state
.storage
.put(&key, bytes.clone().into(), &mime, &cache_control)
.await
.map_err(|_| ApiError::internal("Failed to save the file"))?;
db::register_note_file(&state.db, note.id, &stored, &url, &mime, bytes.len() as i64).await?;
Ok(Json(serde_json::json!({"name": stored, "url": url})))
Ok(Json(serde_json::json!({"name": stored, "url": url, "mime_type": mime})))
}
pub async fn delete_note(
@@ -316,6 +339,12 @@ pub async fn delete_note(
.await
.map_err(|_| ApiError::internal("Failed to delete note files"))?;
}
db::delete_resource_editor_state(
&state.db,
"note",
&format!("{workspace_slug}/{note_slug}"),
)
.await?;
db::delete_note(&state.db, note.id).await?;
Ok(Json(serde_json::json!({"ok": true})))
}
@@ -337,7 +366,7 @@ pub async fn note_files(
.await?;
let mut files = db::list_note_files(&state.db, note.id).await?;
for file in &mut files {
let attached = note.content.contains(&file.url);
let attached = content_references_file(&note.content, &file.filename, &file.url);
if attached != file.is_attached {
db::set_note_file_attached(&state.db, file.id, attached).await?;
file.is_attached = attached;
@@ -464,9 +493,8 @@ async fn serve_token_file(
);
response.headers_mut().insert(
header::CACHE_CONTROL,
HeaderValue::from_str(&format!(
"public, max-age={}",
state.file_cache_max_age_seconds
HeaderValue::from_str(&crate::cache::cache_control(
state.file_cache_max_age_seconds,
))
.expect("valid file cache-control header"),
);