]> git.sesse.net Git - vlc/blob - src/playlist/art.c
Change filename_sanitize() to work on original string like path_sanitize().
[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_fs.h>
33 #include <vlc_strings.h>
34 #include <vlc_stream.h>
35 #include <vlc_url.h>
36
37 #include <limits.h>                                             /* PATH_MAX */
38
39 #ifdef HAVE_SYS_STAT_H
40 #   include <sys/stat.h>
41 #endif
42
43 #include "../libvlc.h"
44 #include "playlist_internal.h"
45
46 static void ArtCacheCreateDir( const char *psz_dir )
47 {
48     char newdir[strlen( psz_dir ) + 1];
49     strcpy( newdir, psz_dir );
50     char * psz_newdir = newdir;
51     char * psz = psz_newdir;
52
53     while( *psz )
54     {
55         while( *psz && *psz != DIR_SEP_CHAR) psz++;
56         if( !*psz ) break;
57         *psz = 0;
58         if( !EMPTY_STR( psz_newdir ) )
59             vlc_mkdir( psz_newdir, 0700 );
60         *psz = DIR_SEP_CHAR;
61         psz++;
62     }
63     vlc_mkdir( psz_dir, 0700 );
64 }
65
66 static char* ArtCacheGetDirPath( const char *psz_title, const char *psz_artist,
67                                  const char *psz_album )
68 {
69     char *psz_dir;
70     char *psz_cachedir = config_GetUserDir(VLC_CACHE_DIR);
71
72     if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
73     {
74         char *psz_album_sanitized = strdup( psz_album );
75         filename_sanitize( psz_album_sanitized );
76         char *psz_artist_sanitized = strdup( psz_artist );
77         filename_sanitize( psz_artist_sanitized );
78         if( asprintf( &psz_dir, "%s" DIR_SEP "art" DIR_SEP "artistalbum"
79                       DIR_SEP "%s" DIR_SEP "%s", psz_cachedir,
80                       psz_artist_sanitized, psz_album_sanitized ) == -1 )
81             psz_dir = NULL;
82         free( psz_album_sanitized );
83         free( psz_artist_sanitized );
84     }
85     else
86     {
87         char * psz_title_sanitized = strdup( psz_title );
88         filename_sanitize( psz_title_sanitized );
89         if( asprintf( &psz_dir, "%s" DIR_SEP "art" DIR_SEP "title" DIR_SEP
90                       "%s", psz_cachedir, psz_title_sanitized ) == -1 )
91             psz_dir = NULL;
92         free( psz_title_sanitized );
93     }
94     free( psz_cachedir );
95     return psz_dir;
96 }
97
98 static char *ArtCachePath( input_item_t *p_item )
99 {
100     char* psz_path = NULL;
101     const char *psz_artist;
102     const char *psz_album;
103     const char *psz_title;
104
105     vlc_mutex_lock( &p_item->lock );
106
107     if( !p_item->p_meta )
108         p_item->p_meta = vlc_meta_New();
109     if( !p_item->p_meta )
110         goto end;
111
112     psz_artist = vlc_meta_Get( p_item->p_meta, vlc_meta_Artist );
113     psz_album = vlc_meta_Get( p_item->p_meta, vlc_meta_Album );
114     psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
115
116     if( !psz_title )
117         psz_title = p_item->psz_name;
118
119     if( (!psz_artist || !psz_album ) && !psz_title )
120         goto end;
121
122     psz_path = ArtCacheGetDirPath( psz_title, psz_artist, psz_album );
123
124 end:
125     vlc_mutex_unlock( &p_item->lock );
126     return psz_path;
127 }
128
129 static char *ArtCacheName( input_item_t *p_item, const char *psz_type )
130 {
131     char *psz_path = ArtCachePath( p_item );
132     if( !psz_path )
133         return NULL;
134
135     ArtCacheCreateDir( psz_path );
136
137     char *psz_ext = strdup( psz_type ? psz_type : "" );
138     filename_sanitize( psz_ext );
139     char *psz_filename;
140     if( asprintf( &psz_filename, "%s" DIR_SEP "art%s", psz_path, psz_ext ) < 0 )
141         psz_filename = NULL;
142
143     free( psz_ext );
144     free( psz_path );
145
146     return psz_filename;
147 }
148
149 /* */
150 int playlist_FindArtInCache( input_item_t *p_item )
151 {
152     char *psz_path = ArtCachePath( p_item );
153
154     if( !psz_path )
155         return VLC_EGENERIC;
156
157     /* Check if file exists */
158     DIR *p_dir = vlc_opendir( psz_path );
159     if( !p_dir )
160     {
161         free( psz_path );
162         return VLC_EGENERIC;
163     }
164
165     bool b_found = false;
166     char *psz_filename;
167     while( !b_found && (psz_filename = vlc_readdir( p_dir )) )
168     {
169         if( !strncmp( psz_filename, "art", 3 ) )
170         {
171             char *psz_file;
172             if( asprintf( &psz_file, "%s" DIR_SEP "%s",
173                           psz_path, psz_filename ) != -1 )
174             {
175                 char *psz_uri = make_URI( psz_file );
176                 if( psz_uri )
177                 {
178                     input_item_SetArtURL( p_item, psz_uri );
179                     free( psz_uri );
180                 }
181                 free( psz_file );
182             }
183
184             b_found = true;
185         }
186         free( psz_filename );
187     }
188
189     /* */
190     closedir( p_dir );
191     free( psz_path );
192     return b_found ? VLC_SUCCESS : VLC_EGENERIC;
193 }
194
195
196 /* */
197 int playlist_SaveArt( playlist_t *p_playlist, input_item_t *p_item,
198                       const uint8_t *p_buffer, int i_buffer, const char *psz_type )
199 {
200     char *psz_filename = ArtCacheName( p_item, psz_type );
201
202     if( !psz_filename )
203         return VLC_EGENERIC;
204
205     char *psz_uri = make_URI( psz_filename );
206     if( !psz_uri )
207     {
208         free( psz_filename );
209         return VLC_EGENERIC;
210     }
211
212     /* Check if we already dumped it */
213     struct stat s;
214     if( !vlc_stat( psz_filename, &s ) )
215     {
216         input_item_SetArtURL( p_item, psz_uri );
217         free( psz_filename );
218         free( psz_uri );
219         return VLC_SUCCESS;
220     }
221
222     /* Dump it otherwise */
223     FILE *f = vlc_fopen( psz_filename, "wb" );
224     if( f )
225     {
226         if( fwrite( p_buffer, i_buffer, 1, f ) != 1 )
227         {
228             msg_Err( p_playlist, "%s: %m", psz_filename );
229         }
230         else
231         {
232             msg_Dbg( p_playlist, "album art saved to %s", psz_filename );
233             input_item_SetArtURL( p_item, psz_uri );
234         }
235         fclose( f );
236     }
237     free( psz_filename );
238     free( psz_uri );
239     return VLC_SUCCESS;
240 }
241