feat: Add thumbnail generation feature and update media model
- Updated workspace members to include `libertas_importer`. - Added a new migration to add `thumbnail_path` column to `media` table. - Enhanced configuration structures to include `thumbnail_config`. - Modified `Media` model to include `thumbnail_path`. - Updated `MediaRepository` trait and its implementation to handle thumbnail path updates. - Created `ThumbnailPlugin` for generating thumbnails based on media configurations. - Integrated thumbnail generation into the media processing workflow. - Updated dependencies in `libertas_worker` and `libertas_importer` for image processing.
This commit is contained in:
891
Cargo.lock
generated
891
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,3 +1,3 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
resolver = "3"
|
resolver = "3"
|
||||||
members = ["libertas_api", "libertas_core", "libertas_infra", "libertas_worker"]
|
members = ["libertas_api", "libertas_core", "libertas_importer", "libertas_infra", "libertas_worker"]
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE media ADD COLUMN thumbnail_path VARCHAR(255);
|
||||||
@@ -20,5 +20,6 @@ pub fn load_config() -> CoreResult<Config> {
|
|||||||
"created_at".to_string(),
|
"created_at".to_string(),
|
||||||
"original_filename".to_string(),
|
"original_filename".to_string(),
|
||||||
]),
|
]),
|
||||||
|
thumbnail_config: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -260,6 +260,7 @@ impl MediaServiceImpl {
|
|||||||
width: None,
|
width: None,
|
||||||
height: None,
|
height: None,
|
||||||
date_taken: None,
|
date_taken: None,
|
||||||
|
thumbnail_path: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
self.repo.create(&media_model).await?;
|
self.repo.create(&media_model).await?;
|
||||||
|
|||||||
@@ -12,6 +12,22 @@ pub struct DatabaseConfig {
|
|||||||
pub url: String,
|
pub url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Clone)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
pub enum ThumbnailFormat {
|
||||||
|
Jpeg,
|
||||||
|
Webp,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Clone)]
|
||||||
|
pub struct ThumbnailConfig {
|
||||||
|
pub format: ThumbnailFormat,
|
||||||
|
pub quality: u8,
|
||||||
|
pub width: u32,
|
||||||
|
pub height: u32,
|
||||||
|
pub library_path: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Clone)]
|
#[derive(Deserialize, Clone)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub database: DatabaseConfig,
|
pub database: DatabaseConfig,
|
||||||
@@ -22,4 +38,5 @@ pub struct Config {
|
|||||||
pub max_upload_size_mb: Option<u32>,
|
pub max_upload_size_mb: Option<u32>,
|
||||||
pub default_storage_quota_gb: Option<u64>,
|
pub default_storage_quota_gb: Option<u64>,
|
||||||
pub allowed_sort_columns: Option<Vec<String>>,
|
pub allowed_sort_columns: Option<Vec<String>>,
|
||||||
|
pub thumbnail_config: Option<ThumbnailConfig>,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ pub struct Media {
|
|||||||
pub width: Option<i32>,
|
pub width: Option<i32>,
|
||||||
pub height: Option<i32>,
|
pub height: Option<i32>,
|
||||||
pub date_taken: Option<chrono::DateTime<chrono::Utc>>,
|
pub date_taken: Option<chrono::DateTime<chrono::Utc>>,
|
||||||
|
pub thumbnail_path: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
|||||||
@@ -3,9 +3,7 @@ use std::sync::Arc;
|
|||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
error::CoreResult,
|
config::Config, error::CoreResult, models::Media, repositories::{AlbumRepository, MediaRepository, UserRepository}
|
||||||
models::Media,
|
|
||||||
repositories::{AlbumRepository, MediaRepository, UserRepository},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct PluginData {
|
pub struct PluginData {
|
||||||
@@ -17,6 +15,7 @@ pub struct PluginContext {
|
|||||||
pub album_repo: Arc<dyn AlbumRepository>,
|
pub album_repo: Arc<dyn AlbumRepository>,
|
||||||
pub user_repo: Arc<dyn UserRepository>,
|
pub user_repo: Arc<dyn UserRepository>,
|
||||||
pub media_library_path: String,
|
pub media_library_path: String,
|
||||||
|
pub config: Arc<Config>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ pub trait MediaRepository: Send + Sync {
|
|||||||
async fn create(&self, media: &Media) -> CoreResult<()>;
|
async fn create(&self, media: &Media) -> CoreResult<()>;
|
||||||
async fn find_by_id(&self, id: Uuid) -> CoreResult<Option<Media>>;
|
async fn find_by_id(&self, id: Uuid) -> CoreResult<Option<Media>>;
|
||||||
async fn list_by_user(&self, user_id: Uuid, options: &ListMediaOptions) -> CoreResult<Vec<Media>>;
|
async fn list_by_user(&self, user_id: Uuid, options: &ListMediaOptions) -> CoreResult<Vec<Media>>;
|
||||||
async fn update_metadata(
|
async fn update_exif_data(
|
||||||
&self,
|
&self,
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
width: Option<i32>,
|
width: Option<i32>,
|
||||||
@@ -20,6 +20,7 @@ pub trait MediaRepository: Send + Sync {
|
|||||||
location: Option<String>,
|
location: Option<String>,
|
||||||
date_taken: Option<chrono::DateTime<chrono::Utc>>,
|
date_taken: Option<chrono::DateTime<chrono::Utc>>,
|
||||||
) -> CoreResult<()>;
|
) -> CoreResult<()>;
|
||||||
|
async fn update_thumbnail_path(&self, id: Uuid, thumbnail_path: String) -> CoreResult<()>;
|
||||||
async fn delete(&self, id: Uuid) -> CoreResult<()>;
|
async fn delete(&self, id: Uuid) -> CoreResult<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
23
libertas_importer/Cargo.toml
Normal file
23
libertas_importer/Cargo.toml
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
[package]
|
||||||
|
name = "libertas_importer"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
anyhow = "1.0.100"
|
||||||
|
async-nats = "0.45.0"
|
||||||
|
bytes = "1.10.1"
|
||||||
|
chrono = "0.4.42"
|
||||||
|
clap = { version = "4.5.51", features = ["derive"] }
|
||||||
|
futures = "0.3.31"
|
||||||
|
libertas_core = { path = "../libertas_core" }
|
||||||
|
libertas_infra = { path = "../libertas_infra" }
|
||||||
|
mime_guess = "2.0.5"
|
||||||
|
nom-exif = { version = "2.5.4", features = ["serde", "tokio", "async"] }
|
||||||
|
serde = { version = "1.0.228", features = ["derive"] }
|
||||||
|
serde_json = "1.0.145"
|
||||||
|
sha2 = "0.10.9"
|
||||||
|
sqlx = { version = "0.8.6", features = ["runtime-tokio", "postgres", "uuid", "chrono"] }
|
||||||
|
tokio = { version = "1.48.0", features = ["full"] }
|
||||||
|
uuid = { version = "1.18.1", features = ["v4", "serde"] }
|
||||||
|
walkdir = "2.5.0"
|
||||||
25
libertas_importer/src/config.rs
Normal file
25
libertas_importer/src/config.rs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
use libertas_core::{
|
||||||
|
config::{Config, DatabaseConfig, DatabaseType},
|
||||||
|
error::CoreResult,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn load_config() -> CoreResult<Config> {
|
||||||
|
Ok(Config {
|
||||||
|
database: DatabaseConfig {
|
||||||
|
db_type: DatabaseType::Postgres,
|
||||||
|
url: "postgres://libertas:libertas_password@localhost:5436/libertas_db".to_string(),
|
||||||
|
},
|
||||||
|
server_address: "127.0.0.1:8080".to_string(),
|
||||||
|
jwt_secret: "super_secret_jwt_key".to_string(),
|
||||||
|
media_library_path: "media_library".to_string(),
|
||||||
|
broker_url: "nats://localhost:4222".to_string(),
|
||||||
|
max_upload_size_mb: Some(100),
|
||||||
|
default_storage_quota_gb: Some(10),
|
||||||
|
allowed_sort_columns: Some(vec![
|
||||||
|
"date_taken".to_string(),
|
||||||
|
"created_at".to_string(),
|
||||||
|
"original_filename".to_string(),
|
||||||
|
]),
|
||||||
|
thumbnail_config: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
194
libertas_importer/src/main.rs
Normal file
194
libertas_importer/src/main.rs
Normal file
@@ -0,0 +1,194 @@
|
|||||||
|
use std::{path::{Path, PathBuf}, sync::Arc};
|
||||||
|
use anyhow::Result;
|
||||||
|
|
||||||
|
use chrono::{DateTime, Datelike, NaiveDateTime, Utc};
|
||||||
|
use clap::Parser;
|
||||||
|
use libertas_core::{config::Config, error::{CoreError, CoreResult}, models::{Media, User}, repositories::{MediaRepository, UserRepository}};
|
||||||
|
use libertas_infra::factory::{build_database_pool, build_media_repository, build_user_repository};
|
||||||
|
use nom_exif::{AsyncMediaParser, AsyncMediaSource, Exif, ExifIter, ExifTag};
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
use uuid::Uuid;
|
||||||
|
use walkdir::WalkDir;
|
||||||
|
use tokio::fs;
|
||||||
|
|
||||||
|
mod config;
|
||||||
|
|
||||||
|
#[derive(Parser, Debug)]
|
||||||
|
#[command(author, version, about, long_about = None)]
|
||||||
|
struct Cli {
|
||||||
|
#[arg(short, long)]
|
||||||
|
username: String,
|
||||||
|
#[arg(short, long)]
|
||||||
|
path: String,
|
||||||
|
#[arg(short, long, default_value_t = false)]
|
||||||
|
recursive: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ImporterState {
|
||||||
|
config: Config,
|
||||||
|
media_repo: Arc<dyn MediaRepository>,
|
||||||
|
user_repo: Arc<dyn UserRepository>,
|
||||||
|
nats_client: async_nats::Client,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<()> {
|
||||||
|
let cli = Cli::parse();
|
||||||
|
println!("Starting import for user: '{}' from path '{}'...", cli.username, cli.path);
|
||||||
|
|
||||||
|
let config = config::load_config()?;
|
||||||
|
let db_pool = build_database_pool(&config.database).await?;
|
||||||
|
let media_repo = build_media_repository(&config, db_pool.clone()).await?;
|
||||||
|
let user_repo = build_user_repository(&config.database, db_pool.clone()).await?;
|
||||||
|
let nats_client = async_nats::connect(&config.broker_url).await?;
|
||||||
|
|
||||||
|
println!("Connected to database and NATS broker.");
|
||||||
|
|
||||||
|
let state = ImporterState {
|
||||||
|
config,
|
||||||
|
media_repo,
|
||||||
|
user_repo,
|
||||||
|
nats_client,
|
||||||
|
};
|
||||||
|
|
||||||
|
let user = state
|
||||||
|
.user_repo
|
||||||
|
.find_by_username(&cli.username)
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("User '{}' not found", cli.username))?;
|
||||||
|
|
||||||
|
println!("User '{}' found with ID: {}", cli.username, user.id);
|
||||||
|
println!("Storage: {} / {}", user.storage_used, user.storage_quota);
|
||||||
|
|
||||||
|
let max_depth = if cli.recursive { usize::MAX } else { 1 };
|
||||||
|
let walker = WalkDir::new(&cli.path).max_depth(max_depth).into_iter();
|
||||||
|
|
||||||
|
for entry in walker.filter_map(Result::ok) {
|
||||||
|
if entry.file_type().is_file() {
|
||||||
|
let path = entry.path();
|
||||||
|
|
||||||
|
match process_file(path, &user, &state).await {
|
||||||
|
Ok(media) => {
|
||||||
|
println!("-> Imported: '{}'", media.original_filename);
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("!! Skipped: '{}' (Reason: {})", path.display(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Import process completed.");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn process_file(
|
||||||
|
file_path: &Path,
|
||||||
|
user: &User,
|
||||||
|
state: &ImporterState,
|
||||||
|
) -> CoreResult<Media> {
|
||||||
|
let file_bytes = fs::read(file_path).await?;
|
||||||
|
let file_size = file_bytes.len() as i64;
|
||||||
|
let hash = format!("{:x}", Sha256::digest(&file_bytes));
|
||||||
|
let filename = file_path
|
||||||
|
.file_name()
|
||||||
|
.unwrap_or_default()
|
||||||
|
.to_string_lossy()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
let user_after_check = state.user_repo.find_by_id(user.id).await?.unwrap();
|
||||||
|
if &user_after_check.storage_used + file_size > user_after_check.storage_quota {
|
||||||
|
return Err(CoreError::Auth("Storage quota exceeded".to_string()));
|
||||||
|
}
|
||||||
|
if state.media_repo.find_by_hash(&hash).await?.is_some() {
|
||||||
|
return Err(CoreError::Duplicate(
|
||||||
|
"A file with this content already exists".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let (width, height, location, date_taken) =
|
||||||
|
match AsyncMediaSource::file_path(file_path).await {
|
||||||
|
Ok(ms) => {
|
||||||
|
if ms.has_exif() {
|
||||||
|
let mut parser = AsyncMediaParser::new();
|
||||||
|
if let Ok(iter) = parser.parse::<_,_, ExifIter>(ms).await {
|
||||||
|
let gps = iter.parse_gps_info().ok().flatten().map(|g| g.format_iso6709());
|
||||||
|
println!(" -> EXIF GPS Info: {:?}", gps);
|
||||||
|
let exif: Exif = iter.into();
|
||||||
|
let modified_date = exif.get(ExifTag::ModifyDate).and_then(|f| f.as_str()).and_then(parse_exif_datetime);
|
||||||
|
println!(" -> EXIF ModifyDate: {:?}", modified_date);
|
||||||
|
let w = exif.get(ExifTag::ExifImageWidth).and_then(|f| f.as_u32()).map(|v| v as i32);
|
||||||
|
println!(" -> EXIF ExifImageWidth: {:?}", w);
|
||||||
|
let h = exif.get(ExifTag::ExifImageHeight).and_then(|f| f.as_u32()).map(|v| v as i32);
|
||||||
|
println!(" -> EXIF ExifImageHeight: {:?}", h);
|
||||||
|
let dt = exif.get(ExifTag::DateTimeOriginal).and_then(|f| f.as_str()).and_then(parse_exif_datetime);
|
||||||
|
println!(" -> EXIF DateTimeOriginal: {:?}", dt);
|
||||||
|
(w, h, gps, dt)
|
||||||
|
} else {
|
||||||
|
(None, None, None, None)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
(None, None, None, None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => (None, None, None, None),
|
||||||
|
};
|
||||||
|
|
||||||
|
let file_date = date_taken.unwrap_or_else(|| chrono::Utc::now());
|
||||||
|
let year = file_date.year().to_string();
|
||||||
|
let month = format!("{:02}", file_date.month());
|
||||||
|
let mut dest_path_buf = PathBuf::from(&state.config.media_library_path);
|
||||||
|
dest_path_buf.push(&year);
|
||||||
|
dest_path_buf.push(&month);
|
||||||
|
|
||||||
|
fs::create_dir_all(&dest_path_buf).await?;
|
||||||
|
|
||||||
|
dest_path_buf.push(&filename);
|
||||||
|
|
||||||
|
fs::copy(file_path, &dest_path_buf).await?;
|
||||||
|
|
||||||
|
let storage_path_str = PathBuf::from(&year)
|
||||||
|
.join(&month)
|
||||||
|
.join(&filename)
|
||||||
|
.to_string_lossy()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
let mime_type = mime_guess::from_path(file_path)
|
||||||
|
.first_or_octet_stream()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
let media_model = Media {
|
||||||
|
id: Uuid::new_v4(),
|
||||||
|
owner_id: user.id,
|
||||||
|
storage_path: storage_path_str,
|
||||||
|
original_filename: filename,
|
||||||
|
mime_type,
|
||||||
|
hash,
|
||||||
|
created_at: chrono::Utc::now(),
|
||||||
|
extracted_location: location,
|
||||||
|
width: width,
|
||||||
|
height: height,
|
||||||
|
date_taken: date_taken,
|
||||||
|
thumbnail_path: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
state.media_repo.create(&media_model).await?;
|
||||||
|
state.user_repo
|
||||||
|
.update_storage_used(user.id, file_size)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let job_payload = serde_json::json!({ "media_id": media_model.id });
|
||||||
|
state.nats_client
|
||||||
|
.publish("media.new".to_string(), job_payload.to_string().into())
|
||||||
|
.await
|
||||||
|
.map_err(|e| CoreError::Unknown(format!("Failed to publish NATS message: {}", e)))?;
|
||||||
|
|
||||||
|
Ok(media_model)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_exif_datetime(s: &str) -> Option<DateTime<Utc>> {
|
||||||
|
NaiveDateTime::parse_from_str(s, "%Y:%m:%d %H:%M:%S")
|
||||||
|
.ok()
|
||||||
|
.map(|ndt| ndt.and_local_timezone(Utc).unwrap())
|
||||||
|
}
|
||||||
@@ -48,6 +48,7 @@ pub struct PostgresMedia {
|
|||||||
pub width: Option<i32>,
|
pub width: Option<i32>,
|
||||||
pub height: Option<i32>,
|
pub height: Option<i32>,
|
||||||
pub date_taken: Option<chrono::DateTime<chrono::Utc>>,
|
pub date_taken: Option<chrono::DateTime<chrono::Utc>>,
|
||||||
|
pub thumbnail_path: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, sqlx::Type, PartialEq, Eq, Deserialize)]
|
#[derive(Debug, Clone, Copy, sqlx::Type, PartialEq, Eq, Deserialize)]
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ impl From<PostgresMedia> for Media {
|
|||||||
width: pg_media.width,
|
width: pg_media.width,
|
||||||
height: pg_media.height,
|
height: pg_media.height,
|
||||||
date_taken: pg_media.date_taken,
|
date_taken: pg_media.date_taken,
|
||||||
|
thumbnail_path: pg_media.thumbnail_path,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,8 +31,8 @@ impl MediaRepository for PostgresMediaRepository {
|
|||||||
async fn create(&self, media: &Media) -> CoreResult<()> {
|
async fn create(&self, media: &Media) -> CoreResult<()> {
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
r#"
|
r#"
|
||||||
INSERT INTO media (id, owner_id, storage_path, original_filename, mime_type, hash, created_at, width, height)
|
INSERT INTO media (id, owner_id, storage_path, original_filename, mime_type, hash, created_at, width, height, thumbnail_path)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||||
"#,
|
"#,
|
||||||
media.id,
|
media.id,
|
||||||
media.owner_id,
|
media.owner_id,
|
||||||
@@ -42,7 +42,8 @@ impl MediaRepository for PostgresMediaRepository {
|
|||||||
media.hash,
|
media.hash,
|
||||||
media.created_at,
|
media.created_at,
|
||||||
media.width,
|
media.width,
|
||||||
media.height
|
media.height,
|
||||||
|
media.thumbnail_path
|
||||||
)
|
)
|
||||||
.execute(&self.pool)
|
.execute(&self.pool)
|
||||||
.await
|
.await
|
||||||
@@ -56,7 +57,7 @@ impl MediaRepository for PostgresMediaRepository {
|
|||||||
PostgresMedia,
|
PostgresMedia,
|
||||||
r#"
|
r#"
|
||||||
SELECT id, owner_id, storage_path, original_filename, mime_type, hash, created_at,
|
SELECT id, owner_id, storage_path, original_filename, mime_type, hash, created_at,
|
||||||
extracted_location, width, height, date_taken
|
extracted_location, width, height, date_taken, thumbnail_path
|
||||||
FROM media
|
FROM media
|
||||||
WHERE hash = $1
|
WHERE hash = $1
|
||||||
"#,
|
"#,
|
||||||
@@ -74,7 +75,7 @@ impl MediaRepository for PostgresMediaRepository {
|
|||||||
PostgresMedia,
|
PostgresMedia,
|
||||||
r#"
|
r#"
|
||||||
SELECT id, owner_id, storage_path, original_filename, mime_type, hash, created_at,
|
SELECT id, owner_id, storage_path, original_filename, mime_type, hash, created_at,
|
||||||
extracted_location, width, height, date_taken
|
extracted_location, width, height, date_taken, thumbnail_path
|
||||||
FROM media
|
FROM media
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
"#,
|
"#,
|
||||||
@@ -91,7 +92,7 @@ impl MediaRepository for PostgresMediaRepository {
|
|||||||
let mut query = sqlx::QueryBuilder::new(
|
let mut query = sqlx::QueryBuilder::new(
|
||||||
r#"
|
r#"
|
||||||
SELECT id, owner_id, storage_path, original_filename, mime_type, hash, created_at,
|
SELECT id, owner_id, storage_path, original_filename, mime_type, hash, created_at,
|
||||||
extracted_location, width, height, date_taken
|
extracted_location, width, height, date_taken, thumbnail_path
|
||||||
FROM media
|
FROM media
|
||||||
WHERE owner_id =
|
WHERE owner_id =
|
||||||
"#,
|
"#,
|
||||||
@@ -113,7 +114,7 @@ impl MediaRepository for PostgresMediaRepository {
|
|||||||
Ok(media_list)
|
Ok(media_list)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_metadata(
|
async fn update_exif_data(
|
||||||
&self,
|
&self,
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
width: Option<i32>,
|
width: Option<i32>,
|
||||||
@@ -125,7 +126,7 @@ impl MediaRepository for PostgresMediaRepository {
|
|||||||
r#"
|
r#"
|
||||||
UPDATE media
|
UPDATE media
|
||||||
SET width = $2, height = $3, extracted_location = $4, date_taken = $5
|
SET width = $2, height = $3, extracted_location = $4, date_taken = $5
|
||||||
WHERE id = $1
|
WHERE id = $1 AND date_taken IS NULL
|
||||||
"#,
|
"#,
|
||||||
id,
|
id,
|
||||||
width,
|
width,
|
||||||
@@ -140,6 +141,23 @@ impl MediaRepository for PostgresMediaRepository {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn update_thumbnail_path(&self, id: Uuid, thumbnail_path: String) -> CoreResult<()> {
|
||||||
|
sqlx::query!(
|
||||||
|
r#"
|
||||||
|
UPDATE media
|
||||||
|
SET thumbnail_path = $2
|
||||||
|
WHERE id = $1
|
||||||
|
"#,
|
||||||
|
id,
|
||||||
|
thumbnail_path
|
||||||
|
)
|
||||||
|
.execute(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
async fn delete(&self, id: Uuid) -> CoreResult<()> {
|
async fn delete(&self, id: Uuid) -> CoreResult<()> {
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
r#"
|
r#"
|
||||||
|
|||||||
@@ -26,3 +26,4 @@ nom-exif = { version = "2.5.4", features = ["serde", "tokio", "async"] }
|
|||||||
async-trait = "0.1.89"
|
async-trait = "0.1.89"
|
||||||
xmp_toolkit = "1.11.0"
|
xmp_toolkit = "1.11.0"
|
||||||
chrono = "0.4.42"
|
chrono = "0.4.42"
|
||||||
|
image = "0.25.8"
|
||||||
|
|||||||
@@ -20,5 +20,6 @@ pub fn load_config() -> CoreResult<Config> {
|
|||||||
"created_at".to_string(),
|
"created_at".to_string(),
|
||||||
"original_filename".to_string(),
|
"original_filename".to_string(),
|
||||||
]),
|
]),
|
||||||
|
thumbnail_config: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
album_repo,
|
album_repo,
|
||||||
user_repo,
|
user_repo,
|
||||||
media_library_path: config.media_library_path.clone(),
|
media_library_path: config.media_library_path.clone(),
|
||||||
|
config: Arc::new(config.clone()),
|
||||||
});
|
});
|
||||||
println!("Plugin context created.");
|
println!("Plugin context created.");
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use libertas_core::{
|
|||||||
plugins::{MediaProcessorPlugin, PluginContext},
|
plugins::{MediaProcessorPlugin, PluginContext},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::plugins::{exif_reader::ExifReaderPlugin, xmp_writer::XmpWriterPlugin};
|
use crate::plugins::{exif_reader::ExifReaderPlugin, thumbnail::ThumbnailPlugin, xmp_writer::XmpWriterPlugin};
|
||||||
|
|
||||||
pub struct PluginManager {
|
pub struct PluginManager {
|
||||||
plugins: Vec<Arc<dyn MediaProcessorPlugin>>,
|
plugins: Vec<Arc<dyn MediaProcessorPlugin>>,
|
||||||
@@ -16,7 +16,7 @@ impl PluginManager {
|
|||||||
let mut plugins: Vec<Arc<dyn MediaProcessorPlugin>> = Vec::new();
|
let mut plugins: Vec<Arc<dyn MediaProcessorPlugin>> = Vec::new();
|
||||||
|
|
||||||
plugins.push(Arc::new(ExifReaderPlugin));
|
plugins.push(Arc::new(ExifReaderPlugin));
|
||||||
|
plugins.push(Arc::new(ThumbnailPlugin));
|
||||||
plugins.push(Arc::new(XmpWriterPlugin));
|
plugins.push(Arc::new(XmpWriterPlugin));
|
||||||
|
|
||||||
println!("PluginManager loaded {} plugins", plugins.len());
|
println!("PluginManager loaded {} plugins", plugins.len());
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ impl MediaProcessorPlugin for ExifReaderPlugin {
|
|||||||
if width.is_some() || height.is_some() || location.is_some() || date_taken.is_some() {
|
if width.is_some() || height.is_some() || location.is_some() || date_taken.is_some() {
|
||||||
context
|
context
|
||||||
.media_repo
|
.media_repo
|
||||||
.update_metadata(media.id, width, height, location.clone(), date_taken)
|
.update_exif_data(media.id, width, height, location.clone(), date_taken)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let message = format!(
|
let message = format!(
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
pub mod exif_reader;
|
pub mod exif_reader;
|
||||||
pub mod xmp_writer;
|
pub mod xmp_writer;
|
||||||
|
pub mod thumbnail;
|
||||||
71
libertas_worker/src/plugins/thumbnail.rs
Normal file
71
libertas_worker/src/plugins/thumbnail.rs
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use libertas_core::{config::ThumbnailFormat, error::{CoreError, CoreResult}, models::Media, plugins::{MediaProcessorPlugin, PluginContext, PluginData}};
|
||||||
|
use tokio::fs;
|
||||||
|
|
||||||
|
pub struct ThumbnailPlugin;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl MediaProcessorPlugin for ThumbnailPlugin {
|
||||||
|
fn name(&self) -> &'static str {
|
||||||
|
"thumbnail_generator"
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn process(&self, media: &Media, context: &PluginContext) -> CoreResult<PluginData> {
|
||||||
|
let config = &context.config.thumbnail_config;
|
||||||
|
if config.is_none() {
|
||||||
|
return Ok(PluginData {
|
||||||
|
message: "Thumbnail generation is disabled in config.".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let config = config.as_ref().unwrap();
|
||||||
|
let original_path = PathBuf::from(&context.media_library_path).join(&media.storage_path);
|
||||||
|
|
||||||
|
let extension = match config.format {
|
||||||
|
ThumbnailFormat::Jpeg => "jpg",
|
||||||
|
ThumbnailFormat::Webp => "webp",
|
||||||
|
};
|
||||||
|
let thumbnail_filename = format!("{}_thumb.{}", media.id, extension);
|
||||||
|
let thumbnail_path = PathBuf::from(&config.library_path).join(thumbnail_filename);
|
||||||
|
|
||||||
|
if let Some(parent) = thumbnail_path.parent() {
|
||||||
|
if !parent.exists() {
|
||||||
|
fs::create_dir_all(parent).await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !media.mime_type.starts_with("image/") {
|
||||||
|
return Ok(PluginData {
|
||||||
|
message: "Media is not an image; skipping thumbnail generation.".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let img = image::open(&original_path)
|
||||||
|
.map_err(|e| CoreError::Unknown(format!("Failed to open image: {}", e)))?;
|
||||||
|
|
||||||
|
let thumb = img.thumbnail(config.width, config.height);
|
||||||
|
|
||||||
|
match config.format {
|
||||||
|
ThumbnailFormat::Jpeg => {
|
||||||
|
thumb
|
||||||
|
.save_with_format(&thumbnail_path, image::ImageFormat::Jpeg)
|
||||||
|
},
|
||||||
|
ThumbnailFormat::Webp => {
|
||||||
|
thumb
|
||||||
|
.save_with_format(&thumbnail_path, image::ImageFormat::WebP)
|
||||||
|
},
|
||||||
|
}.map_err(|e| CoreError::Unknown(format!("Failed to save thumbnail: {}", e)))?;
|
||||||
|
|
||||||
|
let thumb_path_str = thumbnail_path.to_string_lossy().to_string();
|
||||||
|
context
|
||||||
|
.media_repo
|
||||||
|
.update_thumbnail_path(media.id, thumb_path_str)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(PluginData {
|
||||||
|
message: format!("Thumbnail generated at {:?}", thumbnail_path.display()),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user