]> git.sesse.net Git - vlc/blob - src/playlist/art.c
Flag some input_item with uid to speed up cached elements based on lua retrievals
[vlc] / src / playlist / art.c
1 /*****************************************************************************
2  * art.c : Art metadata handling
3  *****************************************************************************
4  * Copyright (C) 1998-2008 VLC authors and VideoLAN
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 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.
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 Lesser General Public License for more details.
19  *
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  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <assert.h>
30 #include <sys/stat.h>
31
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>
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             vlc_mkdir( psz_newdir, 0700 );
57         *psz = DIR_SEP_CHAR;
58         psz++;
59     }
60     vlc_mkdir( psz_dir, 0700 );
61 }
62
63 static char* ArtCacheGetDirPath( const char *psz_arturl, const char *psz_artist,
64                                  const char *psz_album,  const char *psz_title )
65 {
66     char *psz_dir;
67     char *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
68
69     if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
70     {
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 );
81     }
82     else
83     {
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).
91          */
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 );
103     }
104     free( psz_cachedir );
105     return psz_dir;
106 }
107
108 static char *ArtCachePath( input_item_t *p_item )
109 {
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;
115
116     vlc_mutex_lock( &p_item->lock );
117
118     if( !p_item->p_meta )
119         p_item->p_meta = vlc_meta_New();
120     if( !p_item->p_meta )
121         goto end;
122
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;
129
130
131     if( (EMPTY_STR(psz_artist) || EMPTY_STR(psz_album) ) && !psz_arturl )
132         goto end;
133
134     psz_path = ArtCacheGetDirPath( psz_arturl, psz_artist, psz_album, psz_title );
135
136 end:
137     vlc_mutex_unlock( &p_item->lock );
138     return psz_path;
139 }
140
141 static char *ArtCacheName( input_item_t *p_item, const char *psz_type )
142 {
143     char *psz_path = ArtCachePath( p_item );
144     if( !psz_path )
145         return NULL;
146
147     ArtCacheCreateDir( psz_path );
148
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;
154
155     free( psz_ext );
156     free( psz_path );
157
158     return psz_filename;
159 }
160
161 /* */
162 int playlist_FindArtInCache( input_item_t *p_item )
163 {
164     char *psz_path = ArtCachePath( p_item );
165
166     if( !psz_path )
167         return VLC_EGENERIC;
168
169     /* Check if file exists */
170     DIR *p_dir = vlc_opendir( psz_path );
171     if( !p_dir )
172     {
173         free( psz_path );
174         return VLC_EGENERIC;
175     }
176
177     bool b_found = false;
178     char *psz_filename;
179     while( !b_found && (psz_filename = vlc_readdir( p_dir )) )
180     {
181         if( !strncmp( psz_filename, "art", 3 ) )
182         {
183             char *psz_file;
184             if( asprintf( &psz_file, "%s" DIR_SEP "%s",
185                           psz_path, psz_filename ) != -1 )
186             {
187                 char *psz_uri = make_URI( psz_file, "file" );
188                 if( psz_uri )
189                 {
190                     input_item_SetArtURL( p_item, psz_uri );
191                     free( psz_uri );
192                 }
193                 free( psz_file );
194             }
195
196             b_found = true;
197         }
198         free( psz_filename );
199     }
200
201     /* */
202     closedir( p_dir );
203     free( psz_path );
204     return b_found ? VLC_SUCCESS : VLC_EGENERIC;
205 }
206
207 static char * GetDirByItemUIDs( char *psz_uid )
208 {
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 )
215     {
216         psz_dir = NULL;
217     }
218     free( psz_cachedir );
219     return psz_dir;
220 }
221
222 static char * GetFileByItemUID( char *psz_dir, const char *psz_type )
223 {
224     char *psz_file;
225     if( asprintf( &psz_file, "%s" DIR_SEP "%s", psz_dir, psz_type ) == -1 )
226     {
227         psz_file = NULL;
228     }
229     return psz_file;
230 }
231
232 int playlist_FindArtInCacheUsingItemUID( input_item_t *p_item )
233 {
234     char *uid = input_item_GetInfo( p_item, "uid", "md5" );
235     if ( !uid ) return VLC_EGENERIC;
236     /* we have an input item uid set */
237     bool b_done = false;
238     char *psz_byuiddir = GetDirByItemUIDs( uid );
239     char *psz_byuidfile = GetFileByItemUID( psz_byuiddir, "arturl" );
240     free( psz_byuiddir );
241     if( psz_byuidfile )
242     {
243         FILE *fd = vlc_fopen( psz_byuidfile, "rb" );
244         if ( fd )
245         {
246             char sz_cachefile[2049];
247             /* read the cache hash url */
248             if ( fgets( sz_cachefile, 2048, fd ) != NULL )
249             {
250                 input_item_SetArtURL( p_item, sz_cachefile );
251                 b_done = true;
252             }
253             fclose( fd );
254         }
255         free( psz_byuidfile );
256     }
257     free( uid );
258     if ( b_done ) return VLC_SUCCESS;
259
260     return VLC_EGENERIC;
261 }
262
263 /* */
264 int playlist_SaveArt( playlist_t *p_playlist, input_item_t *p_item,
265                       const uint8_t *p_buffer, int i_buffer, const char *psz_type )
266 {
267     char *psz_filename = ArtCacheName( p_item, psz_type );
268
269     if( !psz_filename )
270         return VLC_EGENERIC;
271
272     char *psz_uri = make_URI( psz_filename, "file" );
273     if( !psz_uri )
274     {
275         free( psz_filename );
276         return VLC_EGENERIC;
277     }
278
279     /* Check if we already dumped it */
280     struct stat s;
281     if( !vlc_stat( psz_filename, &s ) )
282     {
283         input_item_SetArtURL( p_item, psz_uri );
284         free( psz_filename );
285         free( psz_uri );
286         return VLC_SUCCESS;
287     }
288
289     /* Dump it otherwise */
290     FILE *f = vlc_fopen( psz_filename, "wb" );
291     if( f )
292     {
293         if( fwrite( p_buffer, i_buffer, 1, f ) != 1 )
294         {
295             msg_Err( p_playlist, "%s: %m", psz_filename );
296         }
297         else
298         {
299             msg_Dbg( p_playlist, "album art saved to %s", psz_filename );
300             input_item_SetArtURL( p_item, psz_uri );
301         }
302         fclose( f );
303     }
304     free( psz_uri );
305
306     /* save uid info */
307     char *uid = input_item_GetInfo( p_item, "uid", "md5" );
308     if ( !uid ) goto end;
309
310     char *psz_byuiddir = GetDirByItemUIDs( uid );
311     char *psz_byuidfile = GetFileByItemUID( psz_byuiddir, "arturl" );
312     ArtCacheCreateDir( psz_byuiddir );
313     free( psz_byuiddir );
314
315     if ( psz_byuidfile )
316     {
317         f = vlc_fopen( psz_byuidfile, "wb" );
318         if ( f )
319         {
320             if( fputs( "file://", f ) < 0 || fputs( psz_filename, f ) < 0 )
321                 msg_Err( p_playlist, "Error writing %s: %m", psz_byuidfile );
322             fclose( f );
323         }
324         free( psz_byuidfile );
325     }
326     free( uid );
327     /* !save uid info */
328 end:
329     free( psz_filename );
330     return VLC_SUCCESS;
331 }
332