-
Notifications
You must be signed in to change notification settings - Fork 0
feat(link): expand crawler galleries into per-image rows #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
src-tauri/src/application/commands/resolve_links_gallery.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| //! Gallery expansion for `handle_resolve_links`. | ||
| //! | ||
| //! Crawler plugins that answer `extract_links` with a `kind: "gallery"` | ||
| //! payload (e.g. vortex-mod-gallery) expand one gallery URL into one | ||
| //! resolved link per image, mirroring the hoster 1→N expansion. Per-image | ||
| //! failures become individual error rows so a partially invalid gallery | ||
| //! never silently cancels the valid images (MAT-134 R-05). | ||
|
|
||
| use serde::Deserialize; | ||
| use uuid::Uuid; | ||
|
|
||
| use crate::application::command_bus::CommandBus; | ||
|
|
||
| use super::resolve_links::{LinkResolutionErrorKind, ResolvedLinkDto, extract_filename_from_url}; | ||
|
|
||
| #[derive(Deserialize)] | ||
| struct GalleryResponse { | ||
| kind: Option<String>, | ||
| #[serde(default)] | ||
| images: Vec<GalleryImage>, | ||
| } | ||
|
|
||
| #[derive(Deserialize)] | ||
| struct GalleryImage { | ||
| #[serde(default)] | ||
| url: String, | ||
| #[serde(default)] | ||
| filename: Option<String>, | ||
| #[serde(default)] | ||
| title: Option<String>, | ||
| } | ||
|
|
||
| impl CommandBus { | ||
| /// Expand a crawler gallery URL into per-image resolved links. | ||
| /// | ||
| /// Returns `None` when the payload parses but is not a gallery, so the | ||
| /// caller can fall through to the generic HEAD probe; an unparseable | ||
| /// payload becomes a single error row instead. At most `max_rows` | ||
| /// images are expanded — extras are dropped (and logged) so one | ||
| /// oversized gallery cannot fail the whole resolve. | ||
| pub(super) fn try_resolve_gallery_links( | ||
| &self, | ||
| url: &str, | ||
| module_name: &str, | ||
| max_rows: usize, | ||
| ) -> Option<Vec<ResolvedLinkDto>> { | ||
| let raw = match self.plugin_loader().extract_links(url) { | ||
| Ok(raw) => raw, | ||
| Err(e) => { | ||
| tracing::debug!(module_name, error = %e, "gallery extraction failed"); | ||
| return Some(vec![gallery_error_row( | ||
| url, | ||
| module_name, | ||
| LinkResolutionErrorKind::Plugin, | ||
| "Could not extract gallery links", | ||
| )]); | ||
| } | ||
| }; | ||
| let parsed: GalleryResponse = match serde_json::from_str(&raw) { | ||
| Ok(parsed) => parsed, | ||
| Err(e) => { | ||
| tracing::debug!(module_name, error = %e, "gallery payload is not valid JSON"); | ||
| return Some(vec![gallery_error_row( | ||
| url, | ||
| module_name, | ||
| LinkResolutionErrorKind::Plugin, | ||
| "Gallery plugin returned an invalid response", | ||
| )]); | ||
| } | ||
| }; | ||
| if parsed.kind.as_deref() != Some("gallery") { | ||
| return None; | ||
| } | ||
| if parsed.images.is_empty() { | ||
| return Some(vec![gallery_error_row( | ||
| url, | ||
| module_name, | ||
| LinkResolutionErrorKind::NoFile, | ||
| "Gallery contains no downloadable images", | ||
| )]); | ||
| } | ||
| if parsed.images.len() > max_rows { | ||
| tracing::warn!( | ||
| module_name, | ||
| total = parsed.images.len(), | ||
| kept = max_rows, | ||
| "gallery exceeds the resolve output limit; extra images dropped" | ||
| ); | ||
| } | ||
| Some( | ||
| parsed | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| .images | ||
| .iter() | ||
| .take(max_rows) | ||
| .enumerate() | ||
| .map(|(index, image)| gallery_image_row(url, module_name, index, image)) | ||
| .collect(), | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| fn gallery_image_row( | ||
| gallery_url: &str, | ||
| module_name: &str, | ||
| index: usize, | ||
| image: &GalleryImage, | ||
| ) -> ResolvedLinkDto { | ||
| let image_url = image.url.trim(); | ||
| if !is_http_image_url(image_url) { | ||
| let label = image | ||
| .title | ||
| .as_deref() | ||
| .filter(|t| !t.trim().is_empty()) | ||
| .map(|t| format!("'{t}'")) | ||
| .unwrap_or_else(|| format!("#{}", index + 1)); | ||
| return gallery_error_row( | ||
| gallery_url, | ||
| module_name, | ||
| LinkResolutionErrorKind::Plugin, | ||
| &format!("Gallery item {label} has an invalid image URL"), | ||
| ); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| let filename = image | ||
| .filename | ||
| .clone() | ||
| .filter(|name| !name.trim().is_empty()) | ||
| .or_else(|| extract_filename_from_url(image_url)); | ||
| ResolvedLinkDto { | ||
| id: Uuid::new_v4().to_string(), | ||
| original_url: image_url.to_string(), | ||
|
mpiton marked this conversation as resolved.
|
||
| resolved_url: Some(image_url.to_string()), | ||
| filename, | ||
| size_bytes: None, | ||
| resumable: None, | ||
| status: "online".to_string(), | ||
| error_message: None, | ||
| error_kind: None, | ||
| module_name: module_name.to_string(), | ||
| account_id: None, | ||
| is_media: false, | ||
| media_type: Some("image".to_string()), | ||
| // The gallery provider API already vouched for these URLs; | ||
| // probing every image would fire one HEAD per row for nothing. | ||
| requires_online_probe: false, | ||
| } | ||
| } | ||
|
|
||
| /// `Url::parse` lowercases the scheme, so `HTTPS://…` passes; a bare | ||
| /// `https://` fails on the missing host. | ||
| fn is_http_image_url(raw: &str) -> bool { | ||
| url::Url::parse(raw).is_ok_and(|u| matches!(u.scheme(), "http" | "https") && u.has_host()) | ||
| } | ||
|
|
||
| fn gallery_error_row( | ||
| gallery_url: &str, | ||
| module_name: &str, | ||
| error_kind: LinkResolutionErrorKind, | ||
| message: &str, | ||
| ) -> ResolvedLinkDto { | ||
| ResolvedLinkDto { | ||
| id: Uuid::new_v4().to_string(), | ||
| original_url: gallery_url.to_string(), | ||
| resolved_url: None, | ||
| filename: None, | ||
| size_bytes: None, | ||
| resumable: None, | ||
| status: "error".to_string(), | ||
| error_message: Some(message.to_string()), | ||
| error_kind: Some(error_kind), | ||
| module_name: module_name.to_string(), | ||
| account_id: None, | ||
| is_media: false, | ||
| media_type: None, | ||
| requires_online_probe: false, | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[path = "resolve_links_gallery_tests.rs"] | ||
| mod tests; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.