From 619373a261ad18c51cd09bc61d116f585c8295ec Mon Sep 17 00:00:00 2001 From: tempest Date: Fri, 18 Apr 2025 00:58:28 -0600 Subject: Read correct page --- src/data/page.rs | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) (limited to 'src/data/page.rs') 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, pub prev_versions: Vec>, + 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 { - 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 { + 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) + } +} -- cgit 1.4.1