Skip to main content

specfold_core/files/
attach.rs

1//! Attaching files to a document: decide where each file is kept, copy or upload it, and record
2//! it in the document's `files` list.
3
4use super::{hash, ignore_file, store};
5use crate::corpus::Corpus;
6use crate::markdown::frontmatter::edit;
7use crate::model::Doc;
8use anyhow::{Context, Result, bail};
9use serde::Serialize;
10use serde_json::{Value, json};
11use std::path::{Path, PathBuf};
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum Where {
15    /// Decide by size: small files in the repository, large ones in the store.
16    Auto,
17    Repo,
18    Store,
19}
20
21#[derive(Debug, Clone, Serialize)]
22pub struct Planned {
23    pub source: String,
24    /// Path inside the bundle.
25    pub path: String,
26    pub sha256: String,
27    pub bytes: u64,
28    pub store: String,
29}
30
31#[derive(Debug, Clone, Serialize)]
32pub struct Plan {
33    pub doc: String,
34    /// Set when a single-file document becomes a bundle first.
35    pub to_bundle: Option<(String, String)>,
36    pub files: Vec<Planned>,
37}
38
39fn bundle_main(doc: &Doc) -> (String, String) {
40    let file = doc.path.rsplit_once('/').map(|x| x.1).unwrap_or(&doc.path);
41    let dir = doc.base_dir();
42    let stem = file.trim_end_matches(".md").trim_end_matches(&format!(".{}", doc.id.ty));
43    let new_dir = format!("{dir}/{stem}");
44    (new_dir.clone(), format!("{new_dir}/index.{}.md", doc.id.ty))
45}
46
47pub fn plan(c: &Corpus, doc: &Doc, sources: &[PathBuf], into: &str, place: Where) -> Result<Plan> {
48    let limit = c.cfg.raw.files.inline_max_bytes;
49    let has_store = c.cfg.raw.files.store.is_some();
50    let to_bundle = match &doc.dir {
51        Some(_) => None,
52        None => Some((doc.path.clone(), bundle_main(doc).1)),
53    };
54    let mut files = vec![];
55    for src in sources {
56        let (sha256, bytes) = hash::file(src).with_context(|| format!("cannot read {}", src.display()))?;
57        let name = src.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
58        let rel = if into.is_empty() { name } else { format!("{}/{name}", into.trim_matches('/')) };
59        let store = match place {
60            Where::Repo if bytes > limit => {
61                bail!("{} is {bytes} bytes, over the {limit}-byte limit for the repository", src.display())
62            }
63            Where::Repo => "repo",
64            Where::Store if !has_store => bail!("no [files.store] is configured"),
65            Where::Store => "store",
66            Where::Auto if bytes > limit && !has_store => {
67                bail!("{} is {bytes} bytes; configure [files.store] or shrink it under {limit}", src.display())
68            }
69            Where::Auto if bytes > limit => "store",
70            Where::Auto => "repo",
71        };
72        files.push(Planned {
73            source: src.to_string_lossy().into_owned(),
74            path: rel,
75            sha256,
76            bytes,
77            store: store.into(),
78        });
79    }
80    Ok(Plan { doc: doc.id.to_string(), to_bundle, files })
81}
82
83pub fn apply(c: &Corpus, doc: &Doc, plan: &Plan) -> Result<()> {
84    let root = &c.cfg.root;
85    let main = match &plan.to_bundle {
86        Some((from, to)) => {
87            std::fs::create_dir_all(root.join(to).parent().unwrap_or(root))?;
88            std::fs::rename(root.join(from), root.join(to))?;
89            to.clone()
90        }
91        None => doc.path.clone(),
92    };
93    let bundle = Path::new(&main).parent().map(|p| p.to_string_lossy().into_owned()).unwrap_or_default();
94    let mut ignored = vec![];
95    for f in &plan.files {
96        let dst = root.join(&bundle).join(&f.path);
97        if let Some(p) = dst.parent() {
98            std::fs::create_dir_all(p)?;
99        }
100        if Path::new(&f.source) != dst {
101            std::fs::copy(&f.source, &dst).with_context(|| format!("cannot copy {} to {}", f.source, dst.display()))?;
102        }
103        if f.store == "store" {
104            let s = store::open(&c.cfg)?.context("no [files.store] is configured")?;
105            s.put(&dst, &f.sha256)?;
106            ignored.push(f.path.clone());
107        }
108    }
109    if !ignored.is_empty() {
110        ignore_file::add(&root.join(&bundle), &ignored)?;
111    }
112    let text = std::fs::read_to_string(root.join(&main))?;
113    let new = edit::rewrite(&text, |m| {
114        let mut list: Vec<Value> = m.get("files").and_then(|v| v.as_array()).cloned().unwrap_or_default();
115        for f in &plan.files {
116            list.retain(|v| v.get("path").and_then(Value::as_str) != Some(f.path.as_str()));
117            list.push(json!({"path": f.path, "sha256": f.sha256, "bytes": f.bytes, "store": f.store}));
118        }
119        m.insert("files".into(), Value::Array(list));
120        m.insert("updated".into(), Value::String(crate::model::stamp::today()));
121    })?;
122    std::fs::write(root.join(&main), new)?;
123    Ok(())
124}