Skip to content

Commit

Permalink
Upload index metadata to index/ when publishing new crates
Browse files Browse the repository at this point in the history
  • Loading branch information
arlosi committed Mar 22, 2022
1 parent 91fcb7d commit afb5a7f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
37 changes: 37 additions & 0 deletions src/uploaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::models::Crate;

const CACHE_CONTROL_IMMUTABLE: &str = "public,max-age=31536000,immutable";
const CACHE_CONTROL_README: &str = "public,max-age=604800";
const CACHE_CONTROL_INDEX: &str = "public,max-age=600";

#[derive(Clone, Debug)]
pub enum Uploader {
Expand Down Expand Up @@ -82,6 +83,17 @@ impl Uploader {
format!("readmes/{name}/{name}-{version}.html")
}

/// Returns the internal path of an uploaded crate's index file.
fn index_path(name: &str) -> String {
let prefix = match name.len() {
1 => String::from("1"),
2 => String::from("2"),
3 => format!("3/{}", &name[..1]),
_ => format!("{}/{}", &name[0..2], &name[2..4]),
};
format!("index/{prefix}/{name}")
}

/// Uploads a file using the configured uploader (either `S3`, `Local`).
///
/// It returns the path of the uploaded file.
Expand Down Expand Up @@ -175,4 +187,29 @@ impl Uploader {
)?;
Ok(())
}

pub(crate) fn upload_index(
&self,
http_client: &Client,
crate_name: &str,
index: String,
) -> Result<()> {
let path = Uploader::index_path(crate_name);
let content_length = index.len() as u64;
let content = Cursor::new(index);
let mut extra_headers = header::HeaderMap::new();
extra_headers.insert(
header::CACHE_CONTROL,
header::HeaderValue::from_static(CACHE_CONTROL_INDEX),
);
self.upload(
http_client,
&path,
content,
content_length,
"text/plain",
extra_headers,
)?;
Ok(())
}
}
13 changes: 11 additions & 2 deletions src/worker/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ pub fn add_crate(env: &Environment, krate: Crate) -> Result<(), PerformError> {

// Add the crate to its relevant file
fs::create_dir_all(dst.parent().unwrap())?;
let mut file = OpenOptions::new().append(true).create(true).open(&dst)?;
let mut file = OpenOptions::new()
.append(true)
.create(true)
.read(true)
.open(&dst)?;
serde_json::to_writer(&mut file, &krate)?;
file.write_all(b"\n")?;

let message: String = format!("Updating crate `{}#{}`", krate.name, krate.vers);
file.rewind()?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
env.uploader
.upload_index(env.http_client(), &krate.name, contents)?;

let message: String = format!("Updating crate `{}#{}`", krate.name, krate.vers);
repo.commit_and_push(&message, &dst)?;

Ok(())
Expand Down

0 comments on commit afb5a7f

Please sign in to comment.