gui: macos: use float for rate
[vlc.git] / src / preparser / art.c
blobc6a416b9b3fa4fe87974cdf7389f356d88797a1a
1 /*****************************************************************************
2 * art.c : Art metadata handling
3 *****************************************************************************
4 * Copyright (C) 1998-2008 VLC authors and VideoLAN
6 * Authors: Antoine Cellerier <dionoea@videolan.org>
7 * Clément Stenac <zorglub@videolan.org
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <sys/stat.h>
29 #include <errno.h>
31 #include <vlc_common.h>
32 #include <vlc_input_item.h>
33 #include <vlc_fs.h>
34 #include <vlc_strings.h>
35 #include <vlc_url.h>
36 #include <vlc_md5.h>
38 #include "art.h"
40 static void ArtCacheCreateDir( const char *psz_dir )
42 char newdir[strlen( psz_dir ) + 1];
43 strcpy( newdir, psz_dir );
44 char * psz_newdir = newdir;
45 char * psz = psz_newdir;
47 while( *psz )
49 while( *psz && *psz != DIR_SEP_CHAR) psz++;
50 if( !*psz ) break;
51 *psz = 0;
52 if( !EMPTY_STR( psz_newdir ) )
53 vlc_mkdir( psz_newdir, 0700 );
54 *psz = DIR_SEP_CHAR;
55 psz++;
57 vlc_mkdir( psz_dir, 0700 );
60 static char* ArtCacheGetDirPath( const char *psz_arturl, const char *psz_artist,
61 const char *psz_album, const char *psz_title )
63 char *psz_dir;
64 char *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
66 if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
68 char *psz_album_sanitized = strdup( psz_album );
69 filename_sanitize( psz_album_sanitized );
70 char *psz_artist_sanitized = strdup( psz_artist );
71 filename_sanitize( psz_artist_sanitized );
72 if( asprintf( &psz_dir, "%s" DIR_SEP "art" DIR_SEP "artistalbum"
73 DIR_SEP "%s" DIR_SEP "%s", psz_cachedir,
74 psz_artist_sanitized, psz_album_sanitized ) == -1 )
75 psz_dir = NULL;
76 free( psz_album_sanitized );
77 free( psz_artist_sanitized );
79 else
81 /* If artist or album are missing, cache by art download URL.
82 * If the URL is an attachment://, add the title to the cache name.
83 * It will be md5 hashed to form a valid cache filename.
84 * We assume that psz_arturl is always the download URL and not the
85 * already hashed filename.
86 * (We should never need to call this function if art has already been
87 * downloaded anyway).
89 struct md5_s md5;
90 InitMD5( &md5 );
91 AddMD5( &md5, psz_arturl, strlen( psz_arturl ) );
92 if( !strncmp( psz_arturl, "attachment://", 13 ) )
93 AddMD5( &md5, psz_title, strlen( psz_title ) );
94 EndMD5( &md5 );
95 char * psz_arturl_sanitized = psz_md5_hash( &md5 );
96 if( asprintf( &psz_dir, "%s" DIR_SEP "art" DIR_SEP "arturl" DIR_SEP
97 "%s", psz_cachedir, psz_arturl_sanitized ) == -1 )
98 psz_dir = NULL;
99 free( psz_arturl_sanitized );
101 free( psz_cachedir );
102 return psz_dir;
105 static char *ArtCachePath( input_item_t *p_item )
107 char* psz_path = NULL;
108 const char *psz_artist;
109 const char *psz_album;
110 const char *psz_arturl;
111 const char *psz_title;
113 vlc_mutex_lock( &p_item->lock );
115 if( !p_item->p_meta )
116 p_item->p_meta = vlc_meta_New();
117 if( !p_item->p_meta )
118 goto end;
120 psz_artist = vlc_meta_Get( p_item->p_meta, vlc_meta_Artist );
121 psz_album = vlc_meta_Get( p_item->p_meta, vlc_meta_Album );
122 psz_arturl = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL );
123 psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
124 if( !psz_title )
125 psz_title = p_item->psz_name;
128 if( (EMPTY_STR(psz_artist) || EMPTY_STR(psz_album) ) && !psz_arturl )
129 goto end;
131 psz_path = ArtCacheGetDirPath( psz_arturl, psz_artist, psz_album, psz_title );
133 end:
134 vlc_mutex_unlock( &p_item->lock );
135 return psz_path;
138 static char *ArtCacheName( input_item_t *p_item, const char *psz_type )
140 char *psz_path = ArtCachePath( p_item );
141 char *psz_ext = strdup( psz_type ? psz_type : "" );
142 char *psz_filename = NULL;
144 if( unlikely( !psz_path || !psz_ext ) )
145 goto end;
147 ArtCacheCreateDir( psz_path );
148 filename_sanitize( psz_ext );
150 if( asprintf( &psz_filename, "%s" DIR_SEP "art%s", psz_path, psz_ext ) < 0 )
151 psz_filename = NULL;
153 end:
154 free( psz_ext );
155 free( psz_path );
157 return psz_filename;
160 /* */
161 int input_FindArtInCache( input_item_t *p_item )
163 char *psz_path = ArtCachePath( p_item );
165 if( !psz_path )
166 return VLC_EGENERIC;
168 /* Check if file exists */
169 DIR *p_dir = vlc_opendir( psz_path );
170 if( !p_dir )
172 free( psz_path );
173 return VLC_EGENERIC;
176 bool b_found = false;
177 const char *psz_filename;
178 while( !b_found && (psz_filename = vlc_readdir( p_dir )) )
180 if( !strncmp( psz_filename, "art", 3 ) )
182 char *psz_file;
183 if( asprintf( &psz_file, "%s" DIR_SEP "%s",
184 psz_path, psz_filename ) != -1 )
186 char *psz_uri = vlc_path2uri( psz_file, "file" );
187 if( psz_uri )
189 input_item_SetArtURL( p_item, psz_uri );
190 free( psz_uri );
192 free( psz_file );
195 b_found = true;
199 /* */
200 closedir( p_dir );
201 free( psz_path );
202 return b_found ? VLC_SUCCESS : VLC_EGENERIC;
205 static char * GetDirByItemUIDs( char *psz_uid )
207 char *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
208 char *psz_dir;
209 if( asprintf( &psz_dir, "%s" DIR_SEP
210 "by-iiuid" DIR_SEP
211 "%s",
212 psz_cachedir, psz_uid ) == -1 )
214 psz_dir = NULL;
216 free( psz_cachedir );
217 return psz_dir;
220 static char * GetFileByItemUID( char *psz_dir, const char *psz_type )
222 char *psz_file;
223 if( asprintf( &psz_file, "%s" DIR_SEP "%s", psz_dir, psz_type ) == -1 )
225 psz_file = NULL;
227 return psz_file;
230 int input_FindArtInCacheUsingItemUID( input_item_t *p_item )
232 char *uid = input_item_GetInfo( p_item, "uid", "md5" );
233 if ( ! *uid )
235 free( uid );
236 return VLC_EGENERIC;
239 /* we have an input item uid set */
240 bool b_done = false;
241 char *psz_byuiddir = GetDirByItemUIDs( uid );
242 char *psz_byuidfile = GetFileByItemUID( psz_byuiddir, "arturl" );
243 free( psz_byuiddir );
244 if( psz_byuidfile )
246 FILE *fd = vlc_fopen( psz_byuidfile, "rb" );
247 if ( fd )
249 char sz_cachefile[2049];
250 /* read the cache hash url */
251 if ( fgets( sz_cachefile, 2048, fd ) != NULL )
253 input_item_SetArtURL( p_item, sz_cachefile );
254 b_done = true;
256 fclose( fd );
258 free( psz_byuidfile );
260 free( uid );
261 if ( b_done ) return VLC_SUCCESS;
263 return VLC_EGENERIC;
266 /* */
267 int input_SaveArt( vlc_object_t *obj, input_item_t *p_item,
268 const void *data, size_t length, const char *psz_type )
270 char *psz_filename = ArtCacheName( p_item, psz_type );
272 if( !psz_filename )
273 return VLC_EGENERIC;
275 char *psz_uri = vlc_path2uri( psz_filename, "file" );
276 if( !psz_uri )
278 free( psz_filename );
279 return VLC_EGENERIC;
282 /* Check if we already dumped it */
283 struct stat s;
284 if( !vlc_stat( psz_filename, &s ) )
286 input_item_SetArtURL( p_item, psz_uri );
287 free( psz_filename );
288 free( psz_uri );
289 return VLC_SUCCESS;
292 /* Dump it otherwise */
293 FILE *f = vlc_fopen( psz_filename, "wb" );
294 if( f )
296 if( fwrite( data, 1, length, f ) != length )
298 msg_Err( obj, "%s: %s", psz_filename, vlc_strerror_c(errno) );
300 else
302 msg_Dbg( obj, "album art saved to %s", psz_filename );
303 input_item_SetArtURL( p_item, psz_uri );
305 fclose( f );
307 free( psz_uri );
309 /* save uid info */
310 char *uid = input_item_GetInfo( p_item, "uid", "md5" );
311 if ( ! *uid )
313 free( uid );
314 goto end;
317 char *psz_byuiddir = GetDirByItemUIDs( uid );
318 char *psz_byuidfile = GetFileByItemUID( psz_byuiddir, "arturl" );
319 ArtCacheCreateDir( psz_byuiddir );
320 free( psz_byuiddir );
322 if ( psz_byuidfile )
324 f = vlc_fopen( psz_byuidfile, "wb" );
325 if ( f )
327 if( fputs( "file://", f ) < 0 || fputs( psz_filename, f ) < 0 )
328 msg_Err( obj, "Error writing %s: %s", psz_byuidfile,
329 vlc_strerror_c(errno) );
330 fclose( f );
332 free( psz_byuidfile );
334 free( uid );
335 /* !save uid info */
336 end:
337 free( psz_filename );
338 return VLC_SUCCESS;