Skip to content

Commit

Permalink
Add tiny fs-atomic-light crate
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillt committed Mar 1, 2024
1 parent ffb2bf3 commit ccc35f2
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 19 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"data-pdf",
"data-resource",
"fs-atomic-versions",
"fs-atomic-light",
"fs-index",
"fs-storage",
]
Expand Down
4 changes: 1 addition & 3 deletions data-link/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
23 changes: 7 additions & 16 deletions data-link/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,20 +27,6 @@ pub struct Properties {
pub title: String,
pub desc: Option<String>,
}
/// 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<Path>,
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<String>) -> Self {
Expand All @@ -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)?;
Expand Down Expand Up @@ -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)?;

Expand Down Expand Up @@ -196,6 +183,7 @@ fn select_og(html: &Html, tag: OpenGraphTag) -> Option<String> {

None
}

fn select_desc(html: &Html) -> Option<String> {
let selector = Selector::parse("meta[name=\"description\"]").unwrap();

Expand All @@ -207,6 +195,7 @@ fn select_desc(html: &Html) -> Option<String> {

None
}

fn select_title(html: &Html) -> Option<String> {
let selector = Selector::parse("title").unwrap();
if let Some(element) = html.select(&selector).next() {
Expand All @@ -215,6 +204,7 @@ fn select_title(html: &Html) -> Option<String> {

None
}

#[derive(Debug, Serialize, Deserialize, Default, Clone)]
pub struct OpenGraph {
/// Represents the "og:title" OpenGraph meta tag.
Expand All @@ -236,6 +226,7 @@ pub struct OpenGraph {
/// Represents the "og:locale" OpenGraph meta tag
locale: Option<String>,
}

impl OpenGraph {
pub async fn fetch_image(&self) -> Option<Vec<u8>> {
if let Some(url) = &self.image {
Expand Down
12 changes: 12 additions & 0 deletions fs-atomic-light/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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" }
23 changes: 23 additions & 0 deletions fs-atomic-light/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<Path>,
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(())
}

0 comments on commit ccc35f2

Please sign in to comment.