Can download uploaded images

This commit is contained in:
2023-08-07 14:53:44 +02:00
parent c6148f6562
commit 75438f4ae0
10 changed files with 166 additions and 6 deletions

View File

@ -1,4 +1,6 @@
use crate::app_config::AppConfig;
use crate::schema::{families, members, memberships, photos, users};
use crate::utils::crypt_utils::sha256;
use diesel::prelude::*;
/// User ID holder
@ -121,6 +123,28 @@ pub struct FamilyMembership {
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize, Eq, PartialEq, Hash)]
pub struct PhotoID(pub i32);
impl PhotoID {
pub fn to_signed_hash(&self) -> String {
let secret = AppConfig::get().secret();
format!(
"{}-{}",
self.0,
sha256(format!("{secret}{}{secret}", self.0).as_bytes())
)
}
pub fn from_signed_hash(hash: &str) -> Option<Self> {
let (id, _) = hash.split_once('-')?;
let id = Self(id.parse().ok()?);
if id.to_signed_hash() != hash {
return None;
}
Some(id)
}
}
#[derive(Queryable, Debug, serde::Serialize)]
pub struct Photo {
id: i32,