From 6b11a582cbb32498803dc68571786a61444f8c9b Mon Sep 17 00:00:00 2001 From: Harsh Singh Date: Fri, 9 Feb 2024 21:00:58 +0530 Subject: [PATCH] fix: remove manifest.json reads from fs fetch --- .../src/manifest/manifest_to_package.rs | 1 + fastn-core/src/package/mod.rs | 3 + fastn-core/src/package/package_doc.rs | 101 +++--------------- 3 files changed, 16 insertions(+), 89 deletions(-) diff --git a/fastn-core/src/manifest/manifest_to_package.rs b/fastn-core/src/manifest/manifest_to_package.rs index f6c718a73d..bd359e8c4a 100644 --- a/fastn-core/src/manifest/manifest_to_package.rs +++ b/fastn-core/src/manifest/manifest_to_package.rs @@ -10,6 +10,7 @@ impl fastn_core::Manifest { package .resolve(&package_root.join(package_name).join("FASTN.ftd"), ds) .await?; + package.files = self.files.keys().map(|f| f.to_string()).collect(); package.auto_import_language( main_package.requested_language.clone(), main_package.selected_language.clone(), diff --git a/fastn-core/src/package/mod.rs b/fastn-core/src/package/mod.rs index a9fa18c904..beccee909f 100644 --- a/fastn-core/src/package/mod.rs +++ b/fastn-core/src/package/mod.rs @@ -8,6 +8,7 @@ pub mod user_group; pub struct Package { pub name: String, /// The `versioned` stores the boolean value storing of the fastn package is versioned or not + pub files: Vec, pub versioned: bool, pub translation_of: Box>, pub translations: Vec, @@ -75,6 +76,7 @@ impl Package { pub fn new(name: &str) -> fastn_core::Package { fastn_core::Package { name: name.to_string(), + files: vec![], versioned: false, translation_of: Box::new(None), translations: vec![], @@ -958,6 +960,7 @@ impl PackageTempIntoPackage for fastn_package::old_fastn::PackageTemp { Package { name: self.name.clone(), + files: vec![], versioned: self.versioned, translation_of: Box::new(translation_of), translations, diff --git a/fastn-core/src/package/package_doc.rs b/fastn-core/src/package/package_doc.rs index 61c8465797..513b1a5b73 100644 --- a/fastn-core/src/package/package_doc.rs +++ b/fastn-core/src/package/package_doc.rs @@ -71,61 +71,27 @@ impl fastn_core::Package { } #[tracing::instrument(skip(self))] - pub(crate) async fn fs_fetch_by_id_using_manifest( + pub(crate) async fn fs_fetch_by_id( &self, id: &str, package_root: Option<&fastn_ds::Path>, ds: &fastn_ds::DocumentStore, - manifest: &fastn_core::Manifest, ) -> fastn_core::Result<(String, Vec)> { let new_id = if fastn_core::file::is_static(id)? { - if manifest.files.contains_key(id.trim_start_matches('/')) { - Some(id.to_string()) - } else { - let new_id = match id.rsplit_once('.') { - Some((remaining, ext)) - if mime_guess::MimeGuess::from_ext(ext) - .first_or_octet_stream() - .to_string() - .starts_with("image/") => - { - if remaining.ends_with("-dark") { - format!( - "{}.{}", - remaining.trim_matches('/').trim_end_matches("-dark"), - ext - ) - } else { - format!("{}-dark.{}", remaining.trim_matches('/'), ext) - } - } - _ => { - tracing::error!(id = id, msg = "id error: can not get the dark"); - return Err(fastn_core::Error::PackageError { - message: format!( - "fs_fetch_by_id:: Corresponding file not found for id: {}. Package: {}", - id, &self.name - ), - }); - } - }; - - if !manifest.files.contains_key(&new_id) { - tracing::error!(id = id, msg = "id error: can not get the dark"); - return Err(fastn_core::Error::PackageError { - message: format!( - "fs_fetch_by_id:: Corresponding file not found for id: {}. Package: {}", - id, &self.name - ), - }); - } - - Some(new_id) + if !self.files.contains(&id.trim_start_matches('/').to_string()) { + return Err(fastn_core::Error::PackageError { + message: format!( + "fs_fetch_by_id:: Corresponding file not found for id: {}. Package: {}", + id, &self.name + ), + }); } + + Some(id.to_string()) } else { file_id_to_names(id) .iter() - .find(|id| manifest.files.contains_key(id.as_str())) + .find(|id| self.files.contains(id)) .map(|id| id.to_string()) }; if let Some(id) = new_id { @@ -150,47 +116,6 @@ impl fastn_core::Package { }) } - #[tracing::instrument(skip(self))] - pub(crate) async fn fs_fetch_by_id( - &self, - id: &str, - package_root: Option<&fastn_ds::Path>, - ds: &fastn_ds::DocumentStore, - manifest: &Option, - ) -> fastn_core::Result<(String, Vec)> { - if let Some(manifest) = manifest { - return self - .fs_fetch_by_id_using_manifest(id, package_root, ds, manifest) - .await; - } - if fastn_core::file::is_static(id)? { - if let Ok(data) = self.fs_fetch_by_file_name(id, package_root, ds).await { - return Ok((id.to_string(), data)); - } - } else { - for name in file_id_to_names(id) { - if let Ok(data) = self - .fs_fetch_by_file_name(name.as_str(), package_root, ds) - .await - { - return Ok((name, data)); - } - } - } - - tracing::error!( - msg = "fs-error: file not found", - document = id, - package = self.name - ); - Err(fastn_core::Error::PackageError { - message: format!( - "fs_fetch_by_id:: Corresponding file not found for id: {}. Package: {}", - id, &self.name - ), - }) - } - #[tracing::instrument(skip_all)] async fn http_fetch_by_file_name(&self, name: &str) -> fastn_core::Result> { let base = self.download_base_url.as_ref().ok_or_else(|| { @@ -390,9 +315,7 @@ impl fastn_core::Package { } } - let manifest = self.get_manifest(ds).await?; - - self.fs_fetch_by_id(id, package_root, ds, &manifest).await + self.fs_fetch_by_id(id, package_root, ds).await } }