]> git.sesse.net Git - vlc/blob - src/playlist/art.c
demux: ts: remove pid array
[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 <sys/stat.h>
30 #include <errno.h>
31
32 #include <vlc_common.h>
33 #include <vlc_input_item.h>
34 #include <vlc_fs.h>
35 #include <vlc_strings.h>
36 #include <vlc_url.h>
37 #include <vlc_md5.h>
38
39 #include "art.h"
40
41 static void ArtCacheCreateDir( const char *psz_dir )
42 {
43     char newdir[strlen( psz_dir ) + 1];
44     strcpy( newdir, psz_dir );
45     char * psz_newdir = newdir;
46     char * psz = psz_newdir;
47
48     while( *psz )
49     {
50         while( *psz && *psz != DIR_SEP_CHAR) psz++;
51         if( !*psz ) break;
52         *psz = 0;
53         if( !EMPTY_STR( psz_newdir ) )
54             vlc_mkdir( psz_newdir, 0700 );
55         *psz = DIR_SEP_CHAR;
56         psz++;
57     }
58     vlc_mkdir( psz_dir, 0700 );
59 }
60
61 static char* ArtCacheGetDirPath( const char *psz_arturl, const char *psz_artist,
62                                  const char *psz_album,  const char *psz_title )
63 {
64     char *psz_dir;
65     char *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
66
67     if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
68     {
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 )
76             psz_dir = NULL;
77         free( psz_album_sanitized );
78         free( psz_artist_sanitized );
79     }
80     else
81     {
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
88          * downloaded anyway).
89          */
90         struct md5_s md5;
91         InitMD5( &md5 );
92         AddMD5( &md5, psz_arturl, strlen( psz_arturl ) );
93         if( !strncmp( psz_arturl, "attachment://", 13 ) )
94             AddMD5( &md5, psz_title, strlen( psz_title ) );
95         EndMD5( &md5 );
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 )
99             psz_dir = NULL;
100         free( psz_arturl_sanitized );
101     }
102     free( psz_cachedir );
103     return psz_dir;
104 }
105
106 static char *ArtCachePath( input_item_t *p_item )
107 {
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;
113
114     vlc_mutex_lock( &p_item->lock );
115
116     if( !p_item->p_meta )
117         p_item->p_meta = vlc_meta_New();
118     if( !p_item->p_meta )
119         goto end;
120
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 );
125     if( !psz_title )
126         psz_title = p_item->psz_name;
127
128
129     if( (EMPTY_STR(psz_artist) || EMPTY_STR(psz_album) ) && !psz_arturl )
130         goto end;
131
132     psz_path = ArtCacheGetDirPath( psz_arturl, psz_artist, psz_album, psz_title );
133
134 end:
135     vlc_mutex_unlock( &p_item->lock );
136     return psz_path;
137 }
138
139 static char *ArtCacheName( input_item_t *p_item, const char *psz_type )
140 {
141     char *psz_path = ArtCachePath( p_item );
142     if( !psz_path )
143         return NULL;
144
145     ArtCacheCreateDir( psz_path );
146
147     char *psz_ext = strdup( psz_type ? psz_type : "" );
148     filename_sanitize( psz_ext );
149     char *psz_filename;
150     if( asprintf( &psz_filename, "%s" DIR_SEP "art%s", psz_path, psz_ext ) < 0 )
151         psz_filename = NULL;
152
153     free( psz_ext );
154     free( psz_path );
155
156     return psz_filename;
157 }
158
159 /* */
160 int playlist_FindArtInCache( input_item_t *p_item )
161 {
162     char *psz_path = ArtCachePath( p_item );
163
164     if( !psz_path )
165         return VLC_EGENERIC;
166
167     /* Check if file exists */
168     DIR *p_dir = vlc_opendir( psz_path );
169     if( !p_dir )
170     {
171         free( psz_path );
172         return VLC_EGENERIC;
173     }
174
175     bool b_found = false;
176     const char *psz_filename;
177     while( !b_found && (psz_filename = vlc_readdir( p_dir )) )
178     {
179         if( !strncmp( psz_filename, "art", 3 ) )
180         {
181             char *psz_file;
182             if( asprintf( &psz_file, "%s" DIR_SEP "%s",
183                           psz_path, psz_filename ) != -1 )
184             {
185                 char *psz_uri = vlc_path2uri( psz_file, "file" );
186                 if( psz_uri )
187                 {
188                     input_item_SetArtURL( p_item, psz_uri );
189                     free( psz_uri );
190                 }
191                 free( psz_file );
192             }
193
194             b_found = true;
195         }
196     }
197
198     /* */
199     closedir( p_dir );
200     free( psz_path );
201     return b_found ? VLC_SUCCESS : VLC_EGENERIC;
202 }
203
204 static char * GetDirByItemUIDs( char *psz_uid )
205 {
206     char *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
207     char *psz_dir;
208     if( asprintf( &psz_dir, "%s" DIR_SEP
209                   "by-iiuid" DIR_SEP
210                   "%s",
211                   psz_cachedir, psz_uid ) == -1 )
212     {
213         psz_dir = NULL;
214     }
215     free( psz_cachedir );
216     return psz_dir;
217 }
218
219 static char * GetFileByItemUID( char *psz_dir, const char *psz_type )
220 {
221     char *psz_file;
222     if( asprintf( &psz_file, "%s" DIR_SEP "%s", psz_dir, psz_type ) == -1 )
223     {
224         psz_file = NULL;
225     }
226     return psz_file;
227 }
228
229 int playlist_FindArtInCacheUsingItemUID( input_item_t *p_item )
230 {
231     char *uid = input_item_GetInfo( p_item, "uid", "md5" );
232     if ( ! *uid )
233     {
234         free( uid );
235         return VLC_EGENERIC;
236     }
237
238     /* we have an input item uid set */
239     bool b_done = false;
240     char *psz_byuiddir = GetDirByItemUIDs( uid );
241     char *psz_byuidfile = GetFileByItemUID( psz_byuiddir, "arturl" );
242     free( psz_byuiddir );
243     if( psz_byuidfile )
244     {
245         FILE *fd = vlc_fopen( psz_byuidfile, "rb" );
246         if ( fd )
247         {
248             char sz_cachefile[2049];
249             /* read the cache hash url */
250             if ( fgets( sz_cachefile, 2048, fd ) != NULL )
251             {
252                 input_item_SetArtURL( p_item, sz_cachefile );
253                 b_done = true;
254             }
255             fclose( fd );
256         }
257         free( psz_byuidfile );
258     }
259     free( uid );
260     if ( b_done ) return VLC_SUCCESS;
261
262     return VLC_EGENERIC;
263 }
264
265 /* */
266 int playlist_SaveArt( vlc_object_t *obj, input_item_t *p_item,
267                       const void *data, size_t length, const char *psz_type )
268 {
269     char *psz_filename = ArtCacheName( p_item, psz_type );
270
271     if( !psz_filename )
272         return VLC_EGENERIC;
273
274     char *psz_uri = vlc_path2uri( psz_filename, "file" );
275     if( !psz_uri )
276     {
277         free( psz_filename );
278         return VLC_EGENERIC;
279     }
280
281     /* Check if we already dumped it */
282     struct stat s;
283     if( !vlc_stat( psz_filename, &s ) )
284     {
285         input_item_SetArtURL( p_item, psz_uri );
286         free( psz_filename );
287         free( psz_uri );
288         return VLC_SUCCESS;
289     }
290
291     /* Dump it otherwise */
292     FILE *f = vlc_fopen( psz_filename, "wb" );
293     if( f )
294     {
295         if( fwrite( data, 1, length, f ) != length )
296         {
297             msg_Err( obj, "%s: %s", psz_filename, vlc_strerror_c(errno) );
298         }
299         else
300         {
301             msg_Dbg( obj, "album art saved to %s", psz_filename );
302             input_item_SetArtURL( p_item, psz_uri );
303         }
304         fclose( f );
305     }
306     free( psz_uri );
307
308     /* save uid info */
309     char *uid = input_item_GetInfo( p_item, "uid", "md5" );
310     if ( ! *uid )
311     {
312         free( uid );
313         goto end;
314     }
315
316     char *psz_byuiddir = GetDirByItemUIDs( uid );
317     char *psz_byuidfile = GetFileByItemUID( psz_byuiddir, "arturl" );
318     ArtCacheCreateDir( psz_byuiddir );
319     free( psz_byuiddir );
320
321     if ( psz_byuidfile )
322     {
323         f = vlc_fopen( psz_byuidfile, "wb" );
324         if ( f )
325         {
326             if( fputs( "file://", f ) < 0 || fputs( psz_filename, f ) < 0 )
327                 msg_Err( obj, "Error writing %s: %s", psz_byuidfile,
328                          vlc_strerror_c(errno) );
329             fclose( f );
330         }
331         free( psz_byuidfile );
332     }
333     free( uid );
334     /* !save uid info */
335 end:
336     free( psz_filename );
337     return VLC_SUCCESS;
338 }
339