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>
33 #include <vlc_strings.h>
34 #include <vlc_stream.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
42 #include "../libvlc.h"
43 #include "playlist_internal.h"
45 static void ArtCacheCreateDir( const char *psz_dir )
47 char newdir[strlen( psz_dir ) + 1];
48 strcpy( newdir, psz_dir );
49 char * psz_newdir = newdir;
50 char * psz = psz_newdir;
54 while( *psz && *psz != DIR_SEP_CHAR) psz++;
57 if( !EMPTY_STR( psz_newdir ) )
58 vlc_mkdir( psz_newdir, 0700 );
62 vlc_mkdir( psz_dir, 0700 );
65 static char* ArtCacheGetDirPath( const char *psz_arturl, const char *psz_artist,
66 const char *psz_album )
69 char *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
71 if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
73 char *psz_album_sanitized = strdup( psz_album );
74 filename_sanitize( psz_album_sanitized );
75 char *psz_artist_sanitized = strdup( psz_artist );
76 filename_sanitize( psz_artist_sanitized );
77 if( asprintf( &psz_dir, "%s" DIR_SEP "art" DIR_SEP "artistalbum"
78 DIR_SEP "%s" DIR_SEP "%s", psz_cachedir,
79 psz_artist_sanitized, psz_album_sanitized ) == -1 )
81 free( psz_album_sanitized );
82 free( psz_artist_sanitized );
86 /* If artist or album missing cache by art download URL. The download
87 URL will be md5 hashed to form a valid cache filename. We assume that
88 psz_arturl is always the download URL and not the already hashed filename.
89 (We should never need to call this function if art has already been
90 downloaded anyway). */
93 AddMD5( &md5, psz_arturl, strlen( psz_arturl ) );
95 char * psz_arturl_sanitized = psz_md5_hash( &md5 );
96 if( asprintf( &psz_dir, "%s" DIR_SEP "art" DIR_SEP "arturl" DIR_SEP
97 "%s", psz_cachedir, psz_arturl_sanitized ) == -1 )
99 free( psz_arturl_sanitized );
101 free( psz_cachedir );
105 static char *ArtCachePath( input_item_t *p_item )
107 char* psz_path = NULL;
108 const char *psz_artist;
109 const char *psz_album;
110 const char *psz_arturl;
112 vlc_mutex_lock( &p_item->lock );
114 if( !p_item->p_meta )
115 p_item->p_meta = vlc_meta_New();
116 if( !p_item->p_meta )
119 psz_artist = vlc_meta_Get( p_item->p_meta, vlc_meta_Artist );
120 psz_album = vlc_meta_Get( p_item->p_meta, vlc_meta_Album );
121 psz_arturl = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
123 if( (EMPTY_STR(psz_artist) || EMPTY_STR(psz_album) ) && !psz_arturl )
126 psz_path = ArtCacheGetDirPath( psz_arturl, psz_artist, psz_album );
129 vlc_mutex_unlock( &p_item->lock );
133 static char *ArtCacheName( input_item_t *p_item, const char *psz_type )
135 char *psz_path = ArtCachePath( p_item );
139 ArtCacheCreateDir( psz_path );
141 char *psz_ext = strdup( psz_type ? psz_type : "" );
142 filename_sanitize( psz_ext );
144 if( asprintf( &psz_filename, "%s" DIR_SEP "art%s", psz_path, psz_ext ) < 0 )
154 int playlist_FindArtInCache( input_item_t *p_item )
156 char *psz_path = ArtCachePath( p_item );
161 /* Check if file exists */
162 DIR *p_dir = vlc_opendir( psz_path );
169 bool b_found = false;
171 while( !b_found && (psz_filename = vlc_readdir( p_dir )) )
173 if( !strncmp( psz_filename, "art", 3 ) )
176 if( asprintf( &psz_file, "%s" DIR_SEP "%s",
177 psz_path, psz_filename ) != -1 )
179 char *psz_uri = make_URI( psz_file, "file" );
182 input_item_SetArtURL( p_item, psz_uri );
190 free( psz_filename );
196 return b_found ? VLC_SUCCESS : VLC_EGENERIC;
201 int playlist_SaveArt( playlist_t *p_playlist, input_item_t *p_item,
202 const uint8_t *p_buffer, int i_buffer, const char *psz_type )
204 char *psz_filename = ArtCacheName( p_item, psz_type );
209 char *psz_uri = make_URI( psz_filename, "file" );
212 free( psz_filename );
216 /* Check if we already dumped it */
218 if( !vlc_stat( psz_filename, &s ) )
220 input_item_SetArtURL( p_item, psz_uri );
221 free( psz_filename );
226 /* Dump it otherwise */
227 FILE *f = vlc_fopen( psz_filename, "wb" );
230 if( fwrite( p_buffer, i_buffer, 1, f ) != 1 )
232 msg_Err( p_playlist, "%s: %m", psz_filename );
236 msg_Dbg( p_playlist, "album art saved to %s", psz_filename );
237 input_item_SetArtURL( p_item, psz_uri );
241 free( psz_filename );