Skip to content

Commit

Permalink
made sure that redirects keep the query arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Jun 10, 2024
1 parent 1ac111a commit 69185f8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
22 changes: 18 additions & 4 deletions server/main-api/src/maps/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::fmt::{Display, Formatter};
use std::io::Cursor;

use actix_web::http::header::LOCATION;
use actix_web::{get, web, HttpResponse};
use image::{ImageBuffer, Rgba};

use log::{debug, error, warn};
use serde::Deserialize;
use serde::{Deserialize, Serializer};
use sqlx::Error::RowNotFound;
use sqlx::PgPool;
use tokio::time::Instant;
Expand Down Expand Up @@ -133,7 +134,7 @@ fn load_default_image() -> Vec<u8> {
w.into_inner()
}

async fn get_possible_redirect_url(conn: &PgPool, query: &str) -> Option<String> {
async fn get_possible_redirect_url(conn: &PgPool, query: &str, args: &QueryArgs) -> Option<String> {
let result = sqlx::query_as!(
LocationKeyAlias,
r#"
Expand All @@ -146,7 +147,12 @@ async fn get_possible_redirect_url(conn: &PgPool, query: &str) -> Option<String>
.fetch_one(conn)
.await;
match result {
Ok(d) => Some(format!("https://nav.tum.de/api/preview/{key}", key = d.key)),
Ok(d) => Some(format!(
"https://nav.tum.de/api/preview/{key}?lang={lang}&format={format}",
key = d.key,
lang = args.lang.serialise(),
format = args.format.serialise()
)),
Err(RowNotFound) => None,
Err(e) => {
error!("Error requesting alias for {query}: {e:?}");
Expand All @@ -162,6 +168,14 @@ enum PreviewFormat {
OpenGraph,
Square,
}
impl PreviewFormat {
fn serialise(&self) -> String {
match self {
PreviewFormat::OpenGraph => "open_graph".to_string(),
PreviewFormat::Square => "square".to_string(),
}
}
}

#[derive(Deserialize, Default, Debug)]
#[serde(rename_all = "snake_case")]
Expand All @@ -182,7 +196,7 @@ pub async fn maps_handler(
let id = params
.into_inner()
.replace(|c: char| c.is_whitespace() || c.is_control(), "");
if let Some(redirect_url) = get_possible_redirect_url(&data.db, &id).await {
if let Some(redirect_url) = get_possible_redirect_url(&data.db, &id, &args).await {
let mut res = HttpResponse::PermanentRedirect();
res.insert_header((LOCATION, redirect_url));
return res.finish();
Expand Down
6 changes: 6 additions & 0 deletions server/main-api/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ impl LangQueryArgs {
pub fn should_use_english(&self) -> bool {
self.lang == LanguageOptions::En
}
pub fn serialise(&self) -> String {
match self.lang {
LanguageOptions::En => "en".to_string(),
LanguageOptions::De => "de".to_string(),
}
}
}

0 comments on commit 69185f8

Please sign in to comment.