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]]
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",

View File

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

View File

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

View File

@ -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<Option<u32>> {
None
}
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)?))
#[derive(sqlx::Type)]
#[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>,
{
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<Option<String>>,
attachment: Unset<Option<Attachment>>,
#[serde(default)]
#[serde(deserialize_with = "deserialize_optional_field")]
attachment_name: Unset<Option<String>>,
@ -291,6 +297,14 @@ pub fn generate_uid() -> String {
.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
mod string {
use std::{fmt::Display, str::FromStr};