art.c 6.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*****************************************************************************
 * art.c : Art metadata handling
 *****************************************************************************
 * Copyright (C) 1998-2008 the VideoLAN team
 * $Id$
 *
 * Authors: Antoine Cellerier <dionoea@videolan.org>
 *          Clément Stenac <zorglub@videolan.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>
#include <vlc_common.h>
#include <vlc_playlist.h>
#include <vlc_charset.h>
#include <vlc_strings.h>
#include <vlc_stream.h>
35
#include <limits.h>                                             /* PATH_MAX */
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif

#include "../libvlc.h"
#include "playlist_internal.h"

static void ArtCacheCreateDir( const char *psz_dir )
{
    char newdir[strlen( psz_dir ) + 1];
    strcpy( newdir, psz_dir );
    char * psz_newdir = newdir;
    char * psz = psz_newdir;

    while( *psz )
    {
        while( *psz && *psz != DIR_SEP_CHAR) psz++;
        if( !*psz ) break;
        *psz = 0;
        if( !EMPTY_STR( psz_newdir ) )
            utf8_mkdir( psz_newdir, 0700 );
        *psz = DIR_SEP_CHAR;
        psz++;
    }
    utf8_mkdir( psz_dir, 0700 );
}

static char *ArtCacheGetSanitizedFileName( const char *psz )
{
    char *dup = strdup(psz);
    int i;

    filename_sanitize( dup );

    /* Doesn't create a filename with invalid characters
     * TODO: several filesystems forbid several characters: list them all
     */
    for( i = 0; dup[i] != '\0'; i++ )
    {
        if( dup[i] == DIR_SEP_CHAR )
            dup[i] = ' ';
    }
    return dup;
}

static void ArtCacheGetDirPath( char *psz_dir,
                                const char *psz_title,
                                const char *psz_artist, const char *psz_album )
{
    char *psz_cachedir = config_GetCacheDir();

    if( !EMPTY_STR(psz_artist) && !EMPTY_STR(psz_album) )
    {
        char * psz_album_sanitized = ArtCacheGetSanitizedFileName( psz_album );
        char * psz_artist_sanitized = ArtCacheGetSanitizedFileName( psz_artist );
        snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
                  "art" DIR_SEP "artistalbum" DIR_SEP "%s" DIR_SEP "%s",
                  psz_cachedir, psz_artist_sanitized, psz_album_sanitized );
        free( psz_album_sanitized );
        free( psz_artist_sanitized );
    }
    else
    {
        char * psz_title_sanitized = ArtCacheGetSanitizedFileName( psz_title );
        snprintf( psz_dir, PATH_MAX, "%s" DIR_SEP
                  "art" DIR_SEP "title" DIR_SEP "%s",
                  psz_cachedir, psz_title_sanitized );
        free( psz_title_sanitized );
    }
    free( psz_cachedir );
}

static char *ArtCachePath( input_item_t *p_item )
{
    char psz_path[PATH_MAX+1]; /* FIXME */

    vlc_mutex_lock( &p_item->lock );

    if( !p_item->p_meta )
        p_item->p_meta = vlc_meta_New();
    if( !p_item->p_meta )
    {
        vlc_mutex_unlock( &p_item->lock );
        return NULL;
    }

    const char *psz_artist = vlc_meta_Get( p_item->p_meta, vlc_meta_Artist );
    const char *psz_album = vlc_meta_Get( p_item->p_meta, vlc_meta_Album );
    const char *psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );

    if( !psz_title )
        psz_title = p_item->psz_name;

    if( (!psz_artist || !psz_album ) && !psz_title )
    {
        vlc_mutex_unlock( &p_item->lock );
        return NULL;
    }

    ArtCacheGetDirPath( psz_path, psz_title, psz_artist, psz_album );

    vlc_mutex_unlock( &p_item->lock );

    return strdup( psz_path );
}

static char *ArtCacheName( input_item_t *p_item, const char *psz_type )
{
    char *psz_path = ArtCachePath( p_item );
    if( !psz_path )
        return NULL;

    ArtCacheCreateDir( psz_path );

    char *psz_ext = strdup( psz_type ? psz_type : "" );
    filename_sanitize( psz_ext );

    char *psz_filename;
    if( asprintf( &psz_filename, "file://%s" DIR_SEP "art%s", psz_path, psz_ext ) < 0 )
        psz_filename = NULL;

    free( psz_ext );
    free( psz_path );

    return psz_filename;
}

164
/* */
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
int playlist_FindArtInCache( input_item_t *p_item )
{
    char *psz_path = ArtCachePath( p_item );

    if( !psz_path )
        return VLC_EGENERIC;

    /* Check if file exists */
    DIR *p_dir = utf8_opendir( psz_path );
    if( !p_dir )
    {
        free( psz_path );
        return VLC_EGENERIC;
    }

    bool b_found = false;
    char *psz_filename;
    while( !b_found && (psz_filename = utf8_readdir( p_dir )) )
    {
        if( !strncmp( psz_filename, "art", 3 ) )
        {
            char *psz_file;
            if( asprintf( &psz_file, "file://%s" DIR_SEP "%s",
                          psz_path, psz_filename ) < 0 )
                psz_file = NULL;
            if( psz_file )
                input_item_SetArtURL( p_item, psz_file );
            free( psz_file );

            b_found = true;
        }
        free( psz_filename );
    }

    /* */
    closedir( p_dir );
    free( psz_path );
    return b_found ? VLC_SUCCESS : VLC_EGENERIC;
}


/* */
int playlist_SaveArt( playlist_t *p_playlist, input_item_t *p_item,
                      const uint8_t *p_buffer, int i_buffer, const char *psz_type )
{
    char *psz_filename = ArtCacheName( p_item, psz_type );

    if( !psz_filename )
        return VLC_EGENERIC;

    /* Check if we already dumped it */
    struct stat s;
    if( !utf8_stat( psz_filename+7, &s ) )
    {
        input_item_SetArtURL( p_item, psz_filename );
        return VLC_SUCCESS;
    }

    /* Dump it otherwise */
    FILE *f = utf8_fopen( psz_filename+7, "w" );
    if( f )
    {
        if( fwrite( p_buffer, i_buffer, 1, f ) != 1 )
        {
            msg_Err( p_playlist, "%s: %m", psz_filename );
        }
        else
        {
            msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename );
            input_item_SetArtURL( p_item, psz_filename );
        }
        fclose( f );
    }
    return VLC_SUCCESS;
}