diff --git a/src/main.rs b/src/main.rs index 30e56a5..606c4a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -209,10 +209,8 @@ async fn get_image(path: &PathBuf) -> Result<(Vec, mime::Mime), std::io::Err async fn rm_tag_from_topic(mut req: Request) -> tide::Result { let topic = normalize_topic(req.param("topic")?); let tag = req.body_json::().await?; - let mut path = req.state().args.root_dir.clone(); - path.push(format!("{}.json", topic)); - rm_tag_for_topic(&mut path, topic, tag).await?; + rm_tag_for_topic(&req.state().args.root_dir, topic, tag).await?; Ok(Response::new(StatusCode::Ok)) } @@ -220,10 +218,8 @@ async fn rm_tag_from_topic(mut req: Request) -> tide::Result { async fn add_tag_to_topic(mut req: Request) -> tide::Result { let topic = normalize_topic(req.param("topic")?); let tag = req.body_json::().await?; - let mut path = req.state().args.root_dir.clone(); - path.push(format!("{}.json", topic)); - add_tag_for_topic(&mut path, topic, tag).await?; + add_tag_for_topic(&req.state().args.root_dir, topic, tag).await?; Ok(Response::new(StatusCode::Ok)) } diff --git a/src/utils.rs b/src/utils.rs index cafe6b4..e1f771e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -134,7 +134,7 @@ pub async fn add_tag_for_topic( let index_paths = get_index_paths(root_dir).await?; let mut tag_path = root_dir.join("indexes"); - tag_path.push(tag.clone()); + tag_path.push(format!("{}.json", tag)); // Read the index if it exists, otherwise create it let mut index = if tag_path.exists() { @@ -154,7 +154,7 @@ pub async fn add_tag_for_topic( let temp_tag_path = tag_path.with_extension(format!("{}.temp.json", topic)); let mut file = smol::fs::File::create(&temp_tag_path).await?; file.write_all(serde_json::to_string(&index)?.as_bytes()).await?; - std::fs::rename(temp_tag_path, &tag_path)?; + std::fs::rename(temp_tag_path, &tag_path.with_extension("json"))?; Ok(()) } @@ -167,7 +167,7 @@ pub async fn rm_tag_for_topic( let index_paths = get_index_paths(root_dir).await?; let mut tag_path = root_dir.join("indexes"); - tag_path.push(tag.clone()); + tag_path.push(format!("{}.json", tag)); // Read the index if it exists, otherwise fail let mut index: Index = if tag_path.exists() { @@ -181,11 +181,16 @@ pub async fn rm_tag_for_topic( index.topics.remove(&topic); - // Write to a temporary {topic}.temp.json and then rename - let temp_tag_path = tag_path.with_extension(format!("{}.temp.json", topic)); - let mut file = smol::fs::File::create(&temp_tag_path).await?; - file.write_all(serde_json::to_string(&index)?.as_bytes()).await?; - std::fs::rename(temp_tag_path, &tag_path)?; + // If topics is empty remove the index file + if index.topics.is_empty() { + smol::fs::remove_file(&tag_path).await?; + } else { + // Write to a temporary {topic}.temp.json and then rename + let temp_tag_path = tag_path.with_extension(format!("{}.temp.json", topic)); + let mut file = smol::fs::File::create(&temp_tag_path).await?; + file.write_all(serde_json::to_string(&index)?.as_bytes()).await?; + std::fs::rename(temp_tag_path, &tag_path.with_extension("json"))?; + } Ok(()) } diff --git a/ui/src/components/TopicSettings.svelte b/ui/src/components/TopicSettings.svelte index ce74a98..8991450 100644 --- a/ui/src/components/TopicSettings.svelte +++ b/ui/src/components/TopicSettings.svelte @@ -1,18 +1,49 @@ + +

Add Tags

@@ -20,19 +51,30 @@

Current Tags

    - {#await get_tags(topic)} + {#await tags}
  • loading...
  • {:then tags} - {#each tags as tag} -
  • {tag}
  • - {/each} +
    + {#each tags as tag} +
    + + {tag} + + +
    + {/each} +
    {:catch error}
  • error: {error.message}
  • {/await}
- - - +
+ + + {#if submit_result} +

{submit_result}

+ {/if} +
diff --git a/ui/src/lib/img.ts b/ui/src/lib/img.ts index f6c7990..1b68b51 100644 --- a/ui/src/lib/img.ts +++ b/ui/src/lib/img.ts @@ -1,12 +1,30 @@ // Img server address -//export const img_server: string = "http://127.0.0.1:2342"; -export const img_server: string = "https://img.smdhi.xyz:8080"; +export const img_server: string = "http://127.0.0.1:2342"; +//export const img_server: string = "https://img.smdhi.xyz:8080"; interface Index { name: string; topics: string[]; } +export async function rm_tag(topic: string, tag: string): Promise { + let response = await fetch(`${img_server}/${topic}/remove-tag`, { + method: 'POST', + body: `"${tag}"`, + }); + + return response; +} + +export async function add_tag(topic: string, tag: string): Promise { + let response = await fetch(`${img_server}/${topic}/new-tag`, { + method: 'POST', + body: `"${tag}"`, + }); + + return response; +} + export async function get_tags(topic: string): Promise { let response = await fetch(`${img_server}/${topic}/tags`);