chore: remove unused update mode
This commit is contained in:
committed by
Andrew Phillips (aider)
parent
afb3d789ba
commit
fddc7670aa
@@ -1,114 +0,0 @@
|
|||||||
use anyhow::{anyhow, Result};
|
|
||||||
use std::path::PathBuf;
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use crate::compression_engine::{CompressionType, get_compression_engine};
|
|
||||||
use crate::db;
|
|
||||||
use crate::meta_plugin;
|
|
||||||
use crate::modes::common::get_digest_type_meta;
|
|
||||||
use crate::config;
|
|
||||||
use clap::Command;
|
|
||||||
use clap::error::ErrorKind;
|
|
||||||
use log::{debug, info};
|
|
||||||
use rusqlite::Connection;
|
|
||||||
|
|
||||||
pub fn mode_update(
|
|
||||||
cmd: &mut Command,
|
|
||||||
settings: &config::Settings,
|
|
||||||
ids: &mut Vec<i64>,
|
|
||||||
tags: &mut Vec<String>,
|
|
||||||
conn: &mut Connection,
|
|
||||||
data_path: PathBuf,
|
|
||||||
) -> Result<()> {
|
|
||||||
if ids.is_empty() {
|
|
||||||
cmd.error(
|
|
||||||
ErrorKind::InvalidValue,
|
|
||||||
"No ID given, you must supply exactly one ID when using --update",
|
|
||||||
)
|
|
||||||
.exit();
|
|
||||||
} else if ids.len() > 1 {
|
|
||||||
cmd.error(ErrorKind::InvalidValue, "More than one ID given, you must supply exactly one ID or atleast one tag when using --update").exit();
|
|
||||||
}
|
|
||||||
|
|
||||||
let item_id = ids.iter().next().expect("Unable to determine item id");
|
|
||||||
let item_maybe = db::get_item(conn, *item_id)?;
|
|
||||||
|
|
||||||
let mut item = item_maybe.expect("Unable to find item in database");
|
|
||||||
debug!("MAIN: Found item {:?}", item);
|
|
||||||
|
|
||||||
// Use a transaction for database operations to ensure atomicity
|
|
||||||
let tx = conn.transaction()?;
|
|
||||||
|
|
||||||
if !tags.is_empty() {
|
|
||||||
debug!("MAIN: Updating item tags");
|
|
||||||
db::set_item_tags(&tx, item.clone(), tags)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let item_id = item.id.ok_or_else(|| anyhow!("Item missing ID"))?;
|
|
||||||
let item_path = {
|
|
||||||
let mut path = data_path.clone();
|
|
||||||
path.push(item_id.to_string());
|
|
||||||
path
|
|
||||||
};
|
|
||||||
|
|
||||||
let compression_type = CompressionType::from_str(&item.compression)?;
|
|
||||||
let compression_engine =
|
|
||||||
get_compression_engine(compression_type).expect("Unable to get compression engine");
|
|
||||||
|
|
||||||
if item.size.is_none() {
|
|
||||||
info!("Updating unknown stream size");
|
|
||||||
let item_file_metadata = item_path.metadata();
|
|
||||||
|
|
||||||
if item_file_metadata.is_ok() {
|
|
||||||
debug!("MAIN: Updating stream size of {:?}", item_path);
|
|
||||||
let size = compression_engine.size(item_path.clone())?;
|
|
||||||
item.size = Some(size as i64);
|
|
||||||
db::update_item(&tx, item.clone())?;
|
|
||||||
} else {
|
|
||||||
debug!(
|
|
||||||
"MAIN: Unable to update size of item due to missing file {:?}",
|
|
||||||
item_path
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let digest_type = crate::modes::common::settings_digest_type(cmd, settings);
|
|
||||||
let digest_meta = get_digest_type_meta(digest_type.clone());
|
|
||||||
let digest_value = db::get_item_meta_value(&tx, &item, digest_meta)?;
|
|
||||||
|
|
||||||
if digest_value.is_none() || digest_value.unwrap().is_empty() {
|
|
||||||
let item_file_metadata = item_path.metadata();
|
|
||||||
|
|
||||||
if item_file_metadata.is_ok() {
|
|
||||||
debug!("MAIN: Updating stream size of {:?}", item_path);
|
|
||||||
|
|
||||||
// Create and initialize digest engine
|
|
||||||
let mut digest_engine = meta_plugin::get_meta_plugin(digest_type.clone());
|
|
||||||
digest_engine.initialize(&tx, item_id)?;
|
|
||||||
|
|
||||||
// Read file content and update digest
|
|
||||||
let mut reader = compression_engine.open(item_path)?;
|
|
||||||
let mut buffer = [0; 4096];
|
|
||||||
loop {
|
|
||||||
let bytes_read = reader.read(&mut buffer)?;
|
|
||||||
if bytes_read == 0 {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
digest_engine.update(&buffer[..bytes_read]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Finalize the digest engine (this will save the metadata)
|
|
||||||
digest_engine.finalize()?;
|
|
||||||
} else {
|
|
||||||
debug!(
|
|
||||||
"MAIN: Unable to update digest of item due to missing file {:?}",
|
|
||||||
item_path
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Commit the transaction
|
|
||||||
tx.commit()?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user