Skip to content

Commit

Permalink
Tags feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jaybutera committed Aug 14, 2023
1 parent d919b68 commit 0352733
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use smol::stream::StreamExt;
//use async_channel::{TryRecvError};

use crate::migrations::generate_thumbnails;
use crate::utils::{save_thumbnail, get_index_paths};
use crate::utils::{save_thumbnail, get_index_paths, get_tags_for_topic};

fn main() -> tide::Result<()> {
smol::block_on(main_async())
Expand Down Expand Up @@ -87,6 +87,7 @@ async fn main_async() -> tide::Result<()> {
app.at("/all-indexes").get(get_index_list);
app.at("/:topic/new-image").post(upload_image);
app.at("/:topic/images").get(get_image_list);
app.at("/:topic/tags").get(get_tag_list);
app.at("/thumbnail/:name").get(get_image_thumbnail);
app.at("/img/:name").get(get_image_full);
app.listen(format!("0.0.0.0:{}", port)).await?;
Expand Down Expand Up @@ -197,6 +198,13 @@ async fn get_image(path: &PathBuf) -> Result<(Vec<u8>, mime::Mime), std::io::Err
Ok((image, mime))
}

async fn get_tag_list(req: Request<ServerState>) -> tide::Result<Body> {
let topic = normalize_topic(req.param("topic")?);
let mut path = req.state().args.root_dir.clone();

let tags = get_tags_for_topic(&mut path, &topic).await?;
Ok(Body::from_json(&tags)?)
}
async fn get_image_list(req: Request<ServerState>) -> tide::Result<Body> {
let topic = normalize_topic(req.param("topic")?);
let mut path = req.state().args.root_dir.clone();
Expand Down
22 changes: 21 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::Result;
use std::path::PathBuf;
use blocking::unblock;
use smol::stream::StreamExt;
use crate::types::TopicData;
use crate::types::{TopicData, Index};
use smol::io::{AsyncReadExt, BufReader};
use image::{io::Reader, imageops::FilterType};
use tide::log;
Expand Down Expand Up @@ -102,3 +102,23 @@ pub async fn get_index_paths(root_dir: &PathBuf) -> Result<Vec<PathBuf>> {

Ok(index_files)
}

/// Search all /indexes/*.json files for the existence of the topic
use std::collections::HashSet;
pub async fn get_tags_for_topic(
root_dir: &PathBuf,
topic: &str,
) -> Result<HashSet<String>> {
let index_paths = get_index_paths(root_dir).await?;
let mut tags = HashSet::new();
for index_path in index_paths {
let mut file = smol::fs::File::open(&index_path).await?;
let mut raw_json = vec![];
file.read_to_end(&mut raw_json).await?;

let index: Index = serde_json::from_slice(&raw_json)?;
tags.extend(index.topics);
}

Ok(tags)
}
4 changes: 3 additions & 1 deletion ui/src/components/TopicSettings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

<div class="vert-new-form">
<div class="new-form">
<h1>Add Tags</h1>
<h2>This doesn't work yet...</h2>
<input bind:value={topics} type="text" class="nt-field nt-form" name="topics" placeholder="topic-1, topic-2, ..." />

<button class="nt-form submit-form" on:click={submit_index}>Create Index</button>
<button class="nt-form submit-form" on:click={submit_index}>Add Tag</button>
</div>
</div>

0 comments on commit 0352733

Please sign in to comment.