Optimize user-generated images for reduced bandwidth (#961)

* Optimize user-generated images for reduced bandwidth

* run prepare

* Finish compression
This commit is contained in:
Geometrically
2024-09-07 17:44:49 -07:00
committed by GitHub
parent cb0f03ca9c
commit 5b5599128a
51 changed files with 1306 additions and 1016 deletions

View File

@@ -33,6 +33,7 @@ impl CollectionBuilder {
created: Utc::now(),
updated: Utc::now(),
icon_url: None,
raw_icon_url: None,
color: None,
status: self.status,
projects: self.projects,
@@ -51,6 +52,7 @@ pub struct Collection {
pub created: DateTime<Utc>,
pub updated: DateTime<Utc>,
pub icon_url: Option<String>,
pub raw_icon_url: Option<String>,
pub color: Option<u32>,
pub status: CollectionStatus,
pub projects: Vec<ProjectId>,
@@ -65,11 +67,11 @@ impl Collection {
"
INSERT INTO collections (
id, user_id, name, description,
created, icon_url, status
created, icon_url, raw_icon_url, status
)
VALUES (
$1, $2, $3, $4,
$5, $6, $7
$5, $6, $7, $8
)
",
self.id as CollectionId,
@@ -78,6 +80,7 @@ impl Collection {
self.description.as_ref(),
self.created,
self.icon_url.as_ref(),
self.raw_icon_url.as_ref(),
self.status.to_string(),
)
.execute(&mut **transaction)
@@ -165,7 +168,7 @@ impl Collection {
let collections = sqlx::query!(
"
SELECT c.id id, c.name name, c.description description,
c.icon_url icon_url, c.color color, c.created created, c.user_id user_id,
c.icon_url icon_url, c.raw_icon_url raw_icon_url, c.color color, c.created created, c.user_id user_id,
c.updated updated, c.status status,
ARRAY_AGG(DISTINCT cm.mod_id) filter (where cm.mod_id is not null) mods
FROM collections c
@@ -183,6 +186,7 @@ impl Collection {
name: m.name.clone(),
description: m.description.clone(),
icon_url: m.icon_url.clone(),
raw_icon_url: m.raw_icon_url.clone(),
color: m.color.map(|x| x as u32),
created: m.created,
updated: m.updated,

View File

@@ -11,6 +11,7 @@ const IMAGES_NAMESPACE: &str = "images";
pub struct Image {
pub id: ImageId,
pub url: String,
pub raw_url: String,
pub size: u64,
pub created: DateTime<Utc>,
pub owner_id: UserId,
@@ -32,14 +33,15 @@ impl Image {
sqlx::query!(
"
INSERT INTO uploaded_images (
id, url, size, created, owner_id, context, mod_id, version_id, thread_message_id, report_id
id, url, raw_url, size, created, owner_id, context, mod_id, version_id, thread_message_id, report_id
)
VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
);
",
self.id as ImageId,
self.url,
self.raw_url,
self.size as i64,
self.created,
self.owner_id as UserId,
@@ -119,7 +121,7 @@ impl Image {
use futures::stream::TryStreamExt;
sqlx::query!(
"
SELECT id, url, size, created, owner_id, context, mod_id, version_id, thread_message_id, report_id
SELECT id, url, raw_url, size, created, owner_id, context, mod_id, version_id, thread_message_id, report_id
FROM uploaded_images
WHERE context = $1
AND (mod_id = $2 OR ($2 IS NULL AND mod_id IS NULL))
@@ -142,6 +144,7 @@ impl Image {
Image {
id,
url: row.url,
raw_url: row.raw_url,
size: row.size as u64,
created: row.created,
owner_id: UserId(row.owner_id),
@@ -185,7 +188,7 @@ impl Image {
|image_ids| async move {
let images = sqlx::query!(
"
SELECT id, url, size, created, owner_id, context, mod_id, version_id, thread_message_id, report_id
SELECT id, url, raw_url, size, created, owner_id, context, mod_id, version_id, thread_message_id, report_id
FROM uploaded_images
WHERE id = ANY($1)
GROUP BY id;
@@ -197,6 +200,7 @@ impl Image {
let img = Image {
id: ImageId(i.id),
url: i.url,
raw_url: i.raw_url,
size: i.size as u64,
created: i.created,
owner_id: UserId(i.owner_id),

View File

@@ -18,6 +18,7 @@ pub struct OAuthClient {
pub id: OAuthClientId,
pub name: String,
pub icon_url: Option<String>,
pub raw_icon_url: Option<String>,
pub max_scopes: Scopes,
pub secret_hash: String,
pub redirect_uris: Vec<OAuthRedirectUri>,
@@ -31,6 +32,7 @@ struct ClientQueryResult {
id: i64,
name: String,
icon_url: Option<String>,
raw_icon_url: Option<String>,
max_scopes: i64,
secret_hash: String,
created: DateTime<Utc>,
@@ -53,6 +55,7 @@ macro_rules! select_clients_with_predicate {
clients.id as "id!",
clients.name as "name!",
clients.icon_url as "icon_url?",
clients.raw_icon_url as "raw_icon_url?",
clients.max_scopes as "max_scopes!",
clients.secret_hash as "secret_hash!",
clients.created as "created!",
@@ -133,15 +136,16 @@ impl OAuthClient {
sqlx::query!(
"
INSERT INTO oauth_clients (
id, name, icon_url, max_scopes, secret_hash, created_by
id, name, icon_url, raw_icon_url, max_scopes, secret_hash, created_by
)
VALUES (
$1, $2, $3, $4, $5, $6
$1, $2, $3, $4, $5, $6, $7
)
",
self.id.0,
self.name,
self.icon_url,
self.raw_icon_url,
self.max_scopes.to_postgres(),
self.secret_hash,
self.created_by.0
@@ -161,11 +165,12 @@ impl OAuthClient {
sqlx::query!(
"
UPDATE oauth_clients
SET name = $1, icon_url = $2, max_scopes = $3, url = $4, description = $5
WHERE (id = $6)
SET name = $1, icon_url = $2, raw_icon_url = $3, max_scopes = $4, url = $5, description = $6
WHERE (id = $7)
",
self.name,
self.icon_url,
self.raw_icon_url,
self.max_scopes.to_postgres(),
self.url,
self.description,
@@ -243,6 +248,7 @@ impl From<ClientQueryResult> for OAuthClient {
id: OAuthClientId(r.id),
name: r.name,
icon_url: r.icon_url,
raw_icon_url: r.raw_icon_url,
max_scopes: Scopes::from_postgres(r.max_scopes),
secret_hash: r.secret_hash,
redirect_uris: redirects,

View File

@@ -30,6 +30,7 @@ pub struct Organization {
/// The display icon for the organization
pub icon_url: Option<String>,
pub raw_icon_url: Option<String>,
pub color: Option<u32>,
}
@@ -40,8 +41,8 @@ impl Organization {
) -> Result<(), super::DatabaseError> {
sqlx::query!(
"
INSERT INTO organizations (id, slug, name, team_id, description, icon_url, color)
VALUES ($1, $2, $3, $4, $5, $6, $7)
INSERT INTO organizations (id, slug, name, team_id, description, icon_url, raw_icon_url, color)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
",
self.id.0,
self.slug,
@@ -49,6 +50,7 @@ impl Organization {
self.team_id as TeamId,
self.description,
self.icon_url,
self.raw_icon_url,
self.color.map(|x| x as i32),
)
.execute(&mut **transaction)
@@ -125,7 +127,7 @@ impl Organization {
let organizations = sqlx::query!(
"
SELECT o.id, o.slug, o.name, o.team_id, o.description, o.icon_url, o.color
SELECT o.id, o.slug, o.name, o.team_id, o.description, o.icon_url, o.raw_icon_url, o.color
FROM organizations o
WHERE o.id = ANY($1) OR LOWER(o.slug) = ANY($2)
GROUP BY o.id;
@@ -142,6 +144,7 @@ impl Organization {
team_id: TeamId(m.team_id),
description: m.description,
icon_url: m.icon_url,
raw_icon_url: m.raw_icon_url,
color: m.color.map(|x| x as u32),
};
@@ -168,7 +171,7 @@ impl Organization {
{
let result = sqlx::query!(
"
SELECT o.id, o.slug, o.name, o.team_id, o.description, o.icon_url, o.color
SELECT o.id, o.slug, o.name, o.team_id, o.description, o.icon_url, o.raw_icon_url, o.color
FROM organizations o
LEFT JOIN mods m ON m.organization_id = o.id
WHERE m.id = $1
@@ -187,6 +190,7 @@ impl Organization {
team_id: TeamId(result.team_id),
description: result.description,
icon_url: result.icon_url,
raw_icon_url: result.raw_icon_url,
color: result.color.map(|x| x as u32),
}))
} else {

View File

@@ -58,6 +58,7 @@ impl LinkUrl {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GalleryItem {
pub image_url: String,
pub raw_image_url: String,
pub featured: bool,
pub name: Option<String>,
pub description: Option<String>,
@@ -71,7 +72,8 @@ impl GalleryItem {
project_id: ProjectId,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), sqlx::error::Error> {
let (project_ids, image_urls, featureds, names, descriptions, orderings): (
let (project_ids, image_urls, raw_image_urls, featureds, names, descriptions, orderings): (
Vec<_>,
Vec<_>,
Vec<_>,
Vec<_>,
@@ -84,6 +86,7 @@ impl GalleryItem {
(
project_id.0,
gi.image_url,
gi.raw_image_url,
gi.featured,
gi.name,
gi.description,
@@ -94,12 +97,13 @@ impl GalleryItem {
sqlx::query!(
"
INSERT INTO mods_gallery (
mod_id, image_url, featured, name, description, ordering
mod_id, image_url, raw_image_url, featured, name, description, ordering
)
SELECT * FROM UNNEST ($1::bigint[], $2::varchar[], $3::bool[], $4::varchar[], $5::varchar[], $6::bigint[])
SELECT * FROM UNNEST ($1::bigint[], $2::varchar[], $3::varchar[], $4::bool[], $5::varchar[], $6::varchar[], $7::bigint[])
",
&project_ids[..],
&image_urls[..],
&raw_image_urls[..],
&featureds[..],
&names[..] as &[Option<String>],
&descriptions[..] as &[Option<String>],
@@ -153,6 +157,7 @@ pub struct ProjectBuilder {
pub summary: String,
pub description: String,
pub icon_url: Option<String>,
pub raw_icon_url: Option<String>,
pub license_url: Option<String>,
pub categories: Vec<CategoryId>,
pub additional_categories: Vec<CategoryId>,
@@ -192,6 +197,7 @@ impl ProjectBuilder {
downloads: 0,
follows: 0,
icon_url: self.icon_url,
raw_icon_url: self.raw_icon_url,
license_url: self.license_url,
license: self.license,
slug: self.slug,
@@ -253,6 +259,7 @@ pub struct Project {
pub downloads: i32,
pub follows: i32,
pub icon_url: Option<String>,
pub raw_icon_url: Option<String>,
pub license_url: Option<String>,
pub license: String,
pub slug: Option<String>,
@@ -273,15 +280,15 @@ impl Project {
"
INSERT INTO mods (
id, team_id, name, summary, description,
published, downloads, icon_url, status, requested_status,
published, downloads, icon_url, raw_icon_url, status, requested_status,
license_url, license,
slug, color, monetization_status, organization_id
)
VALUES (
$1, $2, $3, $4, $5, $6,
$7, $8, $9, $10,
$11, $12,
LOWER($13), $14, $15, $16
$7, $8, $9, $10, $11,
$12, $13,
LOWER($14), $15, $16, $17
)
",
self.id as ProjectId,
@@ -292,6 +299,7 @@ impl Project {
self.published,
self.downloads,
self.icon_url.as_ref(),
self.raw_icon_url.as_ref(),
self.status.as_str(),
self.requested_status.map(|x| x.as_str()),
self.license_url.as_ref(),
@@ -620,7 +628,7 @@ impl Project {
let mods_gallery: DashMap<ProjectId, Vec<GalleryItem>> = sqlx::query!(
"
SELECT DISTINCT mod_id, mg.image_url, mg.featured, mg.name, mg.description, mg.created, mg.ordering
SELECT DISTINCT mod_id, mg.image_url, mg.raw_image_url, mg.featured, mg.name, mg.description, mg.created, mg.ordering
FROM mods_gallery mg
INNER JOIN mods m ON mg.mod_id = m.id
WHERE m.id = ANY($1) OR m.slug = ANY($2)
@@ -633,6 +641,7 @@ impl Project {
.or_default()
.push(GalleryItem {
image_url: m.image_url,
raw_image_url: m.raw_image_url,
featured: m.featured.unwrap_or(false),
name: m.name,
description: m.description,
@@ -742,7 +751,7 @@ impl Project {
let projects = sqlx::query!(
"
SELECT m.id id, m.name name, m.summary summary, m.downloads downloads, m.follows follows,
m.icon_url icon_url, m.description description, m.published published,
m.icon_url icon_url, m.raw_icon_url raw_icon_url, m.description description, m.published published,
m.updated updated, m.approved approved, m.queued, m.status status, m.requested_status requested_status,
m.license_url license_url,
m.team_id team_id, m.organization_id organization_id, m.license license, m.slug slug, m.moderation_message moderation_message, m.moderation_message_body moderation_message_body,
@@ -788,6 +797,7 @@ impl Project {
summary: m.summary.clone(),
downloads: m.downloads,
icon_url: m.icon_url.clone(),
raw_icon_url: m.raw_icon_url.clone(),
published: m.published,
updated: m.updated,
license_url: m.license_url.clone(),

View File

@@ -40,6 +40,7 @@ pub struct User {
pub email: Option<String>,
pub email_verified: bool,
pub avatar_url: Option<String>,
pub raw_avatar_url: Option<String>,
pub bio: Option<String>,
pub created: DateTime<Utc>,
pub role: String,
@@ -57,7 +58,7 @@ impl User {
"
INSERT INTO users (
id, username, email,
avatar_url, bio, created,
avatar_url, raw_avatar_url, bio, created,
github_id, discord_id, gitlab_id, google_id, steam_id, microsoft_id,
email_verified, password, paypal_id, paypal_country, paypal_email,
venmo_handle, stripe_customer_id
@@ -66,13 +67,14 @@ impl User {
$1, $2, $3, $4, $5,
$6, $7,
$8, $9, $10, $11, $12, $13,
$14, $15, $16, $17, $18, $19
$14, $15, $16, $17, $18, $19, $20
)
",
self.id as UserId,
&self.username,
self.email.as_ref(),
self.avatar_url.as_ref(),
self.raw_avatar_url.as_ref(),
self.bio.as_ref(),
self.created,
self.github_id,
@@ -165,7 +167,7 @@ impl User {
let users = sqlx::query!(
"
SELECT id, email,
avatar_url, username, bio,
avatar_url, raw_avatar_url, username, bio,
created, role, badges,
balance,
github_id, discord_id, gitlab_id, google_id, steam_id, microsoft_id,
@@ -190,6 +192,7 @@ impl User {
email: u.email,
email_verified: u.email_verified,
avatar_url: u.avatar_url,
raw_avatar_url: u.raw_avatar_url,
username: u.username.clone(),
bio: u.bio,
created: u.created,