From fcb7dda20d9e1b5b8dd03552da548921bdfae798 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Wed, 30 Nov 2022 00:21:57 -0800 Subject: [PATCH] Use timestamps in file names to fix caching issue --- src/commands/challenge.rs | 49 ++++----------------------------------- 1 file changed, 4 insertions(+), 45 deletions(-) diff --git a/src/commands/challenge.rs b/src/commands/challenge.rs index b593faa..cd37a25 100644 --- a/src/commands/challenge.rs +++ b/src/commands/challenge.rs @@ -74,6 +74,7 @@ pub async fn submit(ctx: Context<'_>, submission: serenity::Attachment) -> Resul let challenge_number = get_challenge_number(); let submission_images_dir = get_submission_images_dir(); + let timestamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_millis(); // Ensure that submission_images_dir exists let path = Path::new(&submission_images_dir); @@ -115,7 +116,7 @@ pub async fn submit(ctx: Context<'_>, submission: serenity::Attachment) -> Resul i + 1, slugify(&author.name), author.discriminator, - images.len() + 1, + timestamp, extension ); images.push(file_name.clone().into()); @@ -154,15 +155,11 @@ pub async fn submit(ctx: Context<'_>, submission: serenity::Attachment) -> Resul } requires_rebuild = true; let file_name = format!( - "{}-{}-{}{}.{}", + "{}-{}-{}-{}.{}", submission_data.len() + 1, slugify(&author.name), author.discriminator, - if images.len() == 0 { - String::from("") - } else { - format!("-{}", images.len() + 1) - }, + timestamp, extension ); images.push(file_name.clone().into()); @@ -204,7 +201,6 @@ pub async fn submit(ctx: Context<'_>, submission: serenity::Attachment) -> Resul Some(data["invite"].as_str().unwrap()) } else { None }; - let timestamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_millis(); for image in submitted_images.iter() { for (other_guild_id, data) in guild_data.iter() { let here = other_guild_id.eq(&ctx.guild_id().unwrap().as_u64().to_string()); @@ -374,43 +370,6 @@ pub async fn imagedelete(ctx: Context<'_>, number: i32) -> Result<(), Error> { submission_data.remove(i); } else { images.remove(index); - // One must rename all of the submitted images to prevent overwrites. - // Consider the following scenario: - // The submissions are ["bob-1234.png", "bob-1234-2.png"] - // Bob uses the command imageDelete 1 - // The submissions become ["bob-1234-2.png"] - // Bob makes a second submission - // The submissions become ["bob-1234-2.png", "bob-1234-2.png"] - // The original bob-1234-2.png gets overwriten, - // and both submissions end up pointing to the same image. - // In order to prevent this, images need to be renamed according to their index. - for (j, image) in images.iter_mut().enumerate() { - let old = image.as_str().unwrap(); - let from = format!("{}/{}", submission_images_dir, old); - let author = ctx.author(); - let new = format!( - "{}-{}-{}{}.{}", - i + 1, - slugify(&author.name), - author.discriminator, - if j == 0 { - String::from("") - } else { - format!("-{}", j + 1) - }, - Path::new(old).extension().unwrap().to_str().unwrap() - ); - let to = format!("{}/{}", submission_images_dir, new); - if old.eq(&new) { - // TODO: This could be made more efficient by - // automatically starting iteration just after deleted image - // since only images with later indexes will be affected - continue; - } - fs_extra::file::move_file(from, to, &fs_extra::file::CopyOptions::default()) - .unwrap(); - *image = new.into(); - } submission["images"] = images.into(); } set_submission_data(submission_data);