Skip to content

Commit 13e12d5

Browse files
committed
Extract a method for incrementing the download counts
1 parent 442c439 commit 13e12d5

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/krate.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,22 @@ fn read_fill<R: Read + ?Sized>(r: &mut R, mut slice: &mut [u8])
827827
pub fn download(req: &mut Request) -> CargoResult<Response> {
828828
let crate_name = &req.params()["crate_id"];
829829
let version = &req.params()["version"];
830+
831+
try!(increment_download_counts(req, crate_name, version));
832+
let redirect_url = format!("https://{}/crates/{}/{}-{}.crate",
833+
req.app().bucket.host(),
834+
crate_name, crate_name, version);
835+
836+
if req.wants_json() {
837+
#[derive(RustcEncodable)]
838+
struct R { url: String }
839+
Ok(req.json(&R{ url: redirect_url }))
840+
} else {
841+
Ok(req.redirect(redirect_url))
842+
}
843+
}
844+
845+
fn increment_download_counts(req: &Request, crate_name: &str, version: &str) -> CargoResult<()> {
830846
let tx = try!(req.tx());
831847
let stmt = try!(tx.prepare("SELECT versions.id as version_id
832848
FROM crates
@@ -836,7 +852,7 @@ pub fn download(req: &mut Request) -> CargoResult<Response> {
836852
canon_crate_name($1)
837853
AND versions.num = $2
838854
LIMIT 1"));
839-
let rows = try!(stmt.query(&[crate_name, version]));
855+
let rows = try!(stmt.query(&[&crate_name, &version]));
840856
let row = try!(rows.iter().next().chain_error(|| {
841857
human("crate or version not found")
842858
}));
@@ -862,19 +878,7 @@ pub fn download(req: &mut Request) -> CargoResult<Response> {
862878
try!(tx.execute("INSERT INTO version_downloads
863879
(version_id) VALUES ($1)", &[&version_id]));
864880
}
865-
866-
// Now that we've done our business, redirect to the actual data.
867-
let redirect_url = format!("https://{}/crates/{}/{}-{}.crate",
868-
req.app().bucket.host(),
869-
crate_name, crate_name, version);
870-
871-
if req.wants_json() {
872-
#[derive(RustcEncodable)]
873-
struct R { url: String }
874-
Ok(req.json(&R{ url: redirect_url }))
875-
} else {
876-
Ok(req.redirect(redirect_url))
877-
}
881+
Ok(())
878882
}
879883

880884
/// Handles the `GET /crates/:crate_id/downloads` route.

0 commit comments

Comments
 (0)