mirror of
https://github.com/owntone/owntone-server.git
synced 2025-01-13 16:03:23 -05:00
Merge pull request #907 from chme/artwork_remove_libspotify_refactor
Remove libspotify as artwork source + refactoring
This commit is contained in:
commit
3b27d50f97
223
src/artwork.c
223
src/artwork.c
@ -46,7 +46,6 @@
|
||||
|
||||
#ifdef HAVE_SPOTIFY_H
|
||||
# include "spotify_webapi.h"
|
||||
# include "spotify.h"
|
||||
#endif
|
||||
|
||||
/* This artwork module will look for artwork by consulting a set of sources one
|
||||
@ -192,7 +191,6 @@ static int source_item_embedded_get(struct artwork_ctx *ctx);
|
||||
static int source_item_own_get(struct artwork_ctx *ctx);
|
||||
static int source_item_stream_get(struct artwork_ctx *ctx);
|
||||
static int source_item_pipe_get(struct artwork_ctx *ctx);
|
||||
static int source_item_libspotify_get(struct artwork_ctx *ctx);
|
||||
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);
|
||||
@ -259,12 +257,6 @@ static struct artwork_source artwork_item_source[] =
|
||||
.data_kinds = (1 << DATA_KIND_PIPE),
|
||||
.cache = NEVER,
|
||||
},
|
||||
{
|
||||
.name = "libspotify",
|
||||
.handler = source_item_libspotify_get,
|
||||
.data_kinds = (1 << DATA_KIND_SPOTIFY),
|
||||
.cache = ON_SUCCESS,
|
||||
},
|
||||
{
|
||||
.name = "Spotify track web api",
|
||||
.handler = source_item_spotifywebapi_track_get,
|
||||
@ -731,52 +723,53 @@ artwork_evbuf_rescale(struct evbuffer *artwork, struct evbuffer *raw, int max_w,
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Looks for an artwork file in a directory. Will rescale if needed.
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
* @out evbuf Image data
|
||||
* @in dir Directory to search
|
||||
* @in max_w Requested width
|
||||
* @in max_h Requested height
|
||||
* @out out_path Path to the artwork file if found, must be a char[PATH_MAX] buffer
|
||||
* @return ART_FMT_* on success, ART_E_NONE on nothing found, ART_E_ERROR on error
|
||||
* 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
|
||||
artwork_get_bydir(struct evbuffer *evbuf, char *dir, int max_w, int max_h, char *out_path)
|
||||
dir_image_find(char *out_path, size_t len, const char *dir)
|
||||
{
|
||||
char path[PATH_MAX];
|
||||
char parentdir[PATH_MAX];
|
||||
int i;
|
||||
int j;
|
||||
int len;
|
||||
int path_len;
|
||||
int ret;
|
||||
cfg_t *lib;
|
||||
int nbasenames;
|
||||
int nextensions;
|
||||
char *ptr;
|
||||
|
||||
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 ART_E_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
len = strlen(path);
|
||||
path_len = strlen(path);
|
||||
|
||||
lib = cfg_getsec(cfg, "library");
|
||||
nbasenames = cfg_size(lib, "artwork_basenames");
|
||||
|
||||
if (nbasenames == 0)
|
||||
return ART_E_NONE;
|
||||
return -1;
|
||||
|
||||
nextensions = sizeof(cover_extension) / sizeof(cover_extension[0]);
|
||||
nextensions = ARRAY_SIZE(cover_extension);
|
||||
|
||||
for (i = 0; i < nbasenames; i++)
|
||||
{
|
||||
for (j = 0; j < nextensions; j++)
|
||||
{
|
||||
ret = snprintf(path + len, sizeof(path) - len, "/%s.%s", cfg_getnstr(lib, "artwork_basenames", i), cover_extension[j]);
|
||||
if ((ret < 0) || (ret >= sizeof(path) - len))
|
||||
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))
|
||||
{
|
||||
DPRINTF(E_LOG, L_ART, "Artwork path will exceed PATH_MAX (%s/%s)\n", dir, cfg_getnstr(lib, "artwork_basenames", i));
|
||||
continue;
|
||||
@ -785,60 +778,108 @@ artwork_get_bydir(struct evbuffer *evbuf, char *dir, int max_w, int max_h, char
|
||||
DPRINTF(E_SPAM, L_ART, "Trying directory artwork file %s\n", path);
|
||||
|
||||
ret = access(path, F_OK);
|
||||
if (ret < 0)
|
||||
continue;
|
||||
|
||||
// If artwork file exists (ret == 0), exit the loop
|
||||
break;
|
||||
}
|
||||
|
||||
// In case the previous loop exited early, we found an existing artwork file and exit the outer loop
|
||||
if (j < nextensions)
|
||||
break;
|
||||
}
|
||||
|
||||
// If the loop for directory artwork did not exit early, look for parent directory artwork
|
||||
if (i == nbasenames)
|
||||
{
|
||||
ptr = strrchr(path, '/');
|
||||
if (ptr)
|
||||
*ptr = '\0';
|
||||
|
||||
ptr = strrchr(path, '/');
|
||||
if ((!ptr) || (strlen(ptr) <= 1))
|
||||
{
|
||||
DPRINTF(E_LOG, L_ART, "Could not find parent dir name (%s)\n", path);
|
||||
return ART_E_ERROR;
|
||||
}
|
||||
strcpy(parentdir, ptr + 1);
|
||||
|
||||
len = strlen(path);
|
||||
|
||||
for (i = 0; i < nextensions; i++)
|
||||
{
|
||||
ret = snprintf(path + len, sizeof(path) - len, "/%s.%s", parentdir, cover_extension[i]);
|
||||
if ((ret < 0) || (ret >= sizeof(path) - len))
|
||||
if (ret == 0)
|
||||
{
|
||||
DPRINTF(E_LOG, L_ART, "Artwork path will exceed PATH_MAX (%s)\n", parentdir);
|
||||
continue;
|
||||
snprintf(out_path, len, "%s", path);
|
||||
return 0;
|
||||
}
|
||||
|
||||
DPRINTF(E_SPAM, L_ART, "Trying parent directory artwork file %s\n", path);
|
||||
|
||||
ret = access(path, F_OK);
|
||||
if (ret < 0)
|
||||
continue;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (i == nextensions)
|
||||
return ART_E_NONE;
|
||||
}
|
||||
|
||||
snprintf(out_path, PATH_MAX, "%s", path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return artwork_get(evbuf, path, NULL, max_w, max_h, false);
|
||||
/*
|
||||
* 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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
ptr = strrchr(path, '/');
|
||||
if ((!ptr) || (strlen(ptr) <= 1))
|
||||
{
|
||||
DPRINTF(E_LOG, L_ART, "Could not find parent dir name (%s)\n", path);
|
||||
return -1;
|
||||
}
|
||||
strcpy(parentdir, ptr + 1);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
DPRINTF(E_SPAM, L_ART, "Trying parent directory artwork file %s\n", path);
|
||||
|
||||
ret = access(path, F_OK);
|
||||
if (ret == 0)
|
||||
{
|
||||
snprintf(out_path, len, "%s", path);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Looks for an artwork file in a directory. Will rescale if needed.
|
||||
*
|
||||
* @out evbuf Image data
|
||||
* @in dir Directory to search
|
||||
* @in max_w Requested width
|
||||
* @in max_h Requested height
|
||||
* @out out_path Path to the artwork file if found, must be a char[PATH_MAX] buffer
|
||||
* @in len Max size of "out_path"
|
||||
* @return ART_FMT_* on success, ART_E_NONE on nothing found, ART_E_ERROR on error
|
||||
*/
|
||||
static int
|
||||
artwork_get_bydir(struct evbuffer *evbuf, char *dir, int max_w, int max_h, char *out_path, size_t len)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = dir_image_find(out_path, len, dir);
|
||||
if (ret >= 0)
|
||||
{
|
||||
return artwork_get(evbuf, out_path, NULL, max_w, max_h, false);
|
||||
}
|
||||
|
||||
ret = parent_dir_image_find(out_path, len, dir);
|
||||
if (ret >= 0)
|
||||
{
|
||||
return artwork_get(evbuf, out_path, NULL, max_w, max_h, false);
|
||||
}
|
||||
|
||||
return ART_E_NONE;
|
||||
}
|
||||
|
||||
/* Retrieves artwork from an URL, will rescale if needed. Checks the cache stash
|
||||
@ -1322,7 +1363,7 @@ source_group_dir_get(struct artwork_ctx *ctx)
|
||||
if (access(dir, F_OK) < 0)
|
||||
continue;
|
||||
|
||||
ret = artwork_get_bydir(ctx->evbuf, dir, ctx->max_w, ctx->max_h, ctx->path);
|
||||
ret = artwork_get_bydir(ctx->evbuf, dir, ctx->max_w, ctx->max_h, ctx->path, sizeof(ctx->path));
|
||||
if (ret > 0)
|
||||
{
|
||||
db_query_end(&qp);
|
||||
@ -1554,34 +1595,6 @@ source_item_coverartarchive_get(struct artwork_ctx *ctx)
|
||||
}
|
||||
|
||||
#ifdef HAVE_SPOTIFY_H
|
||||
static int
|
||||
source_item_libspotify_get(struct artwork_ctx *ctx)
|
||||
{
|
||||
struct evbuffer *raw;
|
||||
int ret;
|
||||
|
||||
CHECK_NULL(L_ART, raw = evbuffer_new());
|
||||
|
||||
ret = spotify_artwork_get(raw, ctx->dbmfi->path, ctx->max_w, ctx->max_h);
|
||||
if (ret < 0)
|
||||
{
|
||||
DPRINTF(E_WARN, L_ART, "No artwork from libspotify for %s\n", ctx->dbmfi->path);
|
||||
evbuffer_free(raw);
|
||||
return ART_E_NONE;
|
||||
}
|
||||
|
||||
ret = artwork_evbuf_rescale(ctx->evbuf, raw, ctx->max_w, ctx->max_h);
|
||||
if (ret < 0)
|
||||
{
|
||||
DPRINTF(E_LOG, L_ART, "Could not rescale libspotify artwork for '%s'\n", ctx->dbmfi->path);
|
||||
evbuffer_free(raw);
|
||||
return ART_E_ERROR;
|
||||
}
|
||||
|
||||
evbuffer_free(raw);
|
||||
return ART_FMT_JPEG;
|
||||
}
|
||||
|
||||
static int
|
||||
source_item_spotifywebapi_track_get(struct artwork_ctx *ctx)
|
||||
{
|
||||
@ -1630,12 +1643,6 @@ source_item_spotifywebapi_search_get(struct artwork_ctx *ctx)
|
||||
return ret;
|
||||
}
|
||||
#else
|
||||
static int
|
||||
source_item_libspotify_get(struct artwork_ctx *ctx)
|
||||
{
|
||||
return ART_E_ERROR;
|
||||
}
|
||||
|
||||
static int
|
||||
source_item_spotifywebapi_track_get(struct artwork_ctx *ctx)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user