data structures

This commit is contained in:
Jamie 2021-10-01 21:10:45 -07:00
parent 7c516fbd25
commit ede01967c2
5 changed files with 149 additions and 46 deletions

View file

@ -0,0 +1,33 @@
use crate::crypto;
use chrono::NaiveDateTime;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
// -------------------------------------
// Entity: Directory
// Represents an item discovered on the filesystem
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, DeriveEntityModel, Default)]
#[sea_orm(table_name = "directories")]
// -------------------------------------
pub struct Directory {
// identity
#[sea_orm(primary_key)]
pub id: u32,
pub name: String,
pub uri String,
// calculations
pub calculated_size_in_bytes: Option<String>,
pub calculated_file_count: Option<u32>,
// ownership
pub storage_device_id: Option<u32>,
pub parent_directory_id: Option<u32>,
// date
pub date_created: DateTime<Utc>,
pub date_modified: DateTime<Utc>,
pub date_indexed: DateTime<Utc>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -1,3 +1,4 @@
use crate::crypto;
use chrono::NaiveDateTime;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
@ -12,27 +13,29 @@ pub struct Model {
// identity
#[sea_orm(primary_key)]
pub id: u32,
pub buffer_checksum: String,
// pub buffer_checksum: String,
#[sea_orm(unique)]
pub meta_checksum: String,
pub uri: String,
// date
pub date_created: Option<NaiveDateTime>,
pub date_modified: Option<NaiveDateTime>,
pub date_indexed: Option<NaiveDateTime>,
// metadata
pub name: String,
pub extension: String,
pub size_in_bytes: String,
// #[sea_orm(column_type = "Int")]
// pub encryption: crypto::Encryption,
// ownership
#[sea_orm(nullable)]
pub ipfs_id: Option<String>,
// ownership
#[sea_orm(nullable)]
pub storage_device_id: Option<u32>,
#[sea_orm(nullable)]
pub capture_device_id: Option<u32>,
#[sea_orm(nullable)]
pub parent_file_id: Option<u32>,
// date
pub date_created: Option<NaiveDateTime>,
pub date_modified: Option<NaiveDateTime>,
pub date_indexed: Option<NaiveDateTime>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]

View file

@ -1,17 +1,79 @@
CREATE TABLE IF NOT EXISTS libraries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
shared BOOLEAN,
encryption INTEGER DEFAULT 0
);
CREATE TABLE IF NOT EXISTS files (
id INTEGER PRIMARY KEY AUTOINCREMENT,
uri STRING NOT NULL,
meta_checksum STRING NOT NULL,
buffer_checksum STRING,
name STRING,
extension STRING,
size_in_bytes STRING NOT NULL,
uri TEXT NOT NULL,
meta_checksum TEXT NOT NULL UNIQUE,
buffer_checksum TEXT,
name TEXT,
extension TEXT,
size_in_bytes TEXT NOT NULL,
encryption INTEGER DEFAULT 0,
ipfs_id STRING,
ipfs_id TEXT,
date_created TEXT NOT NULL,
date_modified TEXT NOT NULL,
date_indexed TEXT NOT NULL,
storage_device_id STRING,
capture_device_id STRING,
parent_file_id STRING
library_id INTEGER NOT NULL,
storage_device_id INTEGER NOT NULL,
directory_id INTEGER,
capture_device_id INTEGER,
parent_file_id INTEGER,
FOREIGN KEY(library_id) REFERENCES libraries(id),
FOREIGN KEY(directory_id) REFERENCES directories(id),
FOREIGN KEY(parent_file_id) REFERENCES files(id),
FOREIGN KEY(storage_device_id) REFERENCES storage_devices(id),
FOREIGN KEY(capture_device_id) REFERENCES capture_devices(id)
);
CREATE TABLE IF NOT EXISTS directories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
uri TEXT NOT NULL,
encryption INTEGER DEFAULT 0,
calculated_size_in_bytes TEXT,
calculated_file_count INTEGER,
date_created TEXT NOT NULL,
date_modified TEXT NOT NULL,
date_indexed TEXT NOT NULL,
library_id INTEGER NOT NULL,
storage_device_id INTEGER,
parent_directory_id INTEGER,
FOREIGN KEY(library_id) REFERENCES libraries(id),
FOREIGN KEY(parent_directory_id) REFERENCES directories(id),
FOREIGN KEY(storage_device_id) REFERENCES storage_devices(id)
);
CREATE TABLE IF NOT EXISTS tags (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
date_created TEXT NOT NULL,
date_modified TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS tags_files (
tag_id INTEGER NOT NULL,
file_id INTEGER NOT NULL,
date_created TEXT NOT NULL,
FOREIGN KEY(tag_id) REFERENCES tags(id),
FOREIGN KEY(file_id) REFERENCES files(id)
);
CREATE TABLE IF NOT EXISTS tags_directories (
tag_id INTEGER NOT NULL,
directory_id INTEGER NOT NULL,
date_created TEXT NOT NULL,
FOREIGN KEY(tag_id) REFERENCES tags(id),
FOREIGN KEY(directory_id) REFERENCES files(id)
);
CREATE TABLE IF NOT EXISTS storage_devices (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
date_created TEXT NOT NULL,
date_modified TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS capture_devices (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
date_created TEXT NOT NULL,
date_modified TEXT NOT NULL
);

View file

@ -12,7 +12,7 @@ use crate::db::entity::file;
// Read a file from path returning the File struct
// Generates meta checksum and extracts metadata
pub async fn read_file(path: &str) -> io::Result<file::ActiveModel> {
pub async fn read_file(path: &str) -> io::Result<()> {
let db = db::connection::get_connection().await.unwrap();
let path_buff = path::PathBuf::from(path);
@ -21,39 +21,44 @@ pub async fn read_file(path: &str) -> io::Result<file::ActiveModel> {
let size = metadata.len();
let meta_checksum = checksum::create_meta_hash(path.to_owned(), size)?;
let existing_file = file::Entity::find()
let existing_files = file::Entity::find()
.filter(file::Column::MetaChecksum.contains(&meta_checksum))
.all(&db)
.await
.unwrap();
println!("Existing file found {:?}", existing_file);
if existing_files.len() == 0 {
let file = file::ActiveModel {
meta_checksum: Set(meta_checksum),
name: Set(extract_name(path_buff.file_name())),
extension: Set(extract_name(path_buff.extension())),
uri: Set(path.to_owned()),
size_in_bytes: Set(size.to_string()),
date_created: Set(Some(
time::system_time_to_date_time(metadata.created()).unwrap(),
)),
date_modified: Set(Some(
time::system_time_to_date_time(metadata.modified()).unwrap(),
)),
date_indexed: Set(Some(
time::system_time_to_date_time(metadata.modified()).unwrap(),
)),
..Default::default()
};
let file = file::ActiveModel {
meta_checksum: Set(meta_checksum),
name: Set(extract_name(path_buff.file_name())),
extension: Set(extract_name(path_buff.extension())),
uri: Set(path.to_owned()),
size_in_bytes: Set(format!("{}", size)),
date_created: Set(Some(
time::system_time_to_date_time(metadata.created()).unwrap(),
)),
date_modified: Set(Some(
time::system_time_to_date_time(metadata.modified()).unwrap(),
)),
date_indexed: Set(Some(
time::system_time_to_date_time(metadata.modified()).unwrap(),
)),
..Default::default()
};
let file = file
.save(&db)
.await
.map_err(|error| println!("Failed to read file: {}", error))
.unwrap();
let file = file
.save(&db)
.await
.map_err(|error| println!("Failed to read file: {}", error))
.unwrap();
println!("FILE: {:?}", file);
Ok(file)
Ok(())
} else {
let file = &existing_files[0];
Ok(())
}
}
// extract name from OsStr returned by PathBuff

View file

@ -15,10 +15,10 @@ use futures::executor::block_on;
use log;
fn main() {
env_logger::builder()
.filter_level(log::LevelFilter::Debug)
.is_test(true)
.init();
// env_logger::builder()
// .filter_level(log::LevelFilter::Debug)
// .is_test(true)
// .init();
let connection = db::connection::create_primary_db();
let primary_db = block_on(connection).unwrap();