Skip to content

Commit

Permalink
/admin/gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
GitHub committed Jan 29, 2024
1 parent 164467e commit b0d7a13
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 36 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/app_router.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
config::CONFIG,
controller::{
admin::{admin, admin_post, admin_view},
admin::{admin, admin_gallery, admin_post, admin_view},
feed::{feed, feed_add, feed_add_post, feed_read, feed_star, feed_subscribe, feed_update},
inn::{
comment_delete, comment_downvote, comment_hide, comment_post, comment_upvote,
Expand Down Expand Up @@ -66,6 +66,7 @@ pub async fn router() -> Router {
.route("/notification", get(notification))
.route("/admin", get(admin).post(admin_post))
.route("/admin/view", get(admin_view))
.route("/admin/gallery", get(admin_gallery))
.route("/mod/:iid", get(mod_inn).post(mod_inn_post))
.route("/mod/feed/:iid", get(mod_inn).post(mod_feed_post))
.route(
Expand Down Expand Up @@ -98,7 +99,7 @@ pub async fn router() -> Router {
"/upload",
get(upload).post(upload_post.layer(DefaultBodyLimit::max(UPLOAD_LIMIT))),
)
.route("/gallery", get(gallery))
.route("/gallery/:uid", get(gallery))
.route("/feed/:uid", get(feed))
.route("/feed/add", get(feed_add).post(feed_add_post))
.route("/feed/update", get(feed_update))
Expand Down
66 changes: 64 additions & 2 deletions src/controller/admin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::{
db_utils::{ivec_to_u32, set_one_with_key, u8_slice_to_u32, IterType},
db_utils::{get_count, get_range, ivec_to_u32, set_one_with_key, u8_slice_to_u32, IterType},
fmt::{clean_html, ts_to_date},
meta_handler::{into_response, PageData, ValidatedForm},
inn::ParamsTag,
meta_handler::{into_response, PageData, ParamsPage, ValidatedForm},
user::Role,
Claim, Feed, FormPost, Item, SiteConfig,
};
Expand Down Expand Up @@ -379,3 +380,64 @@ impl Default for SiteConfig {
}
}
}

/// Page data: `admin_gallery.html`
#[derive(Template)]
#[template(path = "admin_gallery.html")]
struct PageAdminGallery<'a> {
page_data: PageData<'a>,
imgs: Vec<(u32, u32, String)>,
anchor: usize,
is_desc: bool,
n: usize,
}

/// `GET /admin/gallery`
pub(crate) async fn admin_gallery(
cookie: Option<TypedHeader<Cookie>>,
Query(params): Query<ParamsTag>,
) -> Result<impl IntoResponse, AppError> {
let site_config = SiteConfig::get(&DB)?;
let cookie = cookie.ok_or(AppError::NonLogin)?;
let claim = Claim::get(&DB, &cookie, &site_config).ok_or(AppError::NonLogin)?;
if Role::from(claim.role) != Role::Admin {
return Err(AppError::Unauthorized);
}

let has_unread = User::has_unread(&DB, claim.uid)?;

let anchor = params.anchor.unwrap_or(0);
let is_desc = params.is_desc.unwrap_or(true);
let n = 12;

let count = get_count(&DB, "default", "imgs_count")?;
let mut imgs = Vec::new();
for i in &DB.open_tree("user_uploads")? {
let (k, v) = i?;
let uid = u8_slice_to_u32(&k[0..4]);
let img_id = u8_slice_to_u32(&k[4..8]);
let img = String::from_utf8_lossy(&v).to_string();
imgs.push((uid, img_id, img));
}

imgs.sort_unstable_by(|a, b| a.1.cmp(&b.1));

let page_params = ParamsPage { anchor, n, is_desc };
let (start, end) = get_range(count, &page_params);

let mut imgs = imgs[(start - 1)..=(end - 1)].to_vec();
if is_desc {
imgs.reverse();
}

let page_data = PageData::new("Admin gallery", &site_config, Some(claim), has_unread);
let page_gallery = PageAdminGallery {
page_data,
imgs,
anchor,
is_desc,
n,
};

Ok(into_response(&page_gallery))
}
53 changes: 38 additions & 15 deletions src/controller/upload.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use super::{
incr_id, inn::ParamsTag, into_response, meta_handler::PageData, u32_to_ivec, user::InnRole,
db_utils::IterType,
incr_id,
inn::ParamsTag,
into_response,
meta_handler::PageData,
u32_to_ivec,
user::{InnRole, Role},
Claim, SiteConfig, User,
};
use crate::{config::CONFIG, error::AppError, DB};
use askama::Template;
use axum::{
extract::{Multipart, Query},
extract::{Multipart, Path, Query},
response::{IntoResponse, Redirect},
};
use axum_extra::{headers::Cookie, TypedHeader};
Expand Down Expand Up @@ -80,27 +86,38 @@ struct PageGallery<'a> {
imgs: Vec<String>,
anchor: usize,
is_desc: bool,
n: usize,
uid: u32,
}

