From ccc35f2ce9d8d0e461f7f5a2b16cbdf277b9f2cb Mon Sep 17 00:00:00 2001 From: Kirill Taran Date: Fri, 1 Mar 2024 18:10:14 +0300 Subject: [PATCH] Add tiny `fs-atomic-light` crate --- Cargo.toml | 1 + data-link/Cargo.toml | 4 +--- data-link/src/lib.rs | 23 +++++++---------------- fs-atomic-light/Cargo.toml | 12 ++++++++++++ fs-atomic-light/src/lib.rs | 23 +++++++++++++++++++++++ 5 files changed, 44 insertions(+), 19 deletions(-) create mode 100644 fs-atomic-light/Cargo.toml create mode 100644 fs-atomic-light/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index da4f2b16..1a75dffd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ members = [ "data-pdf", "data-resource", "fs-atomic-versions", + "fs-atomic-light", "fs-index", "fs-storage", ] diff --git a/data-link/Cargo.toml b/data-link/Cargo.toml index da585f87..e10cae11 100644 --- a/data-link/Cargo.toml +++ b/data-link/Cargo.toml @@ -3,20 +3,18 @@ name = "data-link" version = "0.1.0" edition = "2021" - [lib] name = "data_link" crate-type = ["rlib"] bench = false [dependencies] -fs-index = { path = "../fs-index" } +fs-atomic-light = { path = "../fs-atomic-light" } fs-atomic-versions = { path = "../fs-atomic-versions" } fs-storage = { path = "../fs-storage" } data-resource = { path = "../data-resource" } data-error = { path = "../data-error" } - log = { version = "0.4.17", features = ["release_max_level_off"] } serde_json = "1.0.82" serde = { version = "1.0.138", features = ["derive"] } diff --git a/data-link/src/lib.rs b/data-link/src/lib.rs index f7d92182..b02391d8 100644 --- a/data-link/src/lib.rs +++ b/data-link/src/lib.rs @@ -1,6 +1,6 @@ use data_error::Result; use data_resource::ResourceId; -use fs_index::AtomicFile; +use fs_atomic_versions::atomic::AtomicFile; use fs_storage::meta::store_metadata; use fs_storage::prop::load_raw_properties; use fs_storage::prop::store_properties; @@ -27,20 +27,6 @@ pub struct Properties { pub title: String, pub desc: Option, } -/// Write data to a tempory file and move that written file to destination -/// -/// May failed if writing or moving failed -fn temp_and_move( - data: &[u8], - dest_dir: impl AsRef, - filename: &str, -) -> Result<()> { - let mut path = std::env::temp_dir(); - path.push(filename); - std::fs::write(&path, data)?; - std::fs::copy(path, dest_dir.as_ref().join(filename))?; - Ok(()) -} impl Link { pub fn new(url: Url, title: String, desc: Option) -> Self { @@ -64,6 +50,7 @@ impl Link { .join(PROPERTIES_STORAGE_FOLDER) .join(id.to_string()); let file = AtomicFile::new(path)?; + let current = file.load()?; let data = current.read_to_string()?; let user_meta: Properties = serde_json::from_str(&data)?; @@ -105,7 +92,7 @@ impl Link { // Resources are stored in the folder chosen by user let bytes = self.url.as_str().as_bytes(); - temp_and_move(bytes, root.as_ref(), &id_string)?; + fs_atomic_light::temp_and_move(bytes, root.as_ref(), &id_string)?; //User defined properties store_properties(&root, id, &self.prop)?; @@ -196,6 +183,7 @@ fn select_og(html: &Html, tag: OpenGraphTag) -> Option { None } + fn select_desc(html: &Html) -> Option { let selector = Selector::parse("meta[name=\"description\"]").unwrap(); @@ -207,6 +195,7 @@ fn select_desc(html: &Html) -> Option { None } + fn select_title(html: &Html) -> Option { let selector = Selector::parse("title").unwrap(); if let Some(element) = html.select(&selector).next() { @@ -215,6 +204,7 @@ fn select_title(html: &Html) -> Option { None } + #[derive(Debug, Serialize, Deserialize, Default, Clone)] pub struct OpenGraph { /// Represents the "og:title" OpenGraph meta tag. @@ -236,6 +226,7 @@ pub struct OpenGraph { /// Represents the "og:locale" OpenGraph meta tag locale: Option, } + impl OpenGraph { pub async fn fetch_image(&self) -> Option> { if let Some(url) = &self.image { diff --git a/fs-atomic-light/Cargo.toml b/fs-atomic-light/Cargo.toml new file mode 100644 index 00000000..5c15bd54 --- /dev/null +++ b/fs-atomic-light/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "fs-atomic-light" +version = "0.1.0" +edition = "2021" + +[lib] +name = "fs_atomic_light" +crate-type = ["rlib"] +bench = false + +[dependencies] +data-error = { path = "../data-error" } diff --git a/fs-atomic-light/src/lib.rs b/fs-atomic-light/src/lib.rs new file mode 100644 index 00000000..25288f6d --- /dev/null +++ b/fs-atomic-light/src/lib.rs @@ -0,0 +1,23 @@ +use data_error::Result; + +use std::env; +use std::fs; +use std::path::Path; +use std::str; + +/// Write data to a tempory file and move that written file to destination +/// +/// May failed if writing or moving failed +pub fn temp_and_move( + data: &[u8], + dest_dir: impl AsRef, + filename: &str, +) -> Result<()> { + let mut path = env::temp_dir(); + path.push(filename); + + fs::write(&path, data)?; + fs::copy(path, dest_dir.as_ref().join(filename))?; + + Ok(()) +}