Skip to content

Commit

Permalink
writing etag back from response
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Feb 8, 2025
1 parent 944c06d commit 0d5a431
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
9 changes: 8 additions & 1 deletion fastn-update/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,9 @@ async fn download_unpack_zip_and_get_manifest(
) -> Result<fastn_core::Manifest, fastn_update::UpdateError> {
use sha2::{digest::FixedOutput, Digest};

let mut archive = utils::download_archive(ds, zip_url, dependency_path)
let etag_file = dependency_path.join(".etag");

let (etag, mut archive) = utils::download_archive(ds, zip_url, &etag_file)
.await
.context(DownloadArchiveSnafu {
package: package_name,
Expand Down Expand Up @@ -362,6 +364,11 @@ async fn download_unpack_zip_and_get_manifest(
fastn_core::Manifest::new(files, zip_url.to_string(), checksum)
};

ds.write_content(&etag_file, etag.as_bytes())
.await
.inspect_err(|e| eprintln!("failed to write etag file for {package_name}: {e}"))
.unwrap_or(());

Ok(manifest)
}

Expand Down
23 changes: 17 additions & 6 deletions fastn-update/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ pub async fn read_current_package(
pub(crate) async fn download_archive(
ds: &fastn_ds::DocumentStore,
url: &str,
path: &fastn_ds::Path,
) -> fastn_core::Result<zip::ZipArchive<std::io::Cursor<bytes::Bytes>>> {
let etag = match ds.read_to_string(&path.join(".etag"), &None).await {
etag_file: &fastn_ds::Path,
) -> fastn_core::Result<(String, zip::ZipArchive<std::io::Cursor<bytes::Bytes>>)> {
use std::io::Seek;

let etag = match ds.read_to_string(etag_file, &None).await {
Ok(v) => format!("\"{v}\""),
Err(fastn_ds::ReadStringError::ReadError(fastn_ds::ReadError::NotFound(_))) => {
"\"0\"".to_string()
Expand All @@ -44,9 +46,18 @@ pub(crate) async fn download_archive(
reqwest::header::HeaderValue::from_str(etag.as_str()).unwrap(),
);

let _resp = fastn_ds::http::DEFAULT_CLIENT.execute(r).await?;

todo!()
let resp = fastn_ds::http::DEFAULT_CLIENT.execute(r).await?;
let etag = resp
.headers()
.get("etag")
.and_then(|v| v.to_str().ok())
.unwrap()
.to_string();
// TODO: handle 304 response
let mut cursor = std::io::Cursor::new(resp.bytes().await?);
cursor.seek(std::io::SeekFrom::Start(0))?;
let archive = zip::ZipArchive::new(cursor)?;
Ok((etag, archive))
}

pub(crate) async fn resolve_dependency_package(
Expand Down

0 comments on commit 0d5a431

Please sign in to comment.