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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_input_item.h>
34 #include <vlc_strings.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
;
49 while( *psz
&& *psz
!= DIR_SEP_CHAR
) psz
++;
52 if( !EMPTY_STR( psz_newdir
) )
53 vlc_mkdir( psz_newdir
, 0700 );
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
)
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 )
76 free( psz_album_sanitized
);
77 free( psz_artist_sanitized
);
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
91 AddMD5( &md5
, psz_arturl
, strlen( psz_arturl
) );
92 if( !strncmp( psz_arturl
, "attachment://", 13 ) )
93 AddMD5( &md5
, psz_title
, strlen( psz_title
) );
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 )
99 free( psz_arturl_sanitized
);
101 free( psz_cachedir
);
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
)
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
);
125 psz_title
= p_item
->psz_name
;
128 if( (EMPTY_STR(psz_artist
) || EMPTY_STR(psz_album
) ) && !psz_arturl
)
131 psz_path
= ArtCacheGetDirPath( psz_arturl
, psz_artist
, psz_album
, psz_title
);
134 vlc_mutex_unlock( &p_item
->lock
);
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
) )
147 ArtCacheCreateDir( psz_path
);
148 filename_sanitize( psz_ext
);
150 if( asprintf( &psz_filename
, "%s" DIR_SEP
"art%s", psz_path
, psz_ext
) < 0 )
161 int input_FindArtInCache( input_item_t
*p_item
)
163 char *psz_path
= ArtCachePath( p_item
);
168 /* Check if file exists */
169 DIR *p_dir
= vlc_opendir( psz_path
);
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 ) )
183 if( asprintf( &psz_file
, "%s" DIR_SEP
"%s",
184 psz_path
, psz_filename
) != -1 )
186 char *psz_uri
= vlc_path2uri( psz_file
, "file" );
189 input_item_SetArtURL( p_item
, psz_uri
);
202 return b_found
? VLC_SUCCESS
: VLC_EGENERIC
;
205 static char * GetDirByItemUIDs( char *psz_uid
)
207 char *psz_cachedir
= config_GetUserDir(VLC_CACHE_DIR
);
209 if( asprintf( &psz_dir
, "%s" DIR_SEP
212 psz_cachedir
, psz_uid
) == -1 )
216 free( psz_cachedir
);
220 static char * GetFileByItemUID( char *psz_dir
, const char *psz_type
)
223 if( asprintf( &psz_file
, "%s" DIR_SEP
"%s", psz_dir
, psz_type
) == -1 )
230 int input_FindArtInCacheUsingItemUID( input_item_t
*p_item
)
232 char *uid
= input_item_GetInfo( p_item
, "uid", "md5" );
239 /* we have an input item uid set */
241 char *psz_byuiddir
= GetDirByItemUIDs( uid
);
242 char *psz_byuidfile
= GetFileByItemUID( psz_byuiddir
, "arturl" );
243 free( psz_byuiddir
);
246 FILE *fd
= vlc_fopen( psz_byuidfile
, "rb" );
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
);
258 free( psz_byuidfile
);
261 if ( b_done
) return VLC_SUCCESS
;
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
);
275 char *psz_uri
= vlc_path2uri( psz_filename
, "file" );
278 free( psz_filename
);
282 /* Check if we already dumped it */
284 if( !vlc_stat( psz_filename
, &s
) )
286 input_item_SetArtURL( p_item
, psz_uri
);
287 free( psz_filename
);
292 /* Dump it otherwise */
293 FILE *f
= vlc_fopen( psz_filename
, "wb" );
296 if( fwrite( data
, 1, length
, f
) != length
)
298 msg_Err( obj
, "%s: %s", psz_filename
, vlc_strerror_c(errno
) );
302 msg_Dbg( obj
, "album art saved to %s", psz_filename
);
303 input_item_SetArtURL( p_item
, psz_uri
);
310 char *uid
= input_item_GetInfo( p_item
, "uid", "md5" );
317 char *psz_byuiddir
= GetDirByItemUIDs( uid
);
318 char *psz_byuidfile
= GetFileByItemUID( psz_byuiddir
, "arturl" );
319 ArtCacheCreateDir( psz_byuiddir
);
320 free( psz_byuiddir
);
324 f
= vlc_fopen( psz_byuidfile
, "wb" );
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
) );
332 free( psz_byuidfile
);
337 free( psz_filename
);