add: move upload server to buckets

This commit is contained in:
trisua 2025-08-21 00:30:29 -04:00
parent 8116307ba0
commit 75fe720f21
83 changed files with 351 additions and 458 deletions

85
Cargo.lock generated
View file

@ -383,6 +383,21 @@ dependencies = [
"serde",
]
[[package]]
name = "buckets-core"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "536e476a5181a9f8a12d65be91615f036a000a1b1a2eaacde1be78be866940fd"
dependencies = [
"oiseau",
"pathbufd",
"serde",
"serde_json",
"tetratto-core 15.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"tetratto-shared 12.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.9.5",
]
[[package]]
name = "built"
version = "0.7.7"
@ -1372,7 +1387,7 @@ dependencies = [
"libc",
"percent-encoding",
"pin-project-lite",
"socket2 0.5.10",
"socket2 0.6.0",
"system-configuration",
"tokio",
"tower-service",
@ -3341,9 +3356,9 @@ dependencies = [
"serde",
"serde_json",
"tera",
"tetratto-core",
"tetratto-l10n",
"tetratto-shared",
"tetratto-core 15.0.2",
"tetratto-l10n 12.0.0",
"tetratto-shared 12.0.6",
"tokio",
"tower-http",
"tracing",
@ -3353,7 +3368,34 @@ dependencies = [
[[package]]
name = "tetratto-core"
version = "15.0.1"
version = "15.0.2"
dependencies = [
"async-recursion",
"base16ct",
"base64 0.22.1",
"bitflags 2.9.2",
"buckets-core",
"emojis",
"md-5",
"oiseau",
"paste",
"pathbufd",
"regex",
"reqwest",
"serde",
"serde_json",
"tetratto-l10n 12.0.0",
"tetratto-shared 12.0.6",
"tokio",
"toml 0.9.5",
"totp-rs",
]
[[package]]
name = "tetratto-core"
version = "15.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "605c03fac71468f57f9c47d9246300640f3f65ec9f19fb86799e10f632d3ea68"
dependencies = [
"async-recursion",
"base16ct",
@ -3368,8 +3410,8 @@ dependencies = [
"reqwest",
"serde",
"serde_json",
"tetratto-l10n",
"tetratto-shared",
"tetratto-l10n 12.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tetratto-shared 12.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio",
"toml 0.9.5",
"totp-rs",
@ -3384,6 +3426,17 @@ dependencies = [
"toml 0.9.5",
]
[[package]]
name = "tetratto-l10n"
version = "12.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d96f5e41633c757e3519efb47c9b85d00d14322c1961360e126d0ecc0ea79b86"
dependencies = [
"pathbufd",
"serde",
"toml 0.9.5",
]
[[package]]
name = "tetratto-shared"
version = "12.0.6"
@ -3400,6 +3453,24 @@ dependencies = [
"uuid 1.18.0",
]
[[package]]
name = "tetratto-shared"
version = "12.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "286290ad09be3c507f9a47d38e92b024e6fcde34dbb515113f5bdb6b926cbee3"
dependencies = [
"ammonia",
"chrono",
"hex_fmt",
"pulldown-cmark",
"rand 0.9.2",
"regex",
"serde",
"sha2",
"snowflaked",
"uuid 1.18.0",
]
[[package]]
name = "thiserror"
version = "1.0.69"

View file

@ -35,7 +35,9 @@ A `docs` directory will be generated in the same directory that you ran the `tet
You can configure your port through the `port` key of the configuration file. You can also run the server with the `PORT` environment variable, which will override whatever is set in the configuration file. You can also use the `CACHE_BREAKER` environment variable to specify a version number to be used in static asset links in order to break cache entries.
You can launch with the `LITTLEWEB=true` environment variable to start the littleweb viewer/fake DNS server. This should be used in combination with `PORT`, as well as set as the `lw_host` in your configuration file. This secondary server is required to allow users to view their littleweb projects.
You can launch with the `LITTLEWEB=true` environment variable to start the littleweb viewer/fake DNS server. This should be used in combination with `PORT`, as well as set as the `littleweb` key in the `[service_hosts]` section of your configuration file. This secondary server is required to allow users to view their littleweb projects.
You are also required to include the `buckets` key of the `[service_hosts]` section of your configuration file. This host should link to [upload server](https://trisua.com/t/buckets). Tetratto provides Buckets with the `media` directory you set (in the `dirs` section of your configuration file). The `uploads` sub-directory of your media directory is also used, and as such should be a symbolic link to the Buckets [directory](https://docs.rs/buckets-core/latest/buckets_core/struct.Config.html#structfield.directory).
## Usage (as a user)

View file

@ -70,6 +70,7 @@
cursor: zoom-in;
max-height: calc(100dvh - var(--padding));
max-width: calc(100dvh - var(--padding));
max-width: 100%;
}
/* avatar/banner */

View file

@ -421,7 +421,7 @@
(span
(str (text "general:label.could_not_find_post"))))
(text "{%- endif %} {%- endif %}"))
(text "{{ self::post_media(upload_ids=post.uploads) }} {% else %}")
(text "{{ self::post_media(upload_ids=post.uploads, bucket=\"post_media\") }} {% else %}")
(details
("class" "card tiny lowered w_full")
(summary
@ -448,7 +448,7 @@
(span
(text "Could not find original post...")))
(text "{%- endif %} {%- endif %}"))
(text "{{ self::post_media(upload_ids=post.uploads) }}")))
(text "{{ self::post_media(upload_ids=post.uploads, bucket=\"post_media\") }}")))
(text "{%- endif %} {%- endif %}")
(text "{% if poll -%} {{ self::poll(post=post, poll=poll) }} {%- endif %}")
@ -465,15 +465,15 @@
(text "{% if community and show_community and community.id != config.town_square or question %}"))
(text "{%- endif %} {%- endmacro %}")
(text "{% macro post_media(upload_ids, custom_click=false) -%} {% if upload_ids|length > 0 -%}")
(text "{% macro post_media(upload_ids, custom_click=false, bucket) -%} {% if upload_ids|length > 0 -%}")
(div
("class" "media_gallery gap_2")
(text "{% for upload in upload_ids %}")
(img
("src" "/api/v1/uploads/{{ upload }}")
("src" "{{ config.service_hosts.buckets }}/{{ bucket }}/{{ upload }}")
("data-upload-id" "{{ upload }}")
("alt" "Image upload")
("onclick" "{% if custom_click -%} {{ custom_click }} {%- else -%} trigger('ui::lightbox_open', ['/api/v1/uploads/{{ upload }}']) {%- endif %}"))
("onclick" "{% if custom_click -%} {{ custom_click }} {%- else -%} trigger('ui::lightbox_open', ['{{ config.service_hosts.buckets }}/{{ bucket }}/{{ upload }}']) {%- endif %}"))
(text "{% endfor %}"))
(text "{%- endif %} {%- endmacro %} {% macro notification(notification) -%}")
(div
@ -825,7 +825,7 @@
("id" "question_content:{{ question.id }}")
(text "{{ question.content|markdown|safe }}"))
; question drawings
(text "{{ self::post_media(upload_ids=question.drawings) }}")
(text "{{ self::post_media(upload_ids=question.drawings, bucket=\"drawings\") }}")
; asking about
(text "{% if asking_about -%}")
(text "{{ self::post(post=asking_about[1], owner=asking_about[0], secondary=not secondary, show_community=false) }}")

View file

@ -41,7 +41,7 @@
display: inline;
width: 100dvw;
height: 100dvh;
background-image: url(\"{{ config.host|safe }}/api/v1/uploads/{{ ad.upload_id }}\");
background-image: url(\"{{ config.service_hosts.buckets }}/ads/{{ ad.upload_id }}\");
background-position: center;
background-size: contain;
}

View file

@ -13,7 +13,7 @@
(str (text "economy:label.thumbnails"))))
(div
("class" "card flex flex_col gap_2")
(text "{{ components::post_media(upload_ids=product.uploads.thumbnails, custom_click=\"remove_thumbnail(event.target)\") }}")
(text "{{ components::post_media(upload_ids=product.uploads.thumbnails, custom_click=\"remove_thumbnail(event.target)\", bucket=\"product_imgs\") }}")
(text "{% if product.uploads.thumbnails|length < 4 -%}")
(button
("onclick" "add_thumbnail()")

View file

@ -4,7 +4,7 @@
(text "{% endblock %} {% block body %} {{ macros::nav(selected=\"\") }}")
(main
("class" "flex flex_col gap_2")
(text "{{ components::post_media(upload_ids=product.uploads.thumbnails) }}")
(text "{{ components::post_media(upload_ids=product.uploads.thumbnails, bucket=\"product_imgs\") }}")
(div
("class" "card flex flex_col gap_2")
(h3

View file

@ -55,7 +55,7 @@
(iframe
("id" "browser_iframe")
("frameborder" "0")
("src" "{% if path -%} {{ config.lw_host }}/api/v1/net/{{ path }}?s={{ session }} {%- endif %}"))
("src" "{% if path -%} {{ config.service_hosts.littleweb }}/api/v1/net/{{ path }}?s={{ session }} {%- endif %}"))
(style
("data-turbo-temporary" "true")
@ -124,7 +124,7 @@
// ...
console.log(\"navigate\", uri);
document.getElementById(\"browser_iframe\").src = `{{ config.lw_host|safe }}/api/v1/net/${uri}?s={{ session }}`;
document.getElementById(\"browser_iframe\").src = `{{ config.service_hosts.littleweb|safe }}/api/v1/net/${uri}?s={{ session }}`;
if (!uri.includes(\"atto://\")) {
document.getElementById(\"uri\").setAttribute(\"true_value\", `atto://${uri}`);

View file

@ -101,7 +101,7 @@
("href" "/developer")
(icon (text "code"))
(str (text "developer:label.apps")))
(text "{% if config.lw_host -%}")
(text "{% if config.service_hosts.littleweb -%}")
(button
("onclick" "document.getElementById('littleweb').showModal()")
(icon (text "globe"))

View file

@ -665,12 +665,12 @@
(span
("class" "date")
(text "{{ upload.created }}"))
(text " ({{ upload.what }})")))
(text " ({{ upload.metadata.what }})")))
(div
("class" "flex gap_2")
(button
("class" "raised small")
("onclick" "trigger('ui::lightbox_open', ['/api/v1/uploads/{{ upload.id }}'])")
("onclick" "trigger('ui::lightbox_open', ['{{ config.service_hosts.buckets }}/{{ upload.bucket }}/{{ upload.id }}'])")
(text "{{ icon \"view\" }}")
(span
(text "{{ text \"general:action.view\" }}")))
@ -694,7 +694,7 @@
("name" "alt")
("class" "w_full")
("placeholder" "Alternative text")
(text "{{ upload.alt|safe }}")))
(text "{{ upload.metadata.alt|safe }}")))
(button
(icon (text "check"))

View file

@ -1152,7 +1152,7 @@ ${option.input_element_type === "textarea" ? `${option.value}</textarea>` : ""}
self.define("lightbox_open", async (_, src) => {
document.getElementById("lightbox_img").src = src;
const data = await (await fetch(`${src}/data`)).json();
const data = await (await fetch(`${src}/json`)).json();
document
.getElementById("lightbox_img")
.setAttribute("alt", data.payload.alt);

View file

@ -42,11 +42,16 @@ pub async fn create_request(
}
let upload = match data
.create_upload(MediaUpload::new(MediaType::Webp, user.id))
.2
.create_upload(MediaUpload::new(
MediaType::Webp,
user.id,
"ads".to_string(),
))
.await
{
Ok(x) => x,
Err(e) => return Json(e.into()),
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
};
match data
@ -55,9 +60,11 @@ pub async fn create_request(
{
Ok(_) => {
// write image
if let Err(e) =
save_webp_buffer(&upload.path(&data.0.0).to_string(), file.to_vec(), None)
{
if let Err(e) = save_webp_buffer(
&upload.path(&data.2.0.0.directory).to_string(),
file.to_vec(),
None,
) {
return Json(Error::MiscError(e.to_string()).into());
}

View file

@ -63,10 +63,11 @@ pub async fn get_request(
};
let upload = data
.2
.get_upload_by_id(emoji.0.unwrap().upload_id)
.await
.unwrap();
let path = upload.path(&data.0.0);
let path = upload.path(&data.2.0.0.directory);
if !exists(&path).unwrap() {
return Err((
@ -80,7 +81,7 @@ pub async fn get_request(
}
Ok((
[("Content-Type", upload.what.mime())],
[("Content-Type", upload.metadata.what.mime())],
Body::from(read_image(path)),
))
}
@ -119,6 +120,7 @@ pub async fn create_request(
// create upload
let upload = match data
.2
.create_upload(MediaUpload::new(
if img.1 == "image/gif" {
MediaType::Gif
@ -126,11 +128,12 @@ pub async fn create_request(
MediaType::Webp
},
user.id,
"emojis".to_string(),
))
.await
{
Ok(u) => u,
Err(e) => return Json(e.into()),
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
};
// create emoji
@ -147,13 +150,15 @@ pub async fn create_request(
{
Ok(_) => {
if is_animated {
if let Err(e) = upload.write(&data.0.0, &img.0) {
if let Err(e) = upload.write(&data.2.0.0.directory, &img.0) {
return Json(Error::MiscError(e.to_string()).into());
}
} else {
if let Err(e) =
save_webp_buffer(&upload.path(&data.0.0).to_string(), img.0.to_vec(), None)
{
if let Err(e) = save_webp_buffer(
&upload.path(&data.2.0.0.directory).to_string(),
img.0.to_vec(),
None,
) {
return Json(Error::MiscError(e.to_string()).into());
}
}
@ -165,8 +170,8 @@ pub async fn create_request(
})
}
Err(e) => {
if let Err(e) = upload.remove(&data.0.0) {
return Json(e.into());
if let Err(e) = upload.remove(&data.2.0.0.directory) {
return Json(Error::MiscError(e.to_string()).into());
}
Json(e.into())

View file

@ -146,11 +146,16 @@ pub async fn create_request(
for _ in 0..images.len() {
props.uploads.push(
match data
.create_upload(MediaUpload::new(MediaType::Webp, props.owner))
.2
.create_upload(MediaUpload::new(
MediaType::Webp,
props.owner,
"post_media".to_string(),
))
.await
{
Ok(u) => u.id,
Err(e) => return Json(e.into()),
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
},
);
}
@ -164,22 +169,24 @@ pub async fn create_request(
let image = match images.get(i) {
Some(img) => img,
None => {
if let Err(e) = data.delete_upload(*upload_id).await {
return Json(e.into());
if let Err(e) = data.2.delete_upload(*upload_id).await {
return Json(Error::MiscError(e.to_string()).into());
}
continue;
}
};
let upload = match data.get_upload_by_id(*upload_id).await {
let upload = match data.2.get_upload_by_id(*upload_id).await {
Ok(u) => u,
Err(e) => return Json(e.into()),
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
};
if let Err(e) =
save_webp_buffer(&upload.path(&data.0.0).to_string(), image.to_vec(), None)
{
if let Err(e) = save_webp_buffer(
&upload.path(&data.2.0.0.directory).to_string(),
image.to_vec(),
None,
) {
return Json(Error::MiscError(e.to_string()).into());
}
}

View file

@ -706,9 +706,7 @@ pub fn routes() -> Router {
delete(notes::delete_by_dir_request),
)
// uploads
.route("/uploads/{id}", get(uploads::get_request))
.route("/uploads/{id}", delete(uploads::delete_request))
.route("/uploads/{id}/data", get(uploads::get_json_request))
.route("/uploads/{id}/alt", post(uploads::update_alt_request))
// services
.route("/services", get(services::list_request))

View file

@ -368,27 +368,34 @@ pub async fn update_uploads_request(
}
let upload = match data
.create_upload(MediaUpload::new(MediaType::Webp, user.id))
.2
.create_upload(MediaUpload::new(
MediaType::Webp,
user.id,
"product_imgs".to_string(),
))
.await
{
Ok(x) => x,
Err(e) => return Json(e.into()),
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
};
product.uploads.thumbnails.push(upload.id);
// write image
if let Err(e) =
save_webp_buffer(&upload.path(&data.0.0).to_string(), file.to_vec(), None)
{
if let Err(e) = save_webp_buffer(
&upload.path(&data.2.0.0.directory).to_string(),
file.to_vec(),
None,
) {
return Json(Error::MiscError(e.to_string()).into());
}
}
ProductUploadTarget::Reward => {
// remove old
if product.uploads.reward != 0 {
if let Err(e) = data.delete_upload(product.uploads.reward).await {
return Json(e.into());
if let Err(e) = data.2.delete_upload(product.uploads.reward).await {
return Json(Error::MiscError(e.to_string()).into());
}
}
@ -403,19 +410,26 @@ pub async fn update_uploads_request(
}
let upload = match data
.create_upload(MediaUpload::new(MediaType::Webp, user.id))
.2
.create_upload(MediaUpload::new(
MediaType::Webp,
user.id,
"product_imgs".to_string(),
))
.await
{
Ok(x) => x,
Err(e) => return Json(e.into()),
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
};
product.uploads.reward = upload.id;
// write image
if let Err(e) =
save_webp_buffer(&upload.path(&data.0.0).to_string(), file.to_vec(), None)
{
if let Err(e) = save_webp_buffer(
&upload.path(&data.2.0.0.directory).to_string(),
file.to_vec(),
None,
) {
return Json(Error::MiscError(e.to_string()).into());
}
}
@ -462,8 +476,8 @@ pub async fn remove_thumbnail_request(
None => return Json(Error::GeneralNotFound("thumbnail".to_string()).into()),
};
if let Err(e) = data.delete_upload(*thumbnail).await {
return Json(e.into());
if let Err(e) = data.2.delete_upload(*thumbnail).await {
return Json(Error::MiscError(e.to_string()).into());
}
product.uploads.thumbnails.remove(req.idx);

View file

@ -1,74 +1,7 @@
use std::fs::exists;
use axum::{body::Body, extract::Path, response::IntoResponse, Extension, Json};
use axum::{extract::Path, response::IntoResponse, Extension, Json};
use crate::cookie::CookieJar;
use pathbufd::PathBufD;
use crate::{get_user_from_token, routes::api::v1::UpdateUploadAlt, State};
use super::auth::images::read_image;
use tetratto_core::model::{carp::CarpGraph, oauth, uploads::MediaType, ApiReturn, Error};
pub async fn get_request(
Path(id): Path<usize>,
Extension(data): Extension<State>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let upload = match data.get_upload_by_id(id).await {
Ok(u) => u,
Err(_) => {
return Err((
[("Content-Type", "image/svg+xml")],
Body::from(read_image(PathBufD::current().extend(&[
data.0.0.dirs.media.as_str(),
"images",
"default-banner.svg",
]))),
));
}
};
let path = upload.path(&data.0.0);
if !exists(&path).unwrap() {
return Err((
[("Content-Type", "image/svg+xml")],
Body::from(read_image(PathBufD::current().extend(&[
data.0.0.dirs.media.as_str(),
"images",
"default-banner.svg",
]))),
));
}
let bytes = read_image(path);
if upload.what == MediaType::Carpgraph {
// conver to svg and return
return Ok((
[("Content-Type", "image/svg+xml".to_string())],
Body::from(CarpGraph::from_bytes(bytes).to_svg()),
));
}
Ok(([("Content-Type", upload.what.mime())], Body::from(bytes)))
}
pub async fn get_json_request(
Path(id): Path<usize>,
Extension(data): Extension<State>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let upload = match data.get_upload_by_id(id).await {
Ok(u) => u,
Err(e) => return Json(e.into()),
};
Json(ApiReturn {
ok: true,
message: "Success".to_string(),
payload: Some(upload),
})
}
use tetratto_core::model::{oauth, ApiReturn, Error};
pub async fn delete_request(
jar: CookieJar,
@ -81,13 +14,22 @@ pub async fn delete_request(
None => return Json(Error::NotAllowed.into()),
};
match data.delete_upload_checked(id, &user).await {
let upload = match data.2.get_upload_by_id(id).await {
Ok(x) => x,
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
};
if user.id != upload.owner {
return Json(Error::NotAllowed.into());
}
match data.2.delete_upload(id).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Upload deleted".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
Err(e) => Json(Error::MiscError(e.to_string()).into()),
}
}
@ -103,12 +45,22 @@ pub async fn update_alt_request(
None => return Json(Error::NotAllowed.into()),
};
match data.update_upload_alt(id, &user, &props.alt).await {
let mut upload = match data.2.get_upload_by_id(id).await {
Ok(x) => x,
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
};
if user.id != upload.owner {
return Json(Error::NotAllowed.into());
}
upload.metadata.alt = props.alt;
match data.2.update_upload_metadata(id, upload.metadata).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Upload updated".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
Err(e) => Json(Error::MiscError(e.to_string()).into()),
}
}

View file

@ -122,10 +122,17 @@ pub async fn settings_request(
Err(e) => return Err(Html(render_error(e, &jar, &data, &None).await)),
};
let uploads = match data.0.get_uploads_by_owner(profile.id, 12, req.page).await {
let uploads = match data
.0
.2
.get_uploads_by_owner(profile.id, 12, req.page)
.await
{
Ok(ua) => ua,
Err(e) => {
return Err(Html(render_error(e, &jar, &data, &None).await));
return Err(Html(
render_error(Error::MiscError(e.to_string()), &jar, &data, &None).await,
));
}
};

View file

@ -1,8 +1,9 @@
[package]
name = "tetratto-core"
description = "The core behind Tetratto"
version = "15.0.1"
version = "15.0.2"
edition = "2024"
readme = "../../README.md"
authors.workspace = true
repository.workspace = true
license.workspace = true
@ -48,3 +49,4 @@ oiseau = { version = "0.1.2", default-features = false, features = [
], optional = true }
paste = { version = "1.0.15", optional = true }
tokio = { version = "1.47.1", features = ["macros", "rt-multi-thread"] }
buckets-core = "1.0.1"

View file

@ -257,6 +257,24 @@ impl Default for ManualsConfig {
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct ServiceHostsConfig {
/// Buckets host <https://trisua.com/t/buckets>.
pub buckets: String,
/// Littleweb browser host.
#[serde(default)]
pub littleweb: String,
}
impl Default for ServiceHostsConfig {
fn default() -> Self {
Self {
buckets: String::new(),
littleweb: String::new(),
}
}
}
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
pub enum StringBan {
/// An exact string.
@ -298,10 +316,9 @@ pub struct Config {
/// so this host should be included in there as well.
#[serde(default = "default_host")]
pub host: String,
/// The main public host of the littleweb server. **Not** used to check against banned hosts,
/// so this host should be included in there as well.
#[serde(default = "default_lw_host")]
pub lw_host: String,
/// The main public host of the required microservices.
#[serde(default = "default_service_hosts")]
pub service_hosts: ServiceHostsConfig,
/// Database security.
#[serde(default = "default_security")]
pub security: SecurityConfig,
@ -382,8 +399,8 @@ fn default_host() -> String {
String::new()
}
fn default_lw_host() -> String {
String::new()
fn default_service_hosts() -> ServiceHostsConfig {
ServiceHostsConfig::default()
}
fn default_security() -> SecurityConfig {
@ -459,7 +476,7 @@ impl Default for Config {
port: default_port(),
banned_hosts: default_banned_hosts(),
host: default_host(),
lw_host: default_lw_host(),
service_hosts: default_service_hosts(),
database: default_database(),
security: default_security(),
dirs: default_dirs(),

View file

@ -143,7 +143,9 @@ impl DataManager {
}
// remove upload
self.delete_upload(ad.upload_id).await?;
if let Err(e) = self.2.delete_upload(ad.upload_id).await {
return Err(Error::MiscError(e.to_string()));
}
// ...
let conn = match self.0.connect().await {

View file

@ -648,8 +648,13 @@ impl DataManager {
}
// delete uploads
for upload in self.get_uploads_by_owner_all(user.id).await? {
self.delete_upload(upload.id).await?;
for upload in match self.2.get_uploads_by_owner_all(user.id).await {
Ok(x) => x,
Err(e) => return Err(Error::MiscError(e.to_string())),
} {
if let Err(e) = self.2.delete_upload(upload.id).await {
return Err(Error::MiscError(e.to_string()));
}
}
// delete polls

View file

@ -28,7 +28,6 @@ impl DataManager {
execute!(&conn, common::CREATE_TABLE_IPBLOCKS).unwrap();
execute!(&conn, common::CREATE_TABLE_CHANNELS).unwrap();
execute!(&conn, common::CREATE_TABLE_MESSAGES).unwrap();
execute!(&conn, common::CREATE_TABLE_UPLOADS).unwrap();
execute!(&conn, common::CREATE_TABLE_EMOJIS).unwrap();
execute!(&conn, common::CREATE_TABLE_STACKS).unwrap();
execute!(&conn, common::CREATE_TABLE_DRAFTS).unwrap();
@ -61,6 +60,7 @@ impl DataManager {
.set("atto.active_connections:chats".to_string(), "0".to_string())
.await;
self.2.init().await.expect("failed to init buckets manager");
Ok(())
}

View file

@ -16,7 +16,6 @@ pub const CREATE_TABLE_QUESTIONS: &str = include_str!("./sql/create_questions.sq
pub const CREATE_TABLE_IPBLOCKS: &str = include_str!("./sql/create_ipblocks.sql");
pub const CREATE_TABLE_CHANNELS: &str = include_str!("./sql/create_channels.sql");
pub const CREATE_TABLE_MESSAGES: &str = include_str!("./sql/create_messages.sql");
pub const CREATE_TABLE_UPLOADS: &str = include_str!("./sql/create_uploads.sql");
pub const CREATE_TABLE_EMOJIS: &str = include_str!("./sql/create_emojis.sql");
pub const CREATE_TABLE_STACKS: &str = include_str!("./sql/create_stacks.sql");
pub const CREATE_TABLE_DRAFTS: &str = include_str!("./sql/create_drafts.sql");

View file

@ -4,13 +4,29 @@ use std::collections::HashMap;
use tetratto_l10n::{read_langs, LangFile};
use oiseau::postgres::{DataManager as OiseauManager, Result};
use crate::config::Config;
use buckets_core::{DataManager as BucketsManager, Config as BucketsConfig};
#[derive(Clone)]
pub struct DataManager(pub OiseauManager<Config>, pub HashMap<String, LangFile>);
pub struct DataManager(
pub OiseauManager<Config>,
pub HashMap<String, LangFile>,
pub BucketsManager,
);
impl DataManager {
/// Create a new [`DataManager`].
pub async fn new(config: Config) -> Result<Self> {
Ok(Self(OiseauManager::new(config).await?, read_langs()))
let buckets_manager = BucketsManager::new(BucketsConfig {
directory: format!("{}/{}", config.dirs.media, "uploads"),
database: config.database.clone(),
})
.await
.expect("failed to create buckets manager");
Ok(Self(
OiseauManager::new(config).await?,
read_langs(),
buckets_manager,
))
}
}

View file

@ -1,7 +0,0 @@
CREATE TABLE IF NOT EXISTS uploads (
id BIGINT NOT NULL PRIMARY KEY,
created BIGINT NOT NULL,
owner BIGINT NOT NULL,
what TEXT NOT NULL,
alt TEXT NOT NULL
)

View file

@ -194,7 +194,9 @@ impl DataManager {
}
// delete upload
self.delete_upload(emoji.upload_id).await?;
if let Err(e) = self.2.delete_upload(emoji.upload_id).await {
return Err(Error::MiscError(e.to_string()));
}
// ...
self.0.1.remove(format!("atto.emoji:{}", id)).await;

View file

@ -33,7 +33,6 @@ mod services;
mod stackblocks;
mod stacks;
mod transfers;
mod uploads;
mod user_warnings;
mod userblocks;
mod userfollows;

View file

@ -2262,7 +2262,9 @@ impl DataManager {
// delete uploads
for upload in y.uploads {
self.delete_upload(upload).await?;
if let Err(e) = self.2.delete_upload(upload).await {
return Err(Error::MiscError(e.to_string()));
}
}
// remove poll
@ -2356,7 +2358,9 @@ impl DataManager {
// delete uploads
for upload in y.uploads {
self.delete_upload(upload).await?;
if let Err(e) = self.2.delete_upload(upload).await {
return Err(Error::MiscError(e.to_string()));
}
}
// delete question (if not global question)

View file

@ -252,11 +252,15 @@ If your product is a purchase of goods or services, please be sure to fulfill th
// remove uploads
for upload in product.uploads.thumbnails {
self.delete_upload(upload).await?;
if let Err(e) = self.2.delete_upload(upload).await {
return Err(Error::MiscError(e.to_string()));
};
}
if product.uploads.reward != 0 {
self.delete_upload(product.uploads.reward).await?;
if let Err(e) = self.2.delete_upload(product.uploads.reward).await {
return Err(Error::MiscError(e.to_string()));
}
}
// ...

View file

@ -4,7 +4,7 @@ use tetratto_shared::unix_epoch_timestamp;
use crate::model::addr::RemoteAddr;
use crate::model::communities::Post;
use crate::model::communities_permissions::CommunityPermission;
use crate::model::uploads::{MediaType, MediaUpload};
use buckets_core::model::{MediaType, MediaUpload};
use crate::model::{
Error, Result,
communities::Question,
@ -463,9 +463,18 @@ impl DataManager {
for _ in 0..drawings.len() {
data.drawings.push(
self.create_upload(MediaUpload::new(MediaType::Carpgraph, data.owner))
.await?
.id,
match self
.2
.create_upload(MediaUpload::new(
MediaType::Carpgraph,
data.owner,
"drawings".to_string(),
))
.await
{
Ok(x) => x.id,
Err(_) => continue,
},
);
}
@ -516,14 +525,23 @@ impl DataManager {
let drawing = match drawings.get(i) {
Some(d) => d,
None => {
self.delete_upload(*drawing_id).await?;
if let Err(e) = self.2.delete_upload(*drawing_id).await {
return Err(Error::MiscError(e.to_string()));
}
continue;
}
};
let upload = self.get_upload_by_id(*drawing_id).await?;
let upload = match self.2.get_upload_by_id(*drawing_id).await {
Ok(x) => x,
Err(e) => return Err(Error::MiscError(e.to_string())),
};
if let Err(e) = std::fs::write(&upload.path(&self.0.0).to_string(), drawing.to_vec()) {
if let Err(e) = std::fs::write(
&upload.path(&self.2.0.0.directory).to_string(),
drawing.to_vec(),
) {
return Err(Error::MiscError(e.to_string()));
}
}
@ -595,7 +613,9 @@ impl DataManager {
// delete uploads
for upload in y.drawings {
self.delete_upload(upload).await?;
if let Err(e) = self.2.delete_upload(upload).await {
return Err(Error::MiscError(e.to_string()));
}
}
// return

View file

@ -1,194 +0,0 @@
use oiseau::cache::Cache;
use crate::model::auth::User;
use crate::model::permissions::FinePermission;
use crate::model::{Error, Result, uploads::MediaUpload};
use crate::{auto_method, DataManager};
use oiseau::PostgresRow;
use oiseau::{execute, get, query_rows, params};
impl DataManager {
/// Get a [`MediaUpload`] from an SQL row.
pub(crate) fn get_upload_from_row(x: &PostgresRow) -> MediaUpload {
MediaUpload {
id: get!(x->0(i64)) as usize,
created: get!(x->1(i64)) as usize,
owner: get!(x->2(i64)) as usize,
what: serde_json::from_str(&get!(x->3(String))).unwrap(),
alt: get!(x->4(String)),
}
}
auto_method!(get_upload_by_id(usize as i64)@get_upload_from_row -> "SELECT * FROM uploads WHERE id = $1" --name="upload" --returns=MediaUpload --cache-key-tmpl="atto.upload:{}");
/// Get all uploads (paginated).
///
/// # Arguments
/// * `batch` - the limit of items in each page
/// * `page` - the page number
pub async fn get_uploads(&self, batch: usize, page: usize) -> Result<Vec<MediaUpload>> {
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_rows!(
&conn,
"SELECT * FROM uploads ORDER BY created DESC LIMIT $1 OFFSET $2",
&[&(batch as i64), &((page * batch) as i64)],
|x| { Self::get_upload_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("upload".to_string()));
}
Ok(res.unwrap())
}
/// Get all uploads by their owner (paginated).
///
/// # Arguments
/// * `owner` - the ID of the owner of the upload
/// * `batch` - the limit of items in each page
/// * `page` - the page number
pub async fn get_uploads_by_owner(
&self,
owner: usize,
batch: usize,
page: usize,
) -> Result<Vec<MediaUpload>> {
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_rows!(
&conn,
"SELECT * FROM uploads WHERE owner = $1 ORDER BY created DESC LIMIT $2 OFFSET $3",
&[&(owner as i64), &(batch as i64), &((page * batch) as i64)],
|x| { Self::get_upload_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("upload".to_string()));
}
Ok(res.unwrap())
}
/// Get all uploads by their owner.
///
/// # Arguments
/// * `owner` - the ID of the owner of the upload
pub async fn get_uploads_by_owner_all(&self, owner: usize) -> Result<Vec<MediaUpload>> {
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_rows!(
&conn,
"SELECT * FROM uploads WHERE owner = $1 ORDER BY created DESC",
&[&(owner as i64)],
|x| { Self::get_upload_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("upload".to_string()));
}
Ok(res.unwrap())
}
/// Create a new upload in the database.
///
/// # Arguments
/// * `data` - a mock [`MediaUpload`] object to insert
pub async fn create_upload(&self, data: MediaUpload) -> Result<MediaUpload> {
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(
&conn,
"INSERT INTO uploads VALUES ($1, $2, $3, $4, $5)",
params![
&(data.id as i64),
&(data.created as i64),
&(data.owner as i64),
&serde_json::to_string(&data.what).unwrap().as_str(),
&data.alt,
]
);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
// return
Ok(data)
}
pub async fn delete_upload(&self, id: usize) -> Result<()> {
// if !user.permissions.check(FinePermission::MANAGE_UPLOADS) {
// return Err(Error::NotAllowed);
// }
// delete file
// it's most important that the file gets off the file system first, even
// if there's an issue in the database
//
// the actual file takes up much more space than the database entry.
let upload = self.get_upload_by_id(id).await?;
upload.remove(&self.0.0)?;
// delete from database
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(&conn, "DELETE FROM uploads WHERE id = $1", &[&(id as i64)]);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
self.0.1.remove(format!("atto.upload:{}", id)).await;
// return
Ok(())
}
pub async fn delete_upload_checked(&self, id: usize, user: &User) -> Result<()> {
let upload = self.get_upload_by_id(id).await?;
// check user permission
if user.id != upload.owner && !user.permissions.check(FinePermission::MANAGE_UPLOADS) {
return Err(Error::NotAllowed);
}
// delete file
upload.remove(&self.0.0)?;
// ...
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(&conn, "DELETE FROM uploads WHERE id = $1", &[&(id as i64)]);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
self.0.1.remove(format!("atto.upload:{}", id)).await;
Ok(())
}
auto_method!(update_upload_alt(&str)@get_upload_by_id:FinePermission::MANAGE_UPLOADS; -> "UPDATE uploads SET alt = $1 WHERE id = $2" --cache-key-tmpl="atto.upload:{}");
}

View file

@ -19,7 +19,6 @@ pub mod stacks;
pub mod uploads;
use std::fmt::Display;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]

View file

@ -1,93 +1,6 @@
use pathbufd::PathBufD;
use serde::{Serialize, Deserialize};
use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp};
use crate::config::Config;
use std::fs::{write, exists, remove_file};
use super::{Error, Result};
#[derive(Serialize, Deserialize, PartialEq, Eq)]
pub enum MediaType {
#[serde(alias = "image/webp")]
Webp,
#[serde(alias = "image/avif")]
Avif,
#[serde(alias = "image/png")]
Png,
#[serde(alias = "image/jpg")]
Jpg,
#[serde(alias = "image/gif")]
Gif,
#[serde(alias = "image/carpgraph")]
Carpgraph,
}
impl MediaType {
pub fn extension(&self) -> &str {
match self {
Self::Webp => "webp",
Self::Avif => "avif",
Self::Png => "png",
Self::Jpg => "jpg",
Self::Gif => "gif",
Self::Carpgraph => "carpgraph",
}
}
pub fn mime(&self) -> String {
format!("image/{}", self.extension())
}
}
#[derive(Serialize, Deserialize)]
pub struct MediaUpload {
pub id: usize,
pub created: usize,
pub owner: usize,
pub what: MediaType,
pub alt: String,
}
impl MediaUpload {
/// Create a new [`MediaUpload`].
pub fn new(what: MediaType, owner: usize) -> Self {
Self {
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
created: unix_epoch_timestamp(),
owner,
what,
alt: String::new(),
}
}
/// Get the path to the fs file for this upload.
pub fn path(&self, config: &Config) -> PathBufD {
PathBufD::current()
.extend(&[config.dirs.media.as_str(), "uploads"])
.join(format!("{}.{}", self.id, self.what.extension()))
}
/// Write to this upload in the file system.
pub fn write(&self, config: &Config, bytes: &[u8]) -> Result<()> {
match write(self.path(config), bytes) {
Ok(_) => Ok(()),
Err(e) => Err(Error::MiscError(e.to_string())),
}
}
/// Delete this upload in the file system.
pub fn remove(&self, config: &Config) -> Result<()> {
let path = self.path(config);
if !exists(&path).unwrap() {
return Ok(());
}
match remove_file(path) {
Ok(_) => Ok(()),
Err(e) => Err(Error::MiscError(e.to_string())),
}
}
}
pub use buckets_core::model::*;
#[derive(Serialize, Deserialize)]
pub struct CustomEmoji {

View file

@ -3,6 +3,7 @@ name = "tetratto-l10n"
description = "Localization for Tetratto"
version = "12.0.0"
edition = "2024"
readme = "../../README.md"
authors.workspace = true
repository.workspace = true
license.workspace = true

View file

@ -3,6 +3,7 @@ name = "tetratto-shared"
description = "Shared stuff for Tetratto"
version = "12.0.6"
edition = "2024"
readme = "../../README.md"
authors.workspace = true
repository.workspace = true
license.workspace = true

View file

@ -4,7 +4,6 @@ color = "#c9b1bc"
port = 4118
banned_hosts = []
host = "http://localhost:4118"
lw_host = "http://localhost:4119"
no_track = []
banned_usernames = [
"admin",
@ -22,6 +21,10 @@ town_square = 166340372315581657
html_footer_path = "public/footer.html"
system_user = 211903918383300608
[service_hosts]
buckets = "http://localhost:8020"
littleweb = "http://localhost:4119"
[security]
registration_enabled = true
real_ip_header = "CF-Connecting-IP"

View file

@ -0,0 +1,43 @@
import postgres from "npm:postgres";
import { parse } from "npm:smol-toml";
const config = parse(await Deno.readTextFile(Deno.cwd() + "/tetratto.toml"), {
integersAsBigInt: true,
});
const db = postgres({
user: config.database.user,
password: config.database.password,
database: config.database.name,
hostname: config.database.url.split(":")[0],
port: config.database.url.split(":")[1],
});
const whats = {};
const alts = {};
for (const row of await db`SELECT * FROM uploads;`) {
whats[row.id] = row.what.replaceAll('"', "");
alts[row.id] = row.alt;
}
await db`ALTER TABLE uploads DROP COLUMN IF EXISTS what;`;
await db`ALTER TABLE uploads DROP COLUMN IF EXISTS alt;`;
await db`ALTER TABLE uploads ADD COLUMN IF NOT EXISTS bucket TEXT NOT NULL DEFAULT '';`;
await db`ALTER TABLE uploads ADD COLUMN IF NOT EXISTS metadata TEXT NOT NULL DEFAULT '';`;
let i = 0;
for (const row of await db`SELECT * FROM uploads;`) {
await db`DELETE FROM uploads WHERE id = ${BigInt(row.id)}`;
await db`INSERT INTO uploads VALUES (${BigInt(row.id)}, ${BigInt(row.created)}, ${BigInt(row.owner)}, DEFAULT, ${JSON.stringify(
{
what: whats[row.id],
alt: whats[row.alt],
},
)});`;
i += 1;
console.log(`done ${i}`);
}
await db.end();