1 /*****************************************************************************
2 * art.c : Art metadata handling
3 *****************************************************************************
4 * Copyright (C) 1998-2008 VLC authors and VideoLAN
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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_input_item.h>
35 #include <vlc_strings.h>
41 static void ArtCacheCreateDir( const char *psz_dir
)
43 char newdir
[strlen( psz_dir
) + 1];
44 strcpy( newdir
, psz_dir
);
45 char * psz_newdir
= newdir
;
46 char * psz
= psz_newdir
;
50 while( *psz
&& *psz
!= DIR_SEP_CHAR
) psz
++;
53 if( !EMPTY_STR( psz_newdir
) )
54 vlc_mkdir( psz_newdir
, 0700 );
58 vlc_mkdir( psz_dir
, 0700 );
61 static char* ArtCacheGetDirPath( const char *psz_arturl
, const char *psz_artist
,
62 const char *psz_album
, const char *psz_title
)
65 char *psz_cachedir
= config_GetUserDir(VLC_CACHE_DIR
);
67 if( !EMPTY_STR(psz_artist
) && !EMPTY_STR(psz_album
) )
69 char *psz_album_sanitized
= strdup( psz_album
);
70 filename_sanitize( psz_album_sanitized
);
71 char *psz_artist_sanitized
= strdup( psz_artist
);
72 filename_sanitize( psz_artist_sanitized
);
73 if( asprintf( &psz_dir
, "%s" DIR_SEP
"art" DIR_SEP
"artistalbum"
74 DIR_SEP
"%s" DIR_SEP
"%s", psz_cachedir
,
75 psz_artist_sanitized
, psz_album_sanitized
) == -1 )
77 free( psz_album_sanitized
);
78 free( psz_artist_sanitized
);
82 /* If artist or album are missing, cache by art download URL.
83 * If the URL is an attachment://, add the title to the cache name.
84 * It will be md5 hashed to form a valid cache filename.
85 * We assume that psz_arturl is always the download URL and not the
86 * already hashed filename.
87 * (We should never need to call this function if art has already been
92 AddMD5( &md5
, psz_arturl
, strlen( psz_arturl
) );
93 if( !strncmp( psz_arturl
, "attachment://", 13 ) )
94 AddMD5( &md5
, psz_title
, strlen( psz_title
) );
96 char * psz_arturl_sanitized
= psz_md5_hash( &md5
);
97 if( asprintf( &psz_dir
, "%s" DIR_SEP
"art" DIR_SEP
"arturl" DIR_SEP
98 "%s", psz_cachedir
, psz_arturl_sanitized
) == -1 )
100 free( psz_arturl_sanitized
);
102 free( psz_cachedir
);
106 static char *ArtCachePath( input_item_t
*p_item
)
108 char* psz_path
= NULL
;
109 const char *psz_artist
;
110 const char *psz_album
;
111 const char *psz_arturl
;
112 const char *psz_title
;
114 vlc_mutex_lock( &p_item
->lock
);
116 if( !p_item
->p_meta
)
117 p_item
->p_meta
= vlc_meta_New();
118 if( !p_item
->p_meta
)
121 psz_artist
= vlc_meta_Get( p_item
->p_meta
, vlc_meta_Artist
);
122 psz_album
= vlc_meta_Get( p_item
->p_meta
, vlc_meta_Album
);
123 psz_arturl
= vlc_meta_Get( p_item
->p_meta
, vlc_meta_ArtworkURL
);
124 psz_title
= vlc_meta_Get( p_item
->p_meta
, vlc_meta_Title
);
126 psz_title
= p_item
->psz_name
;
129 if( (EMPTY_STR(psz_artist
) || EMPTY_STR(psz_album
) ) && !psz_arturl
)
132 psz_path
= ArtCacheGetDirPath( psz_arturl
, psz_artist
, psz_album
, psz_title
);
135 vlc_mutex_unlock( &p_item
->lock
);
139 static char *ArtCacheName( input_item_t
*p_item
, const char *psz_type
)
141 char *psz_path
= ArtCachePath( p_item
);
142 char *psz_ext
= strdup( psz_type
? psz_type
: "" );
143 char *psz_filename
= NULL
;
145 if( unlikely( !psz_path
|| !psz_ext
) )
148 ArtCacheCreateDir( psz_path
);
149 filename_sanitize( psz_ext
);
151 if( asprintf( &psz_filename
, "%s" DIR_SEP
"art%s", psz_path
, psz_ext
) < 0 )
162 int playlist_FindArtInCache( input_item_t
*p_item
)
164 char *psz_path
= ArtCachePath( p_item
);
169 /* Check if file exists */
170 DIR *p_dir
= vlc_opendir( psz_path
);
177 bool b_found
= false;
178 const char *psz_filename
;
179 while( !b_found
&& (psz_filename
= vlc_readdir( p_dir
)) )
181 if( !strncmp( psz_filename
, "art", 3 ) )
184 if( asprintf( &psz_file
, "%s" DIR_SEP
"%s",
185 psz_path
, psz_filename
) != -1 )
187 char *psz_uri
= vlc_path2uri( psz_file
, "file" );
190 input_item_SetArtURL( p_item
, psz_uri
);
203 return b_found
? VLC_SUCCESS
: VLC_EGENERIC
;
206 static char * GetDirByItemUIDs( char *psz_uid
)
208 char *psz_cachedir
= config_GetUserDir(VLC_CACHE_DIR
);
210 if( asprintf( &psz_dir
, "%s" DIR_SEP
213 psz_cachedir
, psz_uid
) == -1 )
217 free( psz_cachedir
);
221 static char * GetFileByItemUID( char *psz_dir
, const char *psz_type
)
224 if( asprintf( &psz_file
, "%s" DIR_SEP
"%s", psz_dir
, psz_type
) == -1 )
231 int playlist_FindArtInCacheUsingItemUID( input_item_t
*p_item
)
233 char *uid
= input_item_GetInfo( p_item
, "uid", "md5" );
240 /* we have an input item uid set */
242 char *psz_byuiddir
= GetDirByItemUIDs( uid
);
243 char *psz_byuidfile
= GetFileByItemUID( psz_byuiddir
, "arturl" );
244 free( psz_byuiddir
);
247 FILE *fd
= vlc_fopen( psz_byuidfile
, "rb" );
250 char sz_cachefile
[2049];
251 /* read the cache hash url */
252 if ( fgets( sz_cachefile
, 2048, fd
) != NULL
)
254 input_item_SetArtURL( p_item
, sz_cachefile
);
259 free( psz_byuidfile
);
262 if ( b_done
) return VLC_SUCCESS
;
268 int playlist_SaveArt( vlc_object_t
*obj
, input_item_t
*p_item
,
269 const void *data
, size_t length
, const char *psz_type
)
271 char *psz_filename
= ArtCacheName( p_item
, psz_type
);
276 char *psz_uri
= vlc_path2uri( psz_filename
, "file" );
279 free( psz_filename
);
283 /* Check if we already dumped it */
285 if( !vlc_stat( psz_filename
, &s
) )
287 input_item_SetArtURL( p_item
, psz_uri
);
288 free( psz_filename
);
293 /* Dump it otherwise */
294 FILE *f
= vlc_fopen( psz_filename
, "wb" );
297 if( fwrite( data
, 1, length
, f
) != length
)
299 msg_Err( obj
, "%s: %s", psz_filename
, vlc_strerror_c(errno
) );
303 msg_Dbg( obj
, "album art saved to %s", psz_filename
);
304 input_item_SetArtURL( p_item
, psz_uri
);
311 char *uid
= input_item_GetInfo( p_item
, "uid", "md5" );
318 char *psz_byuiddir
= GetDirByItemUIDs( uid
);
319 char *psz_byuidfile
= GetFileByItemUID( psz_byuiddir
, "arturl" );
320 ArtCacheCreateDir( psz_byuiddir
);
321 free( psz_byuiddir
);
325 f
= vlc_fopen( psz_byuidfile
, "wb" );
328 if( fputs( "file://", f
) < 0 || fputs( psz_filename
, f
) < 0 )
329 msg_Err( obj
, "Error writing %s: %s", psz_byuidfile
,
330 vlc_strerror_c(errno
) );
333 free( psz_byuidfile
);
338 free( psz_filename
);