Skip to content

Commit

Permalink
Add command-line option for the current fqdn
Browse files Browse the repository at this point in the history
- Add missing error descriptions in the docs
- chage allowed gateway methods to any in preparation for h2 ws

- improve contrast in the light theme of the image viewer
  • Loading branch information
moehreag committed Jan 24, 2025
1 parent b8c179f commit 28de6aa
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 16 deletions.
23 changes: 22 additions & 1 deletion docs/api_documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,10 @@ Fetch a shared image (usually screenshots) with metadata.
- `file`: `string` - The file content, encoded with standard base64
- `shared_at`: `Timestamp`

#### Errors

- `404` - The provided image does not exist or has expired

### `GET` `/image/<id>/raw`

Fetch a raw image
Expand All @@ -567,6 +571,10 @@ Fetch a raw image

The raw bytes of the image

#### Errors

- `404` - The provided image does not exist or has expired

### `GET` `/image/<id>/view`

View an image in a browser.
Expand All @@ -579,6 +587,10 @@ View an image in a browser.

Html page in the style of the AxolotlClient website to display an image, with embedding support.

#### Errors

- `404` - The provided image does not exist or has expired

### `GET` `/image/<id>/oembed?<format>`

Get oEmbed information for an image. See https://oembed.com.
Expand All @@ -602,9 +614,13 @@ Get oEmbed information for an image. See https://oembed.com.
- `provider_name`: `string` - The oEmbed provider name, `AxolotlClient`
- `provider_url`: `string` - The oEmbed provider url, `https://axolotlclient.com`

#### Errors

- `404` - The provided image does not exist or has expired

### `POST` `/image/<filename>` [Authenticated](#Errors)

Share an image
Share an image in PNG format

#### Path Fields

Expand All @@ -618,6 +634,11 @@ The image data, in raw bytes

The image id, in plain text.

#### Errors

- `400` - The png file is malformed
- `413` - The image is over 8MiB in size

### `GET` `/hypixel` [Authenticated](#Errors)

Query cached values from the public hypixel API
Expand Down
4 changes: 2 additions & 2 deletions src/endpoints/global_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ impl GlobalData {
impl Clone for GlobalData {
fn clone(&self) -> Self {
Self {
total_players: self.total_players.clone(),
online_players: self.online_players.clone(),
total_players: self.total_players,
online_players: self.online_players,
modrinth_data: self.modrinth_data.clone(),
notes: self.notes.clone(),
}
Expand Down
42 changes: 31 additions & 11 deletions src/endpoints/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ pub async fn evict_expired(database: &PgPool) -> Result<(), TaskError> {
Ok(())
}

const PAGE_TEMPLATE: &str = include_str!("image_view.html");

pub async fn get_view(
State(ApiState { database, .. }): State<ApiState>,
State(ApiState { database, cl_args, .. }): State<ApiState>,
Path(id): Path<Id>,
) -> Result<Html<String>, ApiError> {
let image = query!("SELECT filename, player, timestamp, file FROM images WHERE id = $1", id as _)
Expand All @@ -97,7 +99,17 @@ pub async fn get_view(
.ok_or(StatusCode::NOT_FOUND)?;

let filename = String::from_utf8(image.filename).unwrap();
let base_url = "https://api.axolotlclient.com/v1/";
let base_url = match &cl_args.domain_name {
Some(name) => {
let n = name.to_owned();
if !n.ends_with("/") {
n + "/"
} else {
n
}
}
None => "https://api.axolotlclient.com/v1/".to_owned(),
};
let image_url = base_url.to_string() + "image/" + &id.to_string();

let username = query!("SELECT username FROM players WHERE uuid = $1", image.player)
Expand All @@ -108,9 +120,9 @@ pub async fn get_view(
let time = image.timestamp.and_utc().format("%Y/%m/%d %H:%M").to_string();
let png = PngInfo::create(&Bytes::from(image.file.clone())).await.unwrap();
Ok(Html(
include_str!("image_view.html")
PAGE_TEMPLATE
.replace("{filename}", &filename)
.replace("{image_data}", &("data:image/png;base64,".to_string() + &STANDARD_NO_PAD.encode(image.file)))
.replace("{image_data}", (image_url.clone() + "/raw").as_str())
.replace("{image_url}", &image_url)
.replace("{image_width}", &png.width.to_string())
.replace("{image_height}", &png.height.to_string())
Expand Down Expand Up @@ -144,7 +156,7 @@ impl OEmbed {
OEmbed {
version: "1.0",
_type: "photo",
title,
title: title + " | AxolotlClient",
url,
width: png.width,
height: png.height,
Expand All @@ -160,7 +172,7 @@ pub struct OEmbedQuery {
}

pub async fn get_oembed(
State(ApiState { database, .. }): State<ApiState>,
State(ApiState { database, cl_args, .. }): State<ApiState>,
Path(id): Path<Id>,
Query(OEmbedQuery { format }): Query<OEmbedQuery>,
) -> Result<Json<OEmbed>, ApiError> {
Expand All @@ -176,11 +188,19 @@ pub async fn get_oembed(

let filename = String::from_utf8(image.filename).unwrap();

let embed = OEmbed::create(
filename,
"https://api.axolotlclient.com/v1/image/".to_owned() + &id.to_string() + "/raw",
png.unwrap(),
);
let base_url = match &cl_args.domain_name {
Some(name) => {
let n = name.to_owned();
if !n.ends_with("/") {
n + "/"
} else {
n
}
}
None => "https://api.axolotlclient.com/v1/".to_owned(),
};

let embed = OEmbed::create(filename, base_url + &id.to_string() + "/raw", png.unwrap());
Ok(if format == "json" {
Json(embed)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/image_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
--about-text: #2a2a2a;
--switcher-color: invert(6%) sepia(1%) saturate(751%) hue-rotate(314deg)
brightness(101%) contrast(71%);
--bg-brightness: 0.8;
--bg-brightness: 1;
}
:root {
--default-transition: color 0.3s ease-in-out,
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::endpoints::user::{self, Activity};
use crate::endpoints::{account, brew_coffee, channel, get_authenticate, image, not_found};
use crate::gateway::gateway;
use axum::extract::DefaultBodyLimit;
use axum::routing::any;
use axum::{routing::get, routing::post, serve, Router};
use clap::{Args, Parser};
use dashmap::DashMap;
Expand Down Expand Up @@ -35,6 +36,9 @@ pub struct ClArgs {

#[arg(long)]
pub notes_file: Option<PathBuf>,

#[arg(long)]
pub domain_name: Option<String>,
}

#[derive(Args)]
Expand Down Expand Up @@ -112,7 +116,7 @@ async fn main() -> anyhow::Result<()> {
let router = Router::new()
.route("/global_data", get(global_data::get))
.route("/authenticate", get(get_authenticate))
.route("/gateway", get(gateway))
.route("/gateway", any(gateway))
.route("/user/:uuid", get(user::get).post(user::post))
.route("/user/:uuid/images", get(user::get_images))
.route("/channels", get(account::get_channels))
Expand Down

0 comments on commit 28de6aa

Please sign in to comment.