~ps-jenkins/mediascanner/latestsnapshot-0.3.93+13.10.20131011-0ubuntu1

« back to all changes in this revision

Viewing changes to src/mediascanner/mediaartcache.cpp

  • Committer: Tarmac
  • Author(s): Jussi Pakkanen
  • Date: 2013-08-29 14:22:59 UTC
  • mfrom: (370.3.15 media-scanner)
  • Revision ID: tarmac-20130829142259-074e8px82vtyq0od
A class to store image data of media elements.

Approved by James Henstridge, PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of the Ubuntu TV Media Scanner
 
3
 * Copyright (C) 2012-2013 Canonical Ltd.
 
4
 *
 
5
 * This program is free software: you can redistribute it and/or modify
 
6
 * it under the terms of the GNU Lesser General Public License version 3 as
 
7
 * published by the Free Software Foundation.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public License
 
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 *
 
17
 * Contact: Jim Hodapp <jim.hodapp@canonical.com>
 
18
 * Authored by: Jussi Pakkanen <jussi.pakkanen@canonical.com>
 
19
 */
 
20
 
 
21
#include"mediaartcache.h"
 
22
 
 
23
#include<dirent.h>
 
24
#include<sys/stat.h>
 
25
#include<fcntl.h>
 
26
#include<unistd.h>
 
27
#include<errno.h>
 
28
#include<stdio.h>
 
29
#include<string.h>
 
30
#include<stdlib.h>
 
31
#include<assert.h>
 
32
#include<utime.h>
 
33
 
 
34
#include<glib.h>
 
35
 
 
36
#include<vector>
 
37
#include<algorithm>
 
38
#include<stdexcept>
 
39
 
 
40
using namespace std;
 
