#include <vlc_charset.h>
#include <vlc_strings.h>
#include <vlc_stream.h>
+#include <vlc_url.h>
+
+#include <limits.h> /* PATH_MAX */
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
utf8_mkdir( psz_dir, 0700 );
}
-static char *ArtCacheGetSanitizedFileName( const char *psz )
-{
- char *dup = strdup(psz);
- int i;
-
- filename_sanitize( dup );
-
- /* Doesn't create a filename with invalid characters
- * TODO: several filesystems forbid several characters: list them all
- */
- for( i = 0; dup[i] != '\0'; i++ )
- {
- if( dup[i] == DIR_SEP_CHAR )
- dup[i] = ' ';
- }
- return dup;
-}
-
static void ArtCacheGetDirPath( char *psz_dir,
const char *psz_title,
const char *psz_artist, const char *psz_album )
if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
{
- char * psz_album_sanitized = ArtCacheGetSanitizedFileName( psz_album );
- char * psz_artist_sanitized = ArtCacheGetSanitizedFileName( psz_artist );
+ char *psz_album_sanitized = filename_sanitize( psz_album );
+ char *psz_artist_sanitized = filename_sanitize( psz_artist );
snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
"art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
psz_cachedir, psz_artist_sanitized, psz_album_sanitized );
}
else
{
- char * psz_title_sanitized = ArtCacheGetSanitizedFileName( psz_title );
+ char * psz_title_sanitized = filename_sanitize( psz_title );
snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
"art" DIR_SEP "title" DIR_SEP "%s",
psz_cachedir, psz_title_sanitized );
ArtCacheCreateDir( psz_path );
- char *psz_ext = strdup( psz_type ? psz_type : "" );
- filename_sanitize( psz_ext );
-
+ char *psz_ext = filename_sanitize( psz_type ? psz_type : "" );
char *psz_filename;
- if( asprintf( &psz_filename, "file://%s" DIR_SEP "art%s", psz_path, psz_ext ) < 0 )
+ if( asprintf( &psz_filename, "%s" DIR_SEP "art%s", psz_path, psz_ext ) < 0 )
psz_filename = NULL;
free( psz_ext );
return psz_filename;
}
+/* */
int playlist_FindArtInCache( input_item_t *p_item )
{
char *psz_path = ArtCachePath( p_item );
if( !strncmp( psz_filename, "art", 3 ) )
{
char *psz_file;
- if( asprintf( &psz_file, "file://%s" DIR_SEP "%s",
+ if( asprintf( &psz_file, "%s" DIR_SEP "%s",
psz_path, psz_filename ) < 0 )
psz_file = NULL;
if( psz_file )
- input_item_SetArtURL( p_item, psz_file );
- free( psz_file );
+ {
+ char *psz_uri = make_URI( psz_file );
+ if( psz_uri )
+ {
+ input_item_SetArtURL( p_item, psz_uri );
+ free( psz_uri );
+ }
+ free( psz_file );
+ }
b_found = true;
}
return b_found ? VLC_SUCCESS : VLC_EGENERIC;
}
-/**
- * Download the art using the URL or an art downloaded
- * This function should be called only if data is not already in cache
- */
-int playlist_DownloadArt( playlist_t *p_playlist, input_item_t *p_item )
-{
- char *psz_arturl = input_item_GetArtURL( p_item );
- assert( *psz_arturl );
-
- if( !strncmp( psz_arturl , "file://", 7 ) )
- {
- msg_Dbg( p_playlist, "Album art is local file, no need to cache" );
- free( psz_arturl );
- return VLC_SUCCESS;
- }
-
- if( !strncmp( psz_arturl , "APIC", 4 ) )
- {
- msg_Warn( p_playlist, "APIC fetch not supported yet" );
- goto error;
- }
-
- stream_t *p_stream = stream_UrlNew( p_playlist, psz_arturl );
- if( !p_stream )
- goto error;
-
- uint8_t *p_data = NULL;
- int i_data = 0;
- for( ;; )
- {
- int i_read = 65536;
-
- if( i_data + i_read <= i_data ) /* Protect gainst overflow */
- break;
-
- p_data = realloc( p_data, i_data + i_read );
- if( !p_data )
- break;
-
- i_read = stream_Read( p_stream, &p_data[i_data], i_read );
- if( i_read <= 0 )
- break;
-
- i_data += i_read;
- }
- stream_Delete( p_stream );
-
- if( p_data && i_data > 0 )
- {
- char *psz_type = strrchr( psz_arturl, '.' );
- if( psz_type && strlen( psz_type ) > 5 )
- psz_type = NULL; /* remove extension if it's > to 4 characters */
-
- playlist_SaveArt( p_playlist, p_item, p_data, i_data, psz_type );
- }
-
- free( p_data );
-
- free( psz_arturl );
- return VLC_SUCCESS;
-
-error:
- free( psz_arturl );
- return VLC_EGENERIC;
-}
/* */
int playlist_SaveArt( playlist_t *p_playlist, input_item_t *p_item,
if( !psz_filename )
return VLC_EGENERIC;
+ char *psz_uri = make_URI( psz_filename );
+ if( !psz_uri )
+ {
+ free( psz_filename );
+ return VLC_EGENERIC;
+ }
+
/* Check if we already dumped it */
struct stat s;
- if( !utf8_stat( psz_filename+7, &s ) )
+ if( !utf8_stat( psz_filename, &s ) )
{
- input_item_SetArtURL( p_item, psz_filename );
+ input_item_SetArtURL( p_item, psz_uri );
+ free( psz_filename );
+ free( psz_uri );
return VLC_SUCCESS;
}
/* Dump it otherwise */
- FILE *f = utf8_fopen( psz_filename+7, "w" );
+ FILE *f = utf8_fopen( psz_filename, "wb" );
if( f )
{
if( fwrite( p_buffer, i_buffer, 1, f ) != 1 )
}
else
{
- msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
- input_item_SetArtURL( p_item, psz_filename );
+ msg_Dbg( p_playlist, "album art saved to %s", psz_filename );
+ input_item_SetArtURL( p_item, psz_uri );
}
fclose( f );
}
+ free( psz_filename );
+ free( psz_uri );
return VLC_SUCCESS;
}