Implement sheet path validation
This commit is contained in:
parent
0addc7c4b9
commit
4f65715967
@ -1,2 +1,2 @@
|
|||||||
CREATE TABLE IF NOT EXISTS sheets (name TEXT, path TEXT, file_size INTEGER, file_hash TEXT);
|
CREATE TABLE IF NOT EXISTS sheets (id integer primary key autoincrement, name TEXT, path TEXT, file_size INTEGER, file_hash TEXT);
|
||||||
CREATE TABLE IF NOT EXISTS composers (name TEXT, id INTEGER);
|
CREATE TABLE IF NOT EXISTS composers (id integer primary key autoincrement, name TEXT);
|
||||||
|
@ -34,16 +34,25 @@ impl Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn insert_sheet(&self, sheet: Sheet) -> sqlx::Result<()> {
|
pub async fn insert_sheet(&self, sheet: Sheet) -> sqlx::Result<()> {
|
||||||
sqlx::query!(
|
sqlx::query(
|
||||||
"
|
"
|
||||||
INSERT INTO sheets (name, path, file_size, file_hash)
|
INSERT INTO sheets (name, path, file_size, file_hash)
|
||||||
VALUES ($1, $2, $3, $4)
|
VALUES ($1, $2, $3, $4)
|
||||||
",
|
",
|
||||||
sheet.name,
|
|
||||||
sheet.path,
|
|
||||||
sheet.file_size,
|
|
||||||
sheet.file_hash,
|
|
||||||
)
|
)
|
||||||
|
.bind(sheet.name)
|
||||||
|
.bind(sheet.path)
|
||||||
|
.bind(sheet.file_size)
|
||||||
|
.bind(sheet.file_hash)
|
||||||
|
.execute(&self.connection)
|
||||||
|
.await
|
||||||
|
.map(|_| ())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_sheet_path(&self, sheet: Sheet) -> sqlx::Result<()> {
|
||||||
|
sqlx::query("UPDATE sheets SET path = $1 WHERE id = $2")
|
||||||
|
.bind(sheet.path)
|
||||||
|
.bind(sheet.id)
|
||||||
.execute(&self.connection)
|
.execute(&self.connection)
|
||||||
.await
|
.await
|
||||||
.map(|_| ())
|
.map(|_| ())
|
||||||
|
73
src/main.rs
73
src/main.rs
@ -3,16 +3,21 @@ mod mcdu;
|
|||||||
mod sheet;
|
mod sheet;
|
||||||
mod sheet_listing;
|
mod sheet_listing;
|
||||||
|
|
||||||
use std::{path::PathBuf, process};
|
use std::{
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
process,
|
||||||
|
};
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use database::Database;
|
use database::Database;
|
||||||
use env_logger::Env;
|
use env_logger::Env;
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use log::error;
|
use log::{debug, error};
|
||||||
use mcdu::McduModel;
|
use mcdu::McduModel;
|
||||||
use relm4::prelude::*;
|
use relm4::prelude::*;
|
||||||
|
use sheet::Sheet;
|
||||||
use sheet_listing::{SheetListingInput, SheetListingModel};
|
use sheet_listing::{SheetListingInput, SheetListingModel};
|
||||||
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
struct AppModel {
|
struct AppModel {
|
||||||
mcdu: Controller<McduModel>,
|
mcdu: Controller<McduModel>,
|
||||||
@ -103,7 +108,6 @@ struct Cli {
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
// dotenvy::dotenv().unwrap();
|
|
||||||
env_logger::Builder::from_env(Env::default().default_filter_or("debug")).init();
|
env_logger::Builder::from_env(Env::default().default_filter_or("debug")).init();
|
||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
if !cli.directory.is_dir() {
|
if !cli.directory.is_dir() {
|
||||||
@ -111,11 +115,72 @@ async fn main() {
|
|||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let database = Database::setup("./testdb.db").await.unwrap();
|
let database = Database::setup("./testdb.sqlite").await.unwrap();
|
||||||
// database.insert_sheet(Sheet::new_debug()).await.unwrap();
|
// database.insert_sheet(Sheet::new_debug()).await.unwrap();
|
||||||
let sheets = database.fetch_all_sheets().await.unwrap();
|
let sheets = database.fetch_all_sheets().await.unwrap();
|
||||||
|
|
||||||
|
debug!("Validating sheets from database...");
|
||||||
|
let validation_result = validate_sheet_files(sheets, &cli.directory);
|
||||||
|
debug!("{}", validation_result.get_stats());
|
||||||
|
for updated in validation_result.updated_sheets {
|
||||||
|
database.update_sheet_path(updated).await.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
let app = RelmApp::new("de.frajul.sheet-organizer");
|
let app = RelmApp::new("de.frajul.sheet-organizer");
|
||||||
// Pass empty command line args to allow my own parsing
|
// Pass empty command line args to allow my own parsing
|
||||||
app.with_args(Vec::new()).run::<AppModel>(cli.directory);
|
app.with_args(Vec::new()).run::<AppModel>(cli.directory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct FileValidationResult {
|
||||||
|
validated_sheets: Vec<Sheet>,
|
||||||
|
invalidated_sheets: Vec<Sheet>,
|
||||||
|
updated_sheets: Vec<Sheet>,
|
||||||
|
|
||||||
|
unassigned_files: Vec<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FileValidationResult {
|
||||||
|
fn get_stats(&self) -> String {
|
||||||
|
format!("Validated sheets: {}\nInvalidated sheets: {}\nUpdated sheets: {}\nUnassigned files: {}", self.validated_sheets.len(), self.invalidated_sheets.len(), self.updated_sheets.len(), self.unassigned_files.len())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_sheet_files(sheets: Vec<Sheet>, dir: impl AsRef<Path>) -> FileValidationResult {
|
||||||
|
let (validated_sheets, mut invalidated_sheets): (Vec<_>, Vec<_>) = sheets
|
||||||
|
.into_iter()
|
||||||
|
.partition(|sheet| sheet.validate_path(&sheet.path).unwrap_or(false));
|
||||||
|
|
||||||
|
let mut updated_sheets = Vec::new();
|
||||||
|
let mut unassigned_files = Vec::new();
|
||||||
|
|
||||||
|
for pdf_file in WalkDir::new(dir)
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|e| e.ok())
|
||||||
|
.filter(|file| file.file_type().is_file())
|
||||||
|
.map(|file| file.into_path())
|
||||||
|
.filter(|path| {
|
||||||
|
path.extension()
|
||||||
|
.map(|s| s.to_string_lossy().to_ascii_lowercase() == "pdf")
|
||||||
|
.unwrap_or(false)
|
||||||
|
})
|
||||||
|
{
|
||||||
|
if let Some((i, _)) = invalidated_sheets
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.find(|(_, sheet)| sheet.validate_path(&pdf_file).unwrap_or(false))
|
||||||
|
{
|
||||||
|
let mut sheet = invalidated_sheets.remove(i);
|
||||||
|
sheet.path = pdf_file.to_str().unwrap().to_string();
|
||||||
|
updated_sheets.push(sheet);
|
||||||
|
} else {
|
||||||
|
unassigned_files.push(pdf_file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
FileValidationResult {
|
||||||
|
validated_sheets,
|
||||||
|
invalidated_sheets,
|
||||||
|
updated_sheets,
|
||||||
|
unassigned_files,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
32
src/sheet.rs
32
src/sheet.rs
@ -1,5 +1,8 @@
|
|||||||
|
use std::{fs, path::Path};
|
||||||
|
|
||||||
#[derive(sqlx::FromRow, Debug)]
|
#[derive(sqlx::FromRow, Debug)]
|
||||||
pub struct Sheet {
|
pub struct Sheet {
|
||||||
|
pub id: i32,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
// #[sqlx(from = "String")]
|
// #[sqlx(from = "String")]
|
||||||
pub path: String,
|
pub path: String,
|
||||||
@ -10,30 +13,21 @@ pub struct Sheet {
|
|||||||
|
|
||||||
#[derive(sqlx::FromRow)]
|
#[derive(sqlx::FromRow)]
|
||||||
pub struct Composer {
|
pub struct Composer {
|
||||||
|
pub id: i32,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub id: u32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sheet {
|
impl Sheet {
|
||||||
pub fn new_debug() -> Self {
|
pub fn validate_path(&self, path: impl AsRef<Path>) -> std::io::Result<bool> {
|
||||||
Sheet {
|
|
||||||
name: "Hello world".to_string(),
|
|
||||||
path: "This/is/my/path".into(),
|
|
||||||
file_size: 42,
|
|
||||||
file_hash: "h4sh".to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn verify_path(&self) -> std::io::Result<bool> {
|
|
||||||
// First compare file size since it is faster than hashing
|
// First compare file size since it is faster than hashing
|
||||||
// let file_size = fs::metadata(&self.path)?.len();
|
let file_size = fs::metadata(path.as_ref())?.len();
|
||||||
// if file_size == self.file_size {
|
if file_size as i32 == self.file_size {
|
||||||
// let file_content = fs::read(&self.path)?;
|
let file_content = fs::read(path.as_ref())?;
|
||||||
// let file_hash = blake3::hash(&file_content);
|
let file_hash = blake3::hash(&file_content);
|
||||||
// if file_hash.to_string() == self.file_hash {
|
if file_hash.to_string() == self.file_hash {
|
||||||
// return Ok(true);
|
return Ok(true);
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
Ok(false)
|
Ok(false)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user