2
* This file is part of the Ubuntu TV Media Scanner
3
* Copyright (C) 2012-2013 Canonical Ltd.
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.
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.
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/>.
17
* Contact: Jim Hodapp <jim.hodapp@canonical.com>
18
* Authored by: Jussi Pakkanen <jussi.pakkanen@canonical.com>
21
#include"mediaartcache.h"
42
namespace mediascanner {
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);
51
buf = (const unsigned char*)normalized;
53
gssize bytes = str.length();
55
result = g_compute_checksum_for_data(G_CHECKSUM_MD5, buf, bytes);
57
g_free((gpointer)normalized);
62
MediaArtCache::MediaArtCache() {
63
string xdg_base = g_get_user_cache_dir();
66
string s("Could not determine cache dir.");
67
throw runtime_error(s);
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);
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);
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;
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 ");
97
throw runtime_error(e);
101
string MediaArtCache::get_art_file(const std::string &artist, const std::string &album) const {
102
string abs_fname = get_full_filename(artist, album);
104
if (access(abs_fname.c_str(), R_OK) == 0) {
105
utime(abs_fname.c_str(), nullptr); // update access times to current time
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);
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";
124
void MediaArtCache::clear() const {
125
DIR *d = opendir(root_dir.c_str());
127
string s = "Something went wrong.";
128
throw runtime_error(s);
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 == "..")
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(),
148
void MediaArtCache::prune() {
149
vector<pair<double, string>> mtimes;
150
DIR *d = opendir(root_dir.c_str());
152
string s = "Something went wrong.";
153
throw runtime_error(s);
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 == "..")
161
string fname = root_dir + "/" + basename;
163
if(stat(fname.c_str(), &sbuf) != 0) {
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));
172
if (mtimes.size() <= MAX_SIZE)
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));