[vlc-devel] [PATCH 2/2] Differentiate between song and album art cover in art.c

Samo Golež samo.golez at outlook.com
Tue Feb 23 08:13:24 UTC 2021


---
 src/preparser/art.c | 81 +++++++++++++++++++++++++++++++--------------
 1 file changed, 57 insertions(+), 24 deletions(-)

diff --git a/src/preparser/art.c b/src/preparser/art.c
index 9fa9e66..23aa263 100644
--- a/src/preparser/art.c
+++ b/src/preparser/art.c
@@ -56,12 +56,12 @@ static void ArtCacheCreateDir( char *psz_dir )
 
 static char* ArtCacheGetDirPath( const char *psz_arturl, const char *psz_artist,
                                  const char *psz_album,  const char *psz_date,
-                                 const char *psz_title )
+                                 const char *psz_title,  bool b_unique )
 {
     char *psz_dir;
     char *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
 
-    if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
+    if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) && !b_unique )
     {
         char *psz_album_sanitized = strdup( psz_album );
         if (!psz_album_sanitized)
@@ -108,6 +108,11 @@ static char* ArtCacheGetDirPath( const char *psz_arturl, const char *psz_artist,
 
         vlc_hash_md5_t md5;
         vlc_hash_md5_Init( &md5 );
+        if (!psz_arturl)
+        {
+            free( psz_cachedir );
+            return NULL;
+        }
         vlc_hash_md5_Update( &md5, psz_arturl, strlen( psz_arturl ) );
         if( !strncmp( psz_arturl, "attachment://", 13 ) )
             vlc_hash_md5_Update( &md5, psz_title, strlen( psz_title ) );
@@ -120,7 +125,7 @@ static char* ArtCacheGetDirPath( const char *psz_arturl, const char *psz_artist,
     return psz_dir;
 }
 
-static char *ArtCachePath( input_item_t *p_item )
+static char *ArtCachePath( input_item_t *p_item, bool b_unique )
 {
     char* psz_path = NULL;
     const char *psz_artist;
@@ -147,16 +152,16 @@ static char *ArtCachePath( input_item_t *p_item )
     if( (EMPTY_STR(psz_artist) || EMPTY_STR(psz_album) ) && !psz_arturl )
         goto end;
 
-    psz_path = ArtCacheGetDirPath( psz_arturl, psz_artist, psz_album, psz_date, psz_title );
+    psz_path = ArtCacheGetDirPath( psz_arturl, psz_artist, psz_album, psz_date, psz_title, b_unique );
 
 end:
     vlc_mutex_unlock( &p_item->lock );
     return psz_path;
 }
 
-static char *ArtCacheName( input_item_t *p_item, const char *psz_type )
+static char *ArtCacheName( input_item_t *p_item, const char *psz_type, bool b_unique )
 {
-    char *psz_path = ArtCachePath( p_item );
+    char *psz_path = ArtCachePath( p_item, b_unique );
     char *psz_ext = strdup( psz_type ? psz_type : "" );
     char *psz_filename = NULL;
 
@@ -177,9 +182,9 @@ end:
 }
 
 /* */
-int input_FindArtInCache( input_item_t *p_item )
+static int input_FindInCache( input_item_t *p_item, bool b_unique )
 {
-    char *psz_path = ArtCachePath( p_item );
+    char *psz_path = ArtCachePath( p_item, b_unique );
 
     if( !psz_path )
         return VLC_EGENERIC;
@@ -221,6 +226,13 @@ int input_FindArtInCache( input_item_t *p_item )
     return b_found ? VLC_SUCCESS : VLC_EGENERIC;
 }
 
+int input_FindArtInCache( input_item_t *p_item )
+{
+    return (input_FindInCache(p_item, true) == VLC_SUCCESS ||
+           input_FindInCache(p_item, false) == VLC_SUCCESS)
+           ? VLC_SUCCESS : VLC_EGENERIC;
+}
+
 static char * GetDirByItemUIDs( char *psz_uid )
 {
     char *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
@@ -282,11 +294,44 @@ int input_FindArtInCacheUsingItemUID( input_item_t *p_item )
     return VLC_EGENERIC;
 }
 
+static int DumpIt( vlc_object_t *obj, const void *data, size_t length,
+                   char *psz_filename)
+{
+    FILE *f = vlc_fopen( psz_filename, "wb" );
+    if( f )
+    {
+        if( fwrite( data, 1, length, f ) != length )
+        {
+            msg_Err( obj, "%s: %s", psz_filename, vlc_strerror_c(errno) );
+            fclose( f );
+        }
+        else
+        {
+            msg_Dbg( obj, "album art saved to %s", psz_filename );
+            fclose( f );
+            return VLC_SUCCESS;
+        }
+
+    }
+    return VLC_EGENERIC;
+}
+
 /* */
 int input_SaveArt( vlc_object_t *obj, input_item_t *p_item,
                    const void *data, size_t length, const char *psz_type )
 {
-    char *psz_filename = ArtCacheName( p_item, psz_type );
+    // save album
+    char *psz_album_filename = ArtCacheName( p_item, psz_type, false );
+
+    struct stat s_album;
+    if( psz_album_filename && vlc_stat( psz_album_filename, &s_album ) )
+    {
+        DumpIt( obj, data, length, psz_album_filename );
+    }
+    free( psz_album_filename );
+
+    // now save unique
+    char *psz_filename = ArtCacheName( p_item, psz_type, true );
 
     if( !psz_filename )
         return VLC_EGENERIC;
@@ -309,20 +354,8 @@ int input_SaveArt( vlc_object_t *obj, input_item_t *p_item,
     }
 
     /* Dump it otherwise */
-    FILE *f = vlc_fopen( psz_filename, "wb" );
-    if( f )
-    {
-        if( fwrite( data, 1, length, f ) != length )
-        {
-            msg_Err( obj, "%s: %s", psz_filename, vlc_strerror_c(errno) );
-        }
-        else
-        {
-            msg_Dbg( obj, "album art saved to %s", psz_filename );
-            input_item_SetArtURL( p_item, psz_uri );
-        }
-        fclose( f );
-    }
+    if (DumpIt( obj, data, length, psz_filename ) == VLC_SUCCESS)
+        input_item_SetArtURL( p_item, psz_uri );
     free( psz_uri );
 
     /* save uid info */
@@ -340,7 +373,7 @@ int input_SaveArt( vlc_object_t *obj, input_item_t *p_item,
 
     if ( psz_byuidfile )
     {
-        f = vlc_fopen( psz_byuidfile, "wb" );
+        FILE *f = vlc_fopen( psz_byuidfile, "wb" );
         if ( f )
         {
             if( fputs( "file://", f ) < 0 || fputs( psz_filename, f ) < 0 )
-- 
2.27.0



More information about the vlc-devel mailing list