1 /*****************************************************************************
2 * art.c : Art metadata handling
3 *****************************************************************************
4 * Copyright (C) 1998-2008 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea@videolan.org>
8 * Clément Stenac <zorglub@videolan.org
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_playlist.h>
32 #include <vlc_charset.h>
33 #include <vlc_strings.h>
34 #include <vlc_stream.h>
35 #include <limits.h> /* PATH_MAX */
37 #ifdef HAVE_SYS_STAT_H
38 # include <sys/stat.h>
41 #include "../libvlc.h"
42 #include "playlist_internal.h"
44 static void ArtCacheCreateDir( const char *psz_dir )
46 char newdir[strlen( psz_dir ) + 1];
47 strcpy( newdir, psz_dir );
48 char * psz_newdir = newdir;
49 char * psz = psz_newdir;
53 while( *psz && *psz != DIR_SEP_CHAR) psz++;
56 if( !EMPTY_STR( psz_newdir ) )
57 utf8_mkdir( psz_newdir, 0700 );
61 utf8_mkdir( psz_dir, 0700 );
64 static char *ArtCacheGetSanitizedFileName( const char *psz )
66 char *dup = strdup(psz);
69 filename_sanitize( dup );
71 /* Doesn't create a filename with invalid characters
72 * TODO: several filesystems forbid several characters: list them all
74 for( i = 0; dup[i] != '\0'; i++ )
76 if( dup[i] == DIR_SEP_CHAR )
82 static void ArtCacheGetDirPath( char *psz_dir,
83 const char *psz_title,
84 const char *psz_artist, const char *psz_album )
86 char *psz_cachedir = config_GetCacheDir();
88 if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
90 char * psz_album_sanitized = ArtCacheGetSanitizedFileName( psz_album );
91 char * psz_artist_sanitized = ArtCacheGetSanitizedFileName( psz_artist );
92 snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
93 "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
94 psz_cachedir, psz_artist_sanitized, psz_album_sanitized );
95 free( psz_album_sanitized );
96 free( psz_artist_sanitized );
100 char * psz_title_sanitized = ArtCacheGetSanitizedFileName( psz_title );
101 snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
102 "art" DIR_SEP "title" DIR_SEP "%s",
103 psz_cachedir, psz_title_sanitized );
104 free( psz_title_sanitized );
106 free( psz_cachedir );
109 static char *ArtCachePath( input_item_t *p_item )
111 char psz_path[PATH_MAX+1]; /* FIXME */
113 vlc_mutex_lock( &p_item->lock );
115 if( !p_item->p_meta )
116 p_item->p_meta = vlc_meta_New();
117 if( !p_item->p_meta )
119 vlc_mutex_unlock( &p_item->lock );
123 const char *psz_artist = vlc_meta_Get( p_item->p_meta, vlc_meta_Artist );
124 const char *psz_album = vlc_meta_Get( p_item->p_meta, vlc_meta_Album );
125 const char *psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
128 psz_title = p_item->psz_name;
130 if( (!psz_artist || !psz_album ) && !psz_title )
132 vlc_mutex_unlock( &p_item->lock );
136 ArtCacheGetDirPath( psz_path, psz_title, psz_artist, psz_album );
138 vlc_mutex_unlock( &p_item->lock );
140 return strdup( psz_path );
143 static char *ArtCacheName( input_item_t *p_item, const char *psz_type )
145 char *psz_path = ArtCachePath( p_item );
149 ArtCacheCreateDir( psz_path );
151 char *psz_ext = strdup( psz_type ? psz_type : "" );
152 filename_sanitize( psz_ext );
155 if( asprintf( &psz_filename, "file://%s" DIR_SEP "art%s", psz_path, psz_ext ) < 0 )
165 int playlist_FindArtInCache( input_item_t *p_item )
167 char *psz_path = ArtCachePath( p_item );
172 /* Check if file exists */
173 DIR *p_dir = utf8_opendir( psz_path );
180 bool b_found = false;
182 while( !b_found && (psz_filename = utf8_readdir( p_dir )) )
184 if( !strncmp( psz_filename, "art", 3 ) )
187 if( asprintf( &psz_file, "file://%s" DIR_SEP "%s",
188 psz_path, psz_filename ) < 0 )
191 input_item_SetArtURL( p_item, psz_file );
196 free( psz_filename );
202 return b_found ? VLC_SUCCESS : VLC_EGENERIC;
207 int playlist_SaveArt( playlist_t *p_playlist, input_item_t *p_item,
208 const uint8_t *p_buffer, int i_buffer, const char *psz_type )
210 char *psz_filename = ArtCacheName( p_item, psz_type );
215 /* Check if we already dumped it */
217 if( !utf8_stat( psz_filename+7, &s ) )
219 input_item_SetArtURL( p_item, psz_filename );
223 /* Dump it otherwise */
224 FILE *f = utf8_fopen( psz_filename+7, "wb" );
227 if( fwrite( p_buffer, i_buffer, 1, f ) != 1 )
229 msg_Err( p_playlist, "%s: %m", psz_filename );
233 msg_Dbg( p_playlist, "album art saved to %s", psz_filename );
234 input_item_SetArtURL( p_item, psz_filename );