|
| 1 | +use crate::config::AppData; |
| 2 | +use crate::endpoints::ApiError; |
| 3 | +use actix_web::{HttpResponse, Responder, get, web}; |
| 4 | +use serde::Deserialize; |
| 5 | +use utoipa::{IntoParams, ToSchema}; |
| 6 | + |
| 7 | +use std::fs; |
| 8 | +use std::path::Path; |
| 9 | +use urlencoding; |
| 10 | + |
| 11 | +const LABEL_COLOR: &str = "#0c0811"; |
| 12 | +const STAT_COLOR: &str = "#5f3d84"; |
| 13 | + |
| 14 | +#[derive(Deserialize, Clone, Copy, PartialEq, Eq, ToSchema)] |
| 15 | +#[serde(rename_all = "snake_case")] |
| 16 | +pub enum StatusBadgeStat { |
| 17 | + Version, |
| 18 | + GdVersion, |
| 19 | + GeodeVersion, |
| 20 | + Downloads, |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Deserialize, IntoParams)] |
| 24 | +pub struct StatusBadgeQuery { |
| 25 | + pub stat: StatusBadgeStat, |
| 26 | +} |
| 27 | + |
| 28 | +#[utoipa::path( |
| 29 | + get, |
| 30 | + path = "/v1/mods/{id}/status_badge", |
| 31 | + tag = "mods", |
| 32 | + params( |
| 33 | + ("id" = String, Path, description = "Mod ID"), |
| 34 | + StatusBadgeQuery |
| 35 | + ), |
| 36 | + responses( |
| 37 | + (status = 302, description = "Redirect to Shields.io badge"), |
| 38 | + (status = 400, description = "Invalid stat or missing parameter"), |
| 39 | + (status = 404, description = "Mod not found") |
| 40 | + ) |
| 41 | +)] |
| 42 | +#[get("/v1/mods/{id}/status_badge")] |
| 43 | +pub async fn status_badge( |
| 44 | + data: web::Data<AppData>, |
| 45 | + id: web::Path<String>, |
| 46 | + query: web::Query<StatusBadgeQuery>, |
| 47 | +) -> Result<impl Responder, ApiError> { |
| 48 | + let (stat, label, svg_path) = match query.stat { |
| 49 | + StatusBadgeStat::Version => ( |
| 50 | + "payload.versions[0].version", |
| 51 | + "Version", |
| 52 | + "storage/public/shields/mod_version.svg", |
| 53 | + ), |
| 54 | + StatusBadgeStat::GdVersion => ( |
| 55 | + "payload.versions[0].gd.win", |
| 56 | + "Geometry Dash", |
| 57 | + "storage/public/shields/mod_gd_version.svg", |
| 58 | + ), |
| 59 | + StatusBadgeStat::GeodeVersion => ( |
| 60 | + "payload.versions[0].geode", |
| 61 | + "Geode", |
| 62 | + "storage/public/shields/mod_geode_version.svg", |
| 63 | + ), |
| 64 | + StatusBadgeStat::Downloads => ( |
| 65 | + "payload.download_count", |
| 66 | + "Downloads", |
| 67 | + "storage/public/shields/mod_downloads.svg", |
| 68 | + ), |
| 69 | + }; |
| 70 | + let svg = fs::read_to_string(Path::new(svg_path)) |
| 71 | + .map_err(|_| ApiError::BadRequest(format!("Could not read SVG file: {}", svg_path)))?; |
| 72 | + let api_url = format!("{}/v1/mods/{}?abbreviate=true", data.app_url(), id); |
| 73 | + let mod_link = format!("{}/mods/{}", data.front_url(), id); |
| 74 | + let svg_data_url = format!("data:image/svg+xml;utf8,{}", urlencoding::encode(&svg)); |
| 75 | + let shields_url = format!( |
| 76 | + "https://img.shields.io/badge/dynamic/json?url={}&query={}&label={}&labelColor={}&color={}&link={}&style=plastic&logo={}", |
| 77 | + urlencoding::encode(&api_url), |
| 78 | + urlencoding::encode(stat), |
| 79 | + label, |
| 80 | + urlencoding::encode(LABEL_COLOR), |
| 81 | + urlencoding::encode(STAT_COLOR), |
| 82 | + urlencoding::encode(&mod_link), |
| 83 | + urlencoding::encode(&svg_data_url) |
| 84 | + ); |
| 85 | + Ok(HttpResponse::Found() |
| 86 | + .append_header(("Location", shields_url)) |
| 87 | + .finish()) |
| 88 | +} |
0 commit comments