Skip to content

Commit afb5a7f

Browse files
committed
Upload index metadata to index/ when publishing new crates
1 parent 91fcb7d commit afb5a7f

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/uploaders.rs

+37
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::models::Crate;
1313

1414
const CACHE_CONTROL_IMMUTABLE: &str = "public,max-age=31536000,immutable";
1515
const CACHE_CONTROL_README: &str = "public,max-age=604800";
16+
const CACHE_CONTROL_INDEX: &str = "public,max-age=600";
1617

1718
#[derive(Clone, Debug)]
1819
pub enum Uploader {
@@ -82,6 +83,17 @@ impl Uploader {
8283
format!("readmes/{name}/{name}-{version}.html")
8384
}
8485

86+
/// Returns the internal path of an uploaded crate's index file.
87+
fn index_path(name: &str) -> String {
88+
let prefix = match name.len() {
89+
1 => String::from("1"),
90+
2 => String::from("2"),
91+
3 => format!("3/{}", &name[..1]),
92+
_ => format!("{}/{}", &name[0..2], &name[2..4]),
93+
};
94+
format!("index/{prefix}/{name}")
95+
}
96+
8597
/// Uploads a file using the configured uploader (either `S3`, `Local`).
8698
///
8799
/// It returns the path of the uploaded file.
@@ -175,4 +187,29 @@ impl Uploader {
175187
)?;
176188
Ok(())
177189
}
190+
191+
pub(crate) fn upload_index(
192+
&self,
193+
http_client: &Client,
194+
crate_name: &str,
195+
index: String,
196+
) -> Result<()> {
197+
let path = Uploader::index_path(crate_name);
198+
let content_length = index.len() as u64;
199+
let content = Cursor::new(index);
200+
let mut extra_headers = header::HeaderMap::new();
201+
extra_headers.insert(
202+
header::CACHE_CONTROL,
203+
header::HeaderValue::from_static(CACHE_CONTROL_INDEX),
204+
);
205+
self.upload(
206+
http_client,
207+
&path,
208+
content,
209+
content_length,
210+
"text/plain",
211+
extra_headers,
212+
)?;
213+
Ok(())
214+
}
178215
}

src/worker/git.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,21 @@ pub fn add_crate(env: &Environment, krate: Crate) -> Result<(), PerformError> {
1717

1818
// Add the crate to its relevant file
1919
fs::create_dir_all(dst.parent().unwrap())?;
20-
let mut file = OpenOptions::new().append(true).create(true).open(&dst)?;
20+
let mut file = OpenOptions::new()
21+
.append(true)
22+
.create(true)
23+
.read(true)
24+
.open(&dst)?;
2125
serde_json::to_writer(&mut file, &krate)?;
2226
file.write_all(b"\n")?;
2327

24-
let message: String = format!("Updating crate `{}#{}`", krate.name, krate.vers);
28+
file.rewind()?;
29+
let mut contents = String::new();
30+
file.read_to_string(&mut contents)?;
31+
env.uploader
32+
.upload_index(env.http_client(), &krate.name, contents)?;
2533

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

2837
Ok(())

0 commit comments

Comments
 (0)