mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 10:26:21 +00:00
feat(cron::update) auto update git repo and update db index
This commit is contained in:
18 files changed
+882
-39
No files matched your search
@@ -1,35 +1,48 @@
|
||||
mod error;
|
||||
mod utils;
|
||||
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::error::Error;
|
||||
use crate::utils::front_matter::{parse_front_matter, split_md_front_matter};
|
||||
|
||||
use base64::{engine::general_purpose, Engine};
|
||||
use serde::Serialize;
|
||||
use serde_json::Value;
|
||||
use sha2::{Digest, Sha256};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::utils::front_matter::{parse_front_matter, split_md_front_matter};
|
||||
|
||||
#[derive(Default, Debug, Serialize)]
|
||||
pub struct MdFile {
|
||||
pub file_id: Uuid,
|
||||
pub file_name: String,
|
||||
pub filename: String,
|
||||
pub meta: Value,
|
||||
pub content: String,
|
||||
pub hash: String,
|
||||
}
|
||||
|
||||
impl MdFile {
|
||||
pub async fn from_file(file_path: &str) -> Result<MdFile, Box<dyn Error>> {
|
||||
let raw_file = fs::read_to_string(file_path)?;
|
||||
pub fn from_file(file_path: &Path) -> Result<MdFile, Error> {
|
||||
let raw_file = match fs::read_to_string(file_path) {
|
||||
Ok(file) => file,
|
||||
Err(e) => return Err(Error::FSError { msg: e.to_string() }),
|
||||
};
|
||||
let raw_file_bytes = raw_file.as_bytes().to_vec();
|
||||
let (front_matter_str, content) = split_md_front_matter(raw_file);
|
||||
let front_matter = parse_front_matter(&front_matter_str)?;
|
||||
let front_matter = match parse_front_matter(&front_matter_str) {
|
||||
Ok(front_matter) => front_matter,
|
||||
Err(e) => return Err(Error::InvalidFrontMatter { msg: e.to_string() }),
|
||||
};
|
||||
let hash = calculate_hash_base64(&raw_file_bytes);
|
||||
let file_id = Uuid::from_str(front_matter["id"].to_string().as_str())?;
|
||||
let file_id = match front_matter["id"].as_str() {
|
||||
Some(id) => match Uuid::from_str(id) {
|
||||
Ok(id) => id,
|
||||
Err(e) => return Err(Error::InvalidID { msg: e.to_string() }),
|
||||
},
|
||||
None => return Err(Error::MissingID),
|
||||
};
|
||||
let file_name = Path::new(file_path)
|
||||
.file_name()
|
||||
.unwrap()
|
||||
@@ -39,7 +52,7 @@ impl MdFile {
|
||||
|
||||
Ok(MdFile {
|
||||
file_id,
|
||||
file_name,
|
||||
filename: file_name,
|
||||
meta: front_matter,
|
||||
content,
|
||||
hash,
|
||||
|
||||
Reference in new issue
Block a user