]> git.sesse.net Git - vlc/blob - src/playlist/art.c
playlist_art: blacklist some forbidden char for windows.
[vlc] / src / playlist / art.c
1 /*****************************************************************************
2  * art.c : Art metadata handling
3  *****************************************************************************
4  * Copyright (C) 1998-2008 the VideoLAN team
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
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 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 General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, 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 <vlc_common.h>
31 #include <vlc_playlist.h>
32 #include <vlc_charset.h>
33 #include <vlc_strings.h>
34 #include <vlc_stream.h>
35 #include <limits.h>                                             /* PATH_MAX */
36
37 #ifdef HAVE_SYS_STAT_H
38 #   include <sys/stat.h>
39 #endif
40
41 #include "../libvlc.h"
42 #include "playlist_internal.h"
43
44 static void ArtCacheCreateDir( const char *psz_dir )
45 {
46     char newdir[strlen( psz_dir ) + 1];
47     strcpy( newdir, psz_dir );
48     char * psz_newdir = newdir;
49     char * psz = psz_newdir;
50
51     while( *psz )
52     {
53         while( *psz && *psz != DIR_SEP_CHAR) psz++;
54         if( !*psz ) break;
55         *psz = 0;
56         if( !EMPTY_STR( psz_newdir ) )
57             utf8_mkdir( psz_newdir, 0700 );
58         *psz = DIR_SEP_CHAR;
59         psz++;
60     }
61     utf8_mkdir( psz_dir, 0700 );
62 }
63
64 static char *ArtCacheGetSanitizedFileName( const char *psz )
65 {
66     char *dup = strdup(psz);
67     int i;
68
69     filename_sanitize( dup );
70
71     /* Doesn't create a filename with invalid characters
72      * TODO: several filesystems forbid several characters: list them all
73      */
74     for( i = 0; dup[i] != '\0'; i++ )
75     {
76         if( dup[i] == DIR_SEP_CHAR )
77             dup[i] = ' ';
78         // "<>:\"/?*" are forbidden for win filenames
79 #if defined( WIN32 ) || defined( UNDER_CE )
80         else if( strchr( "<>:\"/?*", dup[i] ) )
81             dup[i] = '_';
82 #endif
83     }
84     return dup;
85 }
86
87 static void ArtCacheGetDirPath( char *psz_dir,
88                                 const char *psz_title,
89                                 const char *psz_artist, const char *psz_album )
90 {
91     char *psz_cachedir = config_GetCacheDir();
92
93     if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
94     {
95         char * psz_album_sanitized = ArtCacheGetSanitizedFileName( psz_album );
96         char * psz_artist_sanitized = ArtCacheGetSanitizedFileName( psz_artist );
97         snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
98                   "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
99                   psz_cachedir, psz_artist_sanitized, psz_album_sanitized );
100         free( psz_album_sanitized );
101         free( psz_artist_sanitized );
102     }
103     else
104     {
105         char * psz_title_sanitized = ArtCacheGetSanitizedFileName( psz_title );
106         snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
107                   "art" DIR_SEP "title" DIR_SEP "%s",
108                   psz_cachedir, psz_title_sanitized );
109         free( psz_title_sanitized );
110     }
111     free( psz_cachedir );
112 }
113
114 static char *ArtCachePath( input_item_t *p_item )
115 {
116     char psz_path[PATH_MAX+1]; /* FIXME */
117
118     vlc_mutex_lock( &p_item->lock );
119
120     if( !p_item->p_meta )
121         p_item->p_meta = vlc_meta_New();
122     if( !p_item->p_meta )
123     {
124         vlc_mutex_unlock( &p_item->lock );
125         return NULL;
126     }
127
128     const char *psz_artist = vlc_meta_Get( p_item->p_meta, vlc_meta_Artist );
129     const char *psz_album = vlc_meta_Get( p_item->p_meta, vlc_meta_Album );
130     const char *psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
131
132     if( !psz_title )
133         psz_title = p_item->psz_name;
134
135     if( (!psz_artist || !psz_album ) && !psz_title )
136     {
137         vlc_mutex_unlock( &p_item->lock );
138         return NULL;
139     }
140
141     ArtCacheGetDirPath( psz_path, psz_title, psz_artist, psz_album );
142
143     vlc_mutex_unlock( &p_item->lock );
144
145     return strdup( psz_path );
146 }
147
148 static char *ArtCacheName( input_item_t *p_item, const char *psz_type )
149 {
150     char *psz_path = ArtCachePath( p_item );
151     if( !psz_path )
152         return NULL;
153
154     ArtCacheCreateDir( psz_path );
155
156     char *psz_ext = strdup( psz_type ? psz_type : "" );
157     filename_sanitize( psz_ext );
158
159     char *psz_filename;
160     if( asprintf( &psz_filename, "file://%s" DIR_SEP "art%s", psz_path, psz_ext ) < 0 )
161         psz_filename = NULL;
162
163     free( psz_ext );
164     free( psz_path );
165
166     return psz_filename;
167 }
168
169 /* */
170 int playlist_FindArtInCache( input_item_t *p_item )
171 {
172     char *psz_path = ArtCachePath( p_item );
173
174     if( !psz_path )
175         return VLC_EGENERIC;
176
177     /* Check if file exists */
178     DIR *p_dir = utf8_opendir( psz_path );
179     if( !p_dir )
180     {
181         free( psz_path );
182         return VLC_EGENERIC;
183     }
184
185     bool b_found = false;
186     char *psz_filename;
187     while( !b_found && (psz_filename = utf8_readdir( p_dir )) )
188     {
189         if( !strncmp( psz_filename, "art", 3 ) )
190         {
191             char *psz_file;
192             if( asprintf( &psz_file, "file://%s" DIR_SEP "%s",
193                           psz_path, psz_filename ) < 0 )
194                 psz_file = NULL;
195             if( psz_file )
196                 input_item_SetArtURL( p_item, psz_file );
197             free( psz_file );
198
199             b_found = true;
200         }
201         free( psz_filename );
202     }
203
204     /* */
205     closedir( p_dir );
206     free( psz_path );
207     return b_found ? VLC_SUCCESS : VLC_EGENERIC;
208 }
209
210
211 /* */
212 int playlist_SaveArt( playlist_t *p_playlist, input_item_t *p_item,
213                       const uint8_t *p_buffer, int i_buffer, const char *psz_type )
214 {
215     char *psz_filename = ArtCacheName( p_item, psz_type );
216
217     if( !psz_filename )
218         return VLC_EGENERIC;
219
220     /* Check if we already dumped it */
221     struct stat s;
222     if( !utf8_stat( psz_filename+7, &s ) )
223     {
224         input_item_SetArtURL( p_item, psz_filename );
225         free( psz_filename );
226         return VLC_SUCCESS;
227     }
228
229     /* Dump it otherwise */
230     FILE *f = utf8_fopen( psz_filename+7, "wb" );
231     if( f )
232     {
233         if( fwrite( p_buffer, i_buffer, 1, f ) != 1 )
234         {
235             msg_Err( p_playlist, "%s: %m", psz_filename );
236         }
237         else
238         {
239             msg_Dbg( p_playlist, "album art saved to %s", psz_filename );
240             input_item_SetArtURL( p_item, psz_filename );
241         }
242         fclose( f );
243     }
244     free( psz_filename );
245     return VLC_SUCCESS;
246 }
247