Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove manifest.json reads from fs fetch #1760

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fastn-core/src/manifest/manifest_to_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 3 additions & 0 deletions fastn-core/src/package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
pub versioned: bool,
pub translation_of: Box<Option<Package>>,
pub translations: Vec<Package>,
Expand Down Expand Up @@ -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![],
Expand Down Expand Up @@ -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,
Expand Down
101 changes: 12 additions & 89 deletions fastn-core/src/package/package_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>)> {
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 {
Expand All @@ -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::Manifest>,
) -> fastn_core::Result<(String, Vec<u8>)> {
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<Vec<u8>> {
let base = self.download_base_url.as_ref().ok_or_else(|| {
Expand Down Expand Up @@ -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
}
}

Expand Down
Loading