models::File uses Hash instead of plain Vec<u8>

feat/vaults
Tomáš Mládek 2021-02-21 19:43:28 +01:00
parent 105a9c7e0b
commit 61a470dfe3
2 changed files with 12 additions and 17 deletions

View File

@ -1,11 +1,15 @@
use actix::prelude::*;
use anyhow::{anyhow, Result};
use diesel::backend::Backend;
use diesel::deserialize::FromSql;
use diesel::sqlite::Sqlite;
use diesel::{deserialize, sql_types};
use filebuffer::FileBuffer;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use tiny_keccak::{Hasher, KangarooTwelve};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, FromSqlRow, Serialize, Deserialize)]
pub struct Hash(pub Vec<u8>);
impl AsRef<[u8]> for Hash {
@ -14,21 +18,11 @@ impl AsRef<[u8]> for Hash {
}
}
// impl Hash {
// pub fn encode(&self) -> String {
// encode(self.0.clone()).into_string()
// }
//
// pub fn decode<T: AsRef<str>>(string: T) -> Result<Hash> {
// Ok(Hash(decode(string.as_ref())?))
// }
// }
// impl std::fmt::Display for Hash {
// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// write!(f, "{}", self.encode())
// }
// }
impl FromSql<sql_types::Binary, Sqlite> for Hash {
fn from_sql(bytes: Option<&<Sqlite as Backend>::RawValue>) -> deserialize::Result<Self> {
Ok(Hash(Vec::from(not_none!(bytes).read_blob())))
}
}
pub trait Hashable {
fn hash(&self) -> Result<Hash>;

View File

@ -2,11 +2,12 @@ use chrono::NaiveDateTime;
use serde::Serialize;
use super::schema::{data, files};
use crate::hash::Hash;
#[derive(Queryable, Serialize, Debug)]
pub struct File {
pub id: i32,
pub hash: Vec<u8>,
pub hash: Hash,
pub path: String,
pub valid: bool,
pub added: NaiveDateTime,