Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
xSke committed Feb 11, 2024
1 parent 0d42c1c commit cae4ed1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
16 changes: 12 additions & 4 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct ImageMeta {
#[derive(FromRow, Serialize)]
pub struct Stats {
pub total_images: i64,
pub total_file_size: i64
pub total_file_size: i64,
}

#[derive(FromRow)]
Expand Down Expand Up @@ -61,19 +61,27 @@ pub async fn get_by_attachment_id(
)
}

pub async fn pop_queue(pool: &PgPool) -> anyhow::Result<Option<(Transaction<Postgres>, ImageQueueEntry)>> {
pub async fn pop_queue(
pool: &PgPool,
) -> anyhow::Result<Option<(Transaction<Postgres>, ImageQueueEntry)>> {
let mut tx = pool.begin().await?;
let res: Option<ImageQueueEntry> = sqlx::query_as("delete from image_queue where itemid = (select itemid from image_queue order by itemid for update skip locked limit 1) returning *")
.fetch_optional(&mut *tx).await?;
Ok(res.map(|x| (tx, x)))
}

pub async fn get_queue_length(pool: &PgPool) -> anyhow::Result<i64> {
Ok(sqlx::query_scalar("select count(*) from image_queue").fetch_one(pool).await?)
Ok(sqlx::query_scalar("select count(*) from image_queue")
.fetch_one(pool)
.await?)
}

pub async fn get_stats(pool: &PgPool) -> anyhow::Result<Stats> {
Ok(sqlx::query_as("select count(*) as total_images, sum(file_size) as total_file_size from images").fetch_one(pool).await?)
Ok(sqlx::query_as(
"select count(*) as total_images, sum(file_size) as total_file_size from images",
)
.fetch_one(pool)
.await?)
}

pub async fn add_image(pool: &PgPool, meta: ImageMeta) -> anyhow::Result<bool> {
Expand Down
16 changes: 10 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
mod db;
mod hash;
mod migrate;
mod process;
mod pull;
mod store;
mod migrate;

use crate::db::{ImageMeta, Stats};
use crate::pull::Puller;
use crate::store::Storer;
use axum::extract::State;
use axum::routing::get;
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
Expand All @@ -19,10 +21,8 @@ use config::FileFormat;
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
use std::sync::Arc;
use axum::routing::get;
use thiserror::Error;
use tracing::{error, info};
use crate::db::{ImageMeta, Stats};

#[derive(Error, Debug)]
pub enum PKAvatarError {
Expand Down Expand Up @@ -150,7 +150,11 @@ pub async fn stats(State(state): State<AppState>) -> Result<Json<Stats>, PKAvata
fn load_config() -> anyhow::Result<Config> {
config::ConfigBuilder::<DefaultState>::default()
.add_source(config::File::new("config", FileFormat::Toml).required(false))
.add_source(config::Environment::with_prefix("PK_AVATAR").prefix_separator("__").separator("__"))
.add_source(
config::Environment::with_prefix("PK_AVATAR")
.prefix_separator("__")
.separator("__"),
)
.build()?
.try_deserialize::<Config>()
.map_err(Into::into)
Expand Down Expand Up @@ -233,7 +237,7 @@ impl IntoResponse for PKAvatarError {
PKAvatarError::InternalError(ref e) => error!("error: {}", e),
PKAvatarError::NetworkError(ref e) => error!("error: {}", e),
PKAvatarError::ImageFormatError(ref e) => error!("error: {}", e),
_ => error!("error: {}", &self)
_ => error!("error: {}", &self),
}

(
Expand Down Expand Up @@ -262,7 +266,7 @@ struct Config {
base_url: String,

#[serde(default)]
migrate_worker_count: u32
migrate_worker_count: u32,
}

#[derive(Deserialize, Clone)]
Expand Down
18 changes: 14 additions & 4 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ fn resize(image: DynamicImage, kind: ImageKind) -> DynamicImage {
}

// todo: best filter?
let resized = image.resize(target_width, target_height, image::imageops::FilterType::Lanczos3);
let resized = image.resize(
target_width,
target_height,
image::imageops::FilterType::Lanczos3,
);
return resized;
}

Expand All @@ -84,14 +88,20 @@ fn encode(image: DynamicImage) -> ProcessOutput {

let time_before = Instant::now();
let encoded_lossy = webp::Encoder::new(&*image_buf, webp::PixelLayout::Rgba, width, height)
.encode_simple(false, 90.0).expect("encode should be infallible")
.encode_simple(false, 90.0)
.expect("encode should be infallible")
.to_vec();
let time_after = Instant::now();
let time_after = Instant::now();

let lossy_time = time_after - time_before;

let hash = Hash::sha256(&encoded_lossy);
info!("{}: lossy size {}K ({} ms)", hash, encoded_lossy.len()/1024, lossy_time.whole_milliseconds());
info!(
"{}: lossy size {}K ({} ms)",
hash,
encoded_lossy.len() / 1024,
lossy_time.whole_milliseconds()
);

ProcessOutput {
data_webp: encoded_lossy,
Expand Down

0 comments on commit cae4ed1

Please sign in to comment.