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 "../playlist/playlist_internal.h"
38 #include <limits.h> /* PATH_MAX */
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
45 #include "../libvlc.h"
48 input_MetaTypeToLocalizedString( vlc_meta_type_t meta_type )
52 case vlc_meta_Title: return _("Title");
53 case vlc_meta_Artist: return _("Artist");
54 case vlc_meta_Genre: return _("Genre");
55 case vlc_meta_Copyright: return _("Copyright");
56 case vlc_meta_Album: return _("Album");
57 case vlc_meta_TrackNumber: return _("Track number");
58 case vlc_meta_Description: return _("Description");
59 case vlc_meta_Rating: return _("Rating");
60 case vlc_meta_Date: return _("Date");
61 case vlc_meta_Setting: return _("Setting");
62 case vlc_meta_URL: return _("URL");
63 case vlc_meta_Language: return _("Language");
64 case vlc_meta_NowPlaying: return _("Now Playing");
65 case vlc_meta_Publisher: return _("Publisher");
66 case vlc_meta_EncodedBy: return _("Encoded by");
67 case vlc_meta_ArtworkURL: return _("Artwork URL");
68 case vlc_meta_TrackID: return _("Track ID");
74 #define input_FindArtInCache(a,b) __input_FindArtInCache(VLC_OBJECT(a),b)
75 static int __input_FindArtInCache( vlc_object_t *, input_item_t *p_item );
78 * 0 : Art is in cache or is a local file
79 * 1 : Art found, need to download
80 * -X : Error/not found
82 int input_ArtFind( playlist_t *p_playlist, input_item_t *p_item )
84 int i_ret = VLC_EGENERIC;
86 char *psz_title, *psz_artist, *psz_album;
88 psz_artist = input_item_GetArtist( p_item );
89 psz_album = input_item_GetAlbum( p_item );
90 psz_title = input_item_GetTitle( p_item );
92 psz_title = input_item_GetName( p_item );
94 if( !psz_title && !psz_artist && !psz_album )
99 /* If we already checked this album in this session, skip */
100 if( psz_artist && psz_album )
102 FOREACH_ARRAY( playlist_album_t album, pl_priv(p_playlist)->fetcher.albums )
103 if( !strcmp( album.psz_artist, psz_artist ) &&
104 !strcmp( album.psz_album, psz_album ) )
106 msg_Dbg( p_playlist, " %s - %s has already been searched",
107 psz_artist, psz_album );
108 /* TODO-fenrir if we cache art filename too, we can go faster */
113 if( !strncmp( album.psz_arturl, "file://", 7 ) )
114 input_item_SetArtURL( p_item, album.psz_arturl );
115 else /* Actually get URL from cache */
116 input_FindArtInCache( p_playlist, p_item );
129 input_FindArtInCache( p_playlist, p_item );
131 char *psz_arturl = input_item_GetArtURL( p_item );
134 /* We already have an URL */
135 if( !strncmp( psz_arturl, "file://", strlen( "file://" ) ) )
138 return 0; /* Art is in cache, no need to go further */
143 /* Art need to be put in cache */
148 p_playlist->p_private = p_item;
149 psz_album = input_item_GetAlbum( p_item );
150 psz_artist = input_item_GetArtist( p_item );
151 psz_title = input_item_GetTitle( p_item );
153 psz_title = input_item_GetName( p_item );
155 if( psz_album && psz_artist )
157 msg_Dbg( p_playlist, "searching art for %s - %s",
158 psz_artist, psz_album );
162 msg_Dbg( p_playlist, "searching art for %s",
167 p_module = module_need( p_playlist, "art finder", 0, false );
172 msg_Dbg( p_playlist, "unable to find art" );
174 /* Record this album */
175 if( psz_artist && psz_album )
178 a.psz_artist = psz_artist;
179 a.psz_album = psz_album;
180 a.psz_arturl = input_item_GetArtURL( p_item );
181 a.b_found = (i_ret == VLC_EGENERIC ? false : true );
182 ARRAY_APPEND( pl_priv(p_playlist)->fetcher.albums, a );
191 module_unneed( p_playlist, p_module );
192 p_playlist->p_private = NULL;
198 static void ArtCacheCreateDir( const char *psz_dir )
200 char newdir[strlen( psz_dir ) + 1];
201 strcpy( newdir, psz_dir );
202 char * psz_newdir = newdir;
203 char * psz = psz_newdir;
207 while( *psz && *psz != DIR_SEP_CHAR) psz++;
210 if( !EMPTY_STR( psz_newdir ) )
211 utf8_mkdir( psz_newdir, 0700 );
215 utf8_mkdir( psz_dir, 0700 );
218 static char * ArtCacheGetSanitizedFileName( const char *psz )
220 char *dup = strdup(psz);
223 filename_sanitize( dup );
225 /* Doesn't create a filename with invalid characters
226 * TODO: several filesystems forbid several characters: list them all
228 for( i = 0; dup[i] != '\0'; i++ )
230 if( dup[i] == DIR_SEP_CHAR )
236 #define ArtCacheGetDirPath(a,b,c,d,e) __ArtCacheGetDirPath(VLC_OBJECT(a),b,c,d,e)
237 static void __ArtCacheGetDirPath( vlc_object_t *p_obj,
239 const char *psz_title,
240 const char *psz_artist, const char *psz_album )
243 char *psz_cachedir = config_GetCacheDir();
245 if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
247 char * psz_album_sanitized = ArtCacheGetSanitizedFileName( psz_album );
248 char * psz_artist_sanitized = ArtCacheGetSanitizedFileName( psz_artist );
249 snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
250 "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
251 psz_cachedir, psz_artist_sanitized, psz_album_sanitized );
252 free( psz_album_sanitized );
253 free( psz_artist_sanitized );
257 char * psz_title_sanitized = ArtCacheGetSanitizedFileName( psz_title );
258 snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
259 "art" DIR_SEP "title" DIR_SEP "%s",
260 psz_cachedir, psz_title_sanitized );
261 free( psz_title_sanitized );
263 free( psz_cachedir );
268 #define ArtCacheGetFilePath(a,b,c,d,e,f) __ArtCacheGetFilePath(VLC_OBJECT(a),b,c,d,e,f)
269 static void __ArtCacheGetFilePath( vlc_object_t *p_obj,
271 const char *psz_title,
272 const char *psz_artist, const char *psz_album,
273 const char *psz_extension )
275 char psz_dir[PATH_MAX+1];
277 ArtCacheGetDirPath( p_obj, psz_dir, psz_title, psz_artist, psz_album );
281 psz_ext = strndup( psz_extension, 6 );
282 filename_sanitize( psz_ext );
284 else psz_ext = strdup( "" );
286 snprintf( psz_filename, PATH_MAX, "file://%s" DIR_SEP "art%s",
292 static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
297 char psz_dirpath[PATH_MAX+1];
298 char psz_filepath[PATH_MAX+1];
302 psz_artist = input_item_GetArtist( p_item );
303 psz_album = input_item_GetAlbum( p_item );
304 psz_title = input_item_GetTitle( p_item );
305 if( !psz_title ) psz_title = input_item_GetName( p_item );
307 if( !psz_title && ( !psz_album || !psz_artist ) )
315 ArtCacheGetDirPath( p_obj, psz_dirpath, psz_title,
316 psz_artist, psz_album );
322 /* Check if file exists */
323 p_dir = utf8_opendir( psz_dirpath );
327 while( (psz_filename = utf8_readdir( p_dir )) )
329 if( !strncmp( psz_filename, "art", 3 ) )
331 snprintf( psz_filepath, PATH_MAX, "file://%s" DIR_SEP "%s",
332 psz_dirpath, psz_filename );
333 input_item_SetArtURL( p_item, psz_filepath );
334 free( psz_filename );
338 free( psz_filename );
347 * Download the art using the URL or an art downloaded
348 * This function should be called only if data is not already in cache
350 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
352 int i_status = VLC_EGENERIC;
354 char psz_filename[PATH_MAX+1];
355 char *psz_artist = NULL;
356 char *psz_album = NULL;
357 char *psz_title = NULL;
361 psz_artist = input_item_GetArtist( p_item );
362 psz_album = input_item_GetAlbum( p_item );
363 psz_title = input_item_GetTitle( p_item );
365 psz_title = input_item_GetName( p_item );
367 if( !psz_title && (!psz_artist || !psz_album) )
375 psz_arturl = input_item_GetArtURL( p_item );
376 assert( !EMPTY_STR( psz_arturl ) );
378 if( !strncmp( psz_arturl , "file://", 7 ) )
380 msg_Dbg( p_playlist, "Album art is local file, no need to cache" );
384 else if( !strncmp( psz_arturl , "APIC", 4 ) )
386 msg_Warn( p_playlist, "APIC fetch not supported yet" );
391 psz_type = strrchr( psz_arturl, '.' );
392 if( psz_type && strlen( psz_type ) > 5 )
393 psz_type = NULL; /* remove extension if it's > to 4 characters */
395 /* Warning: psz_title, psz_artist, psz_album may change in ArtCache*() */
397 ArtCacheGetDirPath( p_playlist, psz_filename, psz_title, psz_artist,
399 ArtCacheCreateDir( psz_filename );
400 ArtCacheGetFilePath( p_playlist, psz_filename, psz_title, psz_artist,
401 psz_album, psz_type );
407 p_stream = stream_UrlNew( p_playlist, psz_arturl );
410 uint8_t p_buffer[65536];
412 FILE *p_file = utf8_fopen( psz_filename+7, "w" );
413 if( p_file == NULL ) {
414 msg_Err( p_playlist, "Unable write album art in %s",
420 while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
422 if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
428 if( fclose( p_file ) && !err )
430 stream_Delete( p_stream );
435 msg_Err( p_playlist, "%s: %m", psz_filename );
438 msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
440 input_item_SetArtURL( p_item, psz_filename );
441 i_status = VLC_SUCCESS;
447 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
449 input_item_t *p_item = p_input->p->input.p_item;
450 const char *psz_arturl;
451 const char *psz_artist = NULL;
452 const char *psz_album = NULL;
453 const char *psz_title = NULL;
454 char *psz_type = NULL;
455 char psz_filename[PATH_MAX+1];
457 input_attachment_t *p_attachment;
461 /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH
462 * and then set it here to to be faster */
464 psz_arturl = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
466 if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
468 msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
472 if( input_item_IsArtFetched( p_item ) )
474 /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
476 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
477 input_FindArtInCache( p_input, p_item );
482 for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ )
484 if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
485 &psz_arturl[strlen("attachment://")] ) )
487 p_attachment = p_input->p->attachment[i_idx];
491 if( !p_attachment || p_attachment->i_data <= 0 )
493 msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
497 psz_artist = vlc_meta_Get( p_item->p_meta, vlc_meta_Artist );
498 psz_album = vlc_meta_Get( p_item->p_meta, vlc_meta_Album );
499 psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
500 if( !strcmp( p_attachment->psz_mime, "image/jpeg" ) )
501 psz_type = strdup( ".jpg" );
502 else if( !strcmp( p_attachment->psz_mime, "image/png" ) )
503 psz_type = strdup( ".png" );
506 psz_title = p_item->psz_name;
508 if( (!psz_artist || !psz_album ) && !psz_title )
514 ArtCacheGetDirPath( p_input, psz_filename, psz_title, psz_artist, psz_album );
515 ArtCacheCreateDir( psz_filename );
516 ArtCacheGetFilePath( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
519 /* Check if we already dumped it */
520 if( !utf8_stat( psz_filename+7, &s ) )
522 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_filename );
526 f = utf8_fopen( psz_filename+7, "w" );
529 if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 )
530 msg_Err( p_input, "%s: %m", psz_filename );
533 msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
534 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_filename );