67
68
gpointer user_data);
72
checksum_for_data (GChecksumType checksum_type,
79
checksum = g_checksum_new (checksum_type);
84
g_checksum_update (checksum, data, length);
85
retval = g_strdup (g_checksum_get_string (checksum));
86
g_checksum_free (checksum);
92
file_get_checksum_if_exists (GChecksumType checksum_type,
98
GFile *file = g_file_new_for_path (path);
99
GFileInputStream *stream;
103
checksum = g_checksum_new (checksum_type);
106
g_debug ("Can't create checksum engine");
107
g_object_unref (file);
111
stream = g_file_read (file, NULL, NULL);
117
/* File exists & readable always means true retval */
121
if (g_input_stream_read_all (G_INPUT_STREAM (stream), buffer, 3, &rsize, NULL, NULL)) {
122
if (rsize >= 3 && buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff) {
126
/* Add the read bytes to the checksum */
127
g_checksum_update (checksum, buffer, rsize);
129
/* Larger than 3 bytes but incorrect jpeg header */
136
/* Smaller than 3 bytes, not a jpeg */
144
while ((rsize = g_input_stream_read (G_INPUT_STREAM (stream), buffer, 1024, NULL, NULL)) > 0) {
145
g_checksum_update (checksum, buffer, rsize);
149
*md5 = g_strdup (g_checksum_get_string (checksum));
153
g_debug ("%s isn't readable while calculating MD5 checksum", path);
154
/* File doesn't exist or isn't readable */
161
g_object_unref (stream);
163
g_checksum_free (checksum);
164
g_object_unref (file);
170
convert_from_other_format (const gchar *found,
172
const gchar *album_path,
179
target_temp = g_strdup_printf ("%s-tmp", target);
181
retval = tracker_albumart_file_to_jpeg (found, target_temp);
183
if (retval && (artist == NULL || g_strcmp0 (artist, " ") == 0)) {
184
if (g_rename (target_temp, album_path) == -1) {
185
g_debug ("rename(%s, %s) error: %s", target_temp, album_path, g_strerror (errno));
187
} else if (retval && file_get_checksum_if_exists (G_CHECKSUM_MD5, target_temp, &sum1, FALSE, NULL)) {
189
if (file_get_checksum_if_exists (G_CHECKSUM_MD5, album_path, &sum2, FALSE, NULL)) {
190
if (g_strcmp0 (sum1, sum2) == 0) {
192
/* If album-space-md5.jpg is the same as found,
195
if (symlink (album_path, target) != 0) {
196
g_debug ("symlink(%s, %s) error: %s", album_path, target, g_strerror (errno));
202
g_unlink (target_temp);
206
/* If album-space-md5.jpg isn't the same as found,
207
* make a new album-md5-md5.jpg (found -> target) */
209
if (g_rename (target_temp, album_path) == -1) {
210
g_debug ("rename(%s, %s) error: %s", target_temp, album_path, g_strerror (errno));
216
/* If there's not yet a album-space-md5.jpg, make one,
217
* and symlink album-md5-md5.jpg to it */
219
g_rename (target_temp, album_path);
221
if (symlink (album_path, target) != 0) {
222
g_debug ("symlink(%s,%s) error: %s", album_path, target, g_strerror (errno));
232
g_debug ("Can't read %s while calculating checksum", target_temp);
233
/* Can't read the file that it was converted to, strange ... */
234
g_unlink (target_temp);
237
g_free (target_temp);
71
243
albumart_heuristic (const gchar *artist,
72
244
const gchar *album,
221
if (!file && target) {
222
file = g_file_new_for_path (target);
402
tracker_albumart_get_path (" ",
411
if (artist == NULL || g_strcmp0 (artist, " ") == 0) {
226
412
GFile *found_file;
229
found = g_build_filename (dirname, name, NULL);
230
416
g_debug ("Album art (JPEG) found in same directory being used:'%s'", found);
418
target_file = g_file_new_for_path (target);
232
419
found_file = g_file_new_for_path (found);
235
retval = g_file_copy (found_file, file, 0, NULL, NULL, NULL, &error);
420
retval = g_file_copy (found_file, target_file, 0, NULL, NULL, NULL, &err);
422
g_debug ("%s", err->message);
423
g_clear_error (&err);
236
425
g_object_unref (found_file);
238
g_clear_error (&error);
426
g_object_unref (target_file);
427
} else if (file_get_checksum_if_exists (G_CHECKSUM_MD5, found, &sum1, TRUE, &is_jpeg)) {
431
g_debug ("Album art (JPEG) found in same directory being used:'%s'", found);
433
if (file_get_checksum_if_exists (G_CHECKSUM_MD5, album_path, &sum2, FALSE, NULL)) {
434
if (g_strcmp0 (sum1, sum2) == 0) {
435
/* If album-space-md5.jpg is the same as found,
438
if (symlink (album_path, target) != 0) {
439
g_debug ("symlink(%s, %s) error: %s", album_path, target, g_strerror (errno));
449
/* If album-space-md5.jpg isn't the same as found,
450
* make a new album-md5-md5.jpg (found -> target) */
452
target_file = g_file_new_for_path (target);
453
found_file = g_file_new_for_path (found);
454
retval = g_file_copy (found_file, target_file, 0, NULL, NULL, NULL, &err);
456
g_debug ("%s", err->message);
457
g_clear_error (&err);
459
g_object_unref (found_file);
460
g_object_unref (target_file);
467
/* If there's not yet a album-space-md5.jpg, make one,
468
* and symlink album-md5-md5.jpg to it */
470
file = g_file_new_for_path (album_path);
471
found_file = g_file_new_for_path (found);
472
retval = g_file_copy (found_file, file, 0, NULL, NULL, NULL, &err);
475
if (symlink (album_path, target) != 0) {
476
g_debug ("symlink(%s, %s) error: %s", album_path, target, g_strerror (errno));
482
g_debug ("%s", err->message);
483
g_clear_error (&err);
487
g_object_unref (found_file);
488
g_object_unref (file);
491
g_debug ("Album art found in same directory but not a real JPEG file (trying to convert): '%s'", found);
492
retval = convert_from_other_format (found, target, album_path, artist);
497
/* Can't read contents of the cover.jpg file ... */
240
500
} else if (g_str_has_suffix (name_strdown, "png")) {
244
503
tracker_albumart_get_path (artist_stripped,
303
568
tracker_albumart_get_path (artist, album, "album", NULL, &local_path, NULL);
305
retval = tracker_albumart_buffer_to_jpeg (buffer, len, mime, local_path);
570
if (artist == NULL || g_strcmp0 (artist, " ") == 0) {
571
retval = tracker_albumart_buffer_to_jpeg (buffer, len, mime, local_path);
575
tracker_albumart_get_path (" ", album, "album", NULL, &album_path, NULL);
577
if (!g_file_test (album_path, G_FILE_TEST_EXISTS)) {
578
retval = tracker_albumart_buffer_to_jpeg (buffer, len, mime, album_path);
580
/* If album-space-md5.jpg doesn't exist, make one and make a symlink
581
* to album-md5-md5.jpg */
583
if (retval && symlink (album_path, local_path) != 0) {
584
g_debug ("symlink(%s, %s) error: %s", album_path, local_path, g_strerror (errno));
592
if (file_get_checksum_if_exists (G_CHECKSUM_MD5, album_path, &sum2, FALSE, NULL)) {
593
if ( !(g_strcmp0 (mime, "image/jpeg") == 0 || g_strcmp0 (mime, "JPG") == 0) ||
594
( !(len > 2 && buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff) )) {
596
gchar *temp = g_strdup_printf ("%s-tmp", album_path);
598
/* If buffer isn't a JPEG */
600
retval = tracker_albumart_buffer_to_jpeg (buffer, len, mime, temp);
602
if (retval && file_get_checksum_if_exists (G_CHECKSUM_MD5, temp, &sum1, FALSE, NULL)) {
603
if (g_strcmp0 (sum1, sum2) == 0) {
605
/* If album-space-md5.jpg is the same as buffer, make a symlink
606
* to album-md5-md5.jpg */
609
if (symlink (album_path, local_path) != 0) {
610
g_debug ("symlink(%s, %s) error: %s", album_path, local_path, g_strerror (errno));
616
/* If album-space-md5.jpg isn't the same as buffer, make a
617
* new album-md5-md5.jpg */
618
if (g_rename (temp, local_path) == -1) {
619
g_debug ("rename(%s, %s) error: %s", temp, local_path, g_strerror (errno));
624
/* Can't read temp file ... */
632
sum1 = checksum_for_data (G_CHECKSUM_MD5, buffer, len);
633
/* If album-space-md5.jpg is the same as buffer, make a symlink
634
* to album-md5-md5.jpg */
636
if (g_strcmp0 (sum1, sum2) == 0) {
637
if (symlink (album_path, local_path) != 0) {
638
g_debug ("symlink(%s, %s) error: %s", album_path, local_path, g_strerror (errno));
644
/* If album-space-md5.jpg isn't the same as buffer, make a
645
* new album-md5-md5.jpg */
646
retval = tracker_albumart_buffer_to_jpeg (buffer, len, mime, local_path);
307
656
g_free (local_path);