Skip to content

Commit cf274e9

Browse files
committed
Move download endpoint from krate::cargo to version::cargo
This endpoint is for a specific version, not the crate in general.
1 parent 50df329 commit cf274e9

File tree

4 files changed

+59
-55
lines changed

4 files changed

+59
-55
lines changed

src/krate/cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Endpoints that provide cached data about the published crates
2-
//!
2+
//!
33
//! Some of these endpoints include download counts, but other than that this
44
//! information could be reconstructed from the index and crate metadata.
55

src/krate/cargo.rs

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use serde_json;
1515
use app::RequestApp;
1616
use db::RequestTransaction;
1717
use dependency;
18-
use download::VersionDownload;
1918
use git;
2019
use owner::{rights, EncodableOwner, Owner, OwnerKind, Rights};
2120
use pagination::Paginate;
@@ -26,7 +25,7 @@ use user::RequestUser;
2625
use util::{read_fill, read_le_u32};
2726
use util::{human, internal, CargoResult, ChainError, RequestUtils};
2827
use version::NewVersion;
29-
use {Badge, Category, Keyword, Replica, User, Version};
28+
use {Badge, Category, Keyword, User, Version};
3029

3130
use super::{canon_crate_name, Crate, EncodableCrate, NewCrate, ALL_COLUMNS};
3231

@@ -445,53 +444,6 @@ fn parse_new_headers(req: &mut Request) -> CargoResult<(upload::NewCrate, User)>
445444
Ok((new, user.clone()))
446445
}
447446

448-
/// Handles the `GET /crates/:crate_id/:version/download` route.
449-
/// This returns a URL to the location where the crate is stored.
450-
pub fn download(req: &mut Request) -> CargoResult<Response> {
451-
let crate_name = &req.params()["crate_id"];
452-
let version = &req.params()["version"];
453-
454-
// If we are a mirror, ignore failure to update download counts.
455-
// API-only mirrors won't have any crates in their database, and
456-
// incrementing the download count will look up the crate in the
457-
// database. Mirrors just want to pass along a redirect URL.
458-
if req.app().config.mirror == Replica::ReadOnlyMirror {
459-
let _ = increment_download_counts(req, crate_name, version);
460-
} else {
461-
increment_download_counts(req, crate_name, version)?;
462-
}
463-
464-
let redirect_url = req.app()
465-
.config
466-
.uploader
467-
.crate_location(crate_name, version)
468-
.ok_or_else(|| human("crate files not found"))?;
469-
470-
if req.wants_json() {
471-
#[derive(Serialize)]
472-
struct R {
473-
url: String,
474-
}
475-
Ok(req.json(&R { url: redirect_url }))
476-
} else {
477-
Ok(req.redirect(redirect_url))
478-
}
479-
}
480-
481-
fn increment_download_counts(req: &Request, crate_name: &str, version: &str) -> CargoResult<()> {
482-
use self::versions::dsl::*;
483-
484-
let conn = req.db_conn()?;
485-
let version_id = versions
486-
.select(id)
487-
.filter(crate_id.eq_any(Crate::by_name(crate_name).select(crates::id)))
488-
.filter(num.eq(version))
489-
.first(&*conn)?;
490-
491-
VersionDownload::create_or_increment(version_id, &conn)?;
492-
Ok(())
493-
}
494-
495447
/// Handles the `GET /crates/:crate_id/owners` route.
496448
pub fn owners(req: &mut Request) -> CargoResult<Response> {
497449
let crate_name = &req.params()["crate_id"];

src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,14 @@ pub fn middleware(app: Arc<App>) -> MiddlewareBuilder {
142142

143143
// Routes used by `cargo`
144144
api_router.put("/crates/new", C(krate::cargo::new));
145-
api_router.get(
146-
"/crates/:crate_id/:version/download",
147-
C(krate::cargo::download),
148-
);
149145
api_router.get("/crates/:crate_id/owners", C(krate::cargo::owners));
150146
api_router.put("/crates/:crate_id/owners", C(krate::cargo::add_owners));
151147
api_router.delete("/crates/:crate_id/owners", C(krate::cargo::remove_owners));
148+
149+
api_router.get(
150+
"/crates/:crate_id/:version/download",
151+
C(version::cargo::download),
152+
);
152153
api_router.delete("/crates/:crate_id/:version/yank", C(version::cargo::yank));
153154
api_router.put(
154155
"/crates/:crate_id/:version/unyank",

src/version/cargo.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,58 @@ use user::RequestUser;
1616
use util::errors::CargoError;
1717
use util::{human, CargoResult, RequestUtils};
1818

19-
use super::version_and_crate;
19+
use super::{version_and_crate, Crate};
20+
21+
/// Handles the `GET /crates/:crate_id/:version/download` route.
22+
/// This returns a URL to the location where the crate is stored.
23+
pub fn download(req: &mut Request) -> CargoResult<Response> {
24+
use Replica;
25+
use conduit_router::RequestParams;
26+
27+
let crate_name = &req.params()["crate_id"];
28+
let version = &req.params()["version"];
29+
30+
// If we are a mirror, ignore failure to update download counts.
31+
// API-only mirrors won't have any crates in their database, and
32+
// incrementing the download count will look up the crate in the
33+
// database. Mirrors just want to pass along a redirect URL.
34+
if req.app().config.mirror == Replica::ReadOnlyMirror {
35+
let _ = increment_download_counts(req, crate_name, version);
36+
} else {
37+
increment_download_counts(req, crate_name, version)?;
38+
}
39+
40+
let redirect_url = req.app()
41+
.config
42+
.uploader
43+
.crate_location(crate_name, version)
44+
.ok_or_else(|| human("crate files not found"))?;
45+
46+
if req.wants_json() {
47+
#[derive(Serialize)]
48+
struct R {
49+
url: String,
50+
}
51+
Ok(req.json(&R { url: redirect_url }))
52+
} else {
53+
Ok(req.redirect(redirect_url))
54+
}
55+
}
56+
57+
fn increment_download_counts(req: &Request, crate_name: &str, version: &str) -> CargoResult<()> {
58+
use download::VersionDownload;
59+
use self::versions::dsl::*;
60+
61+
let conn = req.db_conn()?;
62+
let version_id = versions
63+
.select(id)
64+
.filter(crate_id.eq_any(Crate::by_name(crate_name).select(crates::id)))
65+
.filter(num.eq(version))
66+
.first(&*conn)?;
67+
68+
VersionDownload::create_or_increment(version_id, &conn)?;
69+
Ok(())
70+
}
2071

2172
/// Handles the `DELETE /crates/:crate_id/:version/yank` route.
2273
/// This does not delete a crate version, it makes the crate

0 commit comments

Comments
 (0)