GET /images/<image>.<filetype> works, pulls images from ./images/<image>.png

This commit is contained in:
SadlyNotSappho 2024-02-23 11:21:07 -08:00
parent a5417649b1
commit f0cc5de640
4 changed files with 259 additions and 10 deletions

217
Cargo.lock generated
View File

@ -186,6 +186,12 @@ version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "383d29d513d8764dcdc42ea295d979eb99c3c9f00607b3692cf68a431f7dca72"
[[package]]
name = "bit_field"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61"
[[package]]
name = "bitflags"
version = "1.3.2"
@ -253,6 +259,12 @@ dependencies = [
"inout",
]
[[package]]
name = "color_quant"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]]
name = "const-oid"
version = "0.9.6"
@ -300,6 +312,34 @@ version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5"
[[package]]
name = "crc32fast"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3855a8a784b474f333699ef2bbca9db2c4a1f6d9088a90a2d25b1eb53111eaa"
dependencies = [
"cfg-if",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-queue"
version = "0.3.9"
@ -312,12 +352,15 @@ dependencies = [
[[package]]
name = "crossbeam-utils"
version = "0.8.17"
version = "0.8.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06d96137f14f244c37f989d9fff8f95e6c18b918e71f36638f8c49112e4c78f"
dependencies = [
"cfg-if",
]
checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345"
[[package]]
name = "crunchy"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
[[package]]
name = "crypto-common"
@ -461,12 +504,37 @@ version = "2.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0"
[[package]]
name = "exr"
version = "1.72.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "887d93f60543e9a9362ef8a21beedd0a833c5d9610e18c67abe15a5963dcb1a4"
dependencies = [
"bit_field",
"flume",
"half",
"lebe",
"miniz_oxide",
"rayon-core",
"smallvec",
"zune-inflate",
]
[[package]]
name = "fastrand"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5"
[[package]]
name = "fdeflate"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f9bfee30e4dedf0ab8b422f03af778d9612b63f502710fc500a334ebe2de645"
dependencies = [
"simd-adler32",
]
[[package]]
name = "figment"
version = "0.10.12"
@ -487,6 +555,16 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fcfdc7a0362c9f4444381a9e697c79d435fe65b52a37466fc2c1184cee9edc6"
[[package]]
name = "flate2"
version = "1.0.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46303f565772937ffe1d394a4fac6f411c6013172fadde9dcdb1e147a086940e"
dependencies = [
"crc32fast",
"miniz_oxide",
]
[[package]]
name = "flume"
version = "0.11.0"
@ -518,6 +596,7 @@ name = "fossil"
version = "0.1.0"
dependencies = [
"base64",
"image",
"rand",
"rand_hc",
"regex",
@ -659,6 +738,16 @@ dependencies = [
"polyval",
]
[[package]]
name = "gif"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80792593675e051cf94a4b111980da2ba60d4a83e43e0048c5693baab3977045"
dependencies = [
"color_quant",
"weezl",
]
[[package]]
name = "gimli"
version = "0.28.0"
@ -690,6 +779,16 @@ dependencies = [
"tracing",
]
[[package]]
name = "half"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc52e53916c08643f1b56ec082790d1e86a32e58dc5268f897f313fbae7b4872"
dependencies = [
"cfg-if",
"crunchy",
]
[[package]]
name = "hashbrown"
version = "0.12.3"
@ -831,6 +930,24 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "image"
version = "0.24.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "034bbe799d1909622a74d1193aa50147769440040ff36cb2baa947609b0a4e23"
dependencies = [
"bytemuck",
"byteorder",
"color_quant",
"exr",
"gif",
"jpeg-decoder",
"num-traits",
"png",
"qoi",
"tiff",
]
[[package]]
name = "indexmap"
version = "1.9.3"
@ -893,6 +1010,15 @@ version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
[[package]]
name = "jpeg-decoder"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5d4a7da358eff58addd2877a45865158f0d78c911d43a5784ceb7bbf52833b0"
dependencies = [
"rayon",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
@ -902,6 +1028,12 @@ dependencies = [
"spin 0.5.2",
]
[[package]]
name = "lebe"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03087c2bad5e1034e8cace5926dec053fb3790248370865f5117a7d0213354c8"
[[package]]
name = "libc"
version = "0.2.150"
@ -1006,6 +1138,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7"
dependencies = [
"adler",
"simd-adler32",
]
[[package]]
@ -1261,6 +1394,19 @@ version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
[[package]]
name = "png"
version = "0.17.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06e4b0d3d1312775e782c86c91a111aa1f910cbb65e1337f9975b5f9a554b5e1"
dependencies = [
"bitflags 1.3.2",
"crc32fast",
"fdeflate",
"flate2",
"miniz_oxide",
]
[[package]]
name = "polyval"
version = "0.6.1"
@ -1307,6 +1453,15 @@ dependencies = [
"yansi",
]
[[package]]
name = "qoi"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f6d64c71eb498fe9eae14ce4ec935c555749aef511cca85b5568910d6e48001"
dependencies = [
"bytemuck",
]
[[package]]
name = "quote"
version = "1.0.33"
@ -1355,6 +1510,26 @@ dependencies = [
"rand_core",
]
[[package]]
name = "rayon"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa7237101a77a10773db45d62004a272517633fbcc3df19d96455ede1122e051"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]]
name = "redox_syscall"
version = "0.4.1"
@ -1752,6 +1927,12 @@ dependencies = [
"rand_core",
]
[[package]]
name = "simd-adler32"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe"
[[package]]
name = "slab"
version = "0.4.9"
@ -2123,6 +2304,17 @@ dependencies = [
"once_cell",
]
[[package]]
name = "tiff"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba1310fcea54c6a9a4fd1aad794ecc02c31682f6bfbecdf460bf19533eed1e3e"
dependencies = [
"flate2",
"jpeg-decoder",
"weezl",
]
[[package]]
name = "time"
version = "0.3.30"
@ -2474,6 +2666,12 @@ version = "0.25.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1778a42e8b3b90bff8d0f5032bf22250792889a5cdc752aa0020c84abe3aaf10"
[[package]]
name = "weezl"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082"
[[package]]
name = "whoami"
version = "1.4.1"
@ -2686,3 +2884,12 @@ name = "zeroize"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "525b4ec142c6b68a2d10f01f7bbf6755599ca3f81ea53b8431b7dd348f5fdb2d"
[[package]]
name = "zune-inflate"
version = "0.2.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73ab332fe2f6680068f3582b16a24f90ad7096d5d39b974d1c0aff0125116f02"
dependencies = [
"simd-adler32",
]

View File

@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
base64 = "0.21.7"
image = "0.24.8"
rand = "0.8.5"
rand_hc = "0.3.2"
regex = "1.10.3"

View File

@ -29,6 +29,9 @@ services:
- type: bind
source: ./web
target: /srv/web
- type: bind
source: ./images
target: /srv/images
# The commented out section below is an example of how to define a PostgreSQL
# database that your application can use. `depends_on` tells Docker Compose to
# start the database before your application. The `db-data` volume persists the

View File

@ -1,6 +1,7 @@
use fossil::StatusTypes;
use image::io::Reader;
use rocket::fairing::AdHoc;
use rocket::fs::FileServer;
use rocket::fs::{FileServer, NamedFile};
use rocket::http::Status;
use rocket::response::content::{self, RawHtml};
use rocket::serde::Serialize;
@ -9,6 +10,7 @@ use rocket_db_pools::sqlx::pool::PoolConnection;
use rocket_db_pools::sqlx::Postgres;
use rocket_db_pools::Connection;
use std::fs;
use std::path::Path;
#[macro_use]
extern crate rocket;
@ -24,12 +26,12 @@ use fossil::tables::{Db, Post, User};
#[get("/login")]
fn login_page() -> RawHtml<String> {
content::RawHtml(fs::read_to_string("/srv/web/login.html").unwrap())
RawHtml(fs::read_to_string("/srv/web/login.html").unwrap())
}
#[get("/createuser")]
fn createuser_page() -> RawHtml<String> {
content::RawHtml(fs::read_to_string("/srv/web/createuser.html").unwrap())
RawHtml(fs::read_to_string("/srv/web/createuser.html").unwrap())
}
#[derive(Deserialize)]
@ -272,6 +274,42 @@ async fn toggleperms(
}
}
#[get("/images/<image>")]
async fn get_image(image: String) -> Result<NamedFile, Status> {
let mut split = image.split('.').collect::<Vec<&str>>();
let format = split.pop().unwrap();
let image = split.join(".");
match Reader::open(format!("/srv/images/{image}.png")) {
Ok(i) => {
match i.decode() {
Ok(img) => {
let encoder = match format {
"jpg" => image::ImageFormat::Jpeg,
"jpeg" => image::ImageFormat::Jpeg,
"png" => image::ImageFormat::Png,
_ => {panic!("invalid format")}
};
img.save_with_format(format!("/tmp/{image}.{format}"), encoder).unwrap();
let file = NamedFile::open(Path::new(&format!("/tmp/{image}.{format}"))).await.unwrap();
fs::remove_file(format!("/tmp/{image}.{format}")).unwrap_or(());
Ok(file)
},
Err(why) => {
Err(match &why.to_string()[..] {
"Format error decoding Png: Invalid PNG signature." => {
Status::NotAcceptable
},
_ => Status::InternalServerError
})
}
}
},
Err(_) => Err(Status::NotFound)
}
}
#[catch(default)]
fn default_catcher(status: Status, _: &Request) -> RawHtml<String> {
content::RawHtml(
@ -349,7 +387,7 @@ async fn migrate(rocket: Rocket<Build>) -> Rocket<Build> {
let _ = conn.execute(sqlx::query(
"ALTER TABLE images
DELETE COLUMN IF EXISTS image"
DROP COLUMN IF EXISTS image"
))
.await;
@ -364,7 +402,7 @@ async fn main() {
.attach(AdHoc::on_ignite("DB Migrations", migrate))
.mount(
"/",
routes![login_page, login, logout, createuser, createuser_page, account, adminpanel, toggleperms],
routes![login_page, login, logout, createuser, createuser_page, account, adminpanel, toggleperms, get_image],
)
.mount("/api", routes![api_perms])
.mount("/css", FileServer::from("/srv/web/css"))