Commit 40ad7fd9 authored by Pierre d'Herbemont's avatar Pierre d'Herbemont

input/meta.c: Better art finding.

parent 18cff171
...@@ -222,97 +222,88 @@ int input_ArtFind( playlist_t *p_playlist, input_item_t *p_item ) ...@@ -222,97 +222,88 @@ int input_ArtFind( playlist_t *p_playlist, input_item_t *p_item )
#ifndef MAX_PATH #ifndef MAX_PATH
# define MAX_PATH 250 # define MAX_PATH 250
#endif #endif
#define ArtCacheCreateName(a,b,c,d,e,f) __ArtCacheCreateName(VLC_OBJECT(a),b,c,d,e,f) static void ArtCacheCreateDir( const char *psz_dir )
static void __ArtCacheCreateName( vlc_object_t *p_obj,
char psz_filename[MAX_PATH+1],
char *psz_title,
char *psz_artist, char *psz_album,
const char *psz_extension )
{ {
char *psz_ext; char newdir[MAX_PATH+1];
if( psz_extension ) strcpy( newdir, psz_dir );
char * psz_newdir = newdir;
char * psz = psz_newdir;
while( *psz_newdir )
{ {
psz_ext = strndup( psz_extension, 6 ); while( *psz && *psz != '/') psz++;
filename_sanitize( psz_ext ); if( !*psz ) break;
*psz = 0;
if( !EMPTY_STR( psz_newdir ) ) utf8_mkdir( psz_newdir );
psz_newdir = psz+1;
} }
else psz_ext = strdup( "" ); }
if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
{ static char *ArtCacheCreateString( const char *psz )
filename_sanitize( psz_title ); {
filename_sanitize( psz_artist ); char *dup = strdup(psz);
snprintf( psz_filename, MAX_PATH, int i;
"file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art" DIR_SEP "artistalbum"
DIR_SEP "%s" DIR_SEP "%s" DIR_SEP "art%s", /* Doesn't create a filename with invalid characters
p_obj->p_libvlc->psz_homedir, * TODO: several filesystems forbid several characters: list them all
psz_artist, psz_album, psz_ext ); */
} for( i = 0; dup[i] != '\0'; i++ )
else
{ {
/* We will use the psz_title name to store the art */ if( dup[i] == '/' )
filename_sanitize( psz_title ); dup[i] = ' ';
snprintf( psz_filename, MAX_PATH,
"file://%s" DIR_SEP CONFIG_DIR DIR_SEP "art" DIR_SEP "title"
DIR_SEP "%s" DIR_SEP "art%s",
p_obj->p_libvlc->psz_homedir,
psz_title, psz_ext );
} }
free( psz_ext ); return dup;
} }
#define ArtCacheCreatePath(a,b,c,d) __ArtCacheCreatePath(VLC_OBJECT(a),b,c,d)
static void __ArtCacheCreatePath( vlc_object_t *p_obj, #define ArtCacheGetDirName(a,b,c,d,e) __ArtCacheGetDirName(VLC_OBJECT(a),b,c,d,e)
static void __ArtCacheGetDirName( vlc_object_t *p_obj,
char *psz_dir,
char *psz_title, char *psz_title,
char *psz_artist, char *psz_album ) char *psz_artist, char *psz_album )
{ {
char psz_dir[MAX_PATH+1];
snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR,
p_obj->p_libvlc->psz_homedir );
utf8_mkdir( psz_dir );
snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP "art",
p_obj->p_libvlc->psz_homedir );
utf8_mkdir( psz_dir );
if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) ) if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
{ {
snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP filename_sanitize( psz_title );
"art" DIR_SEP "artistalbum", filename_sanitize( psz_artist );
p_obj->p_libvlc->psz_homedir );
utf8_mkdir( psz_dir );
snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
"art" DIR_SEP "artistalbum" DIR_SEP "%s",
p_obj->p_libvlc->psz_homedir, psz_artist );
utf8_mkdir( psz_dir );
snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
"art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s", "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
p_obj->p_libvlc->psz_homedir, p_obj->p_libvlc->psz_homedir,
psz_artist, psz_album ); psz_artist, psz_album );
utf8_mkdir( psz_dir );
} }
else else
{ {
snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP filename_sanitize( psz_title );
"art" DIR_SEP "title",
p_obj->p_libvlc->psz_homedir );
utf8_mkdir( psz_dir );
snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP snprintf( psz_dir, MAX_PATH, "%s" DIR_SEP CONFIG_DIR DIR_SEP
"art" DIR_SEP "title" DIR_SEP "%s", "art" DIR_SEP "title" DIR_SEP "%s",
p_obj->p_libvlc->psz_homedir, psz_title ); p_obj->p_libvlc->psz_homedir, psz_title );
utf8_mkdir( psz_dir );
} }
} }
static char *ArtCacheCreateString( const char *psz )
#define ArtCacheGetFileName(a,b,c,d,e,f) __ArtCacheGetFileName(VLC_OBJECT(a),b,c,d,e,f)
static void __ArtCacheGetFileName( vlc_object_t *p_obj,
char * psz_filename,
char *psz_title,
char *psz_artist, char *psz_album,
const char *psz_extension )
{ {
char *dup = strdup(psz); char psz_dir[MAX_PATH+1];
int i; char * psz_ext;
ArtCacheGetDirName( p_obj, psz_dir, psz_title, psz_artist, psz_album );
/* Doesn't create a filename with invalid characters if( psz_extension )
* TODO: several filesystems forbid several characters: list them all
*/
for( i = 0; dup[i] != '\0'; i++ )
{ {
if( dup[i] == '/' ) psz_ext = strndup( psz_extension, 6 );
dup[i] = ' '; filename_sanitize( psz_ext );
} }
return dup; else psz_ext = strdup( "" );
snprintf( psz_filename, MAX_PATH, "file://%s" DIR_SEP "art%s",
psz_dir, psz_ext );
free( psz_ext );
} }
static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item ) static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
...@@ -320,10 +311,10 @@ static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item ) ...@@ -320,10 +311,10 @@ static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
char *psz_artist; char *psz_artist;
char *psz_album; char *psz_album;
char *psz_title; char *psz_title;
char psz_dirname[MAX_PATH+1];
char psz_filename[MAX_PATH+1]; char psz_filename[MAX_PATH+1];
int i; struct dirent * p_de;
struct stat a; DIR * p_dir;
const char *ppsz_type[] = { ".jpg", ".png", ".gif", ".bmp", "" };
if( !p_item->p_meta ) return VLC_EGENERIC; if( !p_item->p_meta ) return VLC_EGENERIC;
...@@ -339,24 +330,32 @@ static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item ) ...@@ -339,24 +330,32 @@ static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
free( psz_title ); free( psz_title );
return VLC_EGENERIC; return VLC_EGENERIC;
} }
ArtCacheGetDirName( p_obj, psz_dirname, psz_title,
psz_artist, psz_album );
free( psz_artist );
free( psz_album );
free( psz_title ); free( psz_title );
for( i = 0; i < 5; i++ ) /* Check if file exists */
{ p_dir = utf8_opendir( psz_dirname );
ArtCacheCreateName( p_obj, psz_filename, psz_title /* Used if none artist nor album is defined */, if( !p_dir )
psz_artist, psz_album, ppsz_type[i] ); return VLC_EGENERIC;
/* Check if file exists */ while( (p_de = readdir( p_dir )) )
if( utf8_stat( psz_filename+7, &a ) == 0 ) {
if( strncmp( p_de->d_name, "art", 3 ) )
{ {
snprintf( psz_filename, MAX_PATH, "%s" DIR_SEP "%s",
psz_dirname, p_de->d_name );
input_item_SetArtURL( p_item, psz_filename ); input_item_SetArtURL( p_item, psz_filename );
free( psz_artist ); closedir( p_dir );
free( psz_album );
return VLC_SUCCESS; return VLC_SUCCESS;
} }
} }
free( psz_artist );
free( psz_album ); closedir( p_dir );
return VLC_EGENERIC; return VLC_EGENERIC;
} }
...@@ -403,14 +402,14 @@ int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item ) ...@@ -403,14 +402,14 @@ int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
if( strlen( psz_type ) > 5 ) if( strlen( psz_type ) > 5 )
psz_type = NULL; /* remove extension if it's > to 4 characters */ psz_type = NULL; /* remove extension if it's > to 4 characters */
/* */ /* Warning: psz_title, psz_artist, psz_album may change in ArtCache*() */
ArtCacheCreateName( p_playlist, psz_filename, psz_title /* Used only if needed*/,
psz_artist, psz_album, psz_type );
/* */ ArtCacheGetDirName( p_playlist, psz_filename, psz_title, psz_artist,
ArtCacheCreatePath( p_playlist, psz_title, psz_artist, psz_album ); psz_album );
ArtCacheCreateDir( psz_filename );
ArtCacheGetFileName( p_playlist, psz_filename, psz_title, psz_artist,
psz_album, psz_type );
/* */
free( psz_artist ); free( psz_artist );
free( psz_album ); free( psz_album );
free( psz_title ); free( psz_title );
...@@ -524,14 +523,15 @@ void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input ) ...@@ -524,14 +523,15 @@ void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
/* */ /* */
psz_type = strrchr( psz_arturl, '.' ); psz_type = strrchr( psz_arturl, '.' );
ArtCacheCreateName( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
ArtCacheGetDirName( p_input, psz_filename, psz_title, psz_artist, psz_album );
ArtCacheCreateDir( psz_filename );
ArtCacheGetFileName( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
/* Check if we already dumped it */ /* Check if we already dumped it */
if( !utf8_stat( psz_filename+7, &s ) ) if( !utf8_stat( psz_filename+7, &s ) )
goto end; goto end;
ArtCacheCreatePath( p_input, psz_title, psz_artist, psz_album );
f = utf8_fopen( psz_filename+7, "w" ); f = utf8_fopen( psz_filename+7, "w" );
if( f ) if( f )
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment