implement persist_images_selector, minimize codebase by using bail, change image table structure to use sha256 hash as the unique image identity

This commit is contained in:
yggverse 2026-01-10 14:38:01 +02:00
parent bc61b5c09c
commit ec0cca64f3
7 changed files with 97 additions and 90 deletions

View file

@ -1,5 +1,5 @@
-- MySQL Script generated by MySQL Workbench
-- пт, 09-січ-2026 17:57:03 +0200
-- сб, 10-січ-2026 14:27:50 +0200
-- Model: New Model Version: 1.0
-- MySQL Workbench Forward Engineering
@ -92,10 +92,12 @@ ENGINE = InnoDB;
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `rssto`.`image` (
`image_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`source` VARCHAR(2048) NOT NULL,
`sha256` CHAR(64) NOT NULL,
`src` VARCHAR(2048) NULL,
`url` VARCHAR(2048) NULL,
`data` MEDIUMBLOB NOT NULL,
PRIMARY KEY (`image_id`),
UNIQUE INDEX `source_UNIQUE` (`source` ASC) VISIBLE)
UNIQUE INDEX `hash_UNIQUE` (`sha256` ASC) VISIBLE)
ENGINE = InnoDB;

View file

@ -37,7 +37,13 @@ pub struct Provider {
#[derive(Debug, PartialEq, Eq, FromRow)]
pub struct Image {
pub image_id: u64,
pub source: String,
/// Keep image unique by comparing its data hash
pub sha256: String,
/// Original `src` tag value to post-replacing
pub src: Option<String>,
/// Resolved absolute URL
pub url: Option<String>,
/// Image data, MEDIUMBLOB (16M)
pub data: Vec<u8>,
}

View file

@ -115,17 +115,23 @@ impl Transaction {
Ok(self.tx.last_insert_id().unwrap())
}
pub fn images_total_by_source(&mut self, source: &str) -> Result<usize, Error> {
Ok(self
.tx
.exec_first("SELECT COUNT(*) FROM `image` WHERE `source` = ?", (source,))?
.unwrap_or(0))
pub fn image_id_by_sha256(&mut self, sha256: &str) -> Result<Option<u64>, Error> {
self.tx.exec_first(
"SELECT `image_id` FROM `image` WHERE `sha256` = ? LIMIT 1",
(sha256,),
)
}
pub fn insert_image(&mut self, source: &str, data: &[u8]) -> Result<u64, Error> {
pub fn insert_image(
&mut self,
sha256: &str,
src: Option<&str>,
url: Option<&str>,
data: &[u8],
) -> Result<u64, Error> {
self.tx.exec_drop(
"INSERT INTO `image` SET `source` = ?, `data` = ?",
(source, data),
"INSERT INTO `image` SET `sha256` = ?, `src` = ?, `url` = ?, `data` = ?",
(sha256, src, url, data),
)?;
Ok(self.tx.last_insert_id().unwrap())
}