/// `GET /gallery`
/// `GET /gallery/:uid`
pub(crate) async fn gallery(
cookie: Option<TypedHeader<Cookie>>,
Path(uid): Path<u32>,
Query(params): Query<ParamsTag>,
) -> Result<impl IntoResponse, AppError> {
let cookie = cookie.ok_or(AppError::NonLogin)?;
let site_config = SiteConfig::get(&DB)?;
let claim = Claim::get(&DB, &cookie, &site_config).ok_or(AppError::NonLogin)?;
if claim.uid != uid && Role::from(claim.role) != Role::Admin {
return Err(AppError::Unauthorized);
}

let has_unread = User::has_unread(&DB, claim.uid)?;

let anchor = params.anchor.unwrap_or(0);
let is_desc = params.is_desc.unwrap_or(true);
let n = 12;

let mut imgs = Vec::with_capacity(n);
let iter = DB.open_tree("user_uploads")?.scan_prefix(u32_to_ivec(uid));
let iter = if is_desc {
IterType::Rev(iter.rev())
} else {
IterType::Iter(iter)
};

let mut imgs = Vec::new();
for (idx, i) in DB
.open_tree("user_uploads")?
.scan_prefix(u32_to_ivec(claim.uid))
.enumerate()
{
for (idx, i) in iter.enumerate() {
if idx < anchor {
continue;
}
Expand All @@ -112,21 +129,19 @@ pub(crate) async fn gallery(
imgs.push(img);
}

if imgs.len() >= 12 {
if imgs.len() >= n {
break;
}
}

if is_desc {
imgs.reverse();
}

let page_data = PageData::new("gallery", &site_config, Some(claim), has_unread);
let page_gallery = PageGallery {
page_data,
imgs,
anchor,
is_desc,
n,
uid,
};

Ok(into_response(&page_gallery))
Expand All @@ -138,6 +153,7 @@ pub(crate) async fn gallery(
struct PageUpload<'a> {
page_data: PageData<'a>,
imgs: Vec<String>,
uid: u32,
}

/// `GET /upload`
Expand All @@ -148,10 +164,12 @@ pub(crate) async fn upload(
let site_config = SiteConfig::get(&DB)?;
let claim = Claim::get(&DB, &cookie, &site_config).ok_or(AppError::NonLogin)?;
let has_unread = User::has_unread(&DB, claim.uid)?;
let uid = claim.uid;
let page_data = PageData::new("upload images", &site_config, Some(claim), has_unread);
let page_upload = PageUpload {
page_data,
imgs: vec![],
uid,
};

Ok(into_response(&page_upload))
Expand Down Expand Up @@ -260,8 +278,13 @@ pub(crate) async fn upload_post(
DB.open_tree("user_uploads")?.apply_batch(batch)?;

let has_unread = User::has_unread(&DB, claim.uid)?;
let uid = claim.uid;
let page_data = PageData::new("upload images", &site_config, Some(claim), has_unread);
let page_upload = PageUpload { page_data, imgs };
let page_upload = PageUpload {
page_data,
imgs,
uid,
};

Ok(into_response(&page_upload))
}
Expand Down
37 changes: 37 additions & 0 deletions templates/admin_gallery.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{% extends "layout.html" %}

{% block content %}
<div class="box content">
<div class="columns is-multiline">
{% for img in imgs %}
<div class="column is-one-third">
<figure>
<p class="image is-32x32 is-hidden-mobile">
<a href="/gallery/{{img.0}}"><img src="/static/avatars/{{img.0}}.png"></a>
</p>
<a href="/static/upload/{{img.2}}" target="_blank"><img src="/static/upload/{{img.2}}"></a>
<figcaption>
<code>{{img.2}}</code>
</figcaption>
</figure>
</div>
{% endfor %}
</div>
</div>

<div class="divider"></div>

<nav class="pagination">
{% if anchor < n %}
<a class="pagination-previous" disabled>Prev</a>
{% else %}
<a class="pagination-previous" href="/admin/gallery?anchor={{anchor - n}}&is_desc={{is_desc}}">Prev</a>
{% endif %}

{% if imgs.len() < n %}
<a class="pagination-next" disabled >Next</a>
{% else %}
<a class="pagination-next" href="/admin/gallery?anchor={{anchor + n}}&is_desc={{is_desc}}">Next</a>
{% endif %}
</nav>
{% endblock %}
15 changes: 10 additions & 5 deletions templates/gallery.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{% extends "layout.html" %}

{% block content %}
<div class="content">
<div class="box content">
<figure>
<p class="image is-32x32 is-hidden-mobile">
<a href="/gallery/{{uid}}"><img src="/static/avatars/{{uid}}.png"></a>
</p>
</figure>
<div class="columns is-multiline">
{% for img in imgs %}
<div class="column is-one-third">
Expand All @@ -19,16 +24,16 @@
<div class="divider"></div>

<nav class="pagination">
{% if anchor < 10 %}
{% if anchor < n %}
<a class="pagination-previous" disabled>Prev</a>
{% else %}
<a class="pagination-previous" href="/gallery?anchor={{anchor - 10}}&is_desc={{is_desc}}">Prev</a>
<a class="pagination-previous" href="/gallery/{{uid}}?anchor={{anchor - n}}&is_desc={{is_desc}}">Prev</a>
{% endif %}

{% if imgs.len() < 10 %}
{% if imgs.len() < n %}
<a class="pagination-next" disabled >Next</a>
{% else %}
<a class="pagination-next" href="/gallery?anchor={{anchor + 10}}&is_desc={{is_desc}}">Next</a>
<a class="pagination-next" href="/gallery/{{uid}}?anchor={{anchor + n}}&is_desc={{is_desc}}">Next</a>
{% endif %}
</nav>
{% endblock %}
2 changes: 1 addition & 1 deletion templates/upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% block content %}

<div class="box">
<a class="button is-success" href="/gallery" target="_blank">Gallery</a>
<a class="button is-success" href="/gallery/{{uid}}" target="_blank">Gallery</a>
</div>

<form id="upload" class="box" action="/upload" method="POST" enctype="multipart/form-data">
Expand Down

0 comments on commit b0d7a13

Please sign in to comment.