1 /*****************************************************************************
2 * meta.c : Metadata handling
3 *****************************************************************************
4 * Copyright (C) 1998-2004 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 *****************************************************************************/
29 #include <vlc_common.h>
30 #include <vlc_input.h>
31 #include <vlc_stream.h>
33 #include <vlc_playlist.h>
34 #include <vlc_charset.h>
35 #include <vlc_strings.h>
36 #include "input_internal.h"
37 #include "../playlist/playlist_internal.h"
39 #include <limits.h> /* PATH_MAX */
42 #ifdef HAVE_SYS_STAT_H
43 # include <sys/stat.h>
46 #include "../libvlc.h"
49 input_MetaTypeToLocalizedString( vlc_meta_type_t meta_type )
53 case vlc_meta_Title: return _("Title");
54 case vlc_meta_Artist: return _("Artist");
55 case vlc_meta_Genre: return _("Genre");
56 case vlc_meta_Copyright: return _("Copyright");
57 case vlc_meta_Album: return _("Album");
58 case vlc_meta_TrackNumber: return _("Track number");
59 case vlc_meta_Description: return _("Description");
60 case vlc_meta_Rating: return _("Rating");
61 case vlc_meta_Date: return _("Date");
62 case vlc_meta_Setting: return _("Setting");
63 case vlc_meta_URL: return _("URL");
64 case vlc_meta_Language: return _("Language");
65 case vlc_meta_NowPlaying: return _("Now Playing");
66 case vlc_meta_Publisher: return _("Publisher");
67 case vlc_meta_EncodedBy: return _("Encoded by");
68 case vlc_meta_ArtworkURL: return _("Artwork URL");
69 case vlc_meta_TrackID: return _("Track ID");
75 #define input_FindArtInCache(a,b) __input_FindArtInCache(VLC_OBJECT(a),b)
76 static int __input_FindArtInCache( vlc_object_t *, input_item_t *p_item );
79 * 0 : Art is in cache or is a local file
80 * 1 : Art found, need to download
81 * -X : Error/not found
83 int input_ArtFind( playlist_t *p_playlist, input_item_t *p_item )
85 int i_ret = VLC_EGENERIC;
87 char *psz_title, *psz_artist, *psz_album;
89 psz_artist = input_item_GetArtist( p_item );
90 psz_album = input_item_GetAlbum( p_item );
91 psz_title = input_item_GetTitle( p_item );
93 psz_title = input_item_GetName( p_item );
95 if( !psz_title && !psz_artist && !psz_album )
100 /* If we already checked this album in this session, skip */
101 if( psz_artist && psz_album )
103 FOREACH_ARRAY( playlist_album_t album, pl_priv(p_playlist)->fetcher.albums )
104 if( !strcmp( album.psz_artist, psz_artist ) &&
105 !strcmp( album.psz_album, psz_album ) )
107 msg_Dbg( p_playlist, " %s - %s has already been searched",
108 psz_artist, psz_album );
109 /* TODO-fenrir if we cache art filename too, we can go faster */
114 if( !strncmp( album.psz_arturl, "file://", 7 ) )
115 input_item_SetArtURL( p_item, album.psz_arturl );
116 else /* Actually get URL from cache */
117 input_FindArtInCache( p_playlist, p_item );
130 input_FindArtInCache( p_playlist, p_item );
132 char *psz_arturl = input_item_GetArtURL( p_item );
135 /* We already have an URL */
136 if( !strncmp( psz_arturl, "file://", strlen( "file://" ) ) )
139 return 0; /* Art is in cache, no need to go further */
144 /* Art need to be put in cache */
149 p_playlist->p_private = p_item;
150 psz_album = input_item_GetAlbum( p_item );
151 psz_artist = input_item_GetArtist( p_item );
152 psz_title = input_item_GetTitle( p_item );
154 psz_title = input_item_GetName( p_item );
156 if( psz_album && psz_artist )
158 msg_Dbg( p_playlist, "searching art for %s - %s",
159 psz_artist, psz_album );
163 msg_Dbg( p_playlist, "searching art for %s",
168 p_module = module_need( p_playlist, "art finder", 0, false );
173 msg_Dbg( p_playlist, "unable to find art" );
175 /* Record this album */
176 if( psz_artist && psz_album )
179 a.psz_artist = psz_artist;
180 a.psz_album = psz_album;
181 a.psz_arturl = input_item_GetArtURL( p_item );
182 a.b_found = (i_ret == VLC_EGENERIC ? false : true );
183 ARRAY_APPEND( pl_priv(p_playlist)->fetcher.albums, a );
192 module_unneed( p_playlist, p_module );
193 p_playlist->p_private = NULL;
199 static void ArtCacheCreateDir( const char *psz_dir )
201 char newdir[strlen( psz_dir ) + 1];
202 strcpy( newdir, psz_dir );
203 char * psz_newdir = newdir;
204 char * psz = psz_newdir;
208 while( *psz && *psz != DIR_SEP_CHAR) psz++;
211 if( !EMPTY_STR( psz_newdir ) )
212 utf8_mkdir( psz_newdir, 0700 );
216 utf8_mkdir( psz_dir, 0700 );
219 static char * ArtCacheGetSanitizedFileName( const char *psz )
221 char *dup = strdup(psz);
224 filename_sanitize( dup );
226 /* Doesn't create a filename with invalid characters
227 * TODO: several filesystems forbid several characters: list them all
229 for( i = 0; dup[i] != '\0'; i++ )
231 if( dup[i] == DIR_SEP_CHAR )
237 #define ArtCacheGetDirPath(a,b,c,d,e) __ArtCacheGetDirPath(VLC_OBJECT(a),b,c,d,e)
238 static void __ArtCacheGetDirPath( vlc_object_t *p_obj,
240 const char *psz_title,
241 const char *psz_artist, const char *psz_album )
244 char *psz_cachedir = config_GetCacheDir();
246 if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
248 char * psz_album_sanitized = ArtCacheGetSanitizedFileName( psz_album );
249 char * psz_artist_sanitized = ArtCacheGetSanitizedFileName( psz_artist );
250 snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
251 "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
252 psz_cachedir, psz_artist_sanitized, psz_album_sanitized );
253 free( psz_album_sanitized );
254 free( psz_artist_sanitized );
258 char * psz_title_sanitized = ArtCacheGetSanitizedFileName( psz_title );
259 snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
260 "art" DIR_SEP "title" DIR_SEP "%s",
261 psz_cachedir, psz_title_sanitized );
262 free( psz_title_sanitized );
264 free( psz_cachedir );
269 #define ArtCacheGetFilePath(a,b,c,d,e,f) __ArtCacheGetFilePath(VLC_OBJECT(a),b,c,d,e,f)
270 static void __ArtCacheGetFilePath( vlc_object_t *p_obj,
272 const char *psz_title,
273 const char *psz_artist, const char *psz_album,
274 const char *psz_extension )
276 char psz_dir[PATH_MAX+1];
278 ArtCacheGetDirPath( p_obj, psz_dir, psz_title, psz_artist, psz_album );
282 psz_ext = strndup( psz_extension, 6 );
283 filename_sanitize( psz_ext );
285 else psz_ext = strdup( "" );
287 snprintf( psz_filename, PATH_MAX, "file://%s" DIR_SEP "art%s",
293 static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
298 char psz_dirpath[PATH_MAX+1];
299 char psz_filepath[PATH_MAX+1];
303 psz_artist = input_item_GetArtist( p_item );
304 psz_album = input_item_GetAlbum( p_item );
305 psz_title = input_item_GetTitle( p_item );
306 if( !psz_title ) psz_title = input_item_GetName( p_item );
308 if( !psz_title && ( !psz_album || !psz_artist ) )
316 ArtCacheGetDirPath( p_obj, psz_dirpath, psz_title,
317 psz_artist, psz_album );
323 /* Check if file exists */
324 p_dir = utf8_opendir( psz_dirpath );
328 while( (psz_filename = utf8_readdir( p_dir )) )
330 if( !strncmp( psz_filename, "art", 3 ) )
332 snprintf( psz_filepath, PATH_MAX, "file://%s" DIR_SEP "%s",
333 psz_dirpath, psz_filename );
334 input_item_SetArtURL( p_item, psz_filepath );
335 free( psz_filename );
339 free( psz_filename );
348 * Download the art using the URL or an art downloaded
349 * This function should be called only if data is not already in cache
351 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
353 int i_status = VLC_EGENERIC;
355 char psz_filename[PATH_MAX+1];
356 char *psz_artist = NULL;
357 char *psz_album = NULL;
358 char *psz_title = NULL;
362 psz_artist = input_item_GetArtist( p_item );
363 psz_album = input_item_GetAlbum( p_item );
364 psz_title = input_item_GetTitle( p_item );
366 psz_title = input_item_GetName( p_item );
368 if( !psz_title && (!psz_artist || !psz_album) )
376 psz_arturl = input_item_GetArtURL( p_item );
377 assert( !EMPTY_STR( psz_arturl ) );
379 if( !strncmp( psz_arturl , "file://", 7 ) )
381 msg_Dbg( p_playlist, "Album art is local file, no need to cache" );
385 else if( !strncmp( psz_arturl , "APIC", 4 ) )
387 msg_Warn( p_playlist, "APIC fetch not supported yet" );
392 psz_type = strrchr( psz_arturl, '.' );
393 if( psz_type && strlen( psz_type ) > 5 )
394 psz_type = NULL; /* remove extension if it's > to 4 characters */
396 /* Warning: psz_title, psz_artist, psz_album may change in ArtCache*() */
398 ArtCacheGetDirPath( p_playlist, psz_filename, psz_title, psz_artist,
400 ArtCacheCreateDir( psz_filename );
401 ArtCacheGetFilePath( p_playlist, psz_filename, psz_title, psz_artist,
402 psz_album, psz_type );
408 p_stream = stream_UrlNew( p_playlist, psz_arturl );
411 uint8_t p_buffer[65536];
413 FILE *p_file = utf8_fopen( psz_filename+7, "w" );
414 if( p_file == NULL ) {
415 msg_Err( p_playlist, "Unable write album art in %s",
421 while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
423 if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
429 if( fclose( p_file ) && !err )
431 stream_Delete( p_stream );
436 msg_Err( p_playlist, "%s: %m", psz_filename );
439 msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
441 input_item_SetArtURL( p_item, psz_filename );
442 i_status = VLC_SUCCESS;
448 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
450 input_item_t *p_item = p_input->p->p_item;
451 const char *psz_arturl;
452 const char *psz_artist = NULL;
453 const char *psz_album = NULL;
454 const char *psz_title = NULL;
455 char *psz_type = NULL;
456 char psz_filename[PATH_MAX+1];
458 input_attachment_t *p_attachment;
462 /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH
463 * and then set it here to to be faster */
465 psz_arturl = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
467 if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
469 msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
473 if( input_item_IsArtFetched( p_item ) )
475 /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
477 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
478 input_FindArtInCache( p_input, p_item );
483 for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ )
485 if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
486 &psz_arturl[strlen("attachment://")] ) )
488 p_attachment = p_input->p->attachment[i_idx];
492 if( !p_attachment || p_attachment->i_data <= 0 )
494 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
498 psz_artist = vlc_meta_Get( p_item->p_meta, vlc_meta_Artist );
499 psz_album = vlc_meta_Get( p_item->p_meta, vlc_meta_Album );
500 psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
501 if( !strcmp( p_attachment->psz_mime, "image/jpeg" ) )
502 psz_type = strdup( ".jpg" );
503 else if( !strcmp( p_attachment->psz_mime, "image/png" ) )
504 psz_type = strdup( ".png" );
507 psz_title = p_item->psz_name;
509 if( (!psz_artist || !psz_album ) && !psz_title )
515 ArtCacheGetDirPath( p_input, psz_filename, psz_title, psz_artist, psz_album );
516 ArtCacheCreateDir( psz_filename );
517 ArtCacheGetFilePath( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
520 /* Check if we already dumped it */
521 if( !utf8_stat( psz_filename+7, &s ) )
523 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_filename );
527 f = utf8_fopen( psz_filename+7, "w" );
530 if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 )
531 msg_Err( p_input, "%s: %m", psz_filename );
534 msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
535 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_filename );