summary refs log tree commit diff
path: root/src/data/page.rs
diff options
context:
space:
mode:
authortempest <git@ashen.earth>2025-04-18 00:58:28 -0600
committertempest <git@ashen.earth>2025-04-18 00:58:28 -0600
commit619373a261ad18c51cd09bc61d116f585c8295ec (patch)
tree041a80d7b57cf221497809bd3889bff1042b842c /src/data/page.rs
parentb856f12cf422b96c37c12df3d7829e4d15ef4453 (diff)
Read correct page HEAD main config
Diffstat (limited to 'src/data/page.rs')
-rw-r--r--src/data/page.rs38
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)
+    }
+}