diff --git a/src-tauri/src/db/entity/dir.rs b/src-tauri/src/db/entity/dir.rs new file mode 100644 index 000000000..eb8cf6fd9 --- /dev/null +++ b/src-tauri/src/db/entity/dir.rs @@ -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, + pub calculated_file_count: Option, + // ownership + pub storage_device_id: Option, + pub parent_directory_id: Option, + // date + pub date_created: DateTime, + pub date_modified: DateTime, + pub date_indexed: DateTime, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} + +impl ActiveModelBehavior for ActiveModel {} \ No newline at end of file diff --git a/src-tauri/src/db/entity/file.rs b/src-tauri/src/db/entity/file.rs index 1404ac148..97b095be6 100644 --- a/src-tauri/src/db/entity/file.rs +++ b/src-tauri/src/db/entity/file.rs @@ -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, + pub date_modified: Option, + pub date_indexed: Option, // 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, - // ownership #[sea_orm(nullable)] pub storage_device_id: Option, #[sea_orm(nullable)] pub capture_device_id: Option, #[sea_orm(nullable)] pub parent_file_id: Option, - // date - pub date_created: Option, - pub date_modified: Option, - pub date_indexed: Option, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] diff --git a/src-tauri/src/db/migrations/primary/V1__initial.sql b/src-tauri/src/db/migrations/primary/V1__initial.sql index 30684350e..405060f7b 100644 --- a/src-tauri/src/db/migrations/primary/V1__initial.sql +++ b/src-tauri/src/db/migrations/primary/V1__initial.sql @@ -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 ); \ No newline at end of file diff --git a/src-tauri/src/filesystem/file.rs b/src-tauri/src/filesystem/file.rs index 32f29308c..ecbe8c2d5 100644 --- a/src-tauri/src/filesystem/file.rs +++ b/src-tauri/src/filesystem/file.rs @@ -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 { +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 { 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 diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 86e263e5e..a75f20676 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -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();