add: move upload server to buckets
This commit is contained in:
parent
8116307ba0
commit
75fe720f21
83 changed files with 351 additions and 458 deletions
|
@ -70,6 +70,7 @@
|
|||
cursor: zoom-in;
|
||||
max-height: calc(100dvh - var(--padding));
|
||||
max-width: calc(100dvh - var(--padding));
|
||||
max-width: 100%;
|
||||
}
|
||||
|
||||
/* avatar/banner */
|
||||
|
|
|
@ -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) }}")
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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()")
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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}`);
|
||||
|
|
|
@ -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"))
|
||||
|
|
|
@ -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"))
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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()),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue