access: http: only warn on deflate errors
[vlc.git] / src / playlist / art.c
blob9a93762ee044de5136486fa8e8c31cab9e2fe26b
1 /*****************************************************************************
2 * art.c : Art metadata handling
3 *****************************************************************************
4 * Copyright (C) 1998-2008 VLC authors and VideoLAN
5 * $Id$
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <assert.h>
30 #include <sys/stat.h>
32 #include <vlc_common.h>
33 #include <vlc_playlist.h>
34 #include <vlc_fs.h>
35 #include <vlc_strings.h>
36 #include <vlc_stream.h>
37 #include <vlc_url.h>
38 #include <vlc_md5.h>
40 #include "../libvlc.h"
41 #include "playlist_internal.h"
43 static void ArtCacheCreateDir( const char *psz_dir )
45 char newdir[strlen( psz_dir ) + 1];
46 strcpy( newdir, psz_dir );
47 char * psz_newdir = newdir;
48 char * psz = psz_newdir;
50 while( *psz )
52 while( *psz && *psz != DIR_SEP_CHAR) psz++;
53 if( !*psz ) break;
54 *psz = 0;
55 if( !EMPTY_STR( psz_newdir ) )
56 vlc_mkdir( psz_newdir, 0700 );
57 *psz = DIR_SEP_CHAR;
58 psz++;
60 vlc_mkdir( psz_dir, 0700 );
63 static char* ArtCacheGetDirPath( const char *psz_arturl, const char *psz_artist,
64 const char *psz_album, const char *psz_title )
66 char *psz_dir;
67 char *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
69 if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
71 char *psz_album_sanitized = strdup( psz_album );
72 filename_sanitize( psz_album_sanitized );
73 char *psz_artist_sanitized = strdup( psz_artist );
74 filename_sanitize( psz_artist_sanitized );
75 if( asprintf( &psz_dir, "%s" DIR_SEP "art" DIR_SEP "artistalbum"
76 DIR_SEP "%s" DIR_SEP "%s", psz_cachedir,
77 psz_artist_sanitized, psz_album_sanitized ) == -1 )
78 psz_dir = NULL;
79 free( psz_album_sanitized );
80 free( psz_artist_sanitized );
82 else
84 /* If artist or album are missing, cache by art download URL.
85 * If the URL is an attachment://, add the title to the cache name.
86 * It will be md5 hashed to form a valid cache filename.
87 * We assume that psz_arturl is always the download URL and not the
88 * already hashed filename.
89 * (We should never need to call this function if art has already been
90 * downloaded anyway).
92 struct md5_s md5;
93 InitMD5( &md5 );
94 AddMD5( &md5, psz_arturl, strlen( psz_arturl ) );
95 if( !strncmp( psz_arturl, "attachment://", 13 ) )
96 AddMD5( &md5, psz_title, strlen( psz_title ) );
97 EndMD5( &md5 );
98 char * psz_arturl_sanitized = psz_md5_hash( &md5 );
99 if( asprintf( &psz_dir, "%s" DIR_SEP "art" DIR_SEP "arturl" DIR_SEP
100 "%s", psz_cachedir, psz_arturl_sanitized ) == -1 )
101 psz_dir = NULL;
102 free( psz_arturl_sanitized );
104 free( psz_cachedir );
105 return psz_dir;
108 static char *ArtCachePath( input_item_t *p_item )
110 char* psz_path = NULL;
111 const char *psz_artist;
112 const char *psz_album;
113 const char *psz_arturl;
114 const char *psz_title;
116 vlc_mutex_lock( &p_item->lock );
118 if( !p_item->p_meta )
119 p_item->p_meta = vlc_meta_New();
120 if( !p_item->p_meta )
121 goto end;
123 psz_artist = vlc_meta_Get( p_item->p_meta, vlc_meta_Artist );
124 psz_album = vlc_meta_Get( p_item->p_meta, vlc_meta_Album );
125 psz_arturl = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
126 psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
127 if( !psz_title )
128 psz_title = p_item->psz_name;
131 if( (EMPTY_STR(psz_artist) || EMPTY_STR(psz_album) ) && !psz_arturl )
132 goto end;
134 psz_path = ArtCacheGetDirPath( psz_arturl, psz_artist, psz_album, psz_title );
136 end:
137 vlc_mutex_unlock( &p_item->lock );
138 return psz_path;
141 static char *ArtCacheName( input_item_t *p_item, const char *psz_type )
143 char *psz_path = ArtCachePath( p_item );
144 if( !psz_path )
145 return NULL;
147 ArtCacheCreateDir( psz_path );
149 char *psz_ext = strdup( psz_type ? psz_type : "" );
150 filename_sanitize( psz_ext );
151 char *psz_filename;
152 if( asprintf( &psz_filename, "%s" DIR_SEP "art%s", psz_path, psz_ext ) < 0 )
153 psz_filename = NULL;
155 free( psz_ext );
156 free( psz_path );
158 return psz_filename;
161 /* */
162 int playlist_FindArtInCache( input_item_t *p_item )
164 char *psz_path = ArtCachePath( p_item );
166 if( !psz_path )
167 return VLC_EGENERIC;
169 /* Check if file exists */
170 DIR *p_dir = vlc_opendir( psz_path );
171 if( !p_dir )
173 free( psz_path );
174 return VLC_EGENERIC;
177 bool b_found = false;
178 char *psz_filename;
179 while( !b_found && (psz_filename = vlc_readdir( p_dir )) )
181 if( !strncmp( psz_filename, "art", 3 ) )
183 char *psz_file;
184 if( asprintf( &psz_file, "%s" DIR_SEP "%s",
185 psz_path, psz_filename ) != -1 )
187 char *psz_uri = vlc_path2uri( psz_file, "file" );
188 if( psz_uri )
190 input_item_SetArtURL( p_item, psz_uri );
191 free( psz_uri );
193 free( psz_file );
196 b_found = true;
198 free( psz_filename );
201 /* */
202 closedir( p_dir );
203 free( psz_path );
204 return b_found ? VLC_SUCCESS : VLC_EGENERIC;
207 static char * GetDirByItemUIDs( char *psz_uid )
209 char *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
210 char *psz_dir;
211 if( asprintf( &psz_dir, "%s" DIR_SEP
212 "by-iiuid" DIR_SEP
213 "%s",
214 psz_cachedir, psz_uid ) == -1 )
216 psz_dir = NULL;
218 free( psz_cachedir );
219 return psz_dir;
222 static char * GetFileByItemUID( char *psz_dir, const char *psz_type )
224 char *psz_file;
225 if( asprintf( &psz_file, "%s" DIR_SEP "%s", psz_dir, psz_type ) == -1 )
227 psz_file = NULL;
229 return psz_file;
232 int playlist_FindArtInCacheUsingItemUID( input_item_t *p_item )
234 char *uid = input_item_GetInfo( p_item, "uid", "md5" );
235 if ( ! *uid )
237 free( uid );
238 return VLC_EGENERIC;
241 /* we have an input item uid set */
242 bool b_done = false;
243 char *psz_byuiddir = GetDirByItemUIDs( uid );
244 char *psz_byuidfile = GetFileByItemUID( psz_byuiddir, "arturl" );
245 free( psz_byuiddir );
246 if( psz_byuidfile )
248 FILE *fd = vlc_fopen( psz_byuidfile, "rb" );
249 if ( fd )
251 char sz_cachefile[2049];
252 /* read the cache hash url */
253 if ( fgets( sz_cachefile, 2048, fd ) != NULL )
255 input_item_SetArtURL( p_item, sz_cachefile );
256 b_done = true;
258 fclose( fd );
260 free( psz_byuidfile );
262 free( uid );
263 if ( b_done ) return VLC_SUCCESS;
265 return VLC_EGENERIC;
268 /* */
269 int playlist_SaveArt( vlc_object_t *obj, input_item_t *p_item,
270 const void *data, size_t length, const char *psz_type )
272 char *psz_filename = ArtCacheName( p_item, psz_type );
274 if( !psz_filename )
275 return VLC_EGENERIC;
277 char *psz_uri = vlc_path2uri( psz_filename, "file" );
278 if( !psz_uri )
280 free( psz_filename );
281 return VLC_EGENERIC;
284 /* Check if we already dumped it */
285 struct stat s;
286 if( !vlc_stat( psz_filename, &s ) )
288 input_item_SetArtURL( p_item, psz_uri );
289 free( psz_filename );
290 free( psz_uri );
291 return VLC_SUCCESS;
294 /* Dump it otherwise */
295 FILE *f = vlc_fopen( psz_filename, "wb" );
296 if( f )
298 if( fwrite( data, 1, length, f ) != length )
300 msg_Err( obj, "%s: %m", psz_filename );
302 else
304 msg_Dbg( obj, "album art saved to %s", psz_filename );
305 input_item_SetArtURL( p_item, psz_uri );
307 fclose( f );
309 free( psz_uri );
311 /* save uid info */
312 char *uid = input_item_GetInfo( p_item, "uid", "md5" );
313 if ( ! *uid )
315 free( uid );
316 goto end;
319 char *psz_byuiddir = GetDirByItemUIDs( uid );
320 char *psz_byuidfile = GetFileByItemUID( psz_byuiddir, "arturl" );
321 ArtCacheCreateDir( psz_byuiddir );
322 free( psz_byuiddir );
324 if ( psz_byuidfile )
326 f = vlc_fopen( psz_byuidfile, "wb" );
327 if ( f )
329 if( fputs( "file://", f ) < 0 || fputs( psz_filename, f ) < 0 )
330 msg_Err( obj, "Error writing %s: %m", psz_byuidfile );
331 fclose( f );
333 free( psz_byuidfile );
335 free( uid );
336 /* !save uid info */
337 end:
338 free( psz_filename );
339 return VLC_SUCCESS;