From e0c60e2ce3339df72cea58086e241eb935f271f8 Mon Sep 17 00:00:00 2001 From: jude Date: Sat, 11 Nov 2023 15:05:35 +0000 Subject: [PATCH] Decode attachments correctly when patching a reminder --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- web/Cargo.toml | 2 +- web/src/routes/dashboard/mod.rs | 30 ++++++++++++++++++++++-------- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ce1f037..3146c26 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2173,7 +2173,7 @@ checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" [[package]] name = "reminder-rs" -version = "1.6.47" +version = "1.6.49" dependencies = [ "base64 0.21.4", "chrono", @@ -2201,7 +2201,7 @@ dependencies = [ [[package]] name = "reminder_web" -version = "0.1.2" +version = "0.1.3" dependencies = [ "base64 0.13.1", "chrono", diff --git a/Cargo.toml b/Cargo.toml index a9296b4..5631d0f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "reminder-rs" -version = "1.6.48" +version = "1.6.49" authors = ["Jude Southworth "] edition = "2021" license = "AGPL-3.0 only" diff --git a/web/Cargo.toml b/web/Cargo.toml index 3eb2db0..f32c49e 100644 --- a/web/Cargo.toml +++ b/web/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "reminder_web" -version = "0.1.2" +version = "0.1.3" authors = ["jellywx "] edition = "2018" diff --git a/web/src/routes/dashboard/mod.rs b/web/src/routes/dashboard/mod.rs index 47ca224..bd96f60 100644 --- a/web/src/routes/dashboard/mod.rs +++ b/web/src/routes/dashboard/mod.rs @@ -4,7 +4,7 @@ use chrono::{naive::NaiveDateTime, Utc}; use rand::{rngs::OsRng, seq::IteratorRandom}; use rocket::{http::CookieJar, response::Redirect, serde::json::json}; use rocket_dyn_templates::Template; -use serde::{Deserialize, Deserializer, Serialize}; +use serde::{de, Deserialize, Deserializer, Serialize}; use serenity::{ client::Context, http::Http, @@ -51,12 +51,18 @@ fn interval_default() -> Unset> { None } -fn deserialize_optional_field<'de, T, D>(deserializer: D) -> Result>, D::Error> -where - D: Deserializer<'de>, - T: Deserialize<'de>, -{ - Ok(Some(Option::deserialize(deserializer)?)) +#[derive(sqlx::Type)] +#[sqlx(transparent)] +struct Attachment(Vec); + +impl<'de> Deserialize<'de> for Attachment { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let string = String::deserialize(deserializer)?; + Ok(Attachment(base64::decode(string).map_err(de::Error::custom)?)) + } } #[derive(Serialize, Deserialize)] @@ -195,7 +201,7 @@ pub struct PatchReminder { uid: String, #[serde(default)] #[serde(deserialize_with = "deserialize_optional_field")] - attachment: Unset>, + attachment: Unset>, #[serde(default)] #[serde(deserialize_with = "deserialize_optional_field")] attachment_name: Unset>, @@ -291,6 +297,14 @@ pub fn generate_uid() -> String { .join("") } +fn deserialize_optional_field<'de, T, D>(deserializer: D) -> Result>, D::Error> +where + D: Deserializer<'de>, + T: Deserialize<'de>, +{ + Ok(Some(Option::deserialize(deserializer)?)) +} + // https://github.com/serde-rs/json/issues/329#issuecomment-305608405 mod string { use std::{fmt::Display, str::FromStr};