Decode attachments correctly when patching a reminder

This commit is contained in:
jude 2023-11-11 15:05:35 +00:00
parent e7160215b0
commit e0c60e2ce3
4 changed files with 26 additions and 12 deletions

4
Cargo.lock generated
View File

@ -2173,7 +2173,7 @@ checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da"
[[package]] [[package]]
name = "reminder-rs" name = "reminder-rs"
version = "1.6.47" version = "1.6.49"
dependencies = [ dependencies = [
"base64 0.21.4", "base64 0.21.4",
"chrono", "chrono",
@ -2201,7 +2201,7 @@ dependencies = [
[[package]] [[package]]
name = "reminder_web" name = "reminder_web"
version = "0.1.2" version = "0.1.3"
dependencies = [ dependencies = [
"base64 0.13.1", "base64 0.13.1",
"chrono", "chrono",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "reminder-rs" name = "reminder-rs"
version = "1.6.48" version = "1.6.49"
authors = ["Jude Southworth <judesouthworth@pm.me>"] authors = ["Jude Southworth <judesouthworth@pm.me>"]
edition = "2021" edition = "2021"
license = "AGPL-3.0 only" license = "AGPL-3.0 only"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "reminder_web" name = "reminder_web"
version = "0.1.2" version = "0.1.3"
authors = ["jellywx <judesouthworth@pm.me>"] authors = ["jellywx <judesouthworth@pm.me>"]
edition = "2018" edition = "2018"

View File

@ -4,7 +4,7 @@ use chrono::{naive::NaiveDateTime, Utc};
use rand::{rngs::OsRng, seq::IteratorRandom}; use rand::{rngs::OsRng, seq::IteratorRandom};
use rocket::{http::CookieJar, response::Redirect, serde::json::json}; use rocket::{http::CookieJar, response::Redirect, serde::json::json};
use rocket_dyn_templates::Template; use rocket_dyn_templates::Template;
use serde::{Deserialize, Deserializer, Serialize}; use serde::{de, Deserialize, Deserializer, Serialize};
use serenity::{ use serenity::{
client::Context, client::Context,
http::Http, http::Http,
@ -51,12 +51,18 @@ fn interval_default() -> Unset<Option<u32>> {
None None
} }
fn deserialize_optional_field<'de, T, D>(deserializer: D) -> Result<Option<Option<T>>, D::Error> #[derive(sqlx::Type)]
where #[sqlx(transparent)]
struct Attachment(Vec<u8>);
impl<'de> Deserialize<'de> for Attachment {
fn deserialize<D>(deserializer: D) -> Result<Attachment, D::Error>
where
D: Deserializer<'de>, D: Deserializer<'de>,
T: Deserialize<'de>, {
{ let string = String::deserialize(deserializer)?;
Ok(Some(Option::deserialize(deserializer)?)) Ok(Attachment(base64::decode(string).map_err(de::Error::custom)?))
}
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
@ -195,7 +201,7 @@ pub struct PatchReminder {
uid: String, uid: String,
#[serde(default)] #[serde(default)]
#[serde(deserialize_with = "deserialize_optional_field")] #[serde(deserialize_with = "deserialize_optional_field")]
attachment: Unset<Option<String>>, attachment: Unset<Option<Attachment>>,
#[serde(default)] #[serde(default)]
#[serde(deserialize_with = "deserialize_optional_field")] #[serde(deserialize_with = "deserialize_optional_field")]
attachment_name: Unset<Option<String>>, attachment_name: Unset<Option<String>>,
@ -291,6 +297,14 @@ pub fn generate_uid() -> String {
.join("") .join("")
} }
fn deserialize_optional_field<'de, T, D>(deserializer: D) -> Result<Option<Option<T>>, D::Error>
where
D: Deserializer<'de>,
T: Deserialize<'de>,
{
Ok(Some(Option::deserialize(deserializer)?))
}
// https://github.com/serde-rs/json/issues/329#issuecomment-305608405 // https://github.com/serde-rs/json/issues/329#issuecomment-305608405
mod string { mod string {
use std::{fmt::Display, str::FromStr}; use std::{fmt::Display, str::FromStr};