]> git.sesse.net Git - vlc/blob - src/playlist/art.c
Clean up preparser/fetcher code.
[vlc] / src / playlist / art.c
1 /*****************************************************************************
2  * art.c : Art metadata handling
3  *****************************************************************************
4  * Copyright (C) 1998-2008 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 <assert.h>
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
36 #ifdef HAVE_SYS_STAT_H
37 #   include <sys/stat.h>
38 #endif
39
40 #include "../libvlc.h"
41 #include "playlist_internal.h"
42
43 static void ArtCacheCreateDir( const char *psz_dir )
44 {
45     char newdir[strlen( psz_dir ) + 1];
46     strcpy( newdir, psz_dir );
47     char * psz_newdir = newdir;
48     char * psz = psz_newdir;
49
50     while( *psz )
51     {
52         while( *psz && *psz != DIR_SEP_CHAR) psz++;
53         if( !*psz ) break;
54         *psz = 0;
55         if( !EMPTY_STR( psz_newdir ) )
56             utf8_mkdir( psz_newdir, 0700 );
57         *psz = DIR_SEP_CHAR;
58         psz++;
59     }
60     utf8_mkdir( psz_dir, 0700 );
61 }
62
63 static char *ArtCacheGetSanitizedFileName( const char *psz )
64 {
65     char *dup = strdup(psz);
66     int i;
67
68     filename_sanitize( dup );
69
70     /* Doesn't create a filename with invalid characters
71      * TODO: several filesystems forbid several characters: list them all
72      */
73     for( i = 0; dup[i] != '\0'; i++ )
74     {
75         if( dup[i] == DIR_SEP_CHAR )
76             dup[i] = ' ';
77     }
78     return dup;
79 }
80
81 static void ArtCacheGetDirPath( char *psz_dir,
82                                 const char *psz_title,
83                                 const char *psz_artist, const char *psz_album )
84 {
85     char *psz_cachedir = config_GetCacheDir();
86
87     if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
88     {
89         char * psz_album_sanitized = ArtCacheGetSanitizedFileName( psz_album );
90         char * psz_artist_sanitized = ArtCacheGetSanitizedFileName( psz_artist );
91         snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
92                   "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
93                   psz_cachedir, psz_artist_sanitized, psz_album_sanitized );
94         free( psz_album_sanitized );
95         free( psz_artist_sanitized );
96     }
97     else
98     {
99         char * psz_title_sanitized = ArtCacheGetSanitizedFileName( psz_title );
100         snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
101                   "art" DIR_SEP "title" DIR_SEP "%s",
102                   psz_cachedir, psz_title_sanitized );
103         free( psz_title_sanitized );
104     }
105     free( psz_cachedir );
106 }
107
108 static char *ArtCachePath( input_item_t *p_item )
109 {
110     char psz_path[PATH_MAX+1]; /* FIXME */
111
112     vlc_mutex_lock( &p_item->lock );
113
114     if( !p_item->p_meta )
115         p_item->p_meta = vlc_meta_New();
116     if( !p_item->p_meta )
117     {
118         vlc_mutex_unlock( &p_item->lock );
119         return NULL;
120     }
121
122     const char *psz_artist = vlc_meta_Get( p_item->p_meta, vlc_meta_Artist );
123     const char *psz_album = vlc_meta_Get( p_item->p_meta, vlc_meta_Album );
124     const char *psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
125
126     if( !psz_title )
127         psz_title = p_item->psz_name;
128
129     if( (!psz_artist || !psz_album ) && !psz_title )
130     {
131         vlc_mutex_unlock( &p_item->lock );
132         return NULL;
133     }
134
135     ArtCacheGetDirPath( psz_path, psz_title, psz_artist, psz_album );
136
137     vlc_mutex_unlock( &p_item->lock );
138
139     return strdup( psz_path );
140 }
141
142 static char *ArtCacheName( input_item_t *p_item, const char *psz_type )
143 {
144     char *psz_path = ArtCachePath( p_item );
145     if( !psz_path )
146         return NULL;
147
148     ArtCacheCreateDir( psz_path );
149
150     char *psz_ext = strdup( psz_type ? psz_type : "" );
151     filename_sanitize( psz_ext );
152
153     char *psz_filename;
154     if( asprintf( &psz_filename, "file://%s" DIR_SEP "art%s", psz_path, psz_ext ) < 0 )
155         psz_filename = NULL;
156
157     free( psz_ext );
158     free( psz_path );
159
160     return psz_filename;
161 }
162
163 int playlist_FindArtInCache( input_item_t *p_item )
164 {
165     char *psz_path = ArtCachePath( p_item );
166
167     if( !psz_path )
168         return VLC_EGENERIC;
169
170     /* Check if file exists */
171     DIR *p_dir = utf8_opendir( psz_path );
172     if( !p_dir )
173     {
174         free( psz_path );
175         return VLC_EGENERIC;
176     }
177
178     bool b_found = false;
179     char *psz_filename;
180     while( !b_found && (psz_filename = utf8_readdir( p_dir )) )
181     {
182         if( !strncmp( psz_filename, "art", 3 ) )
183         {
184             char *psz_file;
185             if( asprintf( &psz_file, "file://%s" DIR_SEP "%s",
186                           psz_path, psz_filename ) < 0 )
187                 psz_file = NULL;
188             if( psz_file )
189                 input_item_SetArtURL( p_item, psz_file );
190             free( psz_file );
191
192             b_found = true;
193         }
194         free( psz_filename );
195     }
196
197     /* */
198     closedir( p_dir );
199     free( psz_path );
200     return b_found ? VLC_SUCCESS : VLC_EGENERIC;
201 }
202
203 /**
204  * Download the art using the URL or an art downloaded
205  * This function should be called only if data is not already in cache
206  */
207 int playlist_DownloadArt( playlist_t *p_playlist, input_item_t *p_item )
208 {
209     char *psz_arturl = input_item_GetArtURL( p_item );
210     assert( *psz_arturl );
211
212     if( !strncmp( psz_arturl , "file://", 7 ) )
213     {
214         msg_Dbg( p_playlist, "Album art is local file, no need to cache" );
215         free( psz_arturl );
216         return VLC_SUCCESS;
217     }
218
219     if( !strncmp( psz_arturl , "APIC", 4 ) )
220     {
221         msg_Warn( p_playlist, "APIC fetch not supported yet" );
222         goto error;
223     }
224
225     stream_t *p_stream = stream_UrlNew( p_playlist, psz_arturl );
226     if( !p_stream )
227         goto error;
228
229     uint8_t *p_data = NULL;
230     int i_data = 0;
231     for( ;; )
232     {
233         int i_read = 65536;
234
235         if( i_data + i_read <= i_data ) /* Protect gainst overflow */
236             break;
237
238         p_data = realloc( p_data, i_data + i_read );
239         if( !p_data )
240             break;
241
242         i_read = stream_Read( p_stream, &p_data[i_data], i_read );
243         if( i_read <= 0 )
244             break;
245
246         i_data += i_read;
247     }
248     stream_Delete( p_stream );
249
250     if( p_data && i_data > 0 )
251     {
252         char *psz_type = strrchr( psz_arturl, '.' );
253         if( psz_type && strlen( psz_type ) > 5 )
254             psz_type = NULL; /* remove extension if it's > to 4 characters */
255
256         playlist_SaveArt( p_playlist, p_item, p_data, i_data, psz_type );
257     }
258
259     free( p_data );
260
261     free( psz_arturl );
262     return VLC_SUCCESS;
263
264 error:
265     free( psz_arturl );
266     return VLC_EGENERIC;
267 }
268
269 /* */
270 int playlist_SaveArt( playlist_t *p_playlist, input_item_t *p_item,
271                       const uint8_t *p_buffer, int i_buffer, const char *psz_type )
272 {
273     char *psz_filename = ArtCacheName( p_item, psz_type );
274
275     if( !psz_filename )
276         return VLC_EGENERIC;
277
278     /* Check if we already dumped it */
279     struct stat s;
280     if( !utf8_stat( psz_filename+7, &s ) )
281     {
282         input_item_SetArtURL( p_item, psz_filename );
283         return VLC_SUCCESS;
284     }
285
286     /* Dump it otherwise */
287     FILE *f = utf8_fopen( psz_filename+7, "w" );
288     if( f )
289     {
290         if( fwrite( p_buffer, i_buffer, 1, f ) != 1 )
291         {
292             msg_Err( p_playlist, "%s: %m", psz_filename );
293         }
294         else
295         {
296             msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
297             input_item_SetArtURL( p_item, psz_filename );
298         }
299         fclose( f );
300     }
301     return VLC_SUCCESS;
302 }
303