Skip to content

Commit

Permalink
create dir if doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
jaybutera committed Aug 15, 2023
1 parent 0c90c1d commit 94a288d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 26 deletions.
26 changes: 0 additions & 26 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ async fn main_async() -> tide::Result<()> {
.allow_credentials(false);
app.with(cors);

app.at("/new-index").post(create_index);
app.at("/index/:name").get(get_index);
app.at("/all-indexes").get(get_index_list);
app.at("/:topic/new-image").post(upload_image);
Expand Down Expand Up @@ -139,31 +138,6 @@ async fn get_index(req: Request<ServerState>) -> tide::Result {
Ok(res)
}

/// Request contains a json file for the index which is saved into /indexes/<name>.json
async fn create_index(mut req: Request<ServerState>) -> tide::Result {
let index: Index = req.body_json().await?;
let name = normalize_topic(&index.name);
let mut path = req.state().args.root_dir.clone();
path.push("indexes");

if !path.exists() {
smol::fs::create_dir_all(&path).await?;
}

path.push(format!("{}.json", name));
if path.exists() {
return Err(to_badreq(anyhow!("Index already exists")));
}

// write index json file
let index_str = serde_json::to_string(&index)?;
let mut file = smol::fs::File::create(path).await?;
file.write_all(index_str.as_bytes()).await?;

Ok(Response::new(StatusCode::Ok))

}

async fn get_image_full(req: Request<ServerState>) -> tide::Result {
let name = req.param("name")?;
let mut path = req.state().args.root_dir.clone();
Expand Down
6 changes: 6 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ pub async fn add_tag_for_topic(
) -> Result<()> {
let index_paths = get_index_paths(root_dir).await?;

// If root_dir/indexes directory doesnt exist, create it
let mut tag_path = root_dir.join("indexes");
if !tag_path.exists() {
smol::fs::create_dir(&tag_path).await?;
}

let mut tag_path = root_dir.join("indexes");
tag_path.push(format!("{}.json", tag));

Expand Down

0 comments on commit 94a288d

Please sign in to comment.