diff options
Diffstat (limited to 'src/data/page.rs')
-rw-r--r-- | src/data/page.rs | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/src/data/page.rs b/src/data/page.rs index 6d0b802..7b7d432 100644 --- a/src/data/page.rs +++ b/src/data/page.rs @@ -2,7 +2,7 @@ use std::{ collections::HashMap, fs::{self, File}, io::{BufRead, BufReader}, - path::Path, + path::{Path, PathBuf}, }; use chrono::{DateTime, Utc}; @@ -18,6 +18,7 @@ pub struct Page { pub title: String, pub current_version: DateTime<Utc>, pub prev_versions: Vec<DateTime<Utc>>, + disk_path: PathBuf, content_offset: usize, } @@ -129,12 +130,13 @@ impl Pages { title: metadata.title, current_version, prev_versions, + disk_path: current_page, content_offset, }) } - pub fn get_page(&self, uuid: PageUuid) -> Option<Page> { - todo!() + pub fn get_page(&self, uuid: &PageUuid) -> Option<&Page> { + self.pages.get(uuid) } pub fn create_page(&self, page: Page) { @@ -149,3 +151,33 @@ impl Pages { todo!() } } + +#[cfg(feature = "ssr")] +impl Page { + pub async fn read_content(&self) -> Result<String, String> { + use std::io::Read; + + let file_meta = + fs::metadata(&self.disk_path).map_err(|_| "Cannot retrieve file size".to_string())?; + let read_length = usize::try_from(file_meta.len()) + .map_err(|_| "Cannot get file offset".to_string())? + - self.content_offset; + + let mut reader = BufReader::new( + File::open(&self.disk_path).map_err(|_| "Could not open page file".to_string())?, + ); + + reader + .seek_relative( + i64::try_from(self.content_offset) + .map_err(|_| "Invalid seek length".to_string())?, + ) + .map_err(|_| "Could not seek in page file".to_string())?; + + let mut contents = String::with_capacity(read_length); + reader + .read_to_string(&mut contents) + .map_err(|_| "Could not read file".to_string())?; + Ok(contents) + } +} |