2010-03-07 05:07:09 -05:00
|
|
|
/*
|
2020-02-19 17:06:16 -05:00
|
|
|
* Copyright (C) 2015-2020 Espen Jürgensen <espenjurgensen@gmail.com>
|
2011-03-30 12:27:23 -04:00
|
|
|
* Copyright (C) 2010-2011 Julien BLACHE <jb@jblache.org>
|
2010-03-07 05:07:09 -05:00
|
|
|
*
|
|
|
|
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-10-27 13:51:37 -04:00
|
|
|
#include <stdbool.h>
|
2010-03-07 05:07:09 -05:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2020-05-15 17:14:14 -04:00
|
|
|
#include <strings.h>
|
2010-03-07 05:07:09 -05:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
2020-02-21 13:51:35 -05:00
|
|
|
#include <pthread.h>
|
2010-03-07 05:07:09 -05:00
|
|
|
|
|
|
|
#include "db.h"
|
2014-01-11 17:34:03 -05:00
|
|
|
#include "misc.h"
|
2020-02-11 08:17:28 -05:00
|
|
|
#include "misc_json.h"
|
2010-03-07 05:07:09 -05:00
|
|
|
#include "logger.h"
|
2013-07-13 06:31:01 -04:00
|
|
|
#include "conffile.h"
|
2020-02-17 16:08:31 -05:00
|
|
|
#include "settings.h"
|
2014-11-10 16:53:08 -05:00
|
|
|
#include "cache.h"
|
2015-03-30 18:08:29 -04:00
|
|
|
#include "http.h"
|
2017-02-28 17:06:01 -05:00
|
|
|
#include "transcode.h"
|
2013-07-13 06:31:01 -04:00
|
|
|
|
2010-03-07 05:07:09 -05:00
|
|
|
#include "artwork.h"
|
|
|
|
|
2021-03-28 14:00:15 -04:00
|
|
|
#ifdef SPOTIFY
|
|
|
|
# include "library/spotify_webapi.h"
|
2014-04-04 17:14:43 -04:00
|
|
|
#endif
|
2010-03-07 05:07:09 -05:00
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
/* This artwork module will look for artwork by consulting a set of sources one
|
|
|
|
* at a time. A source is for instance the local library, the cache or a cover
|
|
|
|
* art database. For each source there is a handler function, which will do the
|
|
|
|
* actual work of getting the artwork.
|
|
|
|
*
|
|
|
|
* There are two types of handlers: item and group. Item handlers are capable of
|
|
|
|
* finding artwork for a single item (a dbmfi), while group handlers can get for
|
|
|
|
* an album or artist (a persistentid).
|
|
|
|
*
|
|
|
|
* An artwork source handler must return one of the following:
|
|
|
|
*
|
2020-11-05 17:06:23 -05:00
|
|
|
* ART_FMT_XXXX (positive) An image, see possible formats in artwork.h
|
2015-12-28 18:26:52 -05:00
|
|
|
* ART_E_NONE (zero) No artwork found
|
|
|
|
* ART_E_ERROR (negative) An error occurred while searching for artwork
|
|
|
|
* ART_E_ABORT (negative) Caller should abort artwork search (may be returned by cache)
|
|
|
|
*/
|
|
|
|
#define ART_E_NONE 0
|
|
|
|
#define ART_E_ERROR -1
|
|
|
|
#define ART_E_ABORT -2
|
|
|
|
|
2020-02-19 17:06:16 -05:00
|
|
|
// See online_source_is_failing()
|
|
|
|
#define ONLINE_SEARCH_COOLDOWN_TIME 3600
|
|
|
|
#define ONLINE_SEARCH_FAILURES_MAX 3
|
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
enum artwork_cache
|
|
|
|
{
|
|
|
|
NEVER = 0, // No caching of any results
|
|
|
|
ON_SUCCESS = 1, // Cache if artwork found
|
|
|
|
ON_FAILURE = 2, // Cache if artwork not found (so we don't keep asking)
|
|
|
|
};
|
|
|
|
|
2020-11-05 17:06:23 -05:00
|
|
|
// Input data to handlers, requested width, height and format. Can be set to
|
|
|
|
// zero then source is returned.
|
|
|
|
struct artwork_req_params {
|
|
|
|
int max_w;
|
|
|
|
int max_h;
|
|
|
|
int format;
|
|
|
|
};
|
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
/* This struct contains the data available to the handler, as well as a char
|
|
|
|
* buffer where the handler should output the path to the artwork (if it is
|
|
|
|
* local - otherwise the buffer can be left empty). The purpose of supplying the
|
|
|
|
* path is that the filescanner can then clear the cache in case the file
|
|
|
|
* changes.
|
|
|
|
*/
|
|
|
|
struct artwork_ctx {
|
2020-02-21 13:51:35 -05:00
|
|
|
// Handler should output path or URL to artwork here
|
2015-12-28 18:26:52 -05:00
|
|
|
char path[PATH_MAX];
|
|
|
|
// Handler should output artwork data to this evbuffer
|
|
|
|
struct evbuffer *evbuf;
|
|
|
|
|
2020-11-05 17:06:23 -05:00
|
|
|
// Requested size and format
|
|
|
|
struct artwork_req_params req_params;
|
|
|
|
|
2015-12-29 17:39:52 -05:00
|
|
|
// Input data to handler, did user configure to look for individual artwork
|
|
|
|
int individual;
|
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
// Input data for item handlers
|
|
|
|
struct db_media_file_info *dbmfi;
|
|
|
|
int id;
|
2020-02-18 16:12:54 -05:00
|
|
|
uint32_t data_kind;
|
2020-04-16 17:40:15 -04:00
|
|
|
uint32_t media_kind;
|
2015-12-28 18:26:52 -05:00
|
|
|
// Input data for group handlers
|
|
|
|
int64_t persistentid;
|
|
|
|
|
|
|
|
// Not to be used by handler - query for item or group
|
|
|
|
struct query_params qp;
|
|
|
|
// Not to be used by handler - should the result be cached
|
|
|
|
enum artwork_cache cache;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Definition of an artwork source. Covers both item and group sources.
|
|
|
|
*/
|
|
|
|
struct artwork_source {
|
|
|
|
// Name of the source, e.g. "cache"
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
// The handler
|
|
|
|
int (*handler)(struct artwork_ctx *ctx);
|
|
|
|
|
2020-04-16 17:40:15 -04:00
|
|
|
// data_kinds the handler can work with, combined with (1 << A) | (1 << B)
|
|
|
|
uint32_t data_kinds;
|
|
|
|
|
|
|
|
// media_kinds the handler supports, combined with A | B
|
|
|
|
uint32_t media_kinds;
|
2015-12-28 18:26:52 -05:00
|
|
|
|
|
|
|
// When should results from the source be cached?
|
|
|
|
enum artwork_cache cache;
|
|
|
|
};
|
|
|
|
|
2020-02-11 08:17:28 -05:00
|
|
|
/* Since online sources of artwork have similar characteristics there generic
|
|
|
|
* callers for them. They use the below info to request artwork.
|
|
|
|
*/
|
|
|
|
enum parse_result {
|
|
|
|
ONLINE_SOURCE_PARSE_OK,
|
|
|
|
ONLINE_SOURCE_PARSE_INVALID,
|
|
|
|
ONLINE_SOURCE_PARSE_NOT_FOUND,
|
|
|
|
ONLINE_SOURCE_PARSE_NO_PARSER,
|
|
|
|
};
|
|
|
|
|
2023-02-05 17:27:54 -05:00
|
|
|
// Remember previous artwork searches, used to avoid futile requests
|
|
|
|
struct online_search_history {
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
int last_id;
|
|
|
|
uint32_t last_hash;
|
|
|
|
int last_max_w;
|
|
|
|
int last_max_h;
|
|
|
|
int last_response_code;
|
|
|
|
char *last_artwork_url;
|
|
|
|
time_t last_timestamp;
|
|
|
|
int count_failures;
|
|
|
|
};
|
|
|
|
|
2020-02-11 08:17:28 -05:00
|
|
|
struct online_source {
|
|
|
|
// Name of online source
|
2020-02-21 13:51:35 -05:00
|
|
|
const char *name;
|
|
|
|
const char *setting_name;
|
2020-02-11 08:17:28 -05:00
|
|
|
|
|
|
|
// How to authorize (using the Authorize http header)
|
2020-02-21 13:51:35 -05:00
|
|
|
const char *auth_header;
|
2023-02-05 17:27:54 -05:00
|
|
|
int (*credentials_get)(char **auth_key, char **auth_secret);
|
2020-02-11 08:17:28 -05:00
|
|
|
|
|
|
|
// How to search for artwork
|
2020-02-21 13:51:35 -05:00
|
|
|
const char *search_endpoint;
|
|
|
|
const char *search_param;
|
2020-02-19 17:06:16 -05:00
|
|
|
struct {
|
2020-02-11 08:17:28 -05:00
|
|
|
const char *key;
|
|
|
|
const char *template;
|
|
|
|
} query_parts[8];
|
|
|
|
|
2023-02-05 17:27:54 -05:00
|
|
|
struct online_search_history *search_history;
|
2020-02-19 17:06:16 -05:00
|
|
|
|
2020-02-11 08:17:28 -05:00
|
|
|
// Function that can extract the artwork url from the parsed json response
|
|
|
|
enum parse_result (*response_jparse)(char **artwork_url, json_object *response, int max_w, int max_h);
|
|
|
|
};
|
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
/* File extensions that we look for or accept
|
|
|
|
*/
|
2010-10-24 12:43:51 -04:00
|
|
|
static const char *cover_extension[] =
|
|
|
|
{
|
2013-07-14 14:54:23 -04:00
|
|
|
"jpg", "png",
|
2010-10-24 12:43:51 -04:00
|
|
|
};
|
|
|
|
|
2016-01-07 16:48:04 -05:00
|
|
|
/* ----------------- DECLARE AND CONFIGURE SOURCE HANDLERS ----------------- */
|
|
|
|
|
|
|
|
/* Forward - group handlers */
|
|
|
|
static int source_group_cache_get(struct artwork_ctx *ctx);
|
|
|
|
static int source_group_dir_get(struct artwork_ctx *ctx);
|
|
|
|
/* Forward - item handlers */
|
|
|
|
static int source_item_cache_get(struct artwork_ctx *ctx);
|
|
|
|
static int source_item_embedded_get(struct artwork_ctx *ctx);
|
|
|
|
static int source_item_own_get(struct artwork_ctx *ctx);
|
2020-05-15 17:14:14 -04:00
|
|
|
static int source_item_artwork_url_get(struct artwork_ctx *ctx);
|
2019-09-09 15:39:25 -04:00
|
|
|
static int source_item_pipe_get(struct artwork_ctx *ctx);
|
2020-02-18 16:12:54 -05:00
|
|
|
static int source_item_spotifywebapi_track_get(struct artwork_ctx *ctx);
|
|
|
|
static int source_item_ownpl_get(struct artwork_ctx *ctx);
|
|
|
|
static int source_item_spotifywebapi_search_get(struct artwork_ctx *ctx);
|
2020-02-11 08:17:28 -05:00
|
|
|
static int source_item_discogs_get(struct artwork_ctx *ctx);
|
|
|
|
static int source_item_coverartarchive_get(struct artwork_ctx *ctx);
|
2016-01-07 16:48:04 -05:00
|
|
|
|
|
|
|
/* List of sources that can provide artwork for a group (i.e. usually an album
|
|
|
|
* identified by a persistentid). The source handlers will be called in the
|
|
|
|
* order of this list. Must be terminated by a NULL struct.
|
|
|
|
*/
|
|
|
|
static struct artwork_source artwork_group_source[] =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
.name = "cache",
|
|
|
|
.handler = source_group_cache_get,
|
|
|
|
.cache = ON_FAILURE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "directory",
|
|
|
|
.handler = source_group_dir_get,
|
|
|
|
.cache = ON_SUCCESS | ON_FAILURE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = NULL,
|
|
|
|
.handler = NULL,
|
|
|
|
.cache = 0,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* List of sources that can provide artwork for an item (a track characterized
|
|
|
|
* by a dbmfi). The source handlers will be called in the order of this list.
|
|
|
|
* The handler will only be called if the data_kind matches. Must be terminated
|
|
|
|
* by a NULL struct.
|
|
|
|
*/
|
|
|
|
static struct artwork_source artwork_item_source[] =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
.name = "cache",
|
|
|
|
.handler = source_item_cache_get,
|
|
|
|
.data_kinds = (1 << DATA_KIND_FILE) | (1 << DATA_KIND_SPOTIFY),
|
2020-04-16 17:40:15 -04:00
|
|
|
.media_kinds = MEDIA_KIND_ALL,
|
2016-01-07 16:48:04 -05:00
|
|
|
.cache = ON_FAILURE,
|
|
|
|
},
|
2020-02-17 16:08:31 -05:00
|
|
|
{
|
2016-01-07 16:48:04 -05:00
|
|
|
.name = "embedded",
|
|
|
|
.handler = source_item_embedded_get,
|
2020-04-25 15:59:18 -04:00
|
|
|
.data_kinds = (1 << DATA_KIND_FILE) | (1 << DATA_KIND_HTTP),
|
2020-04-16 17:40:15 -04:00
|
|
|
.media_kinds = MEDIA_KIND_ALL,
|
2016-01-07 16:48:04 -05:00
|
|
|
.cache = ON_SUCCESS | ON_FAILURE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "own",
|
|
|
|
.handler = source_item_own_get,
|
2023-03-28 16:56:26 -04:00
|
|
|
.data_kinds = (1 << DATA_KIND_FILE),
|
2020-04-16 17:40:15 -04:00
|
|
|
.media_kinds = MEDIA_KIND_ALL,
|
2016-01-07 16:48:04 -05:00
|
|
|
.cache = ON_SUCCESS | ON_FAILURE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "stream",
|
2020-05-15 17:14:14 -04:00
|
|
|
.handler = source_item_artwork_url_get,
|
2022-03-19 17:44:22 -04:00
|
|
|
.data_kinds = (1 << DATA_KIND_HTTP) | (1 << DATA_KIND_PIPE),
|
2020-04-16 17:40:15 -04:00
|
|
|
.media_kinds = MEDIA_KIND_MUSIC,
|
2020-10-20 17:22:49 -04:00
|
|
|
.cache = NEVER,
|
2016-01-07 16:48:04 -05:00
|
|
|
},
|
2019-09-09 15:39:25 -04:00
|
|
|
{
|
|
|
|
.name = "pipe",
|
|
|
|
.handler = source_item_pipe_get,
|
|
|
|
.data_kinds = (1 << DATA_KIND_PIPE),
|
2020-04-16 17:40:15 -04:00
|
|
|
.media_kinds = MEDIA_KIND_ALL,
|
2019-09-09 15:39:25 -04:00
|
|
|
.cache = NEVER,
|
|
|
|
},
|
2019-02-05 05:35:45 -05:00
|
|
|
{
|
2020-02-18 16:12:54 -05:00
|
|
|
.name = "Spotify track web api",
|
|
|
|
.handler = source_item_spotifywebapi_track_get,
|
2019-02-05 05:35:45 -05:00
|
|
|
.data_kinds = (1 << DATA_KIND_SPOTIFY),
|
2020-04-16 17:40:15 -04:00
|
|
|
.media_kinds = MEDIA_KIND_ALL,
|
2019-02-09 02:35:20 -05:00
|
|
|
.cache = ON_SUCCESS | ON_FAILURE,
|
2019-02-05 05:35:45 -05:00
|
|
|
},
|
2017-01-04 17:20:59 -05:00
|
|
|
{
|
2020-10-20 17:22:49 -04:00
|
|
|
// Note that even though caching is set for this handler, it will in most
|
|
|
|
// cases not happen because source_item_artwork_url_get() comes before
|
|
|
|
// and has NEVER. This is intentional because this handler is also a
|
|
|
|
// backup for when we don't get anything from the stream.
|
2017-01-04 17:20:59 -05:00
|
|
|
.name = "playlist own",
|
|
|
|
.handler = source_item_ownpl_get,
|
|
|
|
.data_kinds = (1 << DATA_KIND_HTTP),
|
2020-04-16 17:40:15 -04:00
|
|
|
.media_kinds = MEDIA_KIND_ALL,
|
2020-10-20 17:22:49 -04:00
|
|
|
.cache = ON_SUCCESS | ON_FAILURE,
|
2017-01-04 17:20:59 -05:00
|
|
|
},
|
2020-02-18 16:12:54 -05:00
|
|
|
{
|
|
|
|
.name = "Spotify search web api (files)",
|
|
|
|
.handler = source_item_spotifywebapi_search_get,
|
|
|
|
.data_kinds = (1 << DATA_KIND_FILE),
|
2020-04-16 17:40:15 -04:00
|
|
|
.media_kinds = MEDIA_KIND_MUSIC,
|
2020-02-18 16:12:54 -05:00
|
|
|
.cache = ON_SUCCESS | ON_FAILURE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "Spotify search web api (streams)",
|
|
|
|
.handler = source_item_spotifywebapi_search_get,
|
|
|
|
.data_kinds = (1 << DATA_KIND_HTTP) | (1 << DATA_KIND_PIPE),
|
2020-04-16 17:40:15 -04:00
|
|
|
.media_kinds = MEDIA_KIND_MUSIC,
|
2020-10-20 17:22:49 -04:00
|
|
|
.cache = NEVER,
|
2020-02-18 16:12:54 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "Discogs (files)",
|
|
|
|
.handler = source_item_discogs_get,
|
|
|
|
.data_kinds = (1 << DATA_KIND_FILE),
|
2020-04-16 17:40:15 -04:00
|
|
|
.media_kinds = MEDIA_KIND_MUSIC,
|
2020-02-18 16:12:54 -05:00
|
|
|
.cache = ON_SUCCESS | ON_FAILURE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "Discogs (streams)",
|
|
|
|
.handler = source_item_discogs_get,
|
|
|
|
.data_kinds = (1 << DATA_KIND_HTTP) | (1 << DATA_KIND_PIPE),
|
2020-04-16 17:40:15 -04:00
|
|
|
.media_kinds = MEDIA_KIND_MUSIC,
|
2020-10-20 17:22:49 -04:00
|
|
|
.cache = NEVER,
|
2020-02-18 16:12:54 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// The Cover Art Archive seems rather slow, so low priority
|
|
|
|
.name = "Cover Art Archive (files)",
|
|
|
|
.handler = source_item_coverartarchive_get,
|
|
|
|
.data_kinds = (1 << DATA_KIND_FILE),
|
2020-04-16 17:40:15 -04:00
|
|
|
.media_kinds = MEDIA_KIND_MUSIC,
|
2020-02-18 16:12:54 -05:00
|
|
|
.cache = ON_SUCCESS | ON_FAILURE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// The Cover Art Archive seems rather slow, so low priority
|
|
|
|
.name = "Cover Art Archive (streams)",
|
|
|
|
.handler = source_item_coverartarchive_get,
|
|
|
|
.data_kinds = (1 << DATA_KIND_HTTP) | (1 << DATA_KIND_PIPE),
|
2020-04-16 17:40:15 -04:00
|
|
|
.media_kinds = MEDIA_KIND_MUSIC,
|
2020-10-20 17:22:49 -04:00
|
|
|
.cache = NEVER,
|
2020-02-18 16:12:54 -05:00
|
|
|
},
|
2023-03-28 16:56:26 -04:00
|
|
|
{
|
|
|
|
.name = "own (pipe)",
|
|
|
|
.handler = source_item_own_get,
|
|
|
|
.data_kinds = (1 << DATA_KIND_PIPE),
|
|
|
|
.media_kinds = MEDIA_KIND_ALL,
|
|
|
|
.cache = ON_SUCCESS | ON_FAILURE,
|
|
|
|
},
|
2016-01-07 16:48:04 -05:00
|
|
|
{
|
|
|
|
.name = NULL,
|
|
|
|
.handler = NULL,
|
|
|
|
.data_kinds = 0,
|
|
|
|
.cache = 0,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-11 08:17:28 -05:00
|
|
|
/* Forward - parsers of online source responses */
|
|
|
|
static enum parse_result response_jparse_spotify(char **artwork_url, json_object *response, int max_w, int max_h);
|
|
|
|
static enum parse_result response_jparse_discogs(char **artwork_url, json_object *response, int max_w, int max_h);
|
|
|
|
static enum parse_result response_jparse_musicbrainz(char **artwork_url, json_object *response, int max_w, int max_h);
|
|
|
|
|
2023-02-05 17:27:54 -05:00
|
|
|
static int credentials_get_spotify(char **auth_key, char **auth_secret);
|
|
|
|
static int credentials_get_discogs(char **auth_key, char **auth_secret);
|
|
|
|
|
|
|
|
static struct online_search_history search_history_spotify = { .mutex = PTHREAD_MUTEX_INITIALIZER };
|
|
|
|
static struct online_search_history search_history_discogs = { .mutex = PTHREAD_MUTEX_INITIALIZER };
|
|
|
|
static struct online_search_history search_history_musicbrainz = { .mutex = PTHREAD_MUTEX_INITIALIZER };
|
|
|
|
|
2020-02-11 08:17:28 -05:00
|
|
|
/* Definitions of online sources */
|
2023-02-05 17:27:54 -05:00
|
|
|
static const struct online_source spotify_source =
|
2020-02-11 08:17:28 -05:00
|
|
|
{
|
|
|
|
.name = "Spotify",
|
2020-02-21 13:51:35 -05:00
|
|
|
.setting_name = "use_artwork_source_spotify",
|
2020-02-11 08:17:28 -05:00
|
|
|
.auth_header = "Bearer $SECRET$",
|
2023-02-05 17:27:54 -05:00
|
|
|
.credentials_get = credentials_get_spotify,
|
2020-02-11 08:17:28 -05:00
|
|
|
.search_endpoint = "https://api.spotify.com/v1/search",
|
2020-02-19 17:06:16 -05:00
|
|
|
.search_param = "type=track&limit=1&$QUERY$",
|
2020-02-11 08:17:28 -05:00
|
|
|
.query_parts =
|
|
|
|
{
|
2020-02-22 10:49:00 -05:00
|
|
|
{ "q", "artist:$ARTIST$ album:$ALBUM$" },
|
2020-02-18 16:12:54 -05:00
|
|
|
{ "q", "artist:$ARTIST$ track:$TITLE$" },
|
2020-02-11 08:17:28 -05:00
|
|
|
{ NULL, NULL },
|
|
|
|
},
|
|
|
|
.response_jparse = response_jparse_spotify,
|
2023-02-05 17:27:54 -05:00
|
|
|
.search_history = &search_history_spotify,
|
2020-02-11 08:17:28 -05:00
|
|
|
};
|
|
|
|
|
2023-02-05 17:27:54 -05:00
|
|
|
static const struct online_source discogs_source =
|
2020-02-11 08:17:28 -05:00
|
|
|
{
|
|
|
|
.name = "Discogs",
|
2020-02-21 13:51:35 -05:00
|
|
|
.setting_name = "use_artwork_source_discogs",
|
2020-02-11 08:17:28 -05:00
|
|
|
.auth_header = "Discogs key=$KEY$, secret=$SECRET$",
|
2023-02-05 17:27:54 -05:00
|
|
|
.credentials_get = credentials_get_discogs,
|
2020-02-11 08:17:28 -05:00
|
|
|
.search_endpoint = "https://api.discogs.com/database/search",
|
|
|
|
.search_param = "type=release&per_page=1&$QUERY$",
|
|
|
|
.query_parts =
|
|
|
|
{
|
|
|
|
{ "artist", "$ARTIST$" },
|
|
|
|
{ "release_title", "$ALBUM$" },
|
|
|
|
{ "track", "$TITLE$" },
|
|
|
|
{ NULL, NULL },
|
|
|
|
},
|
|
|
|
.response_jparse = response_jparse_discogs,
|
2023-02-05 17:27:54 -05:00
|
|
|
.search_history = &search_history_discogs,
|
2020-02-11 08:17:28 -05:00
|
|
|
};
|
|
|
|
|
2023-02-05 17:27:54 -05:00
|
|
|
static const struct online_source musicbrainz_source =
|
2020-02-11 08:17:28 -05:00
|
|
|
{
|
|
|
|
.name = "Musicbrainz",
|
2020-02-21 13:51:35 -05:00
|
|
|
.setting_name = "use_artwork_source_coverartarchive",
|
2020-02-11 08:17:28 -05:00
|
|
|
.search_endpoint = "http://musicbrainz.org/ws/2/release-group/",
|
|
|
|
.search_param = "limit=1&fmt=json&$QUERY$",
|
|
|
|
.query_parts =
|
|
|
|
{
|
|
|
|
{ "query", "artist:$ARTIST$ AND release:$ALBUM$ AND status:Official" },
|
2020-02-18 16:12:54 -05:00
|
|
|
{ "query", "artist:$ARTIST$ AND title:$TITLE$ AND status:Official" },
|
2020-02-11 08:17:28 -05:00
|
|
|
{ NULL, NULL },
|
|
|
|
},
|
|
|
|
.response_jparse = response_jparse_musicbrainz,
|
2023-02-05 17:27:54 -05:00
|
|
|
.search_history = &search_history_musicbrainz,
|
2020-02-11 08:17:28 -05:00
|
|
|
};
|
|
|
|
|
2016-01-07 16:48:04 -05:00
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
|
|
|
|
/* -------------------------------- HELPERS -------------------------------- */
|
|
|
|
|
2019-09-17 16:02:45 -04:00
|
|
|
/* Reads an artwork file from the given http url straight into an evbuf
|
2019-02-05 05:35:45 -05:00
|
|
|
*
|
|
|
|
* @out evbuf Image data
|
|
|
|
* @in url URL for the image
|
2020-10-20 17:22:49 -04:00
|
|
|
* @return ART_FMT_* on success, ART_E_NONE on 404, ART_E_ERROR otherwise
|
2019-02-05 05:35:45 -05:00
|
|
|
*/
|
|
|
|
static int
|
2020-02-11 08:17:28 -05:00
|
|
|
artwork_read_byurl(struct evbuffer *evbuf, const char *url)
|
2019-02-05 05:35:45 -05:00
|
|
|
{
|
|
|
|
struct http_client_ctx client;
|
|
|
|
struct keyval *kv;
|
|
|
|
const char *content_type;
|
2020-02-11 08:17:28 -05:00
|
|
|
size_t len;
|
2020-10-20 17:22:49 -04:00
|
|
|
int format;
|
2019-02-05 05:35:45 -05:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
DPRINTF(E_SPAM, L_ART, "Trying internet artwork in %s\n", url);
|
|
|
|
|
2020-10-20 17:22:49 -04:00
|
|
|
format = ART_E_ERROR;
|
2020-02-11 08:17:28 -05:00
|
|
|
CHECK_NULL(L_ART, kv = keyval_alloc());
|
2019-02-05 05:35:45 -05:00
|
|
|
|
|
|
|
len = strlen(url);
|
|
|
|
if ((len < 14) || (len > PATH_MAX)) // Can't be shorter than http://a/1.jpg
|
2020-02-11 08:17:28 -05:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_ART, "Artwork request URL is invalid (len=%zu): '%s'\n", len, url);
|
2020-10-20 17:22:49 -04:00
|
|
|
goto out;
|
2020-02-11 08:17:28 -05:00
|
|
|
}
|
2019-02-05 05:35:45 -05:00
|
|
|
|
|
|
|
memset(&client, 0, sizeof(struct http_client_ctx));
|
|
|
|
client.url = url;
|
|
|
|
client.input_headers = kv;
|
|
|
|
client.input_body = evbuf;
|
|
|
|
|
2019-02-09 11:04:25 -05:00
|
|
|
ret = http_client_request(&client, NULL);
|
2020-02-11 08:17:28 -05:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_ART, "Request to '%s' failed with return value %d\n", url, ret);
|
2020-10-20 17:22:49 -04:00
|
|
|
goto out;
|
2020-02-11 08:17:28 -05:00
|
|
|
}
|
2019-02-05 05:35:45 -05:00
|
|
|
|
2020-10-20 17:22:49 -04:00
|
|
|
if (client.response_code == HTTP_NOTFOUND)
|
|
|
|
{
|
|
|
|
DPRINTF(E_INFO, L_ART, "No artwork found at '%s' (code %d)\n", url, client.response_code);
|
|
|
|
format = ART_E_NONE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else if (client.response_code != HTTP_OK)
|
2020-02-11 08:17:28 -05:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_ART, "Request to '%s' failed with code %d\n", url, client.response_code);
|
2020-10-20 17:22:49 -04:00
|
|
|
goto out;
|
2020-02-11 08:17:28 -05:00
|
|
|
}
|
2019-02-09 02:35:20 -05:00
|
|
|
|
2019-02-05 05:35:45 -05:00
|
|
|
content_type = keyval_get(kv, "Content-Type");
|
2020-05-15 17:10:43 -04:00
|
|
|
if (content_type && (strcasecmp(content_type, "image/jpeg") == 0 || strcasecmp(content_type, "image/jpg") == 0))
|
2020-10-20 17:22:49 -04:00
|
|
|
format = ART_FMT_JPEG;
|
2020-05-15 17:10:43 -04:00
|
|
|
else if (content_type && strcasecmp(content_type, "image/png") == 0)
|
2020-10-20 17:22:49 -04:00
|
|
|
format = ART_FMT_PNG;
|
2020-02-11 08:17:28 -05:00
|
|
|
else
|
2020-10-20 17:22:49 -04:00
|
|
|
DPRINTF(E_LOG, L_ART, "Artwork from '%s' has no known content type\n", url);
|
2019-02-05 05:35:45 -05:00
|
|
|
|
2020-10-20 17:22:49 -04:00
|
|
|
out:
|
2019-02-05 05:35:45 -05:00
|
|
|
keyval_clear(kv);
|
|
|
|
free(kv);
|
2020-10-20 17:22:49 -04:00
|
|
|
return format;
|
2019-02-05 05:35:45 -05:00
|
|
|
}
|
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
/* Reads an artwork file from the filesystem straight into an evbuf
|
|
|
|
* TODO Use evbuffer_add_file or evbuffer_read?
|
|
|
|
*
|
|
|
|
* @out evbuf Image data
|
|
|
|
* @in path Path to the artwork
|
|
|
|
* @return 0 on success, -1 on error
|
|
|
|
*/
|
2010-03-07 05:07:09 -05:00
|
|
|
static int
|
2020-02-11 08:17:28 -05:00
|
|
|
artwork_read_bypath(struct evbuffer *evbuf, char *path)
|
2010-03-07 05:07:09 -05:00
|
|
|
{
|
|
|
|
uint8_t buf[4096];
|
|
|
|
struct stat sb;
|
|
|
|
int fd;
|
|
|
|
int ret;
|
|
|
|
|
2015-01-04 09:12:46 -05:00
|
|
|
fd = open(path, O_RDONLY);
|
2010-03-07 05:07:09 -05:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
2015-01-04 09:12:46 -05:00
|
|
|
DPRINTF(E_WARN, L_ART, "Could not open artwork file '%s': %s\n", path, strerror(errno));
|
2010-03-07 05:07:09 -05:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = fstat(fd, &sb);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2015-01-04 09:12:46 -05:00
|
|
|
DPRINTF(E_WARN, L_ART, "Could not stat() artwork file '%s': %s\n", path, strerror(errno));
|
2010-03-07 05:07:09 -05:00
|
|
|
|
|
|
|
goto out_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = evbuffer_expand(evbuf, sb.st_size);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_ART, "Out of memory for artwork\n");
|
|
|
|
|
|
|
|
goto out_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((ret = read(fd, buf, sizeof(buf))) > 0)
|
|
|
|
evbuffer_add(evbuf, buf, ret);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_fail:
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-11-28 15:20:24 -05:00
|
|
|
/* Calculates new size if the source image will not fit inside the requested
|
|
|
|
* size. If the original fits then dst_w/h will equal src_w/h.
|
2015-12-28 18:26:52 -05:00
|
|
|
*
|
2020-11-28 15:20:24 -05:00
|
|
|
* @out dst_w Rescaled width
|
|
|
|
* @out dst_h Rescaled height
|
|
|
|
* @in src_w Actual width
|
|
|
|
* @in src_h Actual height
|
|
|
|
* @in max_w Requested width
|
|
|
|
* @in max_h Requested height
|
2015-12-28 18:26:52 -05:00
|
|
|
*/
|
2020-11-28 15:20:24 -05:00
|
|
|
static void
|
|
|
|
size_calculate(int *dst_w, int *dst_h, int src_w, int src_h, int max_w, int max_h)
|
2014-01-09 15:07:27 -05:00
|
|
|
{
|
2020-11-28 15:20:24 -05:00
|
|
|
DPRINTF(E_DBG, L_ART, "Original image dimensions: w %d h %d\n", src_w, src_h);
|
2014-01-09 15:07:27 -05:00
|
|
|
|
2020-11-28 15:20:24 -05:00
|
|
|
*dst_w = src_w;
|
|
|
|
*dst_h = src_h;
|
2014-01-09 15:07:27 -05:00
|
|
|
|
2020-11-28 15:20:24 -05:00
|
|
|
// No valid target dimensions, use original
|
|
|
|
if ((max_w <= 0) || (max_h <= 0))
|
|
|
|
return;
|
2014-01-09 15:07:27 -05:00
|
|
|
|
2020-11-28 15:20:24 -05:00
|
|
|
// Smaller than target, use original
|
|
|
|
if ((src_w <= max_w) && (src_h <= max_h))
|
|
|
|
return;
|
2014-01-09 15:07:27 -05:00
|
|
|
|
2020-11-28 15:20:24 -05:00
|
|
|
// Wider aspect ratio than target
|
|
|
|
if (src_w * max_h > src_h * max_w)
|
2014-01-09 15:07:27 -05:00
|
|
|
{
|
2020-11-28 15:20:24 -05:00
|
|
|
*dst_w = max_w;
|
|
|
|
*dst_h = (double)max_w * ((double)src_h / (double)src_w);
|
2014-01-09 15:07:27 -05:00
|
|
|
}
|
2020-11-28 15:20:24 -05:00
|
|
|
// Taller or equal aspect ratio
|
|
|
|
else
|
2014-01-09 15:07:27 -05:00
|
|
|
{
|
2020-11-28 15:20:24 -05:00
|
|
|
*dst_w = (double)max_h * ((double)src_w / (double)src_h);
|
|
|
|
*dst_h = max_h;
|
2014-01-09 15:07:27 -05:00
|
|
|
}
|
|
|
|
|
2020-11-28 15:20:24 -05:00
|
|
|
if (*dst_h > max_h)
|
|
|
|
*dst_h = max_h;
|
2014-01-09 15:07:27 -05:00
|
|
|
|
2020-11-28 15:20:24 -05:00
|
|
|
// PNG prefers even row count
|
|
|
|
*dst_w += *dst_w % 2;
|
2014-01-09 15:07:27 -05:00
|
|
|
|
2020-11-28 15:20:24 -05:00
|
|
|
if (*dst_w > max_w)
|
|
|
|
*dst_w = max_w - (max_w % 2);
|
2014-01-09 15:07:27 -05:00
|
|
|
|
2020-11-28 15:20:24 -05:00
|
|
|
DPRINTF(E_DBG, L_ART, "Rescale required, destination width %d height %d\n", *dst_w, *dst_h);
|
|
|
|
}
|
2010-03-07 05:07:09 -05:00
|
|
|
|
2019-02-05 05:35:45 -05:00
|
|
|
/*
|
2020-04-07 15:44:46 -04:00
|
|
|
* Either gets the artwork file given in "path" (rescaled if needed) or rescales
|
|
|
|
* the artwork given in "inbuf".
|
2015-12-28 18:26:52 -05:00
|
|
|
*
|
2019-02-05 05:35:45 -05:00
|
|
|
* @out evbuf Image data (rescaled if needed)
|
2017-10-27 13:51:37 -04:00
|
|
|
* @in path Path to the artwork file (alternative to inbuf)
|
2020-11-28 15:20:24 -05:00
|
|
|
* @in in_buf Buffer with the artwork (alternative to path)
|
2017-10-27 13:51:37 -04:00
|
|
|
* @in is_embedded Whether the artwork in file is embedded or raw jpeg/png
|
2020-04-07 15:44:46 -04:00
|
|
|
* @in data_kind Used by the transcode module to determine e.g. probe size
|
2020-11-05 17:06:23 -05:00
|
|
|
* @in req_params Requested max size/format
|
2017-10-27 13:51:37 -04:00
|
|
|
* @return ART_FMT_* on success, ART_E_ERROR on error
|
2015-12-28 18:26:52 -05:00
|
|
|
*/
|
2010-03-07 05:07:09 -05:00
|
|
|
static int
|
2020-11-28 15:20:24 -05:00
|
|
|
artwork_get(struct evbuffer *evbuf, char *path, struct evbuffer *in_buf, bool is_embedded, enum data_kind data_kind, struct artwork_req_params req_params)
|
2010-03-07 05:07:09 -05:00
|
|
|
{
|
2020-11-28 15:20:24 -05:00
|
|
|
struct decode_ctx *xcode_decode = NULL;
|
|
|
|
struct encode_ctx *xcode_encode = NULL;
|
2021-04-06 10:16:32 -04:00
|
|
|
struct transcode_evbuf_io xcode_evbuf_io = { 0 };
|
2020-11-28 15:20:24 -05:00
|
|
|
struct evbuffer *xcode_buf = NULL;
|
2017-02-28 17:06:01 -05:00
|
|
|
void *frame;
|
2020-11-05 17:06:23 -05:00
|
|
|
int src_width;
|
|
|
|
int src_height;
|
|
|
|
int src_format;
|
|
|
|
int dst_width;
|
|
|
|
int dst_height;
|
|
|
|
int dst_format;
|
2010-03-07 05:07:09 -05:00
|
|
|
int ret;
|
|
|
|
|
2020-11-05 17:06:23 -05:00
|
|
|
DPRINTF(E_SPAM, L_ART, "Getting artwork (max destination width %d height %d)\n", req_params.max_w, req_params.max_h);
|
2011-03-08 13:18:43 -05:00
|
|
|
|
2020-11-28 15:20:24 -05:00
|
|
|
// At this point we don't know if we will need to rescale/reformat, and we
|
|
|
|
// won't know until probing the source (which the transcode module does). The
|
|
|
|
// act of probing uses evbuffer_remove(), thus consuming some of the buffer.
|
|
|
|
// So that means that if rescaling/reformating turns out not to be required,
|
|
|
|
// we could no longer just add in_buf to the evbuf buffer and return to the
|
|
|
|
// caller. The below makes that possible (with no copying).
|
|
|
|
if (in_buf)
|
|
|
|
{
|
|
|
|
CHECK_NULL(L_ART, xcode_buf = evbuffer_new());
|
|
|
|
ret = evbuffer_add_buffer_reference(xcode_buf, in_buf);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_ART, "Could not copy/ref raw image for rescaling (ret=%d)\n", ret);
|
|
|
|
ret = ART_E_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2021-04-06 10:16:32 -04:00
|
|
|
|
|
|
|
xcode_evbuf_io.evbuf = xcode_buf;
|
|
|
|
xcode_decode = transcode_decode_setup(XCODE_JPEG, NULL, data_kind, NULL, &xcode_evbuf_io, 0); // Covers XCODE_PNG too
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xcode_decode = transcode_decode_setup(XCODE_JPEG, NULL, data_kind, path, NULL, 0); // Covers XCODE_PNG too
|
2020-11-28 15:20:24 -05:00
|
|
|
}
|
|
|
|
|
2017-02-28 17:06:01 -05:00
|
|
|
if (!xcode_decode)
|
2010-03-07 05:07:09 -05:00
|
|
|
{
|
2017-10-27 13:51:37 -04:00
|
|
|
if (path)
|
|
|
|
DPRINTF(E_DBG, L_ART, "No artwork found in '%s'\n", path);
|
|
|
|
else
|
|
|
|
DPRINTF(E_DBG, L_ART, "No artwork provided to artwork_get()\n");
|
2020-11-28 15:20:24 -05:00
|
|
|
|
|
|
|
ret = ART_E_NONE;
|
|
|
|
goto out;
|
2010-03-07 05:07:09 -05:00
|
|
|
}
|
|
|
|
|
2020-11-28 15:20:24 -05:00
|
|
|
// Determine source and destination format
|
2017-02-28 17:06:01 -05:00
|
|
|
if (transcode_decode_query(xcode_decode, "is_jpeg"))
|
2020-11-05 17:06:23 -05:00
|
|
|
src_format = ART_FMT_JPEG;
|
2017-02-28 17:06:01 -05:00
|
|
|
else if (transcode_decode_query(xcode_decode, "is_png"))
|
2020-11-05 17:06:23 -05:00
|
|
|
src_format = ART_FMT_PNG;
|
2017-02-28 17:06:01 -05:00
|
|
|
else
|
2010-03-07 05:07:09 -05:00
|
|
|
{
|
2020-11-28 15:20:24 -05:00
|
|
|
if (path)
|
2017-10-27 13:51:37 -04:00
|
|
|
DPRINTF(E_DBG, L_ART, "File '%s' has no PNG or JPEG artwork\n", path);
|
|
|
|
else
|
|
|
|
DPRINTF(E_LOG, L_ART, "Artwork data provided to artwork_get() is not PNG or JPEG\n");
|
|
|
|
|
2020-11-28 15:20:24 -05:00
|
|
|
ret = ART_E_ERROR;
|
|
|
|
goto out;
|
2010-03-07 05:07:09 -05:00
|
|
|
}
|
|
|
|
|
2020-11-28 15:20:24 -05:00
|
|
|
dst_format = req_params.format ? req_params.format : src_format;
|
|
|
|
|
|
|
|
// Determine source and destination size
|
2020-11-05 17:06:23 -05:00
|
|
|
src_width = transcode_decode_query(xcode_decode, "width");
|
|
|
|
src_height = transcode_decode_query(xcode_decode, "height");
|
2020-11-28 15:20:24 -05:00
|
|
|
if (src_width <= 0 || src_height <= 0)
|
2010-03-07 05:07:09 -05:00
|
|
|
{
|
2020-11-28 15:20:24 -05:00
|
|
|
if (path)
|
|
|
|
DPRINTF(E_DBG, L_ART, "File '%s' has unknown artwork dimensions\n", path);
|
|
|
|
else
|
|
|
|
DPRINTF(E_LOG, L_ART, "Artwork data provided to artwork_get() has unknown dimensions\n");
|
2010-03-07 05:07:09 -05:00
|
|
|
|
2020-11-28 15:20:24 -05:00
|
|
|
ret = ART_E_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_calculate(&dst_width, &dst_height, src_width, src_height, req_params.max_w, req_params.max_h);
|
|
|
|
|
|
|
|
// Fast path. Won't work for embedded, since we need to extract the image from
|
|
|
|
// the file.
|
|
|
|
if (!is_embedded && dst_format == src_format && dst_width == src_width && dst_height == src_height)
|
|
|
|
{
|
|
|
|
if (path)
|
|
|
|
ret = artwork_read_bypath(evbuf, path);
|
2017-10-27 13:51:37 -04:00
|
|
|
else
|
2020-11-28 15:20:24 -05:00
|
|
|
ret = evbuffer_add_buffer(evbuf, in_buf);
|
|
|
|
|
|
|
|
ret = (ret < 0) ? ART_E_ERROR : src_format;
|
|
|
|
goto out;
|
2010-03-07 05:07:09 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 17:06:23 -05:00
|
|
|
if (dst_format == ART_FMT_JPEG)
|
2023-10-20 18:17:20 -04:00
|
|
|
xcode_encode = transcode_encode_setup(XCODE_JPEG, NULL, xcode_decode, dst_width, dst_height);
|
2020-11-05 17:06:23 -05:00
|
|
|
else if (dst_format == ART_FMT_PNG)
|
2023-10-20 18:17:20 -04:00
|
|
|
xcode_encode = transcode_encode_setup(XCODE_PNG, NULL, xcode_decode, dst_width, dst_height);
|
2020-11-05 17:06:23 -05:00
|
|
|
else if (dst_format == ART_FMT_VP8)
|
2023-10-20 18:17:20 -04:00
|
|
|
xcode_encode = transcode_encode_setup(XCODE_VP8, NULL, xcode_decode, dst_width, dst_height);
|
2017-02-28 17:06:01 -05:00
|
|
|
else
|
2023-10-20 18:17:20 -04:00
|
|
|
xcode_encode = transcode_encode_setup(XCODE_JPEG, NULL, xcode_decode, dst_width, dst_height);
|
2010-03-07 05:07:09 -05:00
|
|
|
|
2017-02-28 17:06:01 -05:00
|
|
|
if (!xcode_encode)
|
2014-01-09 15:07:27 -05:00
|
|
|
{
|
2017-10-27 13:51:37 -04:00
|
|
|
if (path)
|
|
|
|
DPRINTF(E_WARN, L_ART, "Error preparing rescaling of '%s'\n", path);
|
|
|
|
else
|
|
|
|
DPRINTF(E_WARN, L_ART, "Error preparing rescaling of artwork data\n");
|
2020-11-28 15:20:24 -05:00
|
|
|
|
|
|
|
ret = ART_E_ERROR;
|
|
|
|
goto out;
|
2014-01-09 15:07:27 -05:00
|
|
|
}
|
2010-03-07 05:07:09 -05:00
|
|
|
|
2017-02-28 17:06:01 -05:00
|
|
|
// We don't use transcode() because we just want to process one frame
|
|
|
|
ret = transcode_decode(&frame, xcode_decode);
|
|
|
|
if (ret < 0)
|
2020-11-28 15:20:24 -05:00
|
|
|
{
|
|
|
|
ret = ART_E_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2017-02-28 17:06:01 -05:00
|
|
|
|
|
|
|
ret = transcode_encode(evbuf, xcode_encode, frame, 1);
|
2014-01-09 15:07:27 -05:00
|
|
|
if (ret < 0)
|
2013-12-25 17:06:41 -05:00
|
|
|
{
|
2017-02-28 17:06:01 -05:00
|
|
|
evbuffer_drain(evbuf, evbuffer_get_length(evbuf));
|
2020-11-28 15:20:24 -05:00
|
|
|
ret = ART_E_ERROR;
|
|
|
|
goto out;
|
2013-12-25 17:06:41 -05:00
|
|
|
}
|
2014-01-09 15:07:27 -05:00
|
|
|
|
2020-11-28 15:20:24 -05:00
|
|
|
ret = dst_format;
|
2017-02-28 17:06:01 -05:00
|
|
|
|
2020-11-28 15:20:24 -05:00
|
|
|
out:
|
2017-02-28 17:06:01 -05:00
|
|
|
transcode_encode_cleanup(&xcode_encode);
|
|
|
|
transcode_decode_cleanup(&xcode_decode);
|
2020-02-11 08:17:28 -05:00
|
|
|
|
2020-11-28 15:20:24 -05:00
|
|
|
if (xcode_buf)
|
|
|
|
evbuffer_free(xcode_buf);
|
2020-02-11 08:17:28 -05:00
|
|
|
|
2020-11-28 15:20:24 -05:00
|
|
|
return ret;
|
2020-02-11 08:17:28 -05:00
|
|
|
}
|
|
|
|
|
2020-02-25 00:07:52 -05:00
|
|
|
/*
|
|
|
|
* Checks if an image file with one of the configured artwork_basenames exists in
|
|
|
|
* the given directory "dir". Returns 0 if an image exists, -1 if no image was
|
|
|
|
* found or an error occurred.
|
2015-04-09 16:22:42 -04:00
|
|
|
*
|
2020-02-25 00:07:52 -05:00
|
|
|
* If an image exists, "out_path" will contain the absolute path to this image.
|
|
|
|
*
|
|
|
|
* @param out_path If return value is 0, contains the absolute path to the image
|
|
|
|
* @param len If return value is 0, contains the length of the absolute path
|
|
|
|
* @param dir The directory to search
|
|
|
|
* @return 0 if image exists, -1 otherwise
|
2015-01-04 09:12:46 -05:00
|
|
|
*/
|
2010-03-07 05:07:09 -05:00
|
|
|
static int
|
2020-02-25 00:07:52 -05:00
|
|
|
dir_image_find(char *out_path, size_t len, const char *dir)
|
2010-03-07 05:07:09 -05:00
|
|
|
{
|
2015-01-04 09:12:46 -05:00
|
|
|
char path[PATH_MAX];
|
2010-10-24 12:43:51 -04:00
|
|
|
int i;
|
|
|
|
int j;
|
2020-02-25 00:07:52 -05:00
|
|
|
int path_len;
|
2010-03-07 05:07:09 -05:00
|
|
|
int ret;
|
2013-07-13 06:31:01 -04:00
|
|
|
cfg_t *lib;
|
|
|
|
int nbasenames;
|
2014-11-10 16:53:08 -05:00
|
|
|
int nextensions;
|
2010-03-07 05:07:09 -05:00
|
|
|
|
2015-01-04 09:12:46 -05:00
|
|
|
ret = snprintf(path, sizeof(path), "%s", dir);
|
|
|
|
if ((ret < 0) || (ret >= sizeof(path)))
|
2010-03-07 05:07:09 -05:00
|
|
|
{
|
2015-01-04 09:12:46 -05:00
|
|
|
DPRINTF(E_LOG, L_ART, "Artwork path exceeds PATH_MAX (%s)\n", dir);
|
2020-02-25 00:07:52 -05:00
|
|
|
return -1;
|
2010-03-07 05:07:09 -05:00
|
|
|
}
|
|
|
|
|
2020-02-25 00:07:52 -05:00
|
|
|
path_len = strlen(path);
|
2010-03-07 05:07:09 -05:00
|
|
|
|
2013-07-13 06:31:01 -04:00
|
|
|
lib = cfg_getsec(cfg, "library");
|
|
|
|
nbasenames = cfg_size(lib, "artwork_basenames");
|
|
|
|
|
|
|
|
if (nbasenames == 0)
|
2020-02-25 00:07:52 -05:00
|
|
|
return -1;
|
2013-07-13 06:31:01 -04:00
|
|
|
|
2020-02-25 00:07:52 -05:00
|
|
|
nextensions = ARRAY_SIZE(cover_extension);
|
2014-11-10 16:53:08 -05:00
|
|
|
|
2013-07-13 06:31:01 -04:00
|
|
|
for (i = 0; i < nbasenames; i++)
|
2010-03-07 05:07:09 -05:00
|
|
|
{
|
2014-11-10 16:53:08 -05:00
|
|
|
for (j = 0; j < nextensions; j++)
|
2010-10-24 12:43:51 -04:00
|
|
|
{
|
2020-02-25 00:07:52 -05:00
|
|
|
ret = snprintf(path + path_len, sizeof(path) - path_len, "/%s.%s", cfg_getnstr(lib, "artwork_basenames", i), cover_extension[j]);
|
|
|
|
if ((ret < 0) || (ret >= sizeof(path) - path_len))
|
2010-10-24 12:43:51 -04:00
|
|
|
{
|
2015-01-04 09:12:46 -05:00
|
|
|
DPRINTF(E_LOG, L_ART, "Artwork path will exceed PATH_MAX (%s/%s)\n", dir, cfg_getnstr(lib, "artwork_basenames", i));
|
2010-10-24 12:43:51 -04:00
|
|
|
continue;
|
|
|
|
}
|
2010-03-07 05:07:09 -05:00
|
|
|
|
2015-01-04 09:12:46 -05:00
|
|
|
DPRINTF(E_SPAM, L_ART, "Trying directory artwork file %s\n", path);
|
2010-03-07 05:07:09 -05:00
|
|
|
|
2015-01-04 09:12:46 -05:00
|
|
|
ret = access(path, F_OK);
|
2020-02-25 00:07:52 -05:00
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
snprintf(out_path, len, "%s", path);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-10-24 12:43:51 -04:00
|
|
|
}
|
2020-02-25 00:07:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checks if an image file exists in the given directory "dir" with the basename
|
|
|
|
* equal to the directory name. Returns 0 if an image exists, -1 if no image was
|
|
|
|
* found or an error occurred.
|
|
|
|
*
|
|
|
|
* If an image exists, "out_path" will contain the absolute path to this image.
|
|
|
|
*
|
|
|
|
* @param out_path If return value is 0, contains the absolute path to the image
|
|
|
|
* @param len If return value is 0, contains the length of the absolute path
|
|
|
|
* @param dir The directory to search
|
|
|
|
* @return 0 if image exists, -1 otherwise
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
parent_dir_image_find(char *out_path, size_t len, const char *dir)
|
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
char parentdir[PATH_MAX];
|
|
|
|
char *ptr;
|
|
|
|
int i;
|
|
|
|
int nextensions;
|
|
|
|
int path_len;
|
|
|
|
int ret;
|
2010-03-07 05:07:09 -05:00
|
|
|
|
2020-02-25 00:07:52 -05:00
|
|
|
ret = snprintf(path, sizeof(path), "%s", dir);
|
|
|
|
if ((ret < 0) || (ret >= sizeof(path)))
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_ART, "Artwork path exceeds PATH_MAX (%s)\n", dir);
|
|
|
|
return -1;
|
2010-10-24 12:43:51 -04:00
|
|
|
}
|
|
|
|
|
2020-02-25 00:07:52 -05:00
|
|
|
ptr = strrchr(path, '/');
|
|
|
|
if ((!ptr) || (strlen(ptr) <= 1))
|
2013-07-14 14:54:23 -04:00
|
|
|
{
|
2020-02-25 00:07:52 -05:00
|
|
|
DPRINTF(E_LOG, L_ART, "Could not find parent dir name (%s)\n", path);
|
|
|
|
return -1;
|
|
|
|
}
|
2022-01-19 18:11:25 -05:00
|
|
|
|
|
|
|
ret = snprintf(parentdir, sizeof(parentdir), "%s", ptr + 1);
|
|
|
|
if ((ret < 0) || (ret >= sizeof(parentdir)))
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_ART, "Impossible error occured in parent_dir_image_find(), cause was: %s\n", ptr + 1);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-14 14:54:23 -04:00
|
|
|
|
2020-02-25 00:07:52 -05:00
|
|
|
path_len = strlen(path);
|
|
|
|
nextensions = ARRAY_SIZE(cover_extension);
|
|
|
|
|
|
|
|
for (i = 0; i < nextensions; i++)
|
|
|
|
{
|
|
|
|
ret = snprintf(path + path_len, sizeof(path) - path_len, "/%s.%s", parentdir, cover_extension[i]);
|
|
|
|
if ((ret < 0) || (ret >= sizeof(path) - path_len))
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_ART, "Artwork path will exceed PATH_MAX (%s)\n", parentdir);
|
|
|
|
continue;
|
2015-01-04 09:12:46 -05:00
|
|
|
}
|
2013-07-14 14:54:23 -04:00
|
|
|
|
2020-02-25 00:07:52 -05:00
|
|
|
DPRINTF(E_SPAM, L_ART, "Trying parent directory artwork file %s\n", path);
|
2013-07-14 14:54:23 -04:00
|
|
|
|
2020-02-25 00:07:52 -05:00
|
|
|
ret = access(path, F_OK);
|
|
|
|
if (ret == 0)
|
2013-07-14 14:54:23 -04:00
|
|
|
{
|
2020-02-25 00:07:52 -05:00
|
|
|
snprintf(out_path, len, "%s", path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2013-07-14 14:54:23 -04:00
|
|
|
|
2020-02-25 00:07:52 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2013-07-14 14:54:23 -04:00
|
|
|
|
2020-02-25 00:07:52 -05:00
|
|
|
/* Looks for an artwork file in a directory. Will rescale if needed.
|
|
|
|
*
|
|
|
|
* @out evbuf Image data
|
|
|
|
* @out out_path Path to the artwork file if found, must be a char[PATH_MAX] buffer
|
|
|
|
* @in len Max size of "out_path"
|
2020-11-05 17:06:23 -05:00
|
|
|
* @in dir Directory to search
|
|
|
|
* @in req_params Requested max size/format
|
2020-02-25 00:07:52 -05:00
|
|
|
* @return ART_FMT_* on success, ART_E_NONE on nothing found, ART_E_ERROR on error
|
|
|
|
*/
|
|
|
|
static int
|
2020-11-05 17:06:23 -05:00
|
|
|
artwork_get_bydir(struct evbuffer *evbuf, char *out_path, size_t len, char *dir, struct artwork_req_params req_params)
|
2020-02-25 00:07:52 -05:00
|
|
|
{
|
|
|
|
int ret;
|
2013-07-14 14:54:23 -04:00
|
|
|
|
2020-02-25 00:07:52 -05:00
|
|
|
ret = dir_image_find(out_path, len, dir);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2020-11-05 17:06:23 -05:00
|
|
|
return artwork_get(evbuf, out_path, NULL, false, DATA_KIND_FILE, req_params);
|
2013-07-14 14:54:23 -04:00
|
|
|
}
|
|
|
|
|
2020-02-25 00:07:52 -05:00
|
|
|
ret = parent_dir_image_find(out_path, len, dir);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2020-11-05 17:06:23 -05:00
|
|
|
return artwork_get(evbuf, out_path, NULL, false, DATA_KIND_FILE, req_params);
|
2020-02-25 00:07:52 -05:00
|
|
|
}
|
2015-01-04 09:12:46 -05:00
|
|
|
|
2020-02-25 00:07:52 -05:00
|
|
|
return ART_E_NONE;
|
2013-07-14 14:54:23 -04:00
|
|
|
}
|
|
|
|
|
2020-02-21 13:51:35 -05:00
|
|
|
/* Retrieves artwork from an URL, will rescale if needed. Checks the cache stash
|
2020-10-20 17:22:49 -04:00
|
|
|
* before making a request. Stashes result in cache, also if negative.
|
2020-02-11 08:17:28 -05:00
|
|
|
*
|
|
|
|
* @out artwork Image data
|
|
|
|
* @in url URL of the artwork
|
2020-11-05 17:06:23 -05:00
|
|
|
* @in req_params Requested max size/format
|
2020-10-20 17:22:49 -04:00
|
|
|
* @return ART_FMT_* on success, ART_E_NONE or ART_E_ERROR
|
2020-02-11 08:17:28 -05:00
|
|
|
*/
|
|
|
|
static int
|
2020-11-05 17:06:23 -05:00
|
|
|
artwork_get_byurl(struct evbuffer *artwork, const char *url, struct artwork_req_params req_params)
|
2020-02-11 08:17:28 -05:00
|
|
|
{
|
|
|
|
struct evbuffer *raw;
|
2020-02-19 17:06:16 -05:00
|
|
|
int format;
|
2020-02-11 08:17:28 -05:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
CHECK_NULL(L_ART, raw = evbuffer_new());
|
2020-10-20 17:22:49 -04:00
|
|
|
format = ART_E_ERROR;
|
2020-02-11 08:17:28 -05:00
|
|
|
|
2020-02-19 17:06:16 -05:00
|
|
|
ret = cache_artwork_read(raw, url, &format);
|
2020-10-20 17:22:49 -04:00
|
|
|
if (ret < 0)
|
2020-02-19 17:06:16 -05:00
|
|
|
{
|
2020-10-20 17:22:49 -04:00
|
|
|
format = artwork_read_byurl(raw, url);
|
|
|
|
cache_artwork_stash(raw, url, format);
|
2020-02-19 17:06:16 -05:00
|
|
|
}
|
|
|
|
|
2020-10-20 17:22:49 -04:00
|
|
|
// If we couldn't read, or we have cached a negative result from the last attempt, we stop now
|
|
|
|
if (format <= 0)
|
|
|
|
goto out;
|
2020-02-11 08:17:28 -05:00
|
|
|
|
2020-11-28 15:20:24 -05:00
|
|
|
// Takes care of resizing
|
|
|
|
ret = artwork_get(artwork, NULL, raw, false, 0, req_params);
|
2020-02-11 08:17:28 -05:00
|
|
|
if (ret < 0)
|
2020-10-20 17:22:49 -04:00
|
|
|
format = ART_E_ERROR;
|
2020-02-11 08:17:28 -05:00
|
|
|
|
2020-10-20 17:22:49 -04:00
|
|
|
out:
|
2020-02-11 08:17:28 -05:00
|
|
|
evbuffer_free(raw);
|
2020-02-19 17:06:16 -05:00
|
|
|
return format;
|
2020-02-11 08:17:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------- ONLINE SOURCE HANDLING ----------------------- */
|
|
|
|
|
2023-02-06 16:59:27 -05:00
|
|
|
#ifdef SPOTIFY
|
2023-02-05 17:27:54 -05:00
|
|
|
static int
|
|
|
|
credentials_get_spotify(char **auth_key, char **auth_secret)
|
|
|
|
{
|
|
|
|
struct spotifywebapi_status_info webapi_info;
|
|
|
|
struct spotifywebapi_access_token webapi_token;
|
|
|
|
|
|
|
|
spotifywebapi_status_info_get(&webapi_info);
|
|
|
|
if (!webapi_info.token_valid)
|
|
|
|
return -1; // Not logged in
|
|
|
|
|
|
|
|
spotifywebapi_access_token_get(&webapi_token);
|
|
|
|
if (!webapi_token.token)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*auth_key = NULL;
|
|
|
|
*auth_secret = webapi_token.token;
|
|
|
|
return 0;
|
|
|
|
}
|
2023-02-06 16:59:27 -05:00
|
|
|
#else
|
|
|
|
static int
|
|
|
|
credentials_get_spotify(char **auth_key, char **auth_secret)
|
|
|
|
{
|
|
|
|
*auth_key = NULL;
|
|
|
|
*auth_secret = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2023-02-05 17:27:54 -05:00
|
|
|
|
|
|
|
static int
|
|
|
|
credentials_get_discogs(char **auth_key, char **auth_secret)
|
|
|
|
{
|
|
|
|
*auth_key = strdup("ivzUxlkUiwpptDKpSCHF");
|
|
|
|
*auth_secret = strdup("CYLZyExtlznKCupoIIhTpHVDReLunhUo");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-11 08:17:28 -05:00
|
|
|
static enum parse_result
|
|
|
|
response_jparse_discogs(char **artwork_url, json_object *response, int max_w, int max_h)
|
|
|
|
{
|
|
|
|
json_object *image;
|
|
|
|
const char *s;
|
|
|
|
const char *key;
|
|
|
|
|
|
|
|
if ((max_w > 0 && max_w <= 150) || (max_h > 0 && max_h <= 150))
|
|
|
|
key = "thumb";
|
|
|
|
else
|
|
|
|
key = "cover_image";
|
|
|
|
|
2020-02-16 15:29:43 -05:00
|
|
|
image = JPARSE_SELECT(response, "results", key);
|
2020-02-11 08:17:28 -05:00
|
|
|
if (!image || json_object_get_type(image) != json_type_string)
|
|
|
|
return ONLINE_SOURCE_PARSE_NOT_FOUND;
|
|
|
|
|
|
|
|
s = json_object_get_string(image);
|
|
|
|
if (!s)
|
|
|
|
return ONLINE_SOURCE_PARSE_INVALID;
|
|
|
|
|
|
|
|
*artwork_url = strdup(s);
|
|
|
|
|
|
|
|
return ONLINE_SOURCE_PARSE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum parse_result
|
|
|
|
response_jparse_musicbrainz(char **artwork_url, json_object *response, int max_w, int max_h)
|
|
|
|
{
|
|
|
|
json_object *id;
|
|
|
|
const char *s;
|
|
|
|
|
2020-02-16 15:29:43 -05:00
|
|
|
id = JPARSE_SELECT(response, "release-groups", "id");
|
2020-02-11 08:17:28 -05:00
|
|
|
if (!id || json_object_get_type(id) != json_type_string)
|
|
|
|
return ONLINE_SOURCE_PARSE_NOT_FOUND;
|
|
|
|
|
|
|
|
s = json_object_get_string(id);
|
|
|
|
if (!s)
|
|
|
|
return ONLINE_SOURCE_PARSE_INVALID;
|
|
|
|
|
|
|
|
// We will request 500 as a default. The use of https is not just for privacy
|
|
|
|
// it is also because the http client only supports redirects for https.
|
|
|
|
if ((max_w > 0 && max_w <= 250) || (max_h > 0 && max_h <= 250))
|
|
|
|
*artwork_url = safe_asprintf("https://coverartarchive.org/release-group/%s/front-250", s);
|
|
|
|
else if ((max_w == 0 && max_h == 0) || (max_w <= 500 && max_h <= 500))
|
|
|
|
*artwork_url = safe_asprintf("https://coverartarchive.org/release-group/%s/front-500", s);
|
|
|
|
else
|
|
|
|
*artwork_url = safe_asprintf("https://coverartarchive.org/release-group/%s/front-1200", s);
|
|
|
|
|
|
|
|
return ONLINE_SOURCE_PARSE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum parse_result
|
|
|
|
response_jparse_spotify(char **artwork_url, json_object *response, int max_w, int max_h)
|
|
|
|
{
|
|
|
|
json_object *images;
|
|
|
|
json_object *image;
|
|
|
|
const char *s;
|
|
|
|
int image_count;
|
|
|
|
int i;
|
|
|
|
|
2020-02-19 17:06:16 -05:00
|
|
|
images = JPARSE_SELECT(response, "tracks", "items", "album", "images");
|
2020-02-11 08:17:28 -05:00
|
|
|
if (!images || json_object_get_type(images) != json_type_array)
|
2020-02-19 17:06:16 -05:00
|
|
|
return ONLINE_SOURCE_PARSE_NOT_FOUND;
|
2020-02-11 08:17:28 -05:00
|
|
|
|
|
|
|
// Find first image that has a smaller width than the given max_w (this should
|
|
|
|
// avoid the need for resizing and improve performance at the cost of some
|
|
|
|
// quality loss). Note that Spotify returns the images ordered descending by
|
|
|
|
// width (widest image first). Special case is if no max width (max_w = 0) is
|
|
|
|
// given, then the widest images will be used.
|
|
|
|
s = NULL;
|
|
|
|
image_count = json_object_array_length(images);
|
|
|
|
for (i = 0; i < image_count; i++)
|
|
|
|
{
|
|
|
|
image = json_object_array_get_idx(images, i);
|
|
|
|
if (image)
|
|
|
|
{
|
|
|
|
s = jparse_str_from_obj(image, "url");
|
|
|
|
|
|
|
|
if (max_w <= 0 || jparse_int_from_obj(image, "width") <= max_w)
|
|
|
|
{
|
|
|
|
// We have the first image that has a smaller width than the given max_w
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return ONLINE_SOURCE_PARSE_NOT_FOUND;
|
|
|
|
|
|
|
|
*artwork_url = strdup(s);
|
|
|
|
return ONLINE_SOURCE_PARSE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum parse_result
|
2023-02-05 17:27:54 -05:00
|
|
|
online_source_response_parse(char **artwork_url, const struct online_source *src, struct evbuffer *response, int max_w, int max_h)
|
2020-02-11 08:17:28 -05:00
|
|
|
{
|
|
|
|
json_object *jresponse;
|
|
|
|
char *body;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
// 0-terminate for safety
|
|
|
|
evbuffer_add(response, "", 1);
|
|
|
|
body = (char *)evbuffer_pullup(response, -1);
|
|
|
|
|
2020-02-22 10:49:00 -05:00
|
|
|
DPRINTF(E_SPAM, L_ART, "Response from '%s': %s\n", src->name, body);
|
2020-02-11 08:17:28 -05:00
|
|
|
|
|
|
|
if (src->response_jparse)
|
|
|
|
{
|
|
|
|
jresponse = json_tokener_parse(body);
|
|
|
|
if (!jresponse)
|
|
|
|
return ONLINE_SOURCE_PARSE_INVALID;
|
|
|
|
|
|
|
|
ret = src->response_jparse(artwork_url, jresponse, max_w, max_h);
|
|
|
|
jparse_free(jresponse);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = ONLINE_SOURCE_PARSE_NO_PARSER;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2023-02-05 17:27:54 -05:00
|
|
|
online_source_request_url_make(char *url, size_t url_size, const struct online_source *src, struct artwork_ctx *ctx)
|
2020-02-11 08:17:28 -05:00
|
|
|
{
|
2020-02-18 16:12:54 -05:00
|
|
|
struct db_queue_item *queue_item;
|
2020-02-11 08:17:28 -05:00
|
|
|
struct keyval query = { 0 };
|
2020-02-18 16:12:54 -05:00
|
|
|
const char *artist = NULL;
|
|
|
|
const char *album = NULL;
|
|
|
|
const char *title = NULL;
|
2020-02-11 08:17:28 -05:00
|
|
|
char param[512];
|
2020-02-18 16:12:54 -05:00
|
|
|
char *encoded_query = NULL;
|
2020-02-11 08:17:28 -05:00
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
2020-02-18 16:12:54 -05:00
|
|
|
// First check if the item is in the queue. When searching for artwork, it is
|
|
|
|
// better to use queue_item metadata. For stream items the queue metadata will
|
|
|
|
// for instance be updated with icy metadata. It is also possible we are asked
|
|
|
|
// for artwork for a non-library item.
|
|
|
|
queue_item = db_queue_fetch_byfileid(ctx->id);
|
|
|
|
if (queue_item && ctx->data_kind == DATA_KIND_HTTP)
|
|
|
|
{
|
|
|
|
// Normally we prefer searching by artist and album, but for streams we
|
|
|
|
// take the below approach, since they have no album information, and the
|
|
|
|
// title is in the album field
|
|
|
|
artist = queue_item->artist;
|
|
|
|
title = queue_item->album;
|
|
|
|
}
|
|
|
|
else if (queue_item)
|
|
|
|
{
|
|
|
|
artist = queue_item->artist;
|
|
|
|
album = queue_item->album;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We will just search for artist and album
|
|
|
|
artist = ctx->dbmfi->artist;
|
|
|
|
album = ctx->dbmfi->album;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!artist || (!album && !title))
|
|
|
|
{
|
|
|
|
DPRINTF(E_DBG, L_ART, "Cannot construct query to %s, missing input data (artist=%s, album=%s, title=%s)\n", src->name, artist, album, title);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2020-02-11 08:17:28 -05:00
|
|
|
for (i = 0; src->query_parts[i].key; i++)
|
|
|
|
{
|
2020-02-18 16:12:54 -05:00
|
|
|
if (!album && strstr(src->query_parts[i].template, "$ALBUM$"))
|
|
|
|
continue;
|
|
|
|
if (!title && strstr(src->query_parts[i].template, "$TITLE$"))
|
|
|
|
continue;
|
|
|
|
|
2020-02-11 08:17:28 -05:00
|
|
|
snprintf(param, sizeof(param), "%s", src->query_parts[i].template);
|
|
|
|
if ((safe_snreplace(param, sizeof(param), "$ARTIST$", artist) < 0) ||
|
|
|
|
(safe_snreplace(param, sizeof(param), "$ALBUM$", album) < 0) ||
|
|
|
|
(safe_snreplace(param, sizeof(param), "$TITLE$", title) < 0))
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_ART, "Cannot make request for online artwork, query string is too long\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = keyval_add(&query, src->query_parts[i].key, param);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_ART, "keyval_add() failed in request_url_make()\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
encoded_query = http_form_urlencode(&query);
|
|
|
|
if (!encoded_query)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
snprintf(url, url_size, "%s?%s", src->search_endpoint, src->search_param);
|
|
|
|
if (safe_snreplace(url, url_size, "$QUERY$", encoded_query) < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_ART, "Cannot make request for online artwork, url is too long (%zu)\n", strlen(encoded_query));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(encoded_query);
|
|
|
|
keyval_clear(&query);
|
2020-02-18 16:12:54 -05:00
|
|
|
free_queue_item(queue_item, 0);
|
2020-02-11 08:17:28 -05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
2020-02-18 16:12:54 -05:00
|
|
|
free(encoded_query);
|
2020-02-11 08:17:28 -05:00
|
|
|
keyval_clear(&query);
|
2020-02-18 16:12:54 -05:00
|
|
|
free_queue_item(queue_item, 0);
|
2020-02-11 08:17:28 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-10-19 16:34:26 -04:00
|
|
|
static int
|
2023-02-05 17:27:54 -05:00
|
|
|
online_source_search_check_last(char **last_artwork_url, const struct online_source *src, uint32_t hash, int max_w, int max_h)
|
2020-02-21 13:51:35 -05:00
|
|
|
{
|
2023-02-05 17:27:54 -05:00
|
|
|
struct online_search_history *history = src->search_history;
|
2020-02-21 13:51:35 -05:00
|
|
|
bool is_same;
|
|
|
|
|
2023-02-05 17:27:54 -05:00
|
|
|
pthread_mutex_lock(&history->mutex);
|
2020-02-21 13:51:35 -05:00
|
|
|
|
2023-02-05 17:27:54 -05:00
|
|
|
is_same = (hash == history->last_hash) &&
|
|
|
|
(max_w == history->last_max_w) &&
|
|
|
|
(max_h == history->last_max_h);
|
2020-02-21 13:51:35 -05:00
|
|
|
|
2020-10-19 16:34:26 -04:00
|
|
|
// Copy this to the caller while we have the lock anyway
|
2020-02-21 13:51:35 -05:00
|
|
|
if (is_same)
|
2023-02-05 17:27:54 -05:00
|
|
|
*last_artwork_url = safe_strdup(history->last_artwork_url);
|
2020-02-21 13:51:35 -05:00
|
|
|
|
2023-02-05 17:27:54 -05:00
|
|
|
pthread_mutex_unlock(&history->mutex);
|
2020-02-21 13:51:35 -05:00
|
|
|
|
2020-10-19 16:34:26 -04:00
|
|
|
return is_same ? 0 : -1;
|
2020-02-21 13:51:35 -05:00
|
|
|
}
|
|
|
|
|
2020-02-19 17:06:16 -05:00
|
|
|
static bool
|
2023-02-05 17:27:54 -05:00
|
|
|
online_source_is_failing(const struct online_source *src, int id)
|
2020-02-19 17:06:16 -05:00
|
|
|
{
|
2023-02-05 17:27:54 -05:00
|
|
|
struct online_search_history *history = src->search_history;
|
2020-02-21 13:51:35 -05:00
|
|
|
bool is_failing;
|
|
|
|
|
2023-02-05 17:27:54 -05:00
|
|
|
pthread_mutex_lock(&history->mutex);
|
2020-02-21 13:51:35 -05:00
|
|
|
|
2020-02-19 17:06:16 -05:00
|
|
|
// If the last request was more than ONLINE_SEARCH_COOLDOWN_TIME ago we will always try again
|
2023-02-05 17:27:54 -05:00
|
|
|
if (time(NULL) > history->last_timestamp + ONLINE_SEARCH_COOLDOWN_TIME)
|
2020-02-21 13:51:35 -05:00
|
|
|
is_failing = false;
|
2020-02-19 17:06:16 -05:00
|
|
|
// We won't try again if the source was not replying as expected
|
2023-02-05 17:27:54 -05:00
|
|
|
else if (history->last_response_code != HTTP_OK)
|
2020-02-21 13:51:35 -05:00
|
|
|
is_failing = true;
|
2020-02-19 17:06:16 -05:00
|
|
|
// The playback source has changed since the last search, let's give it a chance
|
|
|
|
// (internet streams can feed us with garbage search metadata, but will not change id)
|
2023-02-05 17:27:54 -05:00
|
|
|
else if (id != history->last_id)
|
2020-02-21 13:51:35 -05:00
|
|
|
is_failing = false;
|
2020-02-19 17:06:16 -05:00
|
|
|
// We allow up to ONLINE_SEARCH_FAILURES_MAX for the same track id before declaring failure
|
2023-02-05 17:27:54 -05:00
|
|
|
else if (history->count_failures < ONLINE_SEARCH_FAILURES_MAX)
|
2020-02-21 13:51:35 -05:00
|
|
|
is_failing = false;
|
|
|
|
else
|
|
|
|
is_failing = true;
|
2020-02-19 17:06:16 -05:00
|
|
|
|
2023-02-05 17:27:54 -05:00
|
|
|
pthread_mutex_unlock(&history->mutex);
|
2020-02-21 13:51:35 -05:00
|
|
|
|
|
|
|
return is_failing;
|
2020-02-19 17:06:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-02-05 17:27:54 -05:00
|
|
|
online_source_history_update(const struct online_source *src, int id, uint32_t request_hash, int response_code, const char *artwork_url)
|
2020-02-19 17:06:16 -05:00
|
|
|
{
|
2023-02-05 17:27:54 -05:00
|
|
|
struct online_search_history *history = src->search_history;
|
2020-02-21 13:51:35 -05:00
|
|
|
|
2023-02-05 17:27:54 -05:00
|
|
|
pthread_mutex_lock(&history->mutex);
|
2020-02-19 17:06:16 -05:00
|
|
|
|
2023-02-05 17:27:54 -05:00
|
|
|
history->last_id = id;
|
|
|
|
history->last_hash = request_hash;
|
|
|
|
history->last_response_code = response_code;
|
|
|
|
history->last_timestamp = time(NULL);
|
|
|
|
|
|
|
|
free(history->last_artwork_url);
|
|
|
|
history->last_artwork_url = safe_strdup(artwork_url); // FIXME should free this on exit
|
2020-02-19 17:06:16 -05:00
|
|
|
|
|
|
|
if (artwork_url)
|
2023-02-05 17:27:54 -05:00
|
|
|
history->count_failures = 0;
|
2020-02-19 17:06:16 -05:00
|
|
|
else
|
2023-02-05 17:27:54 -05:00
|
|
|
history->count_failures++;
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&history->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
auth_header_add(struct keyval *headers, const struct online_source *src)
|
|
|
|
{
|
|
|
|
char auth_header[256];
|
|
|
|
char *auth_key;
|
|
|
|
char *auth_secret;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!src->auth_header)
|
|
|
|
return 0; // Nothing to do
|
|
|
|
|
|
|
|
ret = src->credentials_get(&auth_key, &auth_secret);
|
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
snprintf(auth_header, sizeof(auth_header), "%s", src->auth_header);
|
|
|
|
ret = ((safe_snreplace(auth_header, sizeof(auth_header), "$KEY$", auth_key) < 0) ||
|
|
|
|
(safe_snreplace(auth_header, sizeof(auth_header), "$SECRET$", auth_secret) < 0));
|
2020-02-21 13:51:35 -05:00
|
|
|
|
2023-02-05 17:27:54 -05:00
|
|
|
free(auth_key);
|
|
|
|
free(auth_secret);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_ART, "Cannot make request for online artwork, auth header is too long\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
keyval_add(headers, "Authorization", auth_header);
|
|
|
|
return 0;
|
2020-02-19 17:06:16 -05:00
|
|
|
}
|
|
|
|
|
2020-02-11 08:17:28 -05:00
|
|
|
static char *
|
2023-02-05 17:27:54 -05:00
|
|
|
online_source_search(const struct online_source *src, struct artwork_ctx *ctx)
|
2020-02-11 08:17:28 -05:00
|
|
|
{
|
|
|
|
char *artwork_url;
|
|
|
|
struct http_client_ctx client = { 0 };
|
|
|
|
struct keyval output_headers = { 0 };
|
2020-02-19 17:06:16 -05:00
|
|
|
uint32_t hash;
|
2020-02-11 08:17:28 -05:00
|
|
|
char url[2048];
|
|
|
|
int ret;
|
|
|
|
|
2020-02-21 13:51:35 -05:00
|
|
|
DPRINTF(E_SPAM, L_ART, "Trying %s for %s\n", src->name, ctx->dbmfi->path);
|
|
|
|
|
2020-02-18 16:12:54 -05:00
|
|
|
ret = online_source_request_url_make(url, sizeof(url), src, ctx);
|
2020-02-11 08:17:28 -05:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_ART, "Skipping artwork source %s, could not construct a request URL\n", src->name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-02-19 17:06:16 -05:00
|
|
|
// Be nice to our peer + improve response times by not repeating search requests
|
|
|
|
hash = djb_hash(url, strlen(url));
|
2020-11-05 17:06:23 -05:00
|
|
|
ret = online_source_search_check_last(&artwork_url, src, hash, ctx->req_params.max_w, ctx->req_params.max_h);
|
2020-10-19 16:34:26 -04:00
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
return artwork_url; // Will be NULL if we are repeating a search that failed
|
|
|
|
}
|
2020-02-19 17:06:16 -05:00
|
|
|
|
|
|
|
// If our recent searches have been futile we may give the source a break
|
|
|
|
if (online_source_is_failing(src, ctx->id))
|
|
|
|
{
|
|
|
|
DPRINTF(E_DBG, L_ART, "Skipping artwork source %s, too many failed requests\n", src->name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-02-05 17:27:54 -05:00
|
|
|
ret = auth_header_add(&output_headers, src);
|
|
|
|
if (ret < 0)
|
2020-02-11 08:17:28 -05:00
|
|
|
{
|
2023-02-05 17:27:54 -05:00
|
|
|
return NULL;
|
2020-02-11 08:17:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_NULL(L_ART, client.input_body = evbuffer_new());
|
|
|
|
client.url = url;
|
|
|
|
client.output_headers = &output_headers;
|
|
|
|
|
2019-02-09 11:04:25 -05:00
|
|
|
ret = http_client_request(&client, NULL);
|
2020-02-11 08:17:28 -05:00
|
|
|
keyval_clear(&output_headers);
|
|
|
|
if (ret < 0 || client.response_code != HTTP_OK)
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_ART, "Artwork request to '%s' failed, response code %d\n", url, client.response_code);
|
2020-02-19 17:06:16 -05:00
|
|
|
goto error;
|
2020-02-11 08:17:28 -05:00
|
|
|
}
|
|
|
|
|
2020-11-05 17:06:23 -05:00
|
|
|
ret = online_source_response_parse(&artwork_url, src, client.input_body, ctx->req_params.max_w, ctx->req_params.max_h);
|
2020-02-11 08:17:28 -05:00
|
|
|
if (ret == ONLINE_SOURCE_PARSE_NOT_FOUND)
|
|
|
|
DPRINTF(E_DBG, L_ART, "No image tag found in response from source '%s'\n", src->name);
|
|
|
|
else if (ret == ONLINE_SOURCE_PARSE_INVALID)
|
|
|
|
DPRINTF(E_WARN, L_ART, "Response from source '%s' was in an unexpected format\n", src->name);
|
|
|
|
else if (ret == ONLINE_SOURCE_PARSE_NO_PARSER)
|
|
|
|
DPRINTF(E_LOG, L_ART, "Bug! Cannot parse response from source '%s', parser missing\n", src->name);
|
|
|
|
else if (ret != ONLINE_SOURCE_PARSE_OK)
|
|
|
|
DPRINTF(E_LOG, L_ART, "Bug! Cannot parse response from source '%s', unknown error\n", src->name);
|
|
|
|
|
|
|
|
if (ret != ONLINE_SOURCE_PARSE_OK)
|
2020-02-19 17:06:16 -05:00
|
|
|
goto error;
|
2020-02-11 08:17:28 -05:00
|
|
|
|
2020-02-19 17:06:16 -05:00
|
|
|
online_source_history_update(src, ctx->id, hash, client.response_code, artwork_url);
|
|
|
|
evbuffer_free(client.input_body);
|
2020-02-11 08:17:28 -05:00
|
|
|
return artwork_url;
|
2020-02-19 17:06:16 -05:00
|
|
|
|
|
|
|
error:
|
|
|
|
online_source_history_update(src, ctx->id, hash, client.response_code, NULL);
|
|
|
|
evbuffer_free(client.input_body);
|
|
|
|
return NULL;
|
2020-02-11 08:17:28 -05:00
|
|
|
}
|
|
|
|
|
2020-02-17 16:08:31 -05:00
|
|
|
static bool
|
2023-02-05 17:27:54 -05:00
|
|
|
online_source_is_enabled(const struct online_source *src)
|
2020-02-17 16:08:31 -05:00
|
|
|
{
|
|
|
|
struct settings_category *category;
|
2020-02-22 10:49:00 -05:00
|
|
|
bool enabled;
|
2020-02-21 13:51:35 -05:00
|
|
|
|
2020-02-22 10:49:00 -05:00
|
|
|
CHECK_NULL(L_ART, category = settings_category_get("artwork"));
|
|
|
|
enabled = settings_option_getbool(settings_option_get(category, src->setting_name));
|
2020-02-17 16:08:31 -05:00
|
|
|
|
2020-02-22 10:49:00 -05:00
|
|
|
if (!enabled)
|
2020-10-20 17:22:49 -04:00
|
|
|
DPRINTF(E_SPAM, L_ART, "Source %s is disabled\n", src->name);
|
2020-02-21 13:51:35 -05:00
|
|
|
|
2020-02-22 10:49:00 -05:00
|
|
|
return enabled;
|
2020-02-17 16:08:31 -05:00
|
|
|
}
|
|
|
|
|
2011-03-08 13:18:43 -05:00
|
|
|
|
2016-01-07 16:48:04 -05:00
|
|
|
/* ---------------------- SOURCE HANDLER IMPLEMENTATION -------------------- */
|
2014-12-27 09:44:16 -05:00
|
|
|
|
2015-12-29 17:39:52 -05:00
|
|
|
/* Looks in the cache for group artwork
|
|
|
|
*/
|
2014-12-27 09:44:16 -05:00
|
|
|
static int
|
2015-12-28 18:26:52 -05:00
|
|
|
source_group_cache_get(struct artwork_ctx *ctx)
|
2014-12-27 09:44:16 -05:00
|
|
|
{
|
2015-01-02 18:31:48 -05:00
|
|
|
int format;
|
2015-12-28 18:26:52 -05:00
|
|
|
int cached;
|
2015-01-04 09:12:46 -05:00
|
|
|
int ret;
|
2015-01-02 18:31:48 -05:00
|
|
|
|
2020-11-05 17:06:23 -05:00
|
|
|
ret = cache_artwork_get(CACHE_ARTWORK_GROUP, ctx->persistentid, ctx->req_params.max_w, ctx->req_params.max_h, &cached, &format, ctx->evbuf);
|
2015-12-28 18:26:52 -05:00
|
|
|
if (ret < 0)
|
|
|
|
return ART_E_ERROR;
|
2015-01-02 18:31:48 -05:00
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
if (!cached)
|
|
|
|
return ART_E_NONE;
|
2015-01-02 18:31:48 -05:00
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
if (!format)
|
|
|
|
return ART_E_ABORT;
|
2015-01-02 18:31:48 -05:00
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
return format;
|
2014-12-27 09:44:16 -05:00
|
|
|
}
|
|
|
|
|
2015-12-29 17:39:52 -05:00
|
|
|
/* Looks for cover files in a directory, so if dir is /foo/bar and the user has
|
|
|
|
* configured the cover file names "cover" and "artwork" it will look for
|
|
|
|
* /foo/bar/cover.{png,jpg}, /foo/bar/artwork.{png,jpg} and also
|
|
|
|
* /foo/bar/bar.{png,jpg} (so-called parentdir artwork)
|
|
|
|
*/
|
2014-11-10 16:53:08 -05:00
|
|
|
static int
|
2015-12-28 18:26:52 -05:00
|
|
|
source_group_dir_get(struct artwork_ctx *ctx)
|
2010-03-07 05:07:09 -05:00
|
|
|
{
|
|
|
|
struct query_params qp;
|
|
|
|
char *dir;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
memset(&qp, 0, sizeof(struct query_params));
|
|
|
|
|
|
|
|
qp.type = Q_GROUP_DIRS;
|
2015-12-28 18:26:52 -05:00
|
|
|
qp.persistentid = ctx->persistentid;
|
2010-03-07 05:07:09 -05:00
|
|
|
|
|
|
|
ret = db_query_start(&qp);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_ART, "Could not start Q_GROUP_DIRS query\n");
|
2015-12-28 18:26:52 -05:00
|
|
|
return ART_E_ERROR;
|
2010-03-07 05:07:09 -05:00
|
|
|
}
|
|
|
|
|
2021-12-28 01:23:56 -05:00
|
|
|
while (((ret = db_query_fetch_string(&dir, &qp)) == 0) && (dir))
|
2010-03-07 05:07:09 -05:00
|
|
|
{
|
2015-01-04 13:38:13 -05:00
|
|
|
/* The db query may return non-directories (eg if item is an internet stream or Spotify) */
|
|
|
|
if (access(dir, F_OK) < 0)
|
2014-09-29 17:40:38 -04:00
|
|
|
continue;
|
|
|
|
|
2020-11-05 17:06:23 -05:00
|
|
|
ret = artwork_get_bydir(ctx->evbuf, ctx->path, sizeof(ctx->path), dir, ctx->req_params);
|
2015-12-28 18:26:52 -05:00
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
db_query_end(&qp);
|
|
|
|
return ret;
|
|
|
|
}
|
2010-03-07 05:07:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
db_query_end(&qp);
|
|
|
|
|
|
|
|
if (ret < 0)
|
2015-12-28 18:26:52 -05:00
|
|
|
{
|
2015-01-04 09:12:46 -05:00
|
|
|
DPRINTF(E_LOG, L_ART, "Error fetching Q_GROUP_DIRS results\n");
|
2015-12-28 18:26:52 -05:00
|
|
|
return ART_E_ERROR;
|
2015-01-04 09:12:46 -05:00
|
|
|
}
|
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
return ART_E_NONE;
|
|
|
|
}
|
|
|
|
|
2015-12-29 17:39:52 -05:00
|
|
|
/* Looks in the cache for item artwork. Only relevant if configured to look for
|
|
|
|
* individual artwork.
|
|
|
|
*/
|
2015-12-28 18:26:52 -05:00
|
|
|
static int
|
|
|
|
source_item_cache_get(struct artwork_ctx *ctx)
|
|
|
|
{
|
|
|
|
int format;
|
|
|
|
int cached;
|
|
|
|
int ret;
|
|
|
|
|
2015-12-29 17:39:52 -05:00
|
|
|
if (!ctx->individual)
|
|
|
|
return ART_E_NONE;
|
|
|
|
|
2020-11-05 17:06:23 -05:00
|
|
|
ret = cache_artwork_get(CACHE_ARTWORK_INDIVIDUAL, ctx->id, ctx->req_params.max_w, ctx->req_params.max_h, &cached, &format, ctx->evbuf);
|
2015-12-28 18:26:52 -05:00
|
|
|
if (ret < 0)
|
|
|
|
return ART_E_ERROR;
|
|
|
|
|
|
|
|
if (!cached)
|
|
|
|
return ART_E_NONE;
|
|
|
|
|
|
|
|
if (!format)
|
|
|
|
return ART_E_ABORT;
|
|
|
|
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
2015-12-29 17:39:52 -05:00
|
|
|
/* Get an embedded artwork file from a media file. Will rescale if needed.
|
|
|
|
*/
|
2015-12-28 18:26:52 -05:00
|
|
|
static int
|
|
|
|
source_item_embedded_get(struct artwork_ctx *ctx)
|
|
|
|
{
|
2020-04-18 05:20:02 -04:00
|
|
|
int artwork;
|
|
|
|
|
2015-12-29 17:39:52 -05:00
|
|
|
DPRINTF(E_SPAM, L_ART, "Trying embedded artwork in %s\n", ctx->dbmfi->path);
|
|
|
|
|
2020-04-18 05:20:02 -04:00
|
|
|
if (safe_atoi32(ctx->dbmfi->artwork, &artwork) < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_ART, "Error converting dbmfi artwork to number for '%s'\n", ctx->dbmfi->path);
|
|
|
|
return ART_E_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (artwork != ARTWORK_EMBEDDED)
|
|
|
|
return ART_E_NONE;
|
|
|
|
|
2017-02-28 17:06:01 -05:00
|
|
|
snprintf(ctx->path, sizeof(ctx->path), "%s", ctx->dbmfi->path);
|
2015-12-28 18:26:52 -05:00
|
|
|
|
2020-11-05 17:06:23 -05:00
|
|
|
return artwork_get(ctx->evbuf, ctx->path, NULL, true, ctx->data_kind, ctx->req_params);
|
2015-12-28 18:26:52 -05:00
|
|
|
}
|
|
|
|
|
2015-12-29 17:39:52 -05:00
|
|
|
/* Looks for basename(in_path).{png,jpg}, so if in_path is /foo/bar.mp3 it
|
|
|
|
* will look for /foo/bar.png and /foo/bar.jpg
|
|
|
|
*/
|
2015-12-28 18:26:52 -05:00
|
|
|
static int
|
|
|
|
source_item_own_get(struct artwork_ctx *ctx)
|
|
|
|
{
|
2015-12-29 17:39:52 -05:00
|
|
|
char path[PATH_MAX];
|
|
|
|
char *ptr;
|
|
|
|
int len;
|
|
|
|
int nextensions;
|
|
|
|
int i;
|
2015-12-28 18:26:52 -05:00
|
|
|
int ret;
|
|
|
|
|
2015-12-29 17:39:52 -05:00
|
|
|
ret = snprintf(path, sizeof(path), "%s", ctx->dbmfi->path);
|
|
|
|
if ((ret < 0) || (ret >= sizeof(path)))
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_ART, "Artwork path exceeds PATH_MAX (%s)\n", ctx->dbmfi->path);
|
|
|
|
return ART_E_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = strrchr(path, '.');
|
|
|
|
if (ptr)
|
|
|
|
*ptr = '\0';
|
|
|
|
|
|
|
|
len = strlen(path);
|
|
|
|
|
|
|
|
nextensions = sizeof(cover_extension) / sizeof(cover_extension[0]);
|
2015-12-28 18:26:52 -05:00
|
|
|
|
2015-12-29 17:39:52 -05:00
|
|
|
for (i = 0; i < nextensions; i++)
|
|
|
|
{
|
|
|
|
ret = snprintf(path + len, sizeof(path) - len, ".%s", cover_extension[i]);
|
|
|
|
if ((ret < 0) || (ret >= sizeof(path) - len))
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_ART, "Artwork path will exceed PATH_MAX (%s)\n", ctx->dbmfi->path);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTF(E_SPAM, L_ART, "Trying own artwork file %s\n", path);
|
|
|
|
|
|
|
|
ret = access(path, F_OK);
|
|
|
|
if (ret < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == nextensions)
|
2015-12-28 18:26:52 -05:00
|
|
|
return ART_E_NONE;
|
|
|
|
|
2015-12-29 17:39:52 -05:00
|
|
|
snprintf(ctx->path, sizeof(ctx->path), "%s", path);
|
|
|
|
|
2020-11-05 17:06:23 -05:00
|
|
|
return artwork_get(ctx->evbuf, path, NULL, false, ctx->data_kind, ctx->req_params);
|
2015-12-28 18:26:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-05-15 17:14:14 -04:00
|
|
|
* Downloads the artwork from the location pointed to by queue_item->artwork_url
|
2015-12-28 18:26:52 -05:00
|
|
|
*/
|
|
|
|
static int
|
2020-05-15 17:14:14 -04:00
|
|
|
source_item_artwork_url_get(struct artwork_ctx *ctx)
|
2015-12-28 18:26:52 -05:00
|
|
|
{
|
2017-01-22 17:13:13 -05:00
|
|
|
struct db_queue_item *queue_item;
|
2015-12-28 18:26:52 -05:00
|
|
|
int ret;
|
|
|
|
|
2020-05-15 17:14:14 -04:00
|
|
|
DPRINTF(E_SPAM, L_ART, "Trying artwork url for %s\n", ctx->dbmfi->path);
|
2015-12-28 18:26:52 -05:00
|
|
|
|
2017-01-22 17:13:13 -05:00
|
|
|
queue_item = db_queue_fetch_byfileid(ctx->id);
|
|
|
|
if (!queue_item || !queue_item->artwork_url)
|
|
|
|
{
|
|
|
|
free_queue_item(queue_item, 0);
|
|
|
|
return ART_E_NONE;
|
|
|
|
}
|
|
|
|
|
2020-11-05 17:06:23 -05:00
|
|
|
ret = artwork_get_byurl(ctx->evbuf, queue_item->artwork_url, ctx->req_params);
|
2015-12-28 18:26:52 -05:00
|
|
|
|
2020-05-15 17:14:14 -04:00
|
|
|
snprintf(ctx->path, sizeof(ctx->path), "%s", queue_item->artwork_url);
|
2010-03-07 05:07:09 -05:00
|
|
|
|
2020-05-15 17:14:14 -04:00
|
|
|
free_queue_item(queue_item, 0);
|
2010-03-07 05:07:09 -05:00
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
return ret;
|
|
|
|
}
|
2010-03-07 05:07:09 -05:00
|
|
|
|
2019-09-09 15:39:25 -04:00
|
|
|
/*
|
2019-09-17 16:02:45 -04:00
|
|
|
* If we are playing a pipe and there is also a metadata pipe, then input/pipe.c
|
|
|
|
* may have saved the incoming artwork in a tmp file
|
2019-09-09 15:39:25 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
source_item_pipe_get(struct artwork_ctx *ctx)
|
|
|
|
{
|
2019-09-17 16:02:45 -04:00
|
|
|
struct db_queue_item *queue_item;
|
|
|
|
const char *proto = "file:";
|
|
|
|
char *path;
|
2020-11-28 15:20:24 -05:00
|
|
|
int ret;
|
2019-09-09 15:39:25 -04:00
|
|
|
|
|
|
|
DPRINTF(E_SPAM, L_ART, "Trying pipe metadata from %s.metadata\n", ctx->dbmfi->path);
|
|
|
|
|
2019-09-17 16:02:45 -04:00
|
|
|
queue_item = db_queue_fetch_byfileid(ctx->id);
|
|
|
|
if (!queue_item || !queue_item->artwork_url || strncmp(queue_item->artwork_url, proto, strlen(proto)) != 0)
|
2021-11-15 16:57:43 -05:00
|
|
|
goto notfound;
|
2019-09-17 16:02:45 -04:00
|
|
|
|
|
|
|
path = queue_item->artwork_url + strlen(proto);
|
|
|
|
|
2020-11-28 15:20:24 -05:00
|
|
|
// Sometimes the file has been replaced, but queue_item->artwork_url hasn't
|
|
|
|
// been updated yet. In that case just stop now.
|
|
|
|
ret = access(path, F_OK);
|
|
|
|
if (ret != 0)
|
2021-11-15 16:57:43 -05:00
|
|
|
goto notfound;
|
2020-11-28 15:20:24 -05:00
|
|
|
|
2019-09-19 17:23:03 -04:00
|
|
|
snprintf(ctx->path, sizeof(ctx->path), "%s", path);
|
2019-09-17 16:02:45 -04:00
|
|
|
|
2020-05-11 15:37:29 -04:00
|
|
|
free_queue_item(queue_item, 0);
|
2020-11-05 17:06:23 -05:00
|
|
|
return artwork_get(ctx->evbuf, ctx->path, NULL, false, ctx->data_kind, ctx->req_params);
|
2021-11-15 16:57:43 -05:00
|
|
|
|
|
|
|
notfound:
|
|
|
|
free_queue_item(queue_item, 0);
|
|
|
|
return ART_E_NONE;
|
2019-09-09 15:39:25 -04:00
|
|
|
}
|
|
|
|
|
2020-02-11 08:17:28 -05:00
|
|
|
static int
|
|
|
|
source_item_discogs_get(struct artwork_ctx *ctx)
|
|
|
|
{
|
|
|
|
char *url;
|
|
|
|
int ret;
|
|
|
|
|
2020-02-21 13:51:35 -05:00
|
|
|
if (!online_source_is_enabled(&discogs_source))
|
2020-02-17 16:08:31 -05:00
|
|
|
return ART_E_NONE;
|
|
|
|
|
2020-02-11 08:17:28 -05:00
|
|
|
url = online_source_search(&discogs_source, ctx);
|
|
|
|
if (!url)
|
|
|
|
return ART_E_NONE;
|
|
|
|
|
2020-02-21 13:51:35 -05:00
|
|
|
snprintf(ctx->path, sizeof(ctx->path), "%s", url);
|
|
|
|
|
2020-11-05 17:06:23 -05:00
|
|
|
ret = artwork_get_byurl(ctx->evbuf, url, ctx->req_params);
|
2020-02-11 08:17:28 -05:00
|
|
|
|
|
|
|
free(url);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
source_item_coverartarchive_get(struct artwork_ctx *ctx)
|
|
|
|
{
|
|
|
|
char *url;
|
|
|
|
int ret;
|
|
|
|
|
2020-02-21 13:51:35 -05:00
|
|
|
if (!online_source_is_enabled(&musicbrainz_source))
|
2020-02-17 16:08:31 -05:00
|
|
|
return ART_E_NONE;
|
|
|
|
|
2020-02-11 08:17:28 -05:00
|
|
|
// We search Musicbrainz to get the Musicbrainz ID, which we need to get the
|
|
|
|
// artwork from the Cover Art Archive
|
|
|
|
url = online_source_search(&musicbrainz_source, ctx);
|
|
|
|
if (!url)
|
|
|
|
return ART_E_NONE;
|
|
|
|
|
2020-02-21 13:51:35 -05:00
|
|
|
snprintf(ctx->path, sizeof(ctx->path), "%s", url);
|
|
|
|
|
2020-11-05 17:06:23 -05:00
|
|
|
ret = artwork_get_byurl(ctx->evbuf, url, ctx->req_params);
|
2020-02-11 08:17:28 -05:00
|
|
|
|
|
|
|
free(url);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-28 14:00:15 -04:00
|
|
|
#ifdef SPOTIFY
|
2019-02-05 05:35:45 -05:00
|
|
|
static int
|
2020-02-18 16:12:54 -05:00
|
|
|
source_item_spotifywebapi_track_get(struct artwork_ctx *ctx)
|
2019-02-05 05:35:45 -05:00
|
|
|
{
|
|
|
|
char *artwork_url;
|
|
|
|
int ret;
|
|
|
|
|
2020-11-05 17:06:23 -05:00
|
|
|
artwork_url = spotifywebapi_artwork_url_get(ctx->dbmfi->path, ctx->req_params.max_w, ctx->req_params.max_h);
|
2019-02-05 05:35:45 -05:00
|
|
|
if (!artwork_url)
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_ART, "No artwork from Spotify for %s\n", ctx->dbmfi->path);
|
|
|
|
return ART_E_NONE;
|
|
|
|
}
|
|
|
|
|
2020-11-05 17:06:23 -05:00
|
|
|
ret = artwork_get_byurl(ctx->evbuf, artwork_url, ctx->req_params);
|
2019-02-05 05:35:45 -05:00
|
|
|
|
|
|
|
free(artwork_url);
|
2020-02-11 08:17:28 -05:00
|
|
|
return ret;
|
|
|
|
}
|
2020-02-18 16:12:54 -05:00
|
|
|
|
2020-02-11 08:17:28 -05:00
|
|
|
static int
|
2020-02-18 16:12:54 -05:00
|
|
|
source_item_spotifywebapi_search_get(struct artwork_ctx *ctx)
|
2020-02-11 08:17:28 -05:00
|
|
|
{
|
2020-02-18 16:12:54 -05:00
|
|
|
char *url;
|
|
|
|
int ret;
|
2019-02-05 05:35:45 -05:00
|
|
|
|
2020-02-21 13:51:35 -05:00
|
|
|
if (!online_source_is_enabled(&spotify_source))
|
2020-02-18 16:12:54 -05:00
|
|
|
return ART_E_NONE;
|
2020-02-11 08:17:28 -05:00
|
|
|
|
2020-02-18 16:12:54 -05:00
|
|
|
url = online_source_search(&spotify_source, ctx);
|
|
|
|
if (!url)
|
|
|
|
return ART_E_NONE;
|
|
|
|
|
2020-02-21 13:51:35 -05:00
|
|
|
snprintf(ctx->path, sizeof(ctx->path), "%s", url);
|
|
|
|
|
2020-11-05 17:06:23 -05:00
|
|
|
ret = artwork_get_byurl(ctx->evbuf, url, ctx->req_params);
|
2020-02-18 16:12:54 -05:00
|
|
|
|
|
|
|
free(url);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#else
|
2019-02-05 05:35:45 -05:00
|
|
|
static int
|
2020-02-18 16:12:54 -05:00
|
|
|
source_item_spotifywebapi_track_get(struct artwork_ctx *ctx)
|
2019-02-05 05:35:45 -05:00
|
|
|
{
|
|
|
|
return ART_E_ERROR;
|
|
|
|
}
|
2020-02-18 16:12:54 -05:00
|
|
|
|
|
|
|
static int
|
|
|
|
source_item_spotifywebapi_search_get(struct artwork_ctx *ctx)
|
|
|
|
{
|
|
|
|
// Silence compiler warning about spotify_source being unused
|
|
|
|
(void)spotify_source;
|
|
|
|
|
|
|
|
return ART_E_NONE;
|
|
|
|
}
|
2015-12-28 18:26:52 -05:00
|
|
|
#endif
|
|
|
|
|
2017-01-04 17:20:59 -05:00
|
|
|
/* First looks of the mfi->path is in any playlist, and if so looks in the dir
|
|
|
|
* of the playlist file (m3u et al) to see if there is any artwork. So if the
|
|
|
|
* playlist is /foo/bar.m3u it will look for /foo/bar.png and /foo/bar.jpg.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
source_item_ownpl_get(struct artwork_ctx *ctx)
|
|
|
|
{
|
|
|
|
struct query_params qp;
|
|
|
|
struct db_playlist_info dbpli;
|
|
|
|
char filter[PATH_MAX + 64];
|
|
|
|
char *mfi_path;
|
|
|
|
int format;
|
|
|
|
int ret;
|
|
|
|
|
2017-10-14 14:16:34 -04:00
|
|
|
ret = db_snprintf(filter, sizeof(filter), "filepath = '%q'", ctx->dbmfi->path);
|
|
|
|
if (ret < 0)
|
2017-01-04 17:20:59 -05:00
|
|
|
{
|
2017-10-14 14:16:34 -04:00
|
|
|
DPRINTF(E_LOG, L_ART, "Artwork path is too long: '%s'\n", ctx->dbmfi->path);
|
2017-01-04 17:20:59 -05:00
|
|
|
return ART_E_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&qp, 0, sizeof(struct query_params));
|
|
|
|
qp.type = Q_FIND_PL;
|
|
|
|
qp.filter = filter;
|
|
|
|
|
|
|
|
ret = db_query_start(&qp);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_ART, "Could not start ownpl query\n");
|
|
|
|
return ART_E_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
mfi_path = ctx->dbmfi->path;
|
|
|
|
|
|
|
|
format = ART_E_NONE;
|
2021-12-28 01:23:56 -05:00
|
|
|
while (((ret = db_query_fetch_pl(&dbpli, &qp)) == 0) && (dbpli.id) && (format == ART_E_NONE))
|
2017-01-04 17:20:59 -05:00
|
|
|
{
|
|
|
|
if (!dbpli.path)
|
|
|
|
continue;
|
|
|
|
|
2020-04-26 11:59:44 -04:00
|
|
|
if (dbpli.artwork_url)
|
|
|
|
{
|
2020-11-05 17:06:23 -05:00
|
|
|
format = artwork_get_byurl(ctx->evbuf, dbpli.artwork_url, ctx->req_params);
|
2020-04-26 11:59:44 -04:00
|
|
|
if (format > 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only handle non-remote paths with source_item_own_get()
|
|
|
|
if (dbpli.path && dbpli.path[0] == '/')
|
|
|
|
{
|
|
|
|
ctx->dbmfi->path = dbpli.path;
|
|
|
|
format = source_item_own_get(ctx);
|
|
|
|
}
|
2017-01-04 17:20:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx->dbmfi->path = mfi_path;
|
|
|
|
|
|
|
|
if ((ret < 0) || (format < 0))
|
|
|
|
format = ART_E_ERROR;
|
|
|
|
|
|
|
|
db_query_end(&qp);
|
|
|
|
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
2014-11-10 16:53:08 -05:00
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
/* --------------------------- SOURCE PROCESSING --------------------------- */
|
2014-11-10 16:53:08 -05:00
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
static int
|
|
|
|
process_items(struct artwork_ctx *ctx, int item_mode)
|
|
|
|
{
|
|
|
|
struct db_media_file_info dbmfi;
|
|
|
|
int i;
|
|
|
|
int ret;
|
2010-03-07 05:07:09 -05:00
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
ret = db_query_start(&ctx->qp);
|
2010-03-07 05:07:09 -05:00
|
|
|
if (ret < 0)
|
2014-12-05 13:37:31 -05:00
|
|
|
{
|
2015-12-28 18:26:52 -05:00
|
|
|
DPRINTF(E_LOG, L_ART, "Could not start query (type=%d)\n", ctx->qp.type);
|
|
|
|
ctx->cache = NEVER;
|
2014-12-05 13:37:31 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2015-01-04 09:12:46 -05:00
|
|
|
|
2021-12-28 01:23:56 -05:00
|
|
|
while ((ret = db_query_fetch_file(&dbmfi, &ctx->qp)) == 0)
|
2015-12-28 18:26:52 -05:00
|
|
|
{
|
|
|
|
// Save the first songalbumid, might need it for process_group() if this search doesn't give anything
|
|
|
|
if (!ctx->persistentid)
|
|
|
|
safe_atoi64(dbmfi.songalbumid, &ctx->persistentid);
|
|
|
|
|
2015-12-29 17:39:52 -05:00
|
|
|
if (item_mode && !ctx->individual)
|
2015-12-28 18:26:52 -05:00
|
|
|
goto no_artwork;
|
|
|
|
|
|
|
|
ret = (safe_atoi32(dbmfi.id, &ctx->id) < 0) ||
|
2020-02-18 16:12:54 -05:00
|
|
|
(safe_atou32(dbmfi.data_kind, &ctx->data_kind) < 0) ||
|
2020-04-16 17:40:15 -04:00
|
|
|
(safe_atou32(dbmfi.media_kind, &ctx->media_kind) < 0) ||
|
2020-02-18 16:12:54 -05:00
|
|
|
(ctx->data_kind > 30);
|
2015-12-28 18:26:52 -05:00
|
|
|
if (ret)
|
|
|
|
{
|
2020-04-16 17:40:15 -04:00
|
|
|
DPRINTF(E_LOG, L_ART, "Error converting dbmfi id, data_kind or media_kind to number for '%s'\n", dbmfi.path);
|
2015-12-28 18:26:52 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; artwork_item_source[i].handler; i++)
|
|
|
|
{
|
2020-02-18 16:12:54 -05:00
|
|
|
if ((artwork_item_source[i].data_kinds & (1 << ctx->data_kind)) == 0)
|
2015-12-28 18:26:52 -05:00
|
|
|
continue;
|
|
|
|
|
2020-04-16 17:40:15 -04:00
|
|
|
if ((artwork_item_source[i].media_kinds & ctx->media_kind) == 0)
|
|
|
|
continue;
|
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
// If just one handler says we should not cache a negative result then we obey that
|
|
|
|
if ((artwork_item_source[i].cache & ON_FAILURE) == 0)
|
|
|
|
ctx->cache = NEVER;
|
|
|
|
|
2015-12-29 17:39:52 -05:00
|
|
|
DPRINTF(E_SPAM, L_ART, "Checking item source '%s'\n", artwork_item_source[i].name);
|
2015-12-28 18:26:52 -05:00
|
|
|
|
|
|
|
ctx->dbmfi = &dbmfi;
|
|
|
|
ret = artwork_item_source[i].handler(ctx);
|
|
|
|
ctx->dbmfi = NULL;
|
|
|
|
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
2016-01-07 16:48:04 -05:00
|
|
|
DPRINTF(E_DBG, L_ART, "Artwork for '%s' found in source '%s'\n", dbmfi.title, artwork_item_source[i].name);
|
2020-02-21 13:51:35 -05:00
|
|
|
ctx->cache = artwork_item_source[i].cache;
|
2015-12-28 18:26:52 -05:00
|
|
|
db_query_end(&ctx->qp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else if (ret == ART_E_ABORT)
|
|
|
|
{
|
2016-01-07 16:48:04 -05:00
|
|
|
DPRINTF(E_DBG, L_ART, "Source '%s' stopped search for artwork for '%s'\n", artwork_item_source[i].name, dbmfi.title);
|
2015-12-28 18:26:52 -05:00
|
|
|
ctx->cache = NEVER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (ret == ART_E_ERROR)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_ART, "Source '%s' returned an error for '%s'\n", artwork_item_source[i].name, dbmfi.title);
|
|
|
|
ctx->cache = NEVER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
2014-12-05 13:37:31 -05:00
|
|
|
{
|
2015-12-28 18:26:52 -05:00
|
|
|
DPRINTF(E_LOG, L_ART, "Error fetching results\n");
|
|
|
|
ctx->cache = NEVER;
|
2014-12-05 13:37:31 -05:00
|
|
|
}
|
2015-12-28 18:26:52 -05:00
|
|
|
|
|
|
|
no_artwork:
|
|
|
|
db_query_end(&ctx->qp);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
process_group(struct artwork_ctx *ctx)
|
|
|
|
{
|
2020-05-11 11:24:05 -04:00
|
|
|
struct db_media_file_info dbmfi;
|
|
|
|
bool is_valid;
|
2015-12-28 18:26:52 -05:00
|
|
|
int i;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!ctx->persistentid)
|
2014-11-10 16:53:08 -05:00
|
|
|
{
|
2015-12-28 18:26:52 -05:00
|
|
|
DPRINTF(E_LOG, L_ART, "Bug! No persistentid in call to process_group()\n");
|
|
|
|
ctx->cache = NEVER;
|
2015-01-04 09:12:46 -05:00
|
|
|
return -1;
|
2014-11-10 16:53:08 -05:00
|
|
|
}
|
|
|
|
|
2020-05-11 11:24:05 -04:00
|
|
|
// Check if the group is valid (exists and is not e.g. "Unknown album")
|
|
|
|
ret = db_query_start(&ctx->qp);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_ART, "Could not start query to check if group is valid (persistentid = %" PRIi64 ")\n", ctx->qp.persistentid);
|
|
|
|
goto invalid_group;
|
|
|
|
}
|
|
|
|
|
2021-12-28 01:23:56 -05:00
|
|
|
is_valid = (db_query_fetch_file(&dbmfi, &ctx->qp) == 0 && strcmp(dbmfi.album, CFG_NAME_UNKNOWN_ALBUM) != 0 && strcmp(dbmfi.album_artist, CFG_NAME_UNKNOWN_ARTIST) != 0);
|
2020-05-11 11:24:05 -04:00
|
|
|
db_query_end(&ctx->qp);
|
|
|
|
if (!is_valid)
|
|
|
|
{
|
|
|
|
DPRINTF(E_SPAM, L_ART, "Skipping group sources due to unknown album or artist\n");
|
|
|
|
goto invalid_group;
|
|
|
|
}
|
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
for (i = 0; artwork_group_source[i].handler; i++)
|
|
|
|
{
|
|
|
|
// If just one handler says we should not cache a negative result then we obey that
|
|
|
|
if ((artwork_group_source[i].cache & ON_FAILURE) == 0)
|
|
|
|
ctx->cache = NEVER;
|
2015-01-04 09:12:46 -05:00
|
|
|
|
2015-12-29 17:39:52 -05:00
|
|
|
DPRINTF(E_SPAM, L_ART, "Checking group source '%s'\n", artwork_group_source[i].name);
|
2010-03-07 05:07:09 -05:00
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
ret = artwork_group_source[i].handler(ctx);
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
2016-01-07 16:48:04 -05:00
|
|
|
DPRINTF(E_DBG, L_ART, "Artwork for group %" PRIi64 " found in source '%s'\n", ctx->persistentid, artwork_group_source[i].name);
|
2020-02-21 13:51:35 -05:00
|
|
|
ctx->cache = artwork_group_source[i].cache;
|
2015-12-28 18:26:52 -05:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else if (ret == ART_E_ABORT)
|
|
|
|
{
|
2016-01-07 16:48:04 -05:00
|
|
|
DPRINTF(E_DBG, L_ART, "Source '%s' stopped search for artwork for group %" PRIi64 "\n", artwork_group_source[i].name, ctx->persistentid);
|
2015-12-28 18:26:52 -05:00
|
|
|
ctx->cache = NEVER;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (ret == ART_E_ERROR)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_ART, "Source '%s' returned an error for group %" PRIi64 "\n", artwork_group_source[i].name, ctx->persistentid);
|
|
|
|
ctx->cache = NEVER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 11:24:05 -04:00
|
|
|
invalid_group:
|
|
|
|
return process_items(ctx, 0);
|
2010-03-07 05:07:09 -05:00
|
|
|
}
|
2014-11-10 16:53:08 -05:00
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
|
|
|
|
/* ------------------------------ ARTWORK API ------------------------------ */
|
|
|
|
|
2014-11-10 16:53:08 -05:00
|
|
|
int
|
2020-11-18 17:23:05 -05:00
|
|
|
artwork_get_item(struct evbuffer *evbuf, int id, int max_w, int max_h, int format)
|
2014-11-10 16:53:08 -05:00
|
|
|
{
|
2015-12-28 18:26:52 -05:00
|
|
|
struct artwork_ctx ctx;
|
|
|
|
char filter[32];
|
|
|
|
int ret;
|
|
|
|
|
2020-02-21 13:51:35 -05:00
|
|
|
DPRINTF(E_DBG, L_ART, "Artwork request for item %d (max_w=%d, max_h=%d)\n", id, max_w, max_h);
|
2014-11-10 16:53:08 -05:00
|
|
|
|
2018-05-10 01:23:11 -04:00
|
|
|
if (id == DB_MEDIA_FILE_NON_PERSISTENT_ID)
|
|
|
|
return -1;
|
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
memset(&ctx, 0, sizeof(struct artwork_ctx));
|
|
|
|
|
|
|
|
ctx.qp.type = Q_ITEMS;
|
|
|
|
ctx.qp.filter = filter;
|
|
|
|
ctx.evbuf = evbuf;
|
2020-11-05 17:06:23 -05:00
|
|
|
ctx.req_params.max_w = max_w;
|
|
|
|
ctx.req_params.max_h = max_h;
|
|
|
|
ctx.req_params.format = format;
|
2015-12-28 18:26:52 -05:00
|
|
|
ctx.cache = ON_FAILURE;
|
2015-12-29 17:39:52 -05:00
|
|
|
ctx.individual = cfg_getbool(cfg_getsec(cfg, "library"), "artwork_individual");
|
2015-12-28 18:26:52 -05:00
|
|
|
|
2017-10-14 14:16:34 -04:00
|
|
|
ret = db_snprintf(filter, sizeof(filter), "id = %d", id);
|
|
|
|
if (ret < 0)
|
2015-01-04 09:12:46 -05:00
|
|
|
{
|
2015-12-28 18:26:52 -05:00
|
|
|
DPRINTF(E_LOG, L_ART, "Could not build filter for file id %d; no artwork will be sent\n", id);
|
2015-01-04 09:12:46 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
// Note: process_items will set ctx.persistentid for the following process_group()
|
2015-12-29 17:39:52 -05:00
|
|
|
// - and do nothing else if artwork_individual is not configured by user
|
2015-12-28 18:26:52 -05:00
|
|
|
ret = process_items(&ctx, 1);
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
2020-02-21 13:51:35 -05:00
|
|
|
if (ctx.cache & ON_SUCCESS)
|
2015-12-28 18:26:52 -05:00
|
|
|
cache_artwork_add(CACHE_ARTWORK_INDIVIDUAL, id, max_w, max_h, ret, ctx.path, evbuf);
|
2014-12-27 09:44:16 -05:00
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
return ret;
|
|
|
|
}
|
2014-11-10 16:53:08 -05:00
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
ctx.qp.type = Q_GROUP_ITEMS;
|
|
|
|
ctx.qp.persistentid = ctx.persistentid;
|
2014-11-10 16:53:08 -05:00
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
ret = process_group(&ctx);
|
|
|
|
if (ret > 0)
|
2015-01-04 09:12:46 -05:00
|
|
|
{
|
2020-02-21 13:51:35 -05:00
|
|
|
if (ctx.cache & ON_SUCCESS)
|
2015-12-28 18:26:52 -05:00
|
|
|
cache_artwork_add(CACHE_ARTWORK_GROUP, ctx.persistentid, max_w, max_h, ret, ctx.path, evbuf);
|
|
|
|
|
|
|
|
return ret;
|
2015-01-04 09:12:46 -05:00
|
|
|
}
|
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
DPRINTF(E_DBG, L_ART, "No artwork found for item %d\n", id);
|
|
|
|
|
2020-02-21 13:51:35 -05:00
|
|
|
if (ctx.cache & ON_FAILURE)
|
2015-12-28 18:26:52 -05:00
|
|
|
cache_artwork_add(CACHE_ARTWORK_GROUP, ctx.persistentid, max_w, max_h, 0, "", evbuf);
|
|
|
|
|
|
|
|
return -1;
|
2014-11-10 16:53:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-11-18 17:23:05 -05:00
|
|
|
artwork_get_group(struct evbuffer *evbuf, int id, int max_w, int max_h, int format)
|
2014-11-10 16:53:08 -05:00
|
|
|
{
|
2015-12-28 18:26:52 -05:00
|
|
|
struct artwork_ctx ctx;
|
|
|
|
int ret;
|
2014-11-10 16:53:08 -05:00
|
|
|
|
2020-02-21 13:51:35 -05:00
|
|
|
DPRINTF(E_DBG, L_ART, "Artwork request for group %d (max_w=%d, max_h=%d)\n", id, max_w, max_h);
|
2014-11-10 16:53:08 -05:00
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
memset(&ctx, 0, sizeof(struct artwork_ctx));
|
|
|
|
|
2015-01-04 09:12:46 -05:00
|
|
|
/* Get the persistent id for the given group id */
|
2015-12-28 18:26:52 -05:00
|
|
|
ret = db_group_persistentid_byid(id, &ctx.persistentid);
|
|
|
|
if (ret < 0)
|
2015-01-04 09:12:46 -05:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_ART, "Error fetching persistent id for group id %d\n", id);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-10 16:53:08 -05:00
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
ctx.qp.type = Q_GROUP_ITEMS;
|
|
|
|
ctx.qp.persistentid = ctx.persistentid;
|
|
|
|
ctx.evbuf = evbuf;
|
2020-11-05 17:06:23 -05:00
|
|
|
ctx.req_params.max_w = max_w;
|
|
|
|
ctx.req_params.max_h = max_h;
|
|
|
|
ctx.req_params.format = format;
|
2015-12-28 18:26:52 -05:00
|
|
|
ctx.cache = ON_FAILURE;
|
2015-12-29 17:39:52 -05:00
|
|
|
ctx.individual = cfg_getbool(cfg_getsec(cfg, "library"), "artwork_individual");
|
2015-12-28 18:26:52 -05:00
|
|
|
|
|
|
|
ret = process_group(&ctx);
|
|
|
|
if (ret > 0)
|
2015-01-04 09:12:46 -05:00
|
|
|
{
|
2020-02-21 13:51:35 -05:00
|
|
|
if (ctx.cache & ON_SUCCESS)
|
2015-12-28 18:26:52 -05:00
|
|
|
cache_artwork_add(CACHE_ARTWORK_GROUP, ctx.persistentid, max_w, max_h, ret, ctx.path, evbuf);
|
|
|
|
|
|
|
|
return ret;
|
2015-01-04 09:12:46 -05:00
|
|
|
}
|
2014-11-10 16:53:08 -05:00
|
|
|
|
2015-12-28 18:26:52 -05:00
|
|
|
DPRINTF(E_DBG, L_ART, "No artwork found for group %d\n", id);
|
|
|
|
|
2020-02-21 13:51:35 -05:00
|
|
|
if (ctx.cache & ON_FAILURE)
|
2015-12-28 18:26:52 -05:00
|
|
|
cache_artwork_add(CACHE_ARTWORK_GROUP, ctx.persistentid, max_w, max_h, 0, "", evbuf);
|
|
|
|
|
|
|
|
return -1;
|
2014-11-10 16:53:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Checks if the file is an artwork file */
|
2020-05-15 17:14:14 -04:00
|
|
|
bool
|
2014-11-10 16:53:08 -05:00
|
|
|
artwork_file_is_artwork(const char *filename)
|
|
|
|
{
|
|
|
|
cfg_t *lib;
|
|
|
|
int n;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
int ret;
|
|
|
|
char artwork[PATH_MAX];
|
|
|
|
|
|
|
|
lib = cfg_getsec(cfg, "library");
|
|
|
|
n = cfg_size(lib, "artwork_basenames");
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
2020-05-15 17:14:14 -04:00
|
|
|
for (j = 0; j < ARRAY_SIZE(cover_extension); j++)
|
2014-11-10 16:53:08 -05:00
|
|
|
{
|
|
|
|
ret = snprintf(artwork, sizeof(artwork), "%s.%s", cfg_getnstr(lib, "artwork_basenames", i), cover_extension[j]);
|
|
|
|
if ((ret < 0) || (ret >= sizeof(artwork)))
|
|
|
|
{
|
|
|
|
DPRINTF(E_INFO, L_ART, "Artwork path exceeds PATH_MAX (%s.%s)\n", cfg_getnstr(lib, "artwork_basenames", i), cover_extension[j]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(artwork, filename) == 0)
|
2020-05-15 17:14:14 -04:00
|
|
|
return true;
|
2014-11-10 16:53:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-15 17:14:14 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
artwork_extension_is_artwork(const char *path)
|
|
|
|
{
|
|
|
|
char *ext;
|
|
|
|
int len;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ext = strrchr(path, '.');
|
|
|
|
if (!ext)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ext++;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(cover_extension); i++)
|
|
|
|
{
|
|
|
|
len = strlen(cover_extension[i]);
|
|
|
|
|
|
|
|
if (strncasecmp(cover_extension[i], ext, len) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check that after the extension we either have the end or "?"
|
|
|
|
if (ext[len] != '\0' && ext[len] != '?')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-11-10 16:53:08 -05:00
|
|
|
}
|