Add Discogs artwork selection UI in album edit modal

Backend:
- Add DiscogsImageOption struct for artwork options
- Add discogs_search_artwork command to search multiple images
- Add discogs_download_artwork command to download selected image
- Extend DiscogsClient with search_artwork_options() method
- Extend DiscogsClient with download_artwork_from_url() method
- Register new commands in lib.rs

Frontend:
- Add Discogs image selection to album edit modal
- Add 'Fetch from Discogs' button with Discogs logo
- Display grid of artwork options from Discogs search results
- Allow user to select preferred artwork option
- Download and save selected artwork on Save
- Add comprehensive CSS styling for Discogs UI elements
- Adjust Discogs logo color filter for proper contrast

User can now click 'Fetch from Discogs', browse multiple artwork
options, select one, and save it as the album's cover art.
This commit is contained in:
vicrodh committed 2026-01-17 00:12:14 -06:00
1 parent dd70f0a8c4
commit dab5c28727
5 files changed
+306

No files matched your search

+99
View File
@@ -32,6 +32,16 @@ pub struct SearchResult {
pub result_type: String,
}
/// Image option for artwork selection
#[derive(Debug, Deserialize, serde::Serialize, Clone)]
pub struct DiscogsImageOption {
pub url: String,
pub width: u32,
pub height: u32,
#[serde(rename = "type")]
pub image_type: String,
}
impl DiscogsClient {
/// Create a new Discogs client (proxy handles credentials)
pub fn new() -> Self {
@@ -135,6 +145,95 @@ impl DiscogsClient {
None
}
/// Search for album artwork options
/// Returns up to 10 image options from the first matching release
pub async fn search_artwork_options(
&self,
artist: &str,
album: &str,
) -> Result<Vec<DiscogsImageOption>, String> {
// Build search query
let query = format!("{} {}", artist, album);
let url = format!(
"{}/search?q={}&type=release",
DISCOGS_PROXY_URL,
urlencoding::encode(&query)
);
log::debug!("Searching Discogs artwork options for: {} - {}", artist, album);
let response = self.client.get(&url).send().await
.map_err(|e| format!("Failed to search Discogs: {}", e))?;
if !response.status().is_success() {
return Err(format!("Discogs search failed with status: {}", response.status()));
}
let search: SearchResponse = response.json().await
.map_err(|e| format!("Failed to parse Discogs response: {}", e))?;
// Collect image options from search results
let mut images = Vec::new();
for result in search.results.iter().take(10) {
if result.result_type == "release" || result.result_type == "master" {
// Add cover image if available
if let Some(cover) = &result.cover_image {
if !cover.is_empty() && !cover.contains("spacer.gif") {
images.push(DiscogsImageOption {
url: cover.clone(),
width: 600, // Discogs standard size
height: 600,
image_type: "primary".to_string(),
});
}
}
// Add thumbnail as alternative
if let Some(thumb) = &result.thumb {
if !thumb.is_empty() && !thumb.contains("spacer.gif") {
images.push(DiscogsImageOption {
url: thumb.clone(),
width: 150, // Discogs thumb size
height: 150,
image_type: "secondary".to_string(),
});
}
}
}
}
if images.is_empty() {
return Err("No artwork found on Discogs".to_string());
}
// Return up to 10 unique images
images.truncate(10);
Ok(images)
}
/// Download image from URL and return local path
pub async fn download_artwork_from_url(
&self,
image_url: &str,
cache_dir: &Path,
artist: &str,
album: &str,
) -> Result<String, String> {
// Generate cache filename
let filename = format!(
"discogs_{:x}.jpg",
Self::simple_hash(&format!("{}_{}", artist, album))
);
let cache_path = cache_dir.join(&filename);
// Download the image
self.download_image(image_url, &cache_path).await
.ok_or_else(|| "Failed to download image".to_string())?;
Ok(cache_path.to_string_lossy().to_string())
}
/// Search for artists and return search results
pub async fn search_artist(&self, query: &str) -> Result<SearchResponse, String> {
let url = format!(
+2
View File
@@ -417,6 +417,8 @@ pub fn run() {
// Discogs artwork commands
library::commands::discogs_has_credentials,
library::commands::discogs_search_artist,
library::commands::discogs_search_artwork,
library::commands::discogs_download_artwork,
library::commands::library_fetch_missing_artwork,
library::commands::library_fetch_album_artwork,
library::commands::library_set_album_artwork,
+29
View File
@@ -1712,3 +1712,32 @@ pub async fn playlist_get_offline_available(
Ok(playlists.iter().map(|p| p.qobuz_playlist_id).collect())
}
// === Discogs Artwork ===
/// Search Discogs for artwork options
#[tauri::command]
pub async fn discogs_search_artwork(
artist: String,
album: String,
) -> Result<Vec<crate::discogs::DiscogsImageOption>, String> {
log::info!("Command: discogs_search_artwork {} - {}", artist, album);
let client = DiscogsClient::new();
client.search_artwork_options(&artist, &album).await
}
/// Download and save Discogs artwork
#[tauri::command]
pub async fn discogs_download_artwork(
image_url: String,
artist: String,
album: String,
) -> Result<String, String> {
log::info!("Command: discogs_download_artwork from {}", image_url);
let cache_dir = get_artwork_cache_dir();
let client = DiscogsClient::new();
client.download_artwork_from_url(&image_url, &cache_dir, &artist, &album).await
}
@@ -113,6 +113,13 @@
lastScan: number | null;
}
interface DiscogsImageOption {
url: string;
width: number;
height: number;
image_type: string;
}
interface Props {
onAlbumClick?: (album: LocalAlbum) => void;
onQobuzArtistClick?: (artistId: number) => void;
@@ -181,6 +188,9 @@
let showAlbumEditModal = $state(false);
let editingAlbumTitle = $state('');
let editingAlbumHidden = $state(false);
let discogsImageOptions = $state<DiscogsImageOption[]>([]);
let selectedDiscogsImage = $state<string | null>(null);
let fetchingDiscogsImages = $state(false);
// Folder selection state (by folder ID)
let selectedFolders = $state<Set<number>>(new Set());
@@ -809,13 +819,54 @@
if (!selectedAlbum) return;
editingAlbumTitle = selectedAlbum.title;
editingAlbumHidden = false;
discogsImageOptions = [];
selectedDiscogsImage = null;
showAlbumEditModal = true;
}
async function fetchDiscogsArtwork() {
if (!selectedAlbum || fetchingDiscogsImages) return;
try {
fetchingDiscogsImages = true;
discogsImageOptions = [];
selectedDiscogsImage = null;
const options = await invoke<DiscogsImageOption[]>('discogs_search_artwork', {
artist: selectedAlbum.artist,
album: selectedAlbum.title
});
discogsImageOptions = options;
console.log(`Found ${options.length} Discogs artwork options`);
} catch (err) {
console.error('Failed to fetch Discogs artwork:', err);
alert(`Failed to fetch Discogs artwork: ${err}`);
} finally {
fetchingDiscogsImages = false;
}
}
async function saveAlbumEdit() {
if (!selectedAlbum) return;
try {
// If a Discogs image was selected, download and set it
if (selectedDiscogsImage) {
const localPath = await invoke<string>('discogs_download_artwork', {
imageUrl: selectedDiscogsImage,
artist: selectedAlbum.artist,
album: selectedAlbum.title
});
await invoke('library_set_album_artwork', {
albumGroupKey: selectedAlbum.id,
artworkPath: localPath
});
applyAlbumArtworkUpdate(selectedAlbum.id, localPath);
}
await invoke('library_set_album_hidden', {
albumGroupKey: selectedAlbum.id,
hidden: editingAlbumHidden
@@ -2148,6 +2199,7 @@
<Disc3 size={24} />
</div>
{/if}
<div class="artwork-actions">
<button
class="secondary-btn"
onclick={handleSetAlbumArtwork}
@@ -2156,8 +2208,36 @@
<Upload size={14} />
<span>{updatingArtwork ? 'Updating...' : 'Change Cover'}</span>
</button>
<button
class="discogs-btn"
onclick={fetchDiscogsArtwork}
disabled={fetchingDiscogsImages}
>
<img src="/discogs_icon.svg" alt="Discogs" class="discogs-icon" />
<span>{fetchingDiscogsImages ? 'Fetching...' : 'Fetch from Discogs'}</span>
</button>
</div>
</div>
</div>
{#if discogsImageOptions.length > 0}
<div class="form-group">
<label>Select Artwork from Discogs</label>
<div class="discogs-options">
{#each discogsImageOptions as option, i}
<button
class="discogs-option"
class:selected={selectedDiscogsImage === option.url}
onclick={() => selectedDiscogsImage = option.url}
>
<img src={option.url} alt={`Option ${i + 1}`} />
<span class="option-info">{option.width}x{option.height}</span>
</button>
{/each}
</div>
<p class="form-hint">Click an image to select it, then click Save</p>
</div>
{/if}
<div class="form-group">
<label class="toggle-label">
@@ -3494,6 +3574,90 @@
color: var(--text-muted);
}
.artwork-actions {
display: flex;
flex-direction: column;
gap: 8px;
}
.discogs-btn {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 16px;
background: var(--bg-tertiary);
border: 1px solid var(--bg-quaternary);
border-radius: 8px;
color: var(--text-primary);
font-size: 13px;
font-weight: 500;
cursor: pointer;
transition: all 150ms ease;
}
.discogs-btn:hover:not(:disabled) {
background: var(--bg-quaternary);
border-color: var(--text-muted);
}
.discogs-btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.discogs-icon {
width: 16px;
height: 16px;
filter: invert(1) brightness(0.8);
}
.discogs-options {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
gap: 12px;
margin-top: 8px;
}
.discogs-option {
position: relative;
aspect-ratio: 1;
padding: 0;
background: var(--bg-tertiary);
border: 2px solid transparent;
border-radius: 8px;
overflow: hidden;
cursor: pointer;
transition: all 150ms ease;
}
.discogs-option:hover {
border-color: var(--text-muted);
transform: scale(1.05);
}
.discogs-option.selected {
border-color: var(--accent);
box-shadow: 0 0 0 2px var(--accent);
}
.discogs-option img {
width: 100%;
height: 100%;
object-fit: cover;
}
.discogs-option .option-info {
position: absolute;
bottom: 0;
left: 0;
right: 0;
padding: 4px 6px;
background: rgba(0, 0, 0, 0.7);
color: white;
font-size: 10px;
text-align: center;
}
.modal-footer {
display: flex;
justify-content: flex-end;
+12
View File
@@ -0,0 +1,12 @@
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" width="900" height="900">
<g transform="matrix(7.9 0 0 7.9 450.48 449.69)">
<path fill="#333" d="M0-54.1c-.2 0-.3 0-.5-.1.2.1.4.1.5.1zM-15.3 0c0-8.5 6.9-15.4 15.4-15.4S15.5-8.5 15.5 0 8.6 15.4.1 15.4-15.3 8.5-15.3 0zm.7 0C-14.6 8.1-8 14.7.1 14.7S14.8 8.1 14.8 0 8.2-14.7.1-14.7c-8.2 0-14.7 6.6-14.7 14.7z"/>
<path fill="#333" d="M2.5 0c0 1.3-1.1 2.4-2.4 2.4-1.3 0-2.4-1.1-2.4-2.4 0-1.3 1.1-2.4 2.4-2.4 1.3 0 2.4 1.1 2.4 2.4z"/>
<path fill="none" d="M.1-20.1C-11-20.1-20-11.1-20 0c0 5.7 2.4 10.8 6.1 14.4l-.1-.1-5.2 5.7c1.8 1.8 3.9 3.3 6.1 4.5l3.8-6.7c2.8 1.5 6 2.3 9.3 2.3 11.1 0 20-9 20-20.1 0-5.4-2.1-10.2-5.5-13.8l5.3-5.7c-1.8-1.8-3.8-3.4-6-4.7l-3.7 6.8c-3-1.7-6.4-2.7-10-2.7zm-25.9 47.3-5.3 5.8c2.8 2.7 6 5 9.5 6.9l3.9-6.9c-3.1-1.6-5.7-3.5-8.1-5.8zm52.3-53.9 5.3-5.8c-2.9-2.8-6.1-5.2-9.6-7.2l-3.8 6.9c3 1.7 5.7 3.8 8.1 6.1zm-6 6.5 5.3-5.7c-2.3-2.3-4.9-4.3-7.8-5.9L14.2-25c2.4 1.3 4.5 2.9 6.3 4.8zm-40.3 40.9-5.3 5.7c2.3 2.2 4.9 4.1 7.8 5.7l3.8-6.8c-2.3-1.2-4.5-2.8-6.3-4.6z"/>
<path fill="#333" d="M-27.7 0c0 7.9 3.3 14.9 8.5 20l5.2-5.7.1.1C-17.7 10.8-20 5.7-20 0c0-11.1 9-20.1 20.1-20.1 3.7 0 7.1 1 10 2.7l3.7-6.8C9.7-26.5 5.1-27.8.1-27.8c-15.3 0-27.8 12.5-27.8 27.8zm47.8 0c0 11.1-9 20.1-20 20.1-3.4 0-6.5-.8-9.3-2.3l-3.8 6.7c3.9 2.1 8.4 3.3 13.1 3.3 15.3 0 27.8-12.5 27.8-27.8 0-7.6-3.1-14.5-8-19.5l-5.3 5.7C18-10.2 20.1-5.4 20.1 0z"/>
<path fill="#333" d="M-36.5 0c0 10.4 4.4 19.8 11.4 26.5l5.3-5.7c-5.5-5.2-8.9-12.6-8.9-20.7 0-15.9 12.9-28.8 28.8-28.8 5.2 0 10 1.4 14.2 3.8l3.8-6.8c-5.3-3-11.4-4.7-18-4.7-20.2-.2-36.6 16.2-36.6 36.4zm65.3 0c0 15.9-12.9 28.8-28.8 28.8-4.9 0-9.5-1.2-13.6-3.4l-3.8 6.8C-12.2 35-6.3 36.6 0 36.6c20.2 0 36.6-16.4 36.6-36.6 0-10.1-4.1-19.3-10.8-25.9l-5.3 5.7C25.7-15 28.8-7.9 28.8 0z"/>
<path fill="#333" d="M37.6 0c0 20.7-16.9 37.6-37.6 37.6-6.5 0-12.6-1.6-17.9-4.5l-3.9 6.9c6.5 3.5 13.9 5.6 21.8 5.6 25.1 0 45.4-20.4 45.4-45.4 0-12.7-5.2-24.2-13.6-32.4l-5.3 5.8c6.9 6.5 11.1 16 11.1 26.4zm-83 0c0 13 5.5 24.7 14.2 33l5.3-5.8C-33.1 20.4-37.6 10.7-37.6 0c0-20.7 16.9-37.6 37.6-37.6 6.7 0 13 1.8 18.4 4.9l3.8-6.9C15.6-43.3 8-45.4 0-45.4-25-45.5-45.4-25.1-45.4 0z"/>
<path fill="#333" d="m37.8-38.8-5.2 5.6c8.6 8.4 14 20.2 14 33.1 0 25.6-20.8 46.4-46.4 46.4-8.1 0-15.6-2.1-22.2-5.7l-3.9 6.9c7.7 4.2 16.6 6.7 26 6.7C30 54.2 54.3 29.9 54.3 0c-.1-15.2-6.4-28.9-16.5-38.8zM-46.4 0c0-25.6 20.8-46.4 46.4-46.4 8.2 0 16 2.2 22.7 6l3.7-6.7C18.8-51.4 10.1-53.9.8-54h-.9C-29.9-54-54-30-54.3-.3v.5c0 15.5 6.6 29.5 17.1 39.4l5.4-5.8c-9-8.6-14.6-20.5-14.6-33.8z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.6 KiB