41
 
 
42
namespace mediascanner {
 
43
 
 
44
static string md5(const string &str) {
 
45
    const unsigned char *buf = (const unsigned char *)str.c_str();
 
46
    char *normalized = g_utf8_normalize((const gchar*)buf, str.size(), G_NORMALIZE_ALL);
 
47
    string final;
 
48
    gchar *result;
 
49
 
 
50
    if(normalized) {
 
51
        buf = (const unsigned char*)normalized;
 
52
    }
 
53
    gssize bytes = str.length();
 
54
 
 
55
    result = g_compute_checksum_for_data(G_CHECKSUM_MD5, buf, bytes);
 
56
    final = result;
 
57
    g_free((gpointer)normalized);
 
58
    g_free(result);
 
59
    return final;
 
60
}
 
61
 
 
62
MediaArtCache::MediaArtCache() {
 
63
    string xdg_base = g_get_user_cache_dir();
 
64
 
 
65
    if (xdg_base == "") {
 
66
        string s("Could not determine cache dir.");
 
67
        throw runtime_error(s);
 
68
    }
 
69
    int ec = mkdir(xdg_base.c_str(), S_IRUSR | S_IWUSR | S_IXUSR);
 
70
    if (ec < 0 && errno != EEXIST) {
 
71
        string s("Could not create base dir.");
 
72
        throw runtime_error(s);
 
73
    }
 
74
    root_dir = xdg_base + "/media-art";
 
75
    ec = mkdir(root_dir.c_str(), S_IRUSR | S_IWUSR | S_IXUSR);
 
76
    if (ec < 0 && errno != EEXIST) {
 
77
        string s("Could not create cache dir.");
 
78
        throw runtime_error(s);
 
79
    }
 
80
}
 
81
 
 
82
bool MediaArtCache::has_art(const std::string &artist, const std::string &album) const {
 
83
    string fname = get_art_file(artist, album);
 
84
    return access(fname.c_str(), R_OK) == 0;
 
85
}
 
86
 
 
87
void MediaArtCache::add_art(const std::string &artist, const std::string &album,
 
88
        char *data, unsigned int datalen) {
 
89
    string abs_fname = get_full_filename(artist, album);
 
90
    GError *err = nullptr;
 
91
    if(!g_file_set_contents(abs_fname.c_str(), data, datalen, &err)) {
 
92
        string e("Could not write file ");
 
93
        e += abs_fname;
 
94
        e += ": ";
 
95
        e += err->message;
 
96
        g_error_free(err);
 
97
        throw runtime_error(e);
 
98
    }
 
99
}
 
100
 
 
101
string MediaArtCache::get_art_file(const std::string &artist, const std::string &album) const {
 
102
    string abs_fname = get_full_filename(artist, album);
 
103
 
 
104
    if (access(abs_fname.c_str(), R_OK) == 0) {
 
105
        utime(abs_fname.c_str(), nullptr); // update access times to current time
 
106
        return abs_fname;
 
107
    }
 
108
    return "";
 
109
}
 
110
 
 
111
std::string MediaArtCache::get_full_filename(const std::string &artist, const std::string & album) const {
 
112
    return root_dir + "/" + compute_base_name(artist, album);
 
113
}
 
114
 
 
115
 
 
116
std::string MediaArtCache::compute_base_name(const std::string &artist, const std::string &album) const {
 
117
    string type = "album";
 
118
    string h1 = md5(artist);
 
119
    string h2 = md5(album);
 
120
    return type + "-" + h1 + "-" + h2 + ".jpg";
 
121
}
 
122
 
 
123
 
 
124
void MediaArtCache::clear() const {
 
125
    DIR *d = opendir(root_dir.c_str());
 
126
    if(!d) {
 
127
        string s = "Something went wrong.";
 
128
        throw runtime_error(s);
 
129
    }
 
130
    struct dirent *entry, *de;
 
131
    entry = (dirent*)malloc(sizeof(dirent) + NAME_MAX);
 
132
    while(readdir_r(d, entry, &de) == 0 && de) {
 
133
        string basename = entry->d_name;
 
134
        if (basename == "." || basename == "..")
 
135
            continue;
 
136
        string fname = root_dir + "/" + basename;
 
137
        if(remove(fname.c_str()) < 0) {
 
138
            // This is not really an error worth
 
139
            // halting everything for.
 
140
            fprintf(stderr, "Could not delete file %s: %s.\n", fname.c_str(),
 
141
                    strerror(errno));
 
142
        }
 
143
    }
 
144
    free(entry);
 
145
    closedir(d);
 
146
}
 
147
 
 
148
void MediaArtCache::prune() {
 
149
    vector<pair<double, string>> mtimes;
 
150
    DIR *d = opendir(root_dir.c_str());
 
151
    if(!d) {
 
152
        string s = "Something went wrong.";
 
153
        throw runtime_error(s);
 
154
    }
 
155
    struct dirent *entry, *de;
 
156
    entry = (dirent*)malloc(sizeof(dirent) + NAME_MAX);
 
157
    while(readdir_r(d, entry, &de) == 0 && de) {
 
158
        string basename = entry->d_name;
 
159
        if (basename == "." || basename == "..")
 
160
            continue;
 
161
        string fname = root_dir + "/" + basename;
 
162
        struct stat sbuf;
 
163
        if(stat(fname.c_str(), &sbuf) != 0) {
 
164
            continue;
 
165
        }
 
166
        // Use mtime because atime is not guaranteed to work if, for example
 
167
        // the filesystem is mounted with noatime or relatime.
 
168
        mtimes.push_back(make_pair(sbuf.st_mtim.tv_sec + sbuf.st_mtim.tv_nsec/1000000000.0, fname));
 
169
    }
 
170
    free(entry);
 
171
    closedir(d);
 
172
    if (mtimes.size() <= MAX_SIZE)
 
173
        return;
 
174
    sort(mtimes.begin(), mtimes.end());
 
175
    for(size_t i=0; i < mtimes.size()-MAX_SIZE; i++) {
 
176
        if(remove(mtimes[i].second.c_str()) < 0) {
 
177
            fprintf(stderr, "Could not remove file %s: %s.\n",
 
178
                    mtimes[i].second.c_str(), strerror(errno));
 
179
        }
 
180
    }
 
181
}
 
182
 
 
183
}
 
184