]> git.sesse.net Git - vlc/blob - src/input/meta.c
Fix potential segfault.
[vlc] / src / input / meta.c
1 /*****************************************************************************
2  * meta.c : Metadata handling
3  *****************************************************************************
4  * Copyright (C) 1998-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea@videolan.org>
8  *          ClĂ©ment Stenac <zorglub@videolan.org
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <vlc_common.h>
30 #include <vlc_input.h>
31 #include <vlc_stream.h>
32 #include <vlc_meta.h>
33 #include <vlc_playlist.h>
34 #include <vlc_charset.h>
35 #include <vlc_strings.h>
36 #include "../playlist/playlist_internal.h"
37 #include <errno.h>
38 #include <limits.h>                                             /* PATH_MAX */
39 #include <assert.h>
40
41 #ifdef HAVE_SYS_STAT_H
42 #   include <sys/stat.h>
43 #endif
44
45 #include "../libvlc.h"
46
47 const char *
48 input_MetaTypeToLocalizedString( vlc_meta_type_t meta_type )
49 {
50     switch( meta_type )
51     {
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");
69
70     default: abort();
71     }
72 };
73
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 );
76
77 /* Return codes:
78  *   0 : Art is in cache or is a local file
79  *   1 : Art found, need to download
80  *  -X : Error/not found
81  */
82 int input_ArtFind( playlist_t *p_playlist, input_item_t *p_item )
83 {
84     int i_ret = VLC_EGENERIC;
85     module_t *p_module;
86     char *psz_title, *psz_artist, *psz_album;
87
88     psz_artist = input_item_GetArtist( p_item );
89     psz_album = input_item_GetAlbum( p_item );
90     psz_title = input_item_GetTitle( p_item );
91     if(!psz_title)
92         psz_title = input_item_GetName( p_item );
93
94     if( !psz_title && !psz_artist && !psz_album )
95         return VLC_EGENERIC;
96
97     free( psz_title );
98
99     /* If we already checked this album in this session, skip */
100     if( psz_artist && psz_album )
101     {
102         FOREACH_ARRAY( playlist_album_t album, p_playlist->p_fetcher->albums )
103             if( !strcmp( album.psz_artist, psz_artist ) &&
104                 !strcmp( album.psz_album, psz_album ) )
105             {
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 */
109                 free( psz_artist );
110                 free( psz_album );
111                 if( album.b_found )
112                 {
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 );
117                     return 0;
118                 }
119                 else
120                 {
121                     return VLC_EGENERIC;
122                 }
123             }
124         FOREACH_END();
125     }
126     free( psz_artist );
127     free( psz_album );
128
129     input_FindArtInCache( p_playlist, p_item );
130
131     char *psz_arturl = input_item_GetArtURL( p_item );
132     if( psz_arturl )
133     {
134         /* We already have an URL */
135         if( !strncmp( psz_arturl, "file://", strlen( "file://" ) ) )
136         {
137             free( psz_arturl );
138             return 0; /* Art is in cache, no need to go further */
139         }
140
141         free( psz_arturl );
142         
143         /* Art need to be put in cache */
144         return 1;
145     }
146
147     PL_LOCK;
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 );
152     if( !psz_title )
153         psz_title = input_item_GetName( p_item );
154
155     if( psz_album && psz_artist )
156     {
157         msg_Dbg( p_playlist, "searching art for %s - %s",
158              psz_artist, psz_album );
159     }
160     else
161     {
162         msg_Dbg( p_playlist, "searching art for %s",
163              psz_title );
164     }
165     free( psz_title );
166
167     p_module = module_Need( p_playlist, "art finder", 0, false );
168
169     if( p_module )
170         i_ret = 1;
171     else
172         msg_Dbg( p_playlist, "unable to find art" );
173
174     /* Record this album */
175     if( psz_artist && psz_album )
176     {
177         playlist_album_t a;
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( p_playlist->p_fetcher->albums, a );
183     }
184     else
185     {
186         free( psz_artist );
187         free( psz_album );
188     }
189
190     if( p_module )
191         module_Unneed( p_playlist, p_module );
192     p_playlist->p_private = NULL;
193     PL_UNLOCK;
194
195     return i_ret;
196 }
197
198 static void ArtCacheCreateDir( const char *psz_dir )
199 {
200     char newdir[strlen( psz_dir ) + 1];
201     strcpy( newdir, psz_dir );
202     char * psz_newdir = newdir;
203     char * psz = psz_newdir;
204
205     while( *psz )
206     {
207         while( *psz && *psz != DIR_SEP_CHAR) psz++;
208         if( !*psz ) break;
209         *psz = 0;
210         if( !EMPTY_STR( psz_newdir ) )
211             utf8_mkdir( psz_newdir, 0700 );
212         *psz = DIR_SEP_CHAR;
213         psz++;
214     }
215     utf8_mkdir( psz_dir, 0700 );
216 }
217
218 static char * ArtCacheGetSanitizedFileName( const char *psz )
219 {
220     char *dup = strdup(psz);
221     int i;
222
223     filename_sanitize( dup );
224
225     /* Doesn't create a filename with invalid characters
226      * TODO: several filesystems forbid several characters: list them all
227      */
228     for( i = 0; dup[i] != '\0'; i++ )
229     {
230         if( dup[i] == DIR_SEP_CHAR )
231             dup[i] = ' ';
232     }
233     return dup;
234 }
235
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,
238                                   char *psz_dir,
239                                   const char *psz_title,
240                                   const char *psz_artist, const char *psz_album )
241 {
242    char *psz_cachedir = config_GetCacheDir();
243
244     if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
245     {
246         char * psz_album_sanitized = ArtCacheGetSanitizedFileName( psz_album );
247         char * psz_artist_sanitized = ArtCacheGetSanitizedFileName( psz_artist );
248         snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
249                   "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
250                   psz_cachedir, psz_artist_sanitized, psz_album_sanitized );
251         free( psz_album_sanitized );
252         free( psz_artist_sanitized );
253     }
254     else
255     {
256         char * psz_title_sanitized = ArtCacheGetSanitizedFileName( psz_title );
257         snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
258                   "art" DIR_SEP "title" DIR_SEP "%s",
259                   psz_cachedir, psz_title_sanitized );
260         free( psz_title_sanitized );
261     }
262     free( psz_cachedir );
263 }
264
265
266
267 #define ArtCacheGetFilePath(a,b,c,d,e,f) __ArtCacheGetFilePath(VLC_OBJECT(a),b,c,d,e,f)
268 static void __ArtCacheGetFilePath( vlc_object_t *p_obj,
269                                    char * psz_filename,
270                                    const char *psz_title,
271                                    const char *psz_artist, const char *psz_album,
272                                    const char *psz_extension )
273 {
274     char psz_dir[PATH_MAX+1];
275     char * psz_ext;
276     ArtCacheGetDirPath( p_obj, psz_dir, psz_title, psz_artist, psz_album );
277
278     if( psz_extension )
279     {
280         psz_ext = strndup( psz_extension, 6 );
281         filename_sanitize( psz_ext );
282     }
283     else psz_ext = strdup( "" );
284
285     snprintf( psz_filename, PATH_MAX, "file://%s" DIR_SEP "art%s",
286               psz_dir, psz_ext );
287
288     free( psz_ext );
289 }
290
291 static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item )
292 {
293     char *psz_artist;
294     char *psz_album;
295     char *psz_title;
296     char psz_dirpath[PATH_MAX+1];
297     char psz_filepath[PATH_MAX+1];
298     char * psz_filename;
299     DIR * p_dir;
300
301     psz_artist = input_item_GetArtist( p_item );
302     psz_album = input_item_GetAlbum( p_item );
303     psz_title = input_item_GetTitle( p_item );
304     if( !psz_title ) psz_title = input_item_GetName( p_item );
305
306     if( !psz_title && ( !psz_album || !psz_artist ) )
307     {
308         free( psz_artist );
309         free( psz_album );
310         free( psz_title );
311         return VLC_EGENERIC;
312     }
313
314     ArtCacheGetDirPath( p_obj, psz_dirpath, psz_title,
315                            psz_artist, psz_album );
316
317     free( psz_artist );
318     free( psz_album );
319     free( psz_title );
320
321     /* Check if file exists */
322     p_dir = utf8_opendir( psz_dirpath );
323     if( !p_dir )
324         return VLC_EGENERIC;
325
326     while( (psz_filename = utf8_readdir( p_dir )) )
327     {
328         if( !strncmp( psz_filename, "art", 3 ) )
329         {
330             snprintf( psz_filepath, PATH_MAX, "file://%s" DIR_SEP "%s",
331                       psz_dirpath, psz_filename );
332             input_item_SetArtURL( p_item, psz_filepath );
333             free( psz_filename );
334             closedir( p_dir );
335             return VLC_SUCCESS;
336         }
337         free( psz_filename );
338     }
339
340     /* Not found */
341     closedir( p_dir );
342     return VLC_EGENERIC;
343 }
344
345 /**
346  * Download the art using the URL or an art downloaded
347  * This function should be called only if data is not already in cache
348  */
349 int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item )
350 {
351     int i_status = VLC_EGENERIC;
352     stream_t *p_stream;
353     char psz_filename[PATH_MAX+1];
354     char *psz_artist = NULL;
355     char *psz_album = NULL;
356     char *psz_title = NULL;
357     char *psz_arturl;
358     char *psz_type;
359
360     psz_artist = input_item_GetArtist( p_item );
361     psz_album = input_item_GetAlbum( p_item );
362     psz_title = input_item_GetTitle( p_item );
363     if( !psz_title )
364         psz_title = input_item_GetName( p_item );
365
366     if( !psz_title && (!psz_artist || !psz_album) )
367     {
368         free( psz_title );
369         free( psz_album );
370         free( psz_artist );
371         return VLC_EGENERIC;
372     }
373
374     psz_arturl = input_item_GetArtURL( p_item );
375     assert( !EMPTY_STR( psz_arturl ) );
376
377     if( !strncmp( psz_arturl , "file://", 7 ) )
378     {
379         msg_Dbg( p_playlist, "Album art is local file, no need to cache" );
380         free( psz_arturl );
381         return VLC_SUCCESS;
382     }
383     else if( !strncmp( psz_arturl , "APIC", 4 ) )
384     {
385         msg_Warn( p_playlist, "APIC fetch not supported yet" );
386         free( psz_arturl );
387         return VLC_EGENERIC;
388     }
389
390     psz_type = strrchr( psz_arturl, '.' );
391     if( psz_type && strlen( psz_type ) > 5 )
392         psz_type = NULL; /* remove extension if it's > to 4 characters */
393
394     /* Warning: psz_title, psz_artist, psz_album may change in ArtCache*() */
395
396     ArtCacheGetDirPath( p_playlist, psz_filename, psz_title, psz_artist,
397                         psz_album );
398     ArtCacheCreateDir( psz_filename );
399     ArtCacheGetFilePath( p_playlist, psz_filename, psz_title, psz_artist,
400                          psz_album, psz_type );
401
402     free( psz_artist );
403     free( psz_album );
404     free( psz_title );
405
406     p_stream = stream_UrlNew( p_playlist, psz_arturl );
407     if( p_stream )
408     {
409         uint8_t p_buffer[65536];
410         long int l_read;
411         FILE *p_file = utf8_fopen( psz_filename+7, "w" );
412         if( p_file == NULL ) {
413             msg_Err( p_playlist, "Unable write album art in %s",
414                      psz_filename + 7 );
415             free( psz_arturl );
416             return VLC_EGENERIC;
417         }
418         int err = 0;
419         while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) )
420         {
421             if( fwrite( p_buffer, l_read, 1, p_file ) != 1 )
422             {
423                 err = errno;
424                 break;
425             }
426         }
427         if( fclose( p_file ) && !err )
428             err = errno;
429         stream_Delete( p_stream );
430
431         if( err )
432         {
433             errno = err;
434             msg_Err( p_playlist, "%s: %m", psz_filename );
435         }
436         else
437             msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
438
439         input_item_SetArtURL( p_item, psz_filename );
440         i_status = VLC_SUCCESS;
441     }
442     free( psz_arturl );
443     return i_status;
444 }
445
446 void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input )
447 {
448     input_item_t *p_item = p_input->p->input.p_item;
449     const char *psz_arturl;
450     const char *psz_artist = NULL;
451     const char *psz_album = NULL;
452     const char *psz_title = NULL;
453     char *psz_type = NULL;
454     char psz_filename[PATH_MAX+1];
455     FILE *f;
456     input_attachment_t *p_attachment;
457     struct stat s;
458     int i_idx;
459
460     /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH
461      * and then set it here to to be faster */
462
463     psz_arturl = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
464
465     if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) )
466     {
467         msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
468         return;
469     }
470
471     if( input_item_IsArtFetched( p_item ) )
472     {
473         /* XXX Weird, we should not have end up with attachment:// art url unless there is a race
474          * condition */
475         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
476         input_FindArtInCache( p_input, p_item );
477         return;
478     }
479
480     /* */
481     for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ )
482     {
483         if( !strcmp( p_input->p->attachment[i_idx]->psz_name,
484                      &psz_arturl[strlen("attachment://")] ) )
485         {
486             p_attachment = p_input->p->attachment[i_idx];
487             break;
488         }
489     }
490     if( !p_attachment || p_attachment->i_data <= 0 )
491     {
492         msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" );
493         return;
494     }
495
496     psz_artist = vlc_meta_Get( p_item->p_meta, vlc_meta_Artist );
497     psz_album = vlc_meta_Get( p_item->p_meta, vlc_meta_Album );
498     psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
499     if( !strcmp( p_attachment->psz_mime, "image/jpeg" ) )
500         psz_type = strdup( ".jpg" );
501     else if( !strcmp( p_attachment->psz_mime, "image/png" ) )
502         psz_type = strdup( ".png" );
503
504     if( !psz_title )
505         psz_title = p_item->psz_name;
506
507     if( (!psz_artist || !psz_album ) && !psz_title )
508         return;
509
510     ArtCacheGetDirPath( p_input, psz_filename, psz_title, psz_artist, psz_album );
511     ArtCacheCreateDir( psz_filename );
512     ArtCacheGetFilePath( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type );
513     free( psz_type );
514
515     /* Check if we already dumped it */
516     if( !utf8_stat( psz_filename+7, &s ) )
517     {
518         vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_filename );
519         return;
520     }
521
522     f = utf8_fopen( psz_filename+7, "w" );
523     if( f )
524     {
525         if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 )
526             msg_Err( p_input, "%s: %m", psz_filename );
527         else
528         {
529             msg_Dbg( p_input, "album art saved to %s\n", psz_filename );
530             vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_filename );
531         }
532         fclose( f );
533     }
534 }