2017-08-12 13:42:06 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Christian Meffert <christian.meffert@googlemail.com>
|
|
|
|
*
|
|
|
|
* Adapted from httpd_adm.c:
|
|
|
|
* Copyright (C) 2015 Stuart NAIFEH <stu@naifeh.org>
|
|
|
|
*
|
|
|
|
* Adapted from httpd_daap.c and httpd.c:
|
|
|
|
* Copyright (C) 2009-2011 Julien BLACHE <jb@jblache.org>
|
|
|
|
* Copyright (C) 2010 Kai Elwert <elwertk@googlemail.com>
|
|
|
|
*
|
|
|
|
* Adapted from mt-daapd:
|
|
|
|
* Copyright (C) 2003-2007 Ron Pedde <ron@pedde.com>
|
|
|
|
*
|
|
|
|
* 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
|
2018-02-22 15:49:11 -05:00
|
|
|
|
2019-04-10 15:04:00 -04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2019-01-28 08:46:36 -05:00
|
|
|
#include <errno.h>
|
2019-01-20 07:13:01 -05:00
|
|
|
#include <limits.h>
|
2017-08-12 13:42:06 -04:00
|
|
|
#include <regex.h>
|
2018-05-26 04:55:51 -04:00
|
|
|
#include <stdbool.h>
|
2017-10-29 02:55:25 -04:00
|
|
|
#include <stdio.h>
|
2019-01-28 08:46:36 -05:00
|
|
|
#include <stdlib.h>
|
2017-08-12 13:42:06 -04:00
|
|
|
#include <string.h>
|
2017-12-27 04:01:36 -05:00
|
|
|
#include <time.h>
|
2017-08-12 13:42:06 -04:00
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
#include "httpd_jsonapi.h"
|
2017-08-12 13:42:06 -04:00
|
|
|
#include "conffile.h"
|
|
|
|
#include "db.h"
|
2017-09-24 00:57:49 -04:00
|
|
|
#ifdef LASTFM
|
|
|
|
# include "lastfm.h"
|
|
|
|
#endif
|
2017-08-12 13:42:06 -04:00
|
|
|
#include "library.h"
|
|
|
|
#include "logger.h"
|
2017-09-15 18:10:26 -04:00
|
|
|
#include "misc.h"
|
2017-08-12 13:42:06 -04:00
|
|
|
#include "misc_json.h"
|
2017-10-29 02:55:25 -04:00
|
|
|
#include "player.h"
|
2017-08-12 13:42:06 -04:00
|
|
|
#include "remote_pairing.h"
|
2019-07-07 03:05:49 -04:00
|
|
|
#include "settings.h"
|
2018-03-17 08:56:48 -04:00
|
|
|
#include "smartpl_query.h"
|
2017-08-12 13:42:06 -04:00
|
|
|
#ifdef HAVE_SPOTIFY_H
|
|
|
|
# include "spotify_webapi.h"
|
|
|
|
# include "spotify.h"
|
|
|
|
#endif
|
|
|
|
|
2019-06-01 02:02:46 -04:00
|
|
|
|
2019-04-10 15:04:00 -04:00
|
|
|
static bool allow_modifying_stored_playlists;
|
2019-06-01 02:02:46 -04:00
|
|
|
static char *default_playlist_directory;
|
2019-04-10 15:04:00 -04:00
|
|
|
|
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
/* -------------------------------- HELPERS --------------------------------- */
|
|
|
|
|
2020-02-22 04:03:13 -05:00
|
|
|
static bool
|
|
|
|
is_modified(struct evhttp_request *req, const char *key)
|
|
|
|
{
|
|
|
|
int64_t db_update = 0;
|
|
|
|
|
|
|
|
db_admin_getint64(&db_update, key);
|
|
|
|
|
|
|
|
return (!db_update || !httpd_request_not_modified_since(req, (time_t)db_update));
|
|
|
|
}
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
static inline void
|
|
|
|
safe_json_add_string(json_object *obj, const char *key, const char *value)
|
2017-11-04 16:24:42 -04:00
|
|
|
{
|
2017-12-27 04:01:36 -05:00
|
|
|
if (value)
|
|
|
|
json_object_object_add(obj, key, json_object_new_string(value));
|
|
|
|
}
|
2017-11-04 16:24:42 -04:00
|
|
|
|
2019-02-14 02:22:50 -05:00
|
|
|
static inline void
|
|
|
|
safe_json_add_string_from_int64(json_object *obj, const char *key, int64_t value)
|
|
|
|
{
|
|
|
|
char tmp[100];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (value > 0)
|
|
|
|
{
|
|
|
|
ret = snprintf(tmp, sizeof(tmp), "%" PRIi64, value);
|
|
|
|
if (ret < sizeof(tmp))
|
|
|
|
json_object_object_add(obj, key, json_object_new_string(tmp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
static inline void
|
|
|
|
safe_json_add_int_from_string(json_object *obj, const char *key, const char *value)
|
|
|
|
{
|
|
|
|
int intval;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ret = safe_atoi32(value, &intval);
|
|
|
|
if (ret == 0)
|
|
|
|
json_object_object_add(obj, key, json_object_new_int(intval));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2020-09-01 14:37:10 -04:00
|
|
|
safe_json_add_time_from_string(json_object *obj, const char *key, const char *value)
|
2017-12-27 04:01:36 -05:00
|
|
|
{
|
|
|
|
uint32_t tmp;
|
|
|
|
time_t timestamp;
|
|
|
|
struct tm tm;
|
|
|
|
char result[32];
|
|
|
|
|
|
|
|
if (!value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (safe_atou32(value, &tmp) != 0)
|
2017-11-04 16:24:42 -04:00
|
|
|
{
|
2017-12-27 04:01:36 -05:00
|
|
|
DPRINTF(E_LOG, L_WEB, "Error converting timestamp to uint32_t: %s\n", value);
|
|
|
|
return;
|
2017-11-04 16:24:42 -04:00
|
|
|
}
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
if (!tmp)
|
|
|
|
return;
|
2017-11-04 16:24:42 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
timestamp = tmp;
|
|
|
|
if (gmtime_r(×tamp, &tm) == NULL)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Error converting timestamp to gmtime: %s\n", value);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-04 16:24:42 -04:00
|
|
|
|
2020-09-01 14:37:10 -04:00
|
|
|
strftime(result, sizeof(result), "%FT%TZ", &tm);
|
|
|
|
|
|
|
|
json_object_object_add(obj, key, json_object_new_string(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
safe_json_add_date_from_string(json_object *obj, const char *key, const char *value)
|
|
|
|
{
|
|
|
|
uint32_t tmp;
|
|
|
|
time_t timestamp;
|
|
|
|
struct tm tm;
|
|
|
|
char result[32];
|
|
|
|
|
|
|
|
if (!value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (safe_atou32(value, &tmp) != 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Error converting timestamp to uint32_t: %s\n", value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tmp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
timestamp = tmp;
|
|
|
|
if (localtime_r(×tamp, &tm) == NULL)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Error converting timestamp to localtime: %s\n", value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
strftime(result, sizeof(result), "%F", &tm);
|
2017-11-04 16:24:42 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
json_object_object_add(obj, key, json_object_new_string(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
static json_object *
|
|
|
|
artist_to_json(struct db_group_info *dbgri)
|
|
|
|
{
|
|
|
|
json_object *item;
|
2020-08-22 06:59:56 -04:00
|
|
|
int intval;
|
2017-12-27 04:01:36 -05:00
|
|
|
char uri[100];
|
2018-10-07 15:54:38 -04:00
|
|
|
char artwork_url[100];
|
2017-12-27 04:01:36 -05:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
item = json_object_new_object();
|
|
|
|
|
|
|
|
safe_json_add_string(item, "id", dbgri->persistentid);
|
|
|
|
safe_json_add_string(item, "name", dbgri->itemname);
|
|
|
|
safe_json_add_string(item, "name_sort", dbgri->itemname_sort);
|
|
|
|
safe_json_add_int_from_string(item, "album_count", dbgri->groupalbumcount);
|
|
|
|
safe_json_add_int_from_string(item, "track_count", dbgri->itemcount);
|
|
|
|
safe_json_add_int_from_string(item, "length_ms", dbgri->song_length);
|
|
|
|
|
2020-09-01 14:37:10 -04:00
|
|
|
safe_json_add_time_from_string(item, "time_played", dbgri->time_played);
|
|
|
|
safe_json_add_time_from_string(item, "time_added", dbgri->time_added);
|
2020-08-30 03:06:57 -04:00
|
|
|
|
|
|
|
ret = safe_atoi32(dbgri->seek, &intval);
|
|
|
|
if (ret == 0)
|
|
|
|
json_object_object_add(item, "in_progress", json_object_new_boolean(intval > 0));
|
|
|
|
|
2020-08-22 06:59:56 -04:00
|
|
|
ret = safe_atoi32(dbgri->media_kind, &intval);
|
|
|
|
if (ret == 0)
|
|
|
|
safe_json_add_string(item, "media_kind", db_media_kind_label(intval));
|
|
|
|
|
|
|
|
ret = safe_atoi32(dbgri->data_kind, &intval);
|
|
|
|
if (ret == 0)
|
|
|
|
safe_json_add_string(item, "data_kind", db_data_kind_label(intval));
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
ret = snprintf(uri, sizeof(uri), "%s:%s:%s", "library", "artist", dbgri->persistentid);
|
|
|
|
if (ret < sizeof(uri))
|
|
|
|
json_object_object_add(item, "uri", json_object_new_string(uri));
|
|
|
|
|
2020-04-28 13:01:25 -04:00
|
|
|
ret = snprintf(artwork_url, sizeof(artwork_url), "./artwork/group/%s", dbgri->id);
|
2018-10-07 15:54:38 -04:00
|
|
|
if (ret < sizeof(artwork_url))
|
|
|
|
json_object_object_add(item, "artwork_url", json_object_new_string(artwork_url));
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
static json_object *
|
|
|
|
album_to_json(struct db_group_info *dbgri)
|
|
|
|
{
|
|
|
|
json_object *item;
|
2020-08-22 06:59:56 -04:00
|
|
|
int intval;
|
2017-12-27 04:01:36 -05:00
|
|
|
char uri[100];
|
2018-10-07 15:54:38 -04:00
|
|
|
char artwork_url[100];
|
2017-12-27 04:01:36 -05:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
item = json_object_new_object();
|
|
|
|
|
|
|
|
safe_json_add_string(item, "id", dbgri->persistentid);
|
|
|
|
safe_json_add_string(item, "name", dbgri->itemname);
|
|
|
|
safe_json_add_string(item, "name_sort", dbgri->itemname_sort);
|
|
|
|
safe_json_add_string(item, "artist", dbgri->songalbumartist);
|
|
|
|
safe_json_add_string(item, "artist_id", dbgri->songartistid);
|
|
|
|
safe_json_add_int_from_string(item, "track_count", dbgri->itemcount);
|
|
|
|
safe_json_add_int_from_string(item, "length_ms", dbgri->song_length);
|
|
|
|
|
2020-09-01 14:37:10 -04:00
|
|
|
safe_json_add_time_from_string(item, "time_played", dbgri->time_played);
|
|
|
|
safe_json_add_time_from_string(item, "time_added", dbgri->time_added);
|
2020-08-30 03:06:57 -04:00
|
|
|
|
|
|
|
ret = safe_atoi32(dbgri->seek, &intval);
|
|
|
|
if (ret == 0)
|
|
|
|
json_object_object_add(item, "in_progress", json_object_new_boolean(intval > 0));
|
|
|
|
|
2020-08-22 06:59:56 -04:00
|
|
|
ret = safe_atoi32(dbgri->media_kind, &intval);
|
|
|
|
if (ret == 0)
|
|
|
|
safe_json_add_string(item, "media_kind", db_media_kind_label(intval));
|
|
|
|
|
|
|
|
ret = safe_atoi32(dbgri->data_kind, &intval);
|
|
|
|
if (ret == 0)
|
|
|
|
safe_json_add_string(item, "data_kind", db_data_kind_label(intval));
|
|
|
|
|
2020-09-01 14:37:10 -04:00
|
|
|
safe_json_add_date_from_string(item, "date_released", dbgri->date_released);
|
2020-08-22 06:59:56 -04:00
|
|
|
safe_json_add_int_from_string(item, "year", dbgri->year);
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
ret = snprintf(uri, sizeof(uri), "%s:%s:%s", "library", "album", dbgri->persistentid);
|
|
|
|
if (ret < sizeof(uri))
|
|
|
|
json_object_object_add(item, "uri", json_object_new_string(uri));
|
|
|
|
|
2020-04-28 13:01:25 -04:00
|
|
|
ret = snprintf(artwork_url, sizeof(artwork_url), "./artwork/group/%s", dbgri->id);
|
2018-10-07 15:54:38 -04:00
|
|
|
if (ret < sizeof(artwork_url))
|
|
|
|
json_object_object_add(item, "artwork_url", json_object_new_string(artwork_url));
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
static json_object *
|
|
|
|
track_to_json(struct db_media_file_info *dbmfi)
|
|
|
|
{
|
|
|
|
json_object *item;
|
|
|
|
char uri[100];
|
2018-10-07 15:54:38 -04:00
|
|
|
char artwork_url[100];
|
2017-12-27 04:01:36 -05:00
|
|
|
int intval;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
item = json_object_new_object();
|
|
|
|
|
|
|
|
safe_json_add_int_from_string(item, "id", dbmfi->id);
|
|
|
|
safe_json_add_string(item, "title", dbmfi->title);
|
2018-12-15 03:01:36 -05:00
|
|
|
safe_json_add_string(item, "title_sort", dbmfi->title_sort);
|
2017-12-27 04:01:36 -05:00
|
|
|
safe_json_add_string(item, "artist", dbmfi->artist);
|
|
|
|
safe_json_add_string(item, "artist_sort", dbmfi->artist_sort);
|
|
|
|
safe_json_add_string(item, "album", dbmfi->album);
|
|
|
|
safe_json_add_string(item, "album_sort", dbmfi->album_sort);
|
2018-03-17 15:20:29 -04:00
|
|
|
safe_json_add_string(item, "album_id", dbmfi->songalbumid);
|
2017-12-27 04:01:36 -05:00
|
|
|
safe_json_add_string(item, "album_artist", dbmfi->album_artist);
|
|
|
|
safe_json_add_string(item, "album_artist_sort", dbmfi->album_artist_sort);
|
2018-03-17 15:20:29 -04:00
|
|
|
safe_json_add_string(item, "album_artist_id", dbmfi->songartistid);
|
2018-11-23 09:52:30 -05:00
|
|
|
safe_json_add_string(item, "composer", dbmfi->composer);
|
2017-12-27 04:01:36 -05:00
|
|
|
safe_json_add_string(item, "genre", dbmfi->genre);
|
|
|
|
safe_json_add_int_from_string(item, "year", dbmfi->year);
|
|
|
|
safe_json_add_int_from_string(item, "track_number", dbmfi->track);
|
|
|
|
safe_json_add_int_from_string(item, "disc_number", dbmfi->disc);
|
|
|
|
safe_json_add_int_from_string(item, "length_ms", dbmfi->song_length);
|
|
|
|
|
2018-12-15 03:01:36 -05:00
|
|
|
safe_json_add_int_from_string(item, "rating", dbmfi->rating);
|
2017-12-27 04:01:36 -05:00
|
|
|
safe_json_add_int_from_string(item, "play_count", dbmfi->play_count);
|
2018-08-18 16:56:27 -04:00
|
|
|
safe_json_add_int_from_string(item, "skip_count", dbmfi->skip_count);
|
2020-09-01 14:37:10 -04:00
|
|
|
safe_json_add_time_from_string(item, "time_played", dbmfi->time_played);
|
|
|
|
safe_json_add_time_from_string(item, "time_skipped", dbmfi->time_skipped);
|
|
|
|
safe_json_add_time_from_string(item, "time_added", dbmfi->time_added);
|
|
|
|
safe_json_add_date_from_string(item, "date_released", dbmfi->date_released);
|
2018-04-22 02:40:23 -04:00
|
|
|
safe_json_add_int_from_string(item, "seek_ms", dbmfi->seek);
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2019-06-04 17:02:15 -04:00
|
|
|
safe_json_add_string(item, "type", dbmfi->type);
|
|
|
|
safe_json_add_int_from_string(item, "samplerate", dbmfi->samplerate);
|
|
|
|
safe_json_add_int_from_string(item, "bitrate", dbmfi->bitrate);
|
|
|
|
safe_json_add_int_from_string(item, "channels", dbmfi->channels);
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
ret = safe_atoi32(dbmfi->media_kind, &intval);
|
|
|
|
if (ret == 0)
|
|
|
|
safe_json_add_string(item, "media_kind", db_media_kind_label(intval));
|
|
|
|
|
|
|
|
ret = safe_atoi32(dbmfi->data_kind, &intval);
|
|
|
|
if (ret == 0)
|
|
|
|
safe_json_add_string(item, "data_kind", db_data_kind_label(intval));
|
|
|
|
|
|
|
|
safe_json_add_string(item, "path", dbmfi->path);
|
|
|
|
|
|
|
|
ret = snprintf(uri, sizeof(uri), "%s:%s:%s", "library", "track", dbmfi->id);
|
|
|
|
if (ret < sizeof(uri))
|
|
|
|
json_object_object_add(item, "uri", json_object_new_string(uri));
|
|
|
|
|
2018-10-07 15:54:38 -04:00
|
|
|
ret = snprintf(artwork_url, sizeof(artwork_url), "/artwork/item/%s", dbmfi->id);
|
|
|
|
if (ret < sizeof(artwork_url))
|
|
|
|
json_object_object_add(item, "artwork_url", json_object_new_string(artwork_url));
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
static json_object *
|
|
|
|
playlist_to_json(struct db_playlist_info *dbpli)
|
|
|
|
{
|
|
|
|
json_object *item;
|
|
|
|
char uri[100];
|
|
|
|
int intval;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
item = json_object_new_object();
|
|
|
|
|
|
|
|
safe_json_add_int_from_string(item, "id", dbpli->id);
|
|
|
|
safe_json_add_string(item, "name", dbpli->title);
|
|
|
|
safe_json_add_string(item, "path", dbpli->path);
|
2020-02-23 05:40:39 -05:00
|
|
|
safe_json_add_string(item, "parent_id", dbpli->parent_id);
|
2017-12-27 04:01:36 -05:00
|
|
|
ret = safe_atoi32(dbpli->type, &intval);
|
|
|
|
if (ret == 0)
|
2020-02-23 05:40:39 -05:00
|
|
|
{
|
|
|
|
safe_json_add_string(item, "type", db_pl_type_label(intval));
|
|
|
|
json_object_object_add(item, "smart_playlist", json_object_new_boolean(intval == PL_SMART));
|
|
|
|
json_object_object_add(item, "folder", json_object_new_boolean(intval == PL_FOLDER));
|
|
|
|
}
|
2017-12-27 04:01:36 -05:00
|
|
|
|
|
|
|
ret = snprintf(uri, sizeof(uri), "%s:%s:%s", "library", "playlist", dbpli->id);
|
|
|
|
if (ret < sizeof(uri))
|
|
|
|
json_object_object_add(item, "uri", json_object_new_string(uri));
|
|
|
|
|
|
|
|
return item;
|
2017-11-04 16:24:42 -04:00
|
|
|
}
|
|
|
|
|
2018-09-02 03:05:18 -04:00
|
|
|
static json_object *
|
|
|
|
genre_to_json(const char *genre)
|
|
|
|
{
|
|
|
|
json_object *item;
|
|
|
|
|
|
|
|
if (genre == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
item = json_object_new_object();
|
|
|
|
safe_json_add_string(item, "name", genre);
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2018-11-24 06:15:31 -05:00
|
|
|
static json_object *
|
|
|
|
directory_to_json(struct directory_info *directory_info)
|
|
|
|
{
|
|
|
|
json_object *item;
|
|
|
|
|
|
|
|
if (directory_info == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
item = json_object_new_object();
|
|
|
|
safe_json_add_string(item, "path", directory_info->path);
|
|
|
|
// json_object_object_add(item, "id", json_object_new_int(directory_info->id));
|
|
|
|
// json_object_object_add(item, "parent_id", json_object_new_int(directory_info->parent_id));
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2018-09-02 03:05:18 -04:00
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
static int
|
2017-12-27 04:01:36 -05:00
|
|
|
fetch_tracks(struct query_params *query_params, json_object *items, int *total)
|
2017-11-04 16:24:42 -04:00
|
|
|
{
|
2017-12-27 04:01:36 -05:00
|
|
|
struct db_media_file_info dbmfi;
|
|
|
|
json_object *item;
|
2018-09-02 03:05:18 -04:00
|
|
|
int ret;
|
2017-11-04 16:24:42 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
ret = db_query_start(query_params);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
2017-11-04 16:24:42 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
while (((ret = db_query_fetch_file(query_params, &dbmfi)) == 0) && (dbmfi.id))
|
|
|
|
{
|
|
|
|
item = track_to_json(&dbmfi);
|
|
|
|
if (!item)
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
goto error;
|
|
|
|
}
|
2017-11-04 16:24:42 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
json_object_array_add(items, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (total)
|
|
|
|
*total = query_params->results;
|
|
|
|
|
|
|
|
error:
|
|
|
|
db_query_end(query_params);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
fetch_artists(struct query_params *query_params, json_object *items, int *total)
|
|
|
|
{
|
|
|
|
struct db_group_info dbgri;
|
|
|
|
json_object *item;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ret = db_query_start(query_params);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
while ((ret = db_query_fetch_group(query_params, &dbgri)) == 0)
|
2017-11-04 16:24:42 -04:00
|
|
|
{
|
2017-12-27 04:01:36 -05:00
|
|
|
/* Don't add item if no name (eg blank album name) */
|
|
|
|
if (strlen(dbgri.itemname) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
item = artist_to_json(&dbgri);
|
|
|
|
if (!item)
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_array_add(items, item);
|
2017-11-04 16:24:42 -04:00
|
|
|
}
|
2017-12-27 04:01:36 -05:00
|
|
|
|
|
|
|
if (total)
|
|
|
|
*total = query_params->results;
|
|
|
|
|
|
|
|
error:
|
|
|
|
db_query_end(query_params);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-03-17 15:20:29 -04:00
|
|
|
static json_object *
|
|
|
|
fetch_artist(const char *artist_id)
|
|
|
|
{
|
|
|
|
struct query_params query_params;
|
|
|
|
json_object *artist;
|
|
|
|
struct db_group_info dbgri;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
|
|
|
artist = NULL;
|
|
|
|
|
|
|
|
query_params.type = Q_GROUP_ARTISTS;
|
|
|
|
query_params.sort = S_ARTIST;
|
|
|
|
query_params.filter = db_mprintf("(f.songartistid = %s)", artist_id);
|
|
|
|
|
|
|
|
ret = db_query_start(&query_params);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if ((ret = db_query_fetch_group(&query_params, &dbgri)) == 0)
|
|
|
|
{
|
|
|
|
artist = artist_to_json(&dbgri);
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
db_query_end(&query_params);
|
2018-09-02 03:05:18 -04:00
|
|
|
free(query_params.filter);
|
2018-03-17 15:20:29 -04:00
|
|
|
|
|
|
|
return artist;
|
|
|
|
}
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
static int
|
|
|
|
fetch_albums(struct query_params *query_params, json_object *items, int *total)
|
|
|
|
{
|
|
|
|
struct db_group_info dbgri;
|
|
|
|
json_object *item;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ret = db_query_start(query_params);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
while ((ret = db_query_fetch_group(query_params, &dbgri)) == 0)
|
2017-11-04 16:24:42 -04:00
|
|
|
{
|
2017-12-27 04:01:36 -05:00
|
|
|
/* Don't add item if no name (eg blank album name) */
|
|
|
|
if (strlen(dbgri.itemname) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
item = album_to_json(&dbgri);
|
|
|
|
if (!item)
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_array_add(items, item);
|
2017-11-04 16:24:42 -04:00
|
|
|
}
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
if (total)
|
|
|
|
*total = query_params->results;
|
2017-11-04 16:24:42 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
error:
|
|
|
|
db_query_end(query_params);
|
2017-11-04 16:24:42 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-03-17 15:20:29 -04:00
|
|
|
static json_object *
|
|
|
|
fetch_album(const char *album_id)
|
|
|
|
{
|
|
|
|
struct query_params query_params;
|
|
|
|
json_object *album;
|
|
|
|
struct db_group_info dbgri;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
|
|
|
album = NULL;
|
|
|
|
|
|
|
|
query_params.type = Q_GROUP_ALBUMS;
|
|
|
|
query_params.sort = S_ALBUM;
|
|
|
|
query_params.filter = db_mprintf("(f.songalbumid = %s)", album_id);
|
|
|
|
|
|
|
|
ret = db_query_start(&query_params);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if ((ret = db_query_fetch_group(&query_params, &dbgri)) == 0)
|
|
|
|
{
|
|
|
|
album = album_to_json(&dbgri);
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
db_query_end(&query_params);
|
2018-09-02 03:05:18 -04:00
|
|
|
free(query_params.filter);
|
2018-03-17 15:20:29 -04:00
|
|
|
|
|
|
|
return album;
|
|
|
|
}
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
static int
|
|
|
|
fetch_playlists(struct query_params *query_params, json_object *items, int *total)
|
|
|
|
{
|
|
|
|
struct db_playlist_info dbpli;
|
|
|
|
json_object *item;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ret = db_query_start(query_params);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
2019-05-28 17:40:41 -04:00
|
|
|
while (((ret = db_query_fetch_pl(query_params, &dbpli)) == 0) && (dbpli.id))
|
2017-12-27 04:01:36 -05:00
|
|
|
{
|
|
|
|
item = playlist_to_json(&dbpli);
|
|
|
|
if (!item)
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_array_add(items, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (total)
|
|
|
|
*total = query_params->results;
|
|
|
|
|
|
|
|
error:
|
|
|
|
db_query_end(query_params);
|
|
|
|
|
|
|
|
return ret;
|
2017-11-04 16:24:42 -04:00
|
|
|
}
|
|
|
|
|
2018-03-17 15:20:29 -04:00
|
|
|
static json_object *
|
2020-03-08 06:55:33 -04:00
|
|
|
fetch_playlist(uint32_t playlist_id)
|
2018-03-17 15:20:29 -04:00
|
|
|
{
|
|
|
|
struct query_params query_params;
|
|
|
|
json_object *playlist;
|
|
|
|
struct db_playlist_info dbpli;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
|
|
|
playlist = NULL;
|
|
|
|
|
|
|
|
query_params.type = Q_PL;
|
|
|
|
query_params.sort = S_PLAYLIST;
|
2020-03-08 06:55:33 -04:00
|
|
|
query_params.filter = db_mprintf("(f.id = %d)", playlist_id);
|
2018-03-17 15:20:29 -04:00
|
|
|
|
|
|
|
ret = db_query_start(&query_params);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
2019-05-28 17:40:41 -04:00
|
|
|
if (((ret = db_query_fetch_pl(&query_params, &dbpli)) == 0) && (dbpli.id))
|
2018-03-17 15:20:29 -04:00
|
|
|
{
|
|
|
|
playlist = playlist_to_json(&dbpli);
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
db_query_end(&query_params);
|
2018-09-02 03:05:18 -04:00
|
|
|
free(query_params.filter);
|
2018-03-17 15:20:29 -04:00
|
|
|
|
|
|
|
return playlist;
|
|
|
|
}
|
|
|
|
|
2018-09-02 03:05:18 -04:00
|
|
|
static int
|
|
|
|
fetch_genres(struct query_params *query_params, json_object *items, int *total)
|
|
|
|
{
|
|
|
|
json_object *item;
|
|
|
|
int ret;
|
|
|
|
char *genre;
|
|
|
|
char *sort_item;
|
|
|
|
|
|
|
|
ret = db_query_start(query_params);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
while (((ret = db_query_fetch_string_sort(query_params, &genre, &sort_item)) == 0) && (genre))
|
|
|
|
{
|
|
|
|
item = genre_to_json(genre);
|
|
|
|
if (!item)
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_array_add(items, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (total)
|
|
|
|
*total = query_params->results;
|
|
|
|
|
|
|
|
error:
|
|
|
|
db_query_end(query_params);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-11-24 06:15:31 -05:00
|
|
|
static int
|
|
|
|
fetch_directories(int parent_id, json_object *items)
|
|
|
|
{
|
|
|
|
json_object *item;
|
|
|
|
int ret;
|
|
|
|
struct directory_info subdir;
|
|
|
|
struct directory_enum dir_enum;
|
|
|
|
|
|
|
|
memset(&dir_enum, 0, sizeof(struct directory_enum));
|
|
|
|
dir_enum.parent_id = parent_id;
|
|
|
|
ret = db_directory_enum_start(&dir_enum);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
while ((ret = db_directory_enum_fetch(&dir_enum, &subdir)) == 0 && subdir.id > 0)
|
|
|
|
{
|
|
|
|
item = directory_to_json(&subdir);
|
|
|
|
if (!item)
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_array_add(items, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
db_directory_enum_end(&dir_enum);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-09-02 03:05:18 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
static int
|
|
|
|
query_params_limit_set(struct query_params *query_params, struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
const char *param;
|
|
|
|
|
|
|
|
query_params->idx_type = I_NONE;
|
|
|
|
query_params->limit = -1;
|
|
|
|
query_params->offset = 0;
|
|
|
|
|
|
|
|
param = evhttp_find_header(hreq->query, "limit");
|
|
|
|
if (param)
|
|
|
|
{
|
|
|
|
query_params->idx_type = I_SUB;
|
|
|
|
|
|
|
|
if (safe_atoi32(param, &query_params->limit) < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid value for query parameter 'limit' (%s)\n", param);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
param = evhttp_find_header(hreq->query, "offset");
|
|
|
|
if (param && safe_atoi32(param, &query_params->offset) < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid value for query parameter 'offset' (%s)\n", param);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-11-04 16:24:42 -04:00
|
|
|
|
|
|
|
/* --------------------------- REPLY HANDLERS ------------------------------- */
|
2017-08-12 13:42:06 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Endpoint to retrieve configuration values
|
|
|
|
*
|
|
|
|
* Example response:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* "websocket_port": 6603,
|
|
|
|
* "version": "25.0"
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
static int
|
2017-11-07 18:32:03 -05:00
|
|
|
jsonapi_reply_config(struct httpd_request *hreq)
|
2017-08-12 13:42:06 -04:00
|
|
|
{
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object *jreply;
|
2017-08-12 13:42:06 -04:00
|
|
|
json_object *buildopts;
|
|
|
|
int websocket_port;
|
|
|
|
char **buildoptions;
|
2018-12-27 09:29:25 -05:00
|
|
|
cfg_t *lib;
|
|
|
|
int ndirs;
|
|
|
|
char *path;
|
2019-01-28 08:46:36 -05:00
|
|
|
char *deref;
|
2018-12-27 09:29:25 -05:00
|
|
|
json_object *directories;
|
2017-08-12 13:42:06 -04:00
|
|
|
int i;
|
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
CHECK_NULL(L_WEB, jreply = json_object_new_object());
|
2017-08-12 13:42:06 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
// library name
|
|
|
|
json_object_object_add(jreply, "library_name", json_object_new_string(cfg_getstr(cfg_getsec(cfg, "library"), "name")));
|
|
|
|
|
|
|
|
// hide singles
|
|
|
|
json_object_object_add(jreply, "hide_singles", json_object_new_boolean(cfg_getbool(cfg_getsec(cfg, "library"), "hide_singles")));
|
|
|
|
|
2017-08-12 13:42:06 -04:00
|
|
|
// Websocket port
|
2017-09-15 19:25:47 -04:00
|
|
|
#ifdef HAVE_LIBWEBSOCKETS
|
2017-08-12 13:42:06 -04:00
|
|
|
websocket_port = cfg_getint(cfg_getsec(cfg, "general"), "websocket_port");
|
|
|
|
#else
|
|
|
|
websocket_port = 0;
|
|
|
|
#endif
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object_object_add(jreply, "websocket_port", json_object_new_int(websocket_port));
|
2017-08-12 13:42:06 -04:00
|
|
|
|
|
|
|
// forked-daapd version
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object_object_add(jreply, "version", json_object_new_string(VERSION));
|
2017-08-12 13:42:06 -04:00
|
|
|
|
|
|
|
// enabled build options
|
|
|
|
buildopts = json_object_new_array();
|
|
|
|
buildoptions = buildopts_get();
|
|
|
|
for (i = 0; buildoptions[i]; i++)
|
|
|
|
{
|
|
|
|
json_object_array_add(buildopts, json_object_new_string(buildoptions[i]));
|
|
|
|
}
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object_object_add(jreply, "buildoptions", buildopts);
|
2017-08-12 13:42:06 -04:00
|
|
|
|
2018-12-27 09:29:25 -05:00
|
|
|
// Library directories
|
|
|
|
lib = cfg_getsec(cfg, "library");
|
|
|
|
ndirs = cfg_size(lib, "directories");
|
|
|
|
directories = json_object_new_array();
|
|
|
|
for (i = 0; i < ndirs; i++)
|
|
|
|
{
|
|
|
|
path = cfg_getnstr(lib, "directories", i);
|
2019-01-28 08:46:36 -05:00
|
|
|
|
|
|
|
// The path in the conf file may have a trailing slash character. Return the realpath like it is done in the bulk_scan function in filescanner.c
|
|
|
|
deref = realpath(path, NULL);
|
|
|
|
if (deref)
|
|
|
|
{
|
|
|
|
json_object_array_add(directories, json_object_new_string(deref));
|
|
|
|
free(deref);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Skipping library directory %s, could not dereference: %s\n", path, strerror(errno));
|
|
|
|
}
|
2018-12-27 09:29:25 -05:00
|
|
|
}
|
|
|
|
json_object_object_add(jreply, "directories", directories);
|
|
|
|
|
2019-05-30 02:46:36 -04:00
|
|
|
// Config for creating/modifying stored playlists
|
2019-06-01 02:02:46 -04:00
|
|
|
json_object_object_add(jreply, "allow_modifying_stored_playlists", json_object_new_boolean(allow_modifying_stored_playlists));
|
|
|
|
safe_json_add_string(jreply, "default_playlist_directory", default_playlist_directory);
|
2019-05-30 02:46:36 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERRNO(L_WEB, evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(jreply)));
|
2017-11-04 16:24:42 -04:00
|
|
|
|
|
|
|
jparse_free(jreply);
|
2017-08-12 13:42:06 -04:00
|
|
|
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_OK;
|
2017-08-12 13:42:06 -04:00
|
|
|
}
|
|
|
|
|
2019-07-07 03:05:49 -04:00
|
|
|
static json_object *
|
|
|
|
option_get_json(struct settings_option *option)
|
|
|
|
{
|
|
|
|
const char *optionname;
|
|
|
|
json_object *json_option;
|
|
|
|
int intval;
|
|
|
|
bool boolval;
|
|
|
|
char *strval;
|
|
|
|
|
|
|
|
|
|
|
|
optionname = option->name;
|
|
|
|
|
|
|
|
CHECK_NULL(L_WEB, json_option = json_object_new_object());
|
|
|
|
json_object_object_add(json_option, "name", json_object_new_string(option->name));
|
|
|
|
json_object_object_add(json_option, "type", json_object_new_int(option->type));
|
|
|
|
|
|
|
|
if (option->type == SETTINGS_TYPE_INT)
|
|
|
|
{
|
|
|
|
intval = settings_option_getint(option);
|
|
|
|
json_object_object_add(json_option, "value", json_object_new_int(intval));
|
|
|
|
}
|
|
|
|
else if (option->type == SETTINGS_TYPE_BOOL)
|
|
|
|
{
|
|
|
|
boolval = settings_option_getbool(option);
|
|
|
|
json_object_object_add(json_option, "value", json_object_new_boolean(boolval));
|
|
|
|
}
|
|
|
|
else if (option->type == SETTINGS_TYPE_STR)
|
|
|
|
{
|
|
|
|
strval = settings_option_getstr(option);
|
|
|
|
if (strval)
|
|
|
|
{
|
|
|
|
json_object_object_add(json_option, "value", json_object_new_string(strval));
|
|
|
|
free(strval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Option '%s' has unknown type %d\n", optionname, option->type);
|
|
|
|
jparse_free(json_option);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return json_option;
|
|
|
|
}
|
|
|
|
|
|
|
|
static json_object *
|
|
|
|
category_get_json(struct settings_category *category)
|
|
|
|
{
|
|
|
|
json_object *json_category;
|
|
|
|
json_object *json_options;
|
|
|
|
json_object *json_option;
|
|
|
|
struct settings_option *option;
|
|
|
|
int count;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
json_category = json_object_new_object();
|
|
|
|
|
|
|
|
json_object_object_add(json_category, "name", json_object_new_string(category->name));
|
|
|
|
|
|
|
|
json_options = json_object_new_array();
|
|
|
|
|
|
|
|
count = settings_option_count(category);
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
option = settings_option_get_byindex(category, i);
|
|
|
|
json_option = option_get_json(option);
|
|
|
|
if (json_option)
|
|
|
|
json_object_array_add(json_options, json_option);
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_object_add(json_category, "options", json_options);
|
|
|
|
|
|
|
|
return json_category;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jsonapi_reply_settings_get(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
struct settings_category *category;
|
|
|
|
json_object *jreply;
|
|
|
|
json_object *json_categories;
|
|
|
|
json_object *json_category;
|
|
|
|
int count;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
CHECK_NULL(L_WEB, jreply = json_object_new_object());
|
|
|
|
|
|
|
|
json_categories = json_object_new_array();
|
|
|
|
|
|
|
|
count = settings_categories_count();
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
category = settings_category_get_byindex(i);
|
|
|
|
json_category = category_get_json(category);
|
|
|
|
if (json_category)
|
|
|
|
json_object_array_add(json_categories, json_category);
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_object_add(jreply, "categories", json_categories);
|
|
|
|
|
|
|
|
CHECK_ERRNO(L_WEB, evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(jreply)));
|
|
|
|
|
|
|
|
jparse_free(jreply);
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jsonapi_reply_settings_category_get(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
const char *categoryname;
|
|
|
|
struct settings_category *category;
|
|
|
|
json_object *jreply;
|
|
|
|
|
|
|
|
|
|
|
|
categoryname = hreq->uri_parsed->path_parts[2];
|
|
|
|
|
|
|
|
category = settings_category_get(categoryname);
|
|
|
|
if (!category)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid category name '%s' given\n", categoryname);
|
|
|
|
return HTTP_NOTFOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
jreply = category_get_json(category);
|
|
|
|
|
|
|
|
if (!jreply)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Error getting value for category '%s'\n", categoryname);
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_ERRNO(L_WEB, evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(jreply)));
|
|
|
|
|
|
|
|
jparse_free(jreply);
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jsonapi_reply_settings_option_get(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
const char *categoryname;
|
|
|
|
const char *optionname;
|
|
|
|
struct settings_category *category;
|
|
|
|
struct settings_option *option;
|
|
|
|
json_object *jreply;
|
|
|
|
|
|
|
|
|
|
|
|
categoryname = hreq->uri_parsed->path_parts[2];
|
|
|
|
optionname = hreq->uri_parsed->path_parts[3];
|
|
|
|
|
|
|
|
category = settings_category_get(categoryname);
|
|
|
|
if (!category)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid category name '%s' given\n", categoryname);
|
|
|
|
return HTTP_NOTFOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
option = settings_option_get(category, optionname);
|
|
|
|
if (!option)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid option name '%s' given\n", optionname);
|
|
|
|
return HTTP_NOTFOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
jreply = option_get_json(option);
|
|
|
|
|
|
|
|
if (!jreply)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Error getting value for option '%s'\n", optionname);
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_ERRNO(L_WEB, evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(jreply)));
|
|
|
|
|
|
|
|
jparse_free(jreply);
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jsonapi_reply_settings_option_put(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
const char *categoryname;
|
|
|
|
const char *optionname;
|
|
|
|
struct settings_category *category;
|
|
|
|
struct settings_option *option;
|
|
|
|
struct evbuffer *in_evbuf;
|
|
|
|
json_object* request;
|
|
|
|
int intval;
|
|
|
|
bool boolval;
|
|
|
|
const char *strval;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
|
|
categoryname = hreq->uri_parsed->path_parts[2];
|
|
|
|
optionname = hreq->uri_parsed->path_parts[3];
|
|
|
|
|
|
|
|
category = settings_category_get(categoryname);
|
|
|
|
if (!category)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid category name '%s' given\n", categoryname);
|
|
|
|
return HTTP_NOTFOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
option = settings_option_get(category, optionname);
|
|
|
|
|
|
|
|
if (!option)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid option name '%s' given\n", optionname);
|
|
|
|
return HTTP_NOTFOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
in_evbuf = evhttp_request_get_input_buffer(hreq->req);
|
|
|
|
request = jparse_obj_from_evbuffer(in_evbuf);
|
|
|
|
if (!request)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Missing request body for setting option '%s' (type %d)\n", optionname, option->type);
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (option->type == SETTINGS_TYPE_INT && jparse_contains_key(request, "value", json_type_int))
|
|
|
|
{
|
|
|
|
intval = jparse_int_from_obj(request, "value");
|
|
|
|
ret = settings_option_setint(option, intval);
|
|
|
|
}
|
|
|
|
else if (option->type == SETTINGS_TYPE_BOOL && jparse_contains_key(request, "value", json_type_boolean))
|
|
|
|
{
|
|
|
|
boolval = jparse_bool_from_obj(request, "value");
|
|
|
|
ret = settings_option_setbool(option, boolval);
|
|
|
|
}
|
|
|
|
else if (option->type == SETTINGS_TYPE_STR && jparse_contains_key(request, "value", json_type_string))
|
|
|
|
{
|
|
|
|
strval = jparse_str_from_obj(request, "value");
|
|
|
|
ret = settings_option_setstr(option, strval);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid value given for option '%s' (type %d): '%s'\n", optionname, option->type, json_object_to_json_string(request));
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Error changing setting '%s' (type %d) to '%s'\n", optionname, option->type, json_object_to_json_string(request));
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTF(E_INFO, L_WEB, "Setting option '%s.%s' changed to '%s'\n", categoryname, optionname, json_object_to_json_string(request));
|
|
|
|
return HTTP_NOCONTENT;
|
|
|
|
}
|
|
|
|
|
2020-05-15 18:04:37 -04:00
|
|
|
static int
|
|
|
|
jsonapi_reply_settings_option_delete(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
const char *categoryname;
|
|
|
|
const char *optionname;
|
|
|
|
struct settings_category *category;
|
|
|
|
struct settings_option *option;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
|
|
categoryname = hreq->uri_parsed->path_parts[2];
|
|
|
|
optionname = hreq->uri_parsed->path_parts[3];
|
|
|
|
|
|
|
|
category = settings_category_get(categoryname);
|
|
|
|
if (!category)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid category name '%s' given\n", categoryname);
|
|
|
|
return HTTP_NOTFOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
option = settings_option_get(category, optionname);
|
|
|
|
if (!option)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid option name '%s' given\n", optionname);
|
|
|
|
return HTTP_NOTFOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = settings_option_delete(option);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Error deleting option '%s'\n", optionname);
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HTTP_NOCONTENT;
|
|
|
|
}
|
|
|
|
|
2017-08-12 13:42:06 -04:00
|
|
|
/*
|
|
|
|
* Endpoint to retrieve informations about the library
|
|
|
|
*
|
|
|
|
* Example response:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* "artists": 84,
|
|
|
|
* "albums": 151,
|
|
|
|
* "songs": 3085,
|
|
|
|
* "db_playtime": 687824,
|
|
|
|
* "updating": false
|
|
|
|
*}
|
|
|
|
*/
|
|
|
|
static int
|
2017-11-07 18:32:03 -05:00
|
|
|
jsonapi_reply_library(struct httpd_request *hreq)
|
2017-08-12 13:42:06 -04:00
|
|
|
{
|
|
|
|
struct query_params qp;
|
|
|
|
struct filecount_info fci;
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object *jreply;
|
2017-08-12 13:42:06 -04:00
|
|
|
int ret;
|
2018-09-02 03:05:18 -04:00
|
|
|
char *s;
|
2017-08-12 13:42:06 -04:00
|
|
|
|
2018-04-22 02:40:23 -04:00
|
|
|
|
|
|
|
CHECK_NULL(L_WEB, jreply = json_object_new_object());
|
2017-08-12 13:42:06 -04:00
|
|
|
|
|
|
|
memset(&qp, 0, sizeof(struct query_params));
|
|
|
|
qp.type = Q_COUNT_ITEMS;
|
|
|
|
ret = db_filecount_get(&fci, &qp);
|
2018-04-22 02:40:23 -04:00
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
json_object_object_add(jreply, "songs", json_object_new_int(fci.count));
|
|
|
|
json_object_object_add(jreply, "db_playtime", json_object_new_int64((fci.length / 1000)));
|
|
|
|
json_object_object_add(jreply, "artists", json_object_new_int(fci.artist_count));
|
|
|
|
json_object_object_add(jreply, "albums", json_object_new_int(fci.album_count));
|
|
|
|
}
|
|
|
|
else
|
2017-08-12 13:42:06 -04:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "library: failed to get file count info\n");
|
|
|
|
}
|
|
|
|
|
2020-02-22 04:03:13 -05:00
|
|
|
ret = db_admin_get(&s, DB_ADMIN_START_TIME);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
2020-09-01 14:37:10 -04:00
|
|
|
safe_json_add_time_from_string(jreply, "started_at", s);
|
2020-02-22 04:03:13 -05:00
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = db_admin_get(&s, DB_ADMIN_DB_UPDATE);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
2020-09-01 14:37:10 -04:00
|
|
|
safe_json_add_time_from_string(jreply, "updated_at", s);
|
2020-02-22 04:03:13 -05:00
|
|
|
free(s);
|
|
|
|
}
|
2017-08-12 13:42:06 -04:00
|
|
|
|
2018-04-22 02:40:23 -04:00
|
|
|
json_object_object_add(jreply, "updating", json_object_new_boolean(library_is_scanning()));
|
2017-08-12 13:42:06 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERRNO(L_WEB, evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(jreply)));
|
2017-11-04 16:24:42 -04:00
|
|
|
jparse_free(jreply);
|
2017-08-12 13:42:06 -04:00
|
|
|
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_OK;
|
2017-08-12 13:42:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Endpoint to trigger a library rescan
|
|
|
|
*/
|
|
|
|
static int
|
2017-11-07 18:32:03 -05:00
|
|
|
jsonapi_reply_update(struct httpd_request *hreq)
|
2017-08-12 13:42:06 -04:00
|
|
|
{
|
|
|
|
library_rescan();
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_NOCONTENT;
|
2017-08-12 13:42:06 -04:00
|
|
|
}
|
|
|
|
|
2019-06-08 14:44:51 -04:00
|
|
|
static int
|
2019-06-18 04:36:57 -04:00
|
|
|
jsonapi_reply_meta_rescan(struct httpd_request *hreq)
|
2019-06-08 14:44:51 -04:00
|
|
|
{
|
|
|
|
library_metarescan();
|
|
|
|
return HTTP_NOCONTENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-12 13:42:06 -04:00
|
|
|
/*
|
|
|
|
* Endpoint to retrieve information about the spotify integration
|
|
|
|
*
|
|
|
|
* Exampe response:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* "enabled": true,
|
|
|
|
* "oauth_uri": "https://accounts.spotify.com/authorize/?client_id=...
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
static int
|
2017-11-07 18:32:03 -05:00
|
|
|
jsonapi_reply_spotify(struct httpd_request *hreq)
|
2017-08-12 13:42:06 -04:00
|
|
|
{
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object *jreply;
|
2017-08-12 13:42:06 -04:00
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
CHECK_NULL(L_WEB, jreply = json_object_new_object());
|
2017-08-12 13:42:06 -04:00
|
|
|
|
|
|
|
#ifdef HAVE_SPOTIFY_H
|
2017-09-16 06:37:32 -04:00
|
|
|
int httpd_port;
|
|
|
|
char redirect_uri[256];
|
|
|
|
char *oauth_uri;
|
2017-08-12 13:42:06 -04:00
|
|
|
struct spotify_status_info info;
|
2018-03-02 13:24:35 -05:00
|
|
|
struct spotifywebapi_status_info webapi_info;
|
2018-03-17 07:21:24 -04:00
|
|
|
struct spotifywebapi_access_token webapi_token;
|
2017-08-12 13:42:06 -04:00
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object_object_add(jreply, "enabled", json_object_new_boolean(true));
|
2017-08-12 13:42:06 -04:00
|
|
|
|
|
|
|
httpd_port = cfg_getint(cfg_getsec(cfg, "library"), "port");
|
|
|
|
snprintf(redirect_uri, sizeof(redirect_uri), "http://forked-daapd.local:%d/oauth/spotify", httpd_port);
|
|
|
|
|
|
|
|
oauth_uri = spotifywebapi_oauth_uri_get(redirect_uri);
|
2017-11-04 16:24:42 -04:00
|
|
|
if (!oauth_uri)
|
2017-08-12 13:42:06 -04:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Cannot display Spotify oauth interface (http_form_uriencode() failed)\n");
|
2017-11-04 16:24:42 -04:00
|
|
|
jparse_free(jreply);
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_INTERNAL;
|
2017-08-12 13:42:06 -04:00
|
|
|
}
|
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object_object_add(jreply, "oauth_uri", json_object_new_string(oauth_uri));
|
|
|
|
free(oauth_uri);
|
|
|
|
|
2017-08-12 13:42:06 -04:00
|
|
|
spotify_status_info_get(&info);
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object_object_add(jreply, "libspotify_installed", json_object_new_boolean(info.libspotify_installed));
|
|
|
|
json_object_object_add(jreply, "libspotify_logged_in", json_object_new_boolean(info.libspotify_logged_in));
|
2018-03-17 07:21:24 -04:00
|
|
|
safe_json_add_string(jreply, "libspotify_user", info.libspotify_user);
|
2018-03-02 13:24:35 -05:00
|
|
|
|
|
|
|
spotifywebapi_status_info_get(&webapi_info);
|
|
|
|
json_object_object_add(jreply, "webapi_token_valid", json_object_new_boolean(webapi_info.token_valid));
|
2018-03-17 07:21:24 -04:00
|
|
|
safe_json_add_string(jreply, "webapi_user", webapi_info.user);
|
|
|
|
safe_json_add_string(jreply, "webapi_country", webapi_info.country);
|
2019-08-03 06:55:53 -04:00
|
|
|
safe_json_add_string(jreply, "webapi_granted_scope", webapi_info.granted_scope);
|
|
|
|
safe_json_add_string(jreply, "webapi_required_scope", webapi_info.required_scope);
|
2018-03-17 07:21:24 -04:00
|
|
|
|
|
|
|
spotifywebapi_access_token_get(&webapi_token);
|
|
|
|
safe_json_add_string(jreply, "webapi_token", webapi_token.token);
|
|
|
|
json_object_object_add(jreply, "webapi_token_expires_in", json_object_new_int(webapi_token.expires_in));
|
2020-02-14 16:10:15 -05:00
|
|
|
free(webapi_token.token);
|
2017-08-12 13:42:06 -04:00
|
|
|
#else
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object_object_add(jreply, "enabled", json_object_new_boolean(false));
|
2017-08-12 13:42:06 -04:00
|
|
|
#endif
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERRNO(L_WEB, evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(jreply)));
|
2017-11-04 16:24:42 -04:00
|
|
|
|
|
|
|
jparse_free(jreply);
|
2017-08-12 13:42:06 -04:00
|
|
|
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_OK;
|
2017-08-12 13:42:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-11-07 18:32:03 -05:00
|
|
|
jsonapi_reply_spotify_login(struct httpd_request *hreq)
|
2017-08-12 13:42:06 -04:00
|
|
|
{
|
2017-09-16 06:37:32 -04:00
|
|
|
#ifdef HAVE_SPOTIFY_H
|
2017-08-12 13:42:06 -04:00
|
|
|
struct evbuffer *in_evbuf;
|
|
|
|
json_object* request;
|
|
|
|
const char *user;
|
|
|
|
const char *password;
|
|
|
|
char *errmsg = NULL;
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object* jreply;
|
2017-08-12 13:42:06 -04:00
|
|
|
json_object* errors;
|
|
|
|
int ret;
|
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
DPRINTF(E_DBG, L_WEB, "Received Spotify login request\n");
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
in_evbuf = evhttp_request_get_input_buffer(hreq->req);
|
2017-08-12 13:42:06 -04:00
|
|
|
|
|
|
|
request = jparse_obj_from_evbuffer(in_evbuf);
|
|
|
|
if (!request)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Failed to parse incoming request\n");
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_BADREQUEST;
|
2017-08-12 13:42:06 -04:00
|
|
|
}
|
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
CHECK_NULL(L_WEB, jreply = json_object_new_object());
|
2017-08-12 13:42:06 -04:00
|
|
|
|
|
|
|
user = jparse_str_from_obj(request, "user");
|
|
|
|
password = jparse_str_from_obj(request, "password");
|
|
|
|
if (user && strlen(user) > 0 && password && strlen(password) > 0)
|
|
|
|
{
|
|
|
|
ret = spotify_login_user(user, password, &errmsg);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object_object_add(jreply, "success", json_object_new_boolean(false));
|
2017-08-12 13:42:06 -04:00
|
|
|
errors = json_object_new_object();
|
|
|
|
json_object_object_add(errors, "error", json_object_new_string(errmsg));
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object_object_add(jreply, "errors", errors);
|
2017-08-12 13:42:06 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object_object_add(jreply, "success", json_object_new_boolean(true));
|
2017-08-12 13:42:06 -04:00
|
|
|
}
|
|
|
|
free(errmsg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "No user or password in spotify login post request\n");
|
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object_object_add(jreply, "success", json_object_new_boolean(false));
|
2017-08-12 13:42:06 -04:00
|
|
|
errors = json_object_new_object();
|
|
|
|
if (!user || strlen(user) == 0)
|
|
|
|
json_object_object_add(errors, "user", json_object_new_string("Username is required"));
|
|
|
|
if (!password || strlen(password) == 0)
|
|
|
|
json_object_object_add(errors, "password", json_object_new_string("Password is required"));
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object_object_add(jreply, "errors", errors);
|
2017-08-12 13:42:06 -04:00
|
|
|
}
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERRNO(L_WEB, evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(jreply)));
|
2017-11-04 16:24:42 -04:00
|
|
|
|
|
|
|
jparse_free(jreply);
|
2017-08-12 13:42:06 -04:00
|
|
|
|
|
|
|
#else
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Received spotify login request but was not compiled with enable-spotify\n");
|
|
|
|
#endif
|
|
|
|
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_OK;
|
2017-08-12 13:42:06 -04:00
|
|
|
}
|
|
|
|
|
2020-06-29 16:20:29 -04:00
|
|
|
static int
|
|
|
|
jsonapi_reply_spotify_logout(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_SPOTIFY_H
|
|
|
|
spotify_logout();
|
|
|
|
#endif
|
|
|
|
return HTTP_NOCONTENT;
|
|
|
|
}
|
|
|
|
|
2017-09-24 00:57:49 -04:00
|
|
|
static int
|
2017-11-07 18:32:03 -05:00
|
|
|
jsonapi_reply_lastfm(struct httpd_request *hreq)
|
2017-09-24 00:57:49 -04:00
|
|
|
{
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object *jreply;
|
2017-09-24 00:57:49 -04:00
|
|
|
bool enabled = false;
|
|
|
|
bool scrobbling_enabled = false;
|
|
|
|
|
|
|
|
#ifdef LASTFM
|
|
|
|
enabled = true;
|
|
|
|
scrobbling_enabled = lastfm_is_enabled();
|
|
|
|
#endif
|
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
CHECK_NULL(L_WEB, jreply = json_object_new_object());
|
2017-09-24 00:57:49 -04:00
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object_object_add(jreply, "enabled", json_object_new_boolean(enabled));
|
|
|
|
json_object_object_add(jreply, "scrobbling_enabled", json_object_new_boolean(scrobbling_enabled));
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERRNO(L_WEB, evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(jreply)));
|
2017-11-04 16:24:42 -04:00
|
|
|
|
|
|
|
jparse_free(jreply);
|
2017-09-24 00:57:49 -04:00
|
|
|
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_OK;
|
2017-09-24 00:57:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Endpoint to log into LastFM
|
|
|
|
*/
|
|
|
|
static int
|
2017-11-07 18:32:03 -05:00
|
|
|
jsonapi_reply_lastfm_login(struct httpd_request *hreq)
|
2017-09-24 00:57:49 -04:00
|
|
|
{
|
|
|
|
#ifdef LASTFM
|
|
|
|
struct evbuffer *in_evbuf;
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object *request;
|
2017-09-24 00:57:49 -04:00
|
|
|
const char *user;
|
|
|
|
const char *password;
|
|
|
|
char *errmsg = NULL;
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object *jreply;
|
|
|
|
json_object *errors;
|
2017-09-24 00:57:49 -04:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
DPRINTF(E_DBG, L_WEB, "Received LastFM login request\n");
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
in_evbuf = evhttp_request_get_input_buffer(hreq->req);
|
2017-09-24 00:57:49 -04:00
|
|
|
request = jparse_obj_from_evbuffer(in_evbuf);
|
|
|
|
if (!request)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Failed to parse incoming request\n");
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_BADREQUEST;
|
2017-09-24 00:57:49 -04:00
|
|
|
}
|
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
CHECK_NULL(L_WEB, jreply = json_object_new_object());
|
2017-09-24 00:57:49 -04:00
|
|
|
|
|
|
|
user = jparse_str_from_obj(request, "user");
|
|
|
|
password = jparse_str_from_obj(request, "password");
|
|
|
|
if (user && strlen(user) > 0 && password && strlen(password) > 0)
|
|
|
|
{
|
|
|
|
ret = lastfm_login_user(user, password, &errmsg);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object_object_add(jreply, "success", json_object_new_boolean(false));
|
2017-09-24 00:57:49 -04:00
|
|
|
errors = json_object_new_object();
|
|
|
|
if (errmsg)
|
|
|
|
json_object_object_add(errors, "error", json_object_new_string(errmsg));
|
|
|
|
else
|
|
|
|
json_object_object_add(errors, "error", json_object_new_string("Unknown error"));
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object_object_add(jreply, "errors", errors);
|
2017-09-24 00:57:49 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object_object_add(jreply, "success", json_object_new_boolean(true));
|
2017-09-24 00:57:49 -04:00
|
|
|
}
|
|
|
|
free(errmsg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "No user or password in LastFM login post request\n");
|
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object_object_add(jreply, "success", json_object_new_boolean(false));
|
2017-09-24 00:57:49 -04:00
|
|
|
errors = json_object_new_object();
|
|
|
|
if (!user || strlen(user) == 0)
|
|
|
|
json_object_object_add(errors, "user", json_object_new_string("Username is required"));
|
|
|
|
if (!password || strlen(password) == 0)
|
|
|
|
json_object_object_add(errors, "password", json_object_new_string("Password is required"));
|
2017-11-04 16:24:42 -04:00
|
|
|
json_object_object_add(jreply, "errors", errors);
|
2017-09-24 00:57:49 -04:00
|
|
|
}
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_ERRNO(L_WEB, evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(jreply)));
|
2017-11-04 16:24:42 -04:00
|
|
|
|
|
|
|
jparse_free(jreply);
|
2017-09-24 00:57:49 -04:00
|
|
|
|
|
|
|
#else
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Received LastFM login request but was not compiled with enable-lastfm\n");
|
|
|
|
#endif
|
|
|
|
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_OK;
|
2017-09-24 00:57:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-11-07 18:32:03 -05:00
|
|
|
jsonapi_reply_lastfm_logout(struct httpd_request *hreq)
|
2017-09-24 00:57:49 -04:00
|
|
|
{
|
|
|
|
#ifdef LASTFM
|
|
|
|
lastfm_logout();
|
|
|
|
#endif
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_NOCONTENT;
|
2017-09-24 00:57:49 -04:00
|
|
|
}
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
/*
|
|
|
|
* Kicks off pairing of a daap/dacp client
|
|
|
|
*
|
|
|
|
* Expects the paring pin to be present in the post request body, e. g.:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* "pin": "1234"
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
static int
|
2020-03-16 16:03:17 -04:00
|
|
|
jsonapi_reply_pairing_pair(struct httpd_request *hreq)
|
2018-02-11 02:53:51 -05:00
|
|
|
{
|
2017-12-27 04:01:36 -05:00
|
|
|
struct evbuffer *evbuf;
|
|
|
|
json_object* request;
|
2020-03-16 16:03:17 -04:00
|
|
|
const char* pin;
|
|
|
|
int ret;
|
2018-02-11 02:53:51 -05:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
evbuf = evhttp_request_get_input_buffer(hreq->req);
|
|
|
|
request = jparse_obj_from_evbuffer(evbuf);
|
|
|
|
if (!request)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Failed to parse incoming request\n");
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
2017-10-29 02:55:25 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
DPRINTF(E_DBG, L_WEB, "Received pairing post request: %s\n", json_object_to_json_string(request));
|
2017-10-29 02:55:25 -04:00
|
|
|
|
2020-03-16 16:03:17 -04:00
|
|
|
pin = jparse_str_from_obj(request, "pin");
|
|
|
|
if (pin)
|
|
|
|
{
|
|
|
|
ret = remote_pairing_pair(pin);
|
|
|
|
}
|
2017-12-27 04:01:36 -05:00
|
|
|
else
|
2020-03-16 16:03:17 -04:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Missing pin in request body: %s\n", json_object_to_json_string(request));
|
|
|
|
ret = REMOTE_INVALID_PIN;
|
|
|
|
}
|
2017-10-29 02:55:25 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
jparse_free(request);
|
|
|
|
|
2020-03-16 16:03:17 -04:00
|
|
|
if (ret == 0)
|
|
|
|
return HTTP_NOCONTENT;
|
|
|
|
else if (ret == REMOTE_INVALID_PIN)
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
|
|
|
|
return HTTP_INTERNAL;
|
2018-02-11 02:53:51 -05:00
|
|
|
}
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
/*
|
|
|
|
* Retrieves pairing information
|
|
|
|
*
|
|
|
|
* Example response:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* "active": true,
|
|
|
|
* "remote": "remote name"
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
jsonapi_reply_pairing_get(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
char *remote_name;
|
|
|
|
json_object *jreply;
|
|
|
|
|
|
|
|
remote_name = remote_pairing_get_name();
|
|
|
|
|
|
|
|
CHECK_NULL(L_WEB, jreply = json_object_new_object());
|
|
|
|
|
|
|
|
if (remote_name)
|
|
|
|
{
|
|
|
|
json_object_object_add(jreply, "active", json_object_new_boolean(true));
|
|
|
|
json_object_object_add(jreply, "remote", json_object_new_string(remote_name));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
json_object_object_add(jreply, "active", json_object_new_boolean(false));
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_ERRNO(L_WEB, evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(jreply)));
|
|
|
|
|
|
|
|
jparse_free(jreply);
|
|
|
|
free(remote_name);
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct outputs_param
|
|
|
|
{
|
|
|
|
json_object *output;
|
|
|
|
uint64_t output_id;
|
2019-01-20 07:13:01 -05:00
|
|
|
int output_volume;
|
2017-12-27 04:01:36 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static json_object *
|
2019-01-22 11:47:15 -05:00
|
|
|
speaker_to_json(struct player_speaker_info *spk)
|
2017-12-27 04:01:36 -05:00
|
|
|
{
|
|
|
|
json_object *output;
|
|
|
|
char output_id[21];
|
|
|
|
|
|
|
|
output = json_object_new_object();
|
|
|
|
|
|
|
|
snprintf(output_id, sizeof(output_id), "%" PRIu64, spk->id);
|
|
|
|
json_object_object_add(output, "id", json_object_new_string(output_id));
|
|
|
|
json_object_object_add(output, "name", json_object_new_string(spk->name));
|
|
|
|
json_object_object_add(output, "type", json_object_new_string(spk->output_type));
|
|
|
|
json_object_object_add(output, "selected", json_object_new_boolean(spk->selected));
|
|
|
|
json_object_object_add(output, "has_password", json_object_new_boolean(spk->has_password));
|
|
|
|
json_object_object_add(output, "requires_auth", json_object_new_boolean(spk->requires_auth));
|
|
|
|
json_object_object_add(output, "needs_auth_key", json_object_new_boolean(spk->needs_auth_key));
|
|
|
|
json_object_object_add(output, "volume", json_object_new_int(spk->absvol));
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-01-22 11:47:15 -05:00
|
|
|
speaker_enum_cb(struct player_speaker_info *spk, void *arg)
|
2017-12-27 04:01:36 -05:00
|
|
|
{
|
2018-02-11 02:53:51 -05:00
|
|
|
json_object *outputs;
|
|
|
|
json_object *output;
|
|
|
|
|
|
|
|
outputs = arg;
|
|
|
|
|
|
|
|
output = speaker_to_json(spk);
|
2017-10-29 02:55:25 -04:00
|
|
|
json_object_array_add(outputs, output);
|
|
|
|
}
|
|
|
|
|
2018-02-11 02:53:51 -05:00
|
|
|
/*
|
|
|
|
* GET /api/outputs/[output_id]
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
jsonapi_reply_outputs_get_byid(struct httpd_request *hreq)
|
|
|
|
{
|
2019-01-22 11:47:15 -05:00
|
|
|
struct player_speaker_info speaker_info;
|
2018-02-11 02:53:51 -05:00
|
|
|
uint64_t output_id;
|
2019-01-20 07:13:01 -05:00
|
|
|
json_object *jreply;
|
2018-02-11 02:53:51 -05:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = safe_atou64(hreq->uri_parsed->path_parts[2], &output_id);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "No valid output id given to outputs endpoint '%s'\n", hreq->uri_parsed->path);
|
|
|
|
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
2020-01-03 13:38:52 -05:00
|
|
|
ret = player_speaker_get_byid(&speaker_info, output_id);
|
2018-02-11 02:53:51 -05:00
|
|
|
|
2019-01-20 07:13:01 -05:00
|
|
|
if (ret < 0)
|
2018-02-11 02:53:51 -05:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "No output found for '%s'\n", hreq->uri_parsed->path);
|
|
|
|
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
2019-01-20 07:13:01 -05:00
|
|
|
jreply = speaker_to_json(&speaker_info);
|
|
|
|
CHECK_ERRNO(L_WEB, evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(jreply)));
|
2018-02-11 02:53:51 -05:00
|
|
|
|
2019-01-20 07:13:01 -05:00
|
|
|
jparse_free(jreply);
|
2018-02-11 02:53:51 -05:00
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PUT /api/outputs/[output_id]
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
jsonapi_reply_outputs_put_byid(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
uint64_t output_id;
|
|
|
|
struct evbuffer *in_evbuf;
|
|
|
|
json_object* request;
|
|
|
|
bool selected;
|
|
|
|
int volume;
|
2020-05-26 16:46:49 -04:00
|
|
|
const char *pin;
|
2018-02-11 02:53:51 -05:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = safe_atou64(hreq->uri_parsed->path_parts[2], &output_id);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "No valid output id given to outputs endpoint '%s'\n", hreq->uri_parsed->path);
|
|
|
|
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
in_evbuf = evhttp_request_get_input_buffer(hreq->req);
|
|
|
|
request = jparse_obj_from_evbuffer(in_evbuf);
|
|
|
|
if (!request)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Failed to parse incoming request\n");
|
|
|
|
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
if (jparse_contains_key(request, "selected", json_type_boolean))
|
|
|
|
{
|
|
|
|
selected = jparse_bool_from_obj(request, "selected");
|
|
|
|
if (selected)
|
|
|
|
ret = player_speaker_enable(output_id);
|
|
|
|
else
|
|
|
|
ret = player_speaker_disable(output_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == 0 && jparse_contains_key(request, "volume", json_type_int))
|
|
|
|
{
|
|
|
|
volume = jparse_int_from_obj(request, "volume");
|
|
|
|
ret = player_volume_setabs_speaker(output_id, volume);
|
|
|
|
}
|
|
|
|
|
2020-05-26 16:46:49 -04:00
|
|
|
if (ret == 0 && jparse_contains_key(request, "pin", json_type_string))
|
|
|
|
{
|
|
|
|
pin = jparse_str_from_obj(request, "pin");
|
|
|
|
if (pin)
|
|
|
|
ret = player_speaker_authorize(output_id, pin);
|
|
|
|
}
|
|
|
|
|
2018-02-11 02:53:51 -05:00
|
|
|
jparse_free(request);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_NOCONTENT;
|
|
|
|
}
|
|
|
|
|
2019-09-26 02:00:03 -04:00
|
|
|
/*
|
|
|
|
* PUT /api/outputs/[output_id]/toggle
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
jsonapi_reply_outputs_toggle_byid(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
uint64_t output_id;
|
|
|
|
struct player_speaker_info spk;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = safe_atou64(hreq->uri_parsed->path_parts[2], &output_id);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "No valid output id given to outputs endpoint '%s'\n", hreq->uri_parsed->path);
|
|
|
|
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
2020-01-03 13:38:52 -05:00
|
|
|
ret = player_speaker_get_byid(&spk, output_id);
|
2019-09-26 02:00:03 -04:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "No output found for the given output id, toggle failed for '%s'\n", hreq->uri_parsed->path);
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spk.selected)
|
|
|
|
ret = player_speaker_disable(output_id);
|
|
|
|
else
|
|
|
|
ret = player_speaker_enable(output_id);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_NOCONTENT;
|
|
|
|
}
|
|
|
|
|
2018-02-11 02:53:51 -05:00
|
|
|
/*
|
|
|
|
* Endpoint "/api/outputs"
|
|
|
|
*/
|
2017-10-29 02:55:25 -04:00
|
|
|
static int
|
|
|
|
jsonapi_reply_outputs(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
json_object *outputs;
|
2018-02-11 02:53:51 -05:00
|
|
|
json_object *jreply;
|
2017-10-29 02:55:25 -04:00
|
|
|
|
|
|
|
outputs = json_object_new_array();
|
|
|
|
|
|
|
|
player_speaker_enumerate(speaker_enum_cb, outputs);
|
|
|
|
|
|
|
|
jreply = json_object_new_object();
|
|
|
|
json_object_object_add(jreply, "outputs", outputs);
|
|
|
|
|
|
|
|
CHECK_ERRNO(L_WEB, evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(jreply)));
|
|
|
|
|
|
|
|
jparse_free(jreply);
|
|
|
|
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_OK;
|
2017-10-29 02:55:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jsonapi_reply_verification(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
struct evbuffer *in_evbuf;
|
|
|
|
json_object* request;
|
|
|
|
const char* message;
|
|
|
|
|
|
|
|
in_evbuf = evhttp_request_get_input_buffer(hreq->req);
|
|
|
|
request = jparse_obj_from_evbuffer(in_evbuf);
|
|
|
|
if (!request)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Failed to parse incoming request\n");
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_BADREQUEST;
|
2017-10-29 02:55:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTF(E_DBG, L_WEB, "Received verification post request: %s\n", json_object_to_json_string(request));
|
|
|
|
|
|
|
|
message = jparse_str_from_obj(request, "pin");
|
|
|
|
if (message)
|
|
|
|
player_raop_verification_kickoff((char **)&message);
|
|
|
|
else
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Missing pin in request body: %s\n", json_object_to_json_string(request));
|
|
|
|
|
|
|
|
jparse_free(request);
|
|
|
|
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_NOCONTENT;
|
2017-10-29 02:55:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-02-11 02:53:51 -05:00
|
|
|
jsonapi_reply_outputs_set(struct httpd_request *hreq)
|
2017-10-29 02:55:25 -04:00
|
|
|
{
|
|
|
|
struct evbuffer *in_evbuf;
|
|
|
|
json_object *request;
|
|
|
|
json_object *outputs;
|
|
|
|
json_object *output_id;
|
|
|
|
int nspk, i, ret;
|
|
|
|
uint64_t *ids;
|
|
|
|
|
|
|
|
in_evbuf = evhttp_request_get_input_buffer(hreq->req);
|
|
|
|
request = jparse_obj_from_evbuffer(in_evbuf);
|
|
|
|
if (!request)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Failed to parse incoming request\n");
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_BADREQUEST;
|
2017-10-29 02:55:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTF(E_DBG, L_WEB, "Received select-outputs post request: %s\n", json_object_to_json_string(request));
|
|
|
|
|
|
|
|
ret = jparse_array_from_obj(request, "outputs", &outputs);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
nspk = json_object_array_length(outputs);
|
|
|
|
|
|
|
|
ids = calloc((nspk + 1), sizeof(uint64_t));
|
|
|
|
ids[0] = nspk;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
for (i = 0; i < nspk; i++)
|
|
|
|
{
|
|
|
|
output_id = json_object_array_get_idx(outputs, i);
|
|
|
|
ret = safe_atou64(json_object_get_string(output_id), &ids[i + 1]);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Failed to convert output id: %s\n", json_object_to_json_string(request));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
player_speaker_set(ids);
|
|
|
|
|
|
|
|
free(ids);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Missing outputs in request body: %s\n", json_object_to_json_string(request));
|
|
|
|
|
|
|
|
jparse_free(request);
|
|
|
|
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_NOCONTENT;
|
2017-10-29 02:55:25 -04:00
|
|
|
}
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
static int
|
|
|
|
play_item_with_id(const char *param)
|
|
|
|
{
|
|
|
|
uint32_t item_id;
|
|
|
|
struct db_queue_item *queue_item;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = safe_atou32(param, &item_id);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "No valid item id given '%s'\n", param);
|
|
|
|
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
queue_item = db_queue_fetch_byitemid(item_id);
|
|
|
|
if (!queue_item)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "No queue item with item id '%d'\n", item_id);
|
|
|
|
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
player_playback_stop();
|
|
|
|
ret = player_playback_start_byitem(queue_item);
|
|
|
|
free_queue_item(queue_item, 0);
|
|
|
|
|
2019-01-01 04:15:23 -05:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Failed to start playback from item with id '%d'\n", item_id);
|
|
|
|
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
}
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
return HTTP_NOCONTENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
play_item_at_position(const char *param)
|
|
|
|
{
|
|
|
|
uint32_t position;
|
2018-12-02 01:14:26 -05:00
|
|
|
struct player_status status;
|
2017-12-27 04:01:36 -05:00
|
|
|
struct db_queue_item *queue_item;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = safe_atou32(param, &position);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "No valid position given '%s'\n", param);
|
|
|
|
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
2018-12-02 01:14:26 -05:00
|
|
|
player_get_status(&status);
|
|
|
|
|
|
|
|
queue_item = db_queue_fetch_bypos(position, status.shuffle);
|
2017-12-27 04:01:36 -05:00
|
|
|
if (!queue_item)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "No queue item at position '%d'\n", position);
|
|
|
|
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
player_playback_stop();
|
|
|
|
ret = player_playback_start_byitem(queue_item);
|
|
|
|
free_queue_item(queue_item, 0);
|
|
|
|
|
2019-01-01 04:15:23 -05:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Failed to start playback from position '%d'\n", position);
|
|
|
|
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
}
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
return HTTP_NOCONTENT;
|
|
|
|
}
|
|
|
|
|
2017-11-01 04:47:26 -04:00
|
|
|
static int
|
|
|
|
jsonapi_reply_player_play(struct httpd_request *hreq)
|
|
|
|
{
|
2017-12-27 04:01:36 -05:00
|
|
|
const char *param;
|
2017-11-01 04:47:26 -04:00
|
|
|
int ret;
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
if ((param = evhttp_find_header(hreq->query, "item_id")))
|
|
|
|
{
|
|
|
|
return play_item_with_id(param);
|
|
|
|
}
|
|
|
|
else if ((param = evhttp_find_header(hreq->query, "position")))
|
|
|
|
{
|
|
|
|
return play_item_at_position(param);
|
|
|
|
}
|
|
|
|
|
2017-11-01 04:47:26 -04:00
|
|
|
ret = player_playback_start();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Error starting playback.\n");
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_INTERNAL;
|
2017-11-01 04:47:26 -04:00
|
|
|
}
|
|
|
|
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_NOCONTENT;
|
2017-11-01 04:47:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jsonapi_reply_player_pause(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = player_playback_pause();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Error pausing playback.\n");
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_INTERNAL;
|
2017-11-01 04:47:26 -04:00
|
|
|
}
|
|
|
|
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_NOCONTENT;
|
2017-11-01 04:47:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jsonapi_reply_player_stop(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = player_playback_stop();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Error stopping playback.\n");
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_INTERNAL;
|
2017-11-01 04:47:26 -04:00
|
|
|
}
|
|
|
|
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_NOCONTENT;
|
2017-11-01 04:47:26 -04:00
|
|
|
}
|
|
|
|
|
2019-01-20 06:03:50 -05:00
|
|
|
static int
|
|
|
|
jsonapi_reply_player_toggle(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
struct player_status status;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
player_get_status(&status);
|
|
|
|
DPRINTF(E_DBG, L_WEB, "Toggle playback request with current state %d.\n", status.status);
|
|
|
|
|
|
|
|
if (status.status == PLAY_PLAYING)
|
|
|
|
{
|
|
|
|
ret = player_playback_pause();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = player_playback_start();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Error toggling playback state.\n");
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HTTP_NOCONTENT;
|
|
|
|
}
|
|
|
|
|
2017-11-01 04:47:26 -04:00
|
|
|
static int
|
|
|
|
jsonapi_reply_player_next(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = player_playback_next();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-06-28 05:56:51 -04:00
|
|
|
// If skipping to the next song failed, it is most likely we reached the end of the queue,
|
|
|
|
// ignore the error (play status change will be reported to the client over the websocket)
|
|
|
|
DPRINTF(E_DBG, L_WEB, "Error switching to next item (possibly end of queue reached).\n");
|
|
|
|
return HTTP_NOCONTENT;
|
2017-11-01 04:47:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = player_playback_start();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Error starting playback after switching to next item.\n");
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_INTERNAL;
|
2017-11-01 04:47:26 -04:00
|
|
|
}
|
|
|
|
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_NOCONTENT;
|
2017-11-01 04:47:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jsonapi_reply_player_previous(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = player_playback_prev();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Error switching to previous item.\n");
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_INTERNAL;
|
2017-11-01 04:47:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = player_playback_start();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Error starting playback after switching to previous item.\n");
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_INTERNAL;
|
2017-11-01 04:47:26 -04:00
|
|
|
}
|
|
|
|
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_NOCONTENT;
|
2017-11-01 04:47:26 -04:00
|
|
|
}
|
|
|
|
|
2018-03-17 15:20:29 -04:00
|
|
|
static int
|
|
|
|
jsonapi_reply_player_seek(struct httpd_request *hreq)
|
|
|
|
{
|
2019-09-01 02:50:49 -04:00
|
|
|
const char *param_pos;
|
|
|
|
const char *param_seek;
|
2018-03-17 15:20:29 -04:00
|
|
|
int position_ms;
|
2019-09-01 02:50:49 -04:00
|
|
|
int seek_ms;
|
2018-03-17 15:20:29 -04:00
|
|
|
int ret;
|
|
|
|
|
2019-09-01 02:50:49 -04:00
|
|
|
param_pos = evhttp_find_header(hreq->query, "position_ms");
|
|
|
|
param_seek = evhttp_find_header(hreq->query, "seek_ms");
|
|
|
|
if (!param_pos && !param_seek)
|
2018-03-17 15:20:29 -04:00
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
|
2019-09-01 02:50:49 -04:00
|
|
|
if (param_pos)
|
|
|
|
{
|
|
|
|
ret = safe_atoi32(param_pos, &position_ms);
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
|
2019-09-22 03:18:53 -04:00
|
|
|
ret = player_playback_seek(position_ms, PLAYER_SEEK_POSITION);
|
2019-09-01 02:50:49 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = safe_atoi32(param_seek, &seek_ms);
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
|
2019-09-22 03:18:53 -04:00
|
|
|
ret = player_playback_seek(seek_ms, PLAYER_SEEK_RELATIVE);
|
2019-09-01 02:50:49 -04:00
|
|
|
}
|
2018-03-17 15:20:29 -04:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-09-01 02:50:49 -04:00
|
|
|
DPRINTF(E_LOG, L_WEB, "Error seeking (position_ms=%s, seek_ms=%s).\n",
|
|
|
|
(param_pos ? param_pos : ""), (param_seek ? param_seek : ""));
|
2018-03-17 15:20:29 -04:00
|
|
|
return HTTP_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = player_playback_start();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-09-01 02:50:49 -04:00
|
|
|
DPRINTF(E_LOG, L_WEB, "Error starting playback after seeking (position_ms=%s, seek_ms=%s).\n",
|
|
|
|
(param_pos ? param_pos : ""), (param_seek ? param_seek : ""));
|
2018-03-17 15:20:29 -04:00
|
|
|
return HTTP_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HTTP_NOCONTENT;
|
|
|
|
}
|
|
|
|
|
2017-11-01 04:47:26 -04:00
|
|
|
static int
|
|
|
|
jsonapi_reply_player(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
struct player_status status;
|
2017-12-27 04:01:36 -05:00
|
|
|
struct db_queue_item *queue_item;
|
2017-11-01 04:47:26 -04:00
|
|
|
json_object *reply;
|
|
|
|
|
|
|
|
player_get_status(&status);
|
|
|
|
|
|
|
|
reply = json_object_new_object();
|
|
|
|
|
|
|
|
switch (status.status)
|
|
|
|
{
|
|
|
|
case PLAY_PAUSED:
|
|
|
|
json_object_object_add(reply, "state", json_object_new_string("pause"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PLAY_PLAYING:
|
|
|
|
json_object_object_add(reply, "state", json_object_new_string("play"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
json_object_object_add(reply, "state", json_object_new_string("stop"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (status.repeat)
|
|
|
|
{
|
|
|
|
case REPEAT_SONG:
|
|
|
|
json_object_object_add(reply, "repeat", json_object_new_string("single"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REPEAT_ALL:
|
|
|
|
json_object_object_add(reply, "repeat", json_object_new_string("all"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
json_object_object_add(reply, "repeat", json_object_new_string("off"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_object_add(reply, "consume", json_object_new_boolean(status.consume));
|
|
|
|
json_object_object_add(reply, "shuffle", json_object_new_boolean(status.shuffle));
|
|
|
|
json_object_object_add(reply, "volume", json_object_new_int(status.volume));
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
if (status.item_id)
|
|
|
|
{
|
|
|
|
json_object_object_add(reply, "item_id", json_object_new_int(status.item_id));
|
|
|
|
json_object_object_add(reply, "item_length_ms", json_object_new_int(status.len_ms));
|
|
|
|
json_object_object_add(reply, "item_progress_ms", json_object_new_int(status.pos_ms));
|
2020-04-28 13:01:25 -04:00
|
|
|
json_object_object_add(reply, "artwork_url", json_object_new_string("./artwork/nowplaying"));
|
2017-12-27 04:01:36 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
queue_item = db_queue_fetch_bypos(0, status.shuffle);
|
|
|
|
|
|
|
|
if (queue_item)
|
|
|
|
{
|
|
|
|
json_object_object_add(reply, "item_id", json_object_new_int(queue_item->id));
|
|
|
|
json_object_object_add(reply, "item_length_ms", json_object_new_int(queue_item->song_length));
|
|
|
|
json_object_object_add(reply, "item_progress_ms", json_object_new_int(0));
|
|
|
|
free_queue_item(queue_item, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
json_object_object_add(reply, "item_id", json_object_new_int(0));
|
|
|
|
json_object_object_add(reply, "item_length_ms", json_object_new_int(0));
|
|
|
|
json_object_object_add(reply, "item_progress_ms", json_object_new_int(0));
|
|
|
|
}
|
|
|
|
}
|
2017-11-01 04:47:26 -04:00
|
|
|
|
|
|
|
CHECK_ERRNO(L_WEB, evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(reply)));
|
|
|
|
|
|
|
|
jparse_free(reply);
|
|
|
|
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_OK;
|
2017-11-01 04:47:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static json_object *
|
2017-12-27 04:01:36 -05:00
|
|
|
queue_item_to_json(struct db_queue_item *queue_item, char shuffle)
|
2017-11-01 04:47:26 -04:00
|
|
|
{
|
|
|
|
json_object *item;
|
2017-12-27 04:01:36 -05:00
|
|
|
char uri[100];
|
2018-10-07 15:54:38 -04:00
|
|
|
char artwork_url[100];
|
2017-12-27 04:01:36 -05:00
|
|
|
int ret;
|
2017-11-01 04:47:26 -04:00
|
|
|
|
|
|
|
item = json_object_new_object();
|
|
|
|
|
|
|
|
json_object_object_add(item, "id", json_object_new_int(queue_item->id));
|
2017-12-27 04:01:36 -05:00
|
|
|
if (shuffle)
|
|
|
|
json_object_object_add(item, "position", json_object_new_int(queue_item->shuffle_pos));
|
|
|
|
else
|
|
|
|
json_object_object_add(item, "position", json_object_new_int(queue_item->pos));
|
|
|
|
|
2018-11-25 03:31:29 -05:00
|
|
|
if (queue_item->file_id > 0 && queue_item->file_id != DB_MEDIA_FILE_NON_PERSISTENT_ID)
|
|
|
|
json_object_object_add(item, "track_id", json_object_new_int(queue_item->file_id));
|
2017-12-27 04:01:36 -05:00
|
|
|
|
|
|
|
safe_json_add_string(item, "title", queue_item->title);
|
|
|
|
safe_json_add_string(item, "artist", queue_item->artist);
|
|
|
|
safe_json_add_string(item, "artist_sort", queue_item->artist_sort);
|
|
|
|
safe_json_add_string(item, "album", queue_item->album);
|
|
|
|
safe_json_add_string(item, "album_sort", queue_item->album_sort);
|
2019-02-14 02:22:50 -05:00
|
|
|
safe_json_add_string_from_int64(item, "album_id", queue_item->songalbumid);
|
2017-12-27 04:01:36 -05:00
|
|
|
safe_json_add_string(item, "album_artist", queue_item->album_artist);
|
|
|
|
safe_json_add_string(item, "album_artist_sort", queue_item->album_artist_sort);
|
2019-02-14 02:22:50 -05:00
|
|
|
safe_json_add_string_from_int64(item, "album_artist_id", queue_item->songartistid);
|
2018-11-23 09:52:30 -05:00
|
|
|
safe_json_add_string(item, "composer", queue_item->composer);
|
2017-12-27 04:01:36 -05:00
|
|
|
safe_json_add_string(item, "genre", queue_item->genre);
|
|
|
|
|
2017-11-01 04:47:26 -04:00
|
|
|
json_object_object_add(item, "year", json_object_new_int(queue_item->year));
|
2017-12-27 04:01:36 -05:00
|
|
|
json_object_object_add(item, "track_number", json_object_new_int(queue_item->track));
|
|
|
|
json_object_object_add(item, "disc_number", json_object_new_int(queue_item->disc));
|
2017-11-01 04:47:26 -04:00
|
|
|
json_object_object_add(item, "length_ms", json_object_new_int(queue_item->song_length));
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
safe_json_add_string(item, "media_kind", db_media_kind_label(queue_item->media_kind));
|
|
|
|
safe_json_add_string(item, "data_kind", db_data_kind_label(queue_item->data_kind));
|
|
|
|
|
|
|
|
safe_json_add_string(item, "path", queue_item->path);
|
|
|
|
|
2018-11-25 03:31:29 -05:00
|
|
|
if (queue_item->file_id > 0 && queue_item->file_id != DB_MEDIA_FILE_NON_PERSISTENT_ID)
|
2017-12-27 04:01:36 -05:00
|
|
|
{
|
|
|
|
ret = snprintf(uri, sizeof(uri), "%s:%s:%d", "library", "track", queue_item->file_id);
|
|
|
|
if (ret < sizeof(uri))
|
|
|
|
json_object_object_add(item, "uri", json_object_new_string(uri));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
safe_json_add_string(item, "uri", queue_item->path);
|
2018-11-25 03:31:29 -05:00
|
|
|
}
|
|
|
|
|
2019-10-20 02:39:18 -04:00
|
|
|
if (queue_item->artwork_url
|
|
|
|
&& (strncmp(queue_item->artwork_url, "http://", strlen("http://")) == 0
|
|
|
|
|| strncmp(queue_item->artwork_url, "https://", strlen("https://")) == 0))
|
2018-11-25 03:31:29 -05:00
|
|
|
{
|
2019-10-20 02:39:18 -04:00
|
|
|
// The queue item contains a valid http url for an artwork image, there is no need
|
|
|
|
// for the client to request the image through the forked-daapd artwork handler.
|
|
|
|
// Directly pass the artwork url to the client.
|
2018-10-07 15:54:38 -04:00
|
|
|
safe_json_add_string(item, "artwork_url", queue_item->artwork_url);
|
2017-12-27 04:01:36 -05:00
|
|
|
}
|
2018-11-25 03:31:29 -05:00
|
|
|
else if (queue_item->file_id > 0 && queue_item->file_id != DB_MEDIA_FILE_NON_PERSISTENT_ID)
|
|
|
|
{
|
2020-02-19 17:25:59 -05:00
|
|
|
if (queue_item->data_kind == DATA_KIND_FILE)
|
2019-10-20 02:39:18 -04:00
|
|
|
{
|
|
|
|
// Queue item does not have a valid artwork url, construct artwork url to
|
|
|
|
// get the image through the httpd_artworkapi (uses the artwork handlers).
|
2020-04-28 13:01:25 -04:00
|
|
|
ret = snprintf(artwork_url, sizeof(artwork_url), "./artwork/item/%d", queue_item->file_id);
|
2019-10-20 02:39:18 -04:00
|
|
|
if (ret < sizeof(artwork_url))
|
|
|
|
json_object_object_add(item, "artwork_url", json_object_new_string(artwork_url));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-19 17:25:59 -05:00
|
|
|
// Pipe and stream metadata can change if the queue version changes. Construct artwork url
|
2019-10-20 02:39:18 -04:00
|
|
|
// similar to non-pipe items, but append the queue version to the url to force
|
|
|
|
// clients to reload image if the queue version changes (additional metadata was found).
|
2020-04-28 13:01:25 -04:00
|
|
|
ret = snprintf(artwork_url, sizeof(artwork_url), "./artwork/item/%d?v=%d", queue_item->file_id, queue_item->queue_version);
|
2019-10-20 02:39:18 -04:00
|
|
|
if (ret < sizeof(artwork_url))
|
|
|
|
json_object_object_add(item, "artwork_url", json_object_new_string(artwork_url));
|
|
|
|
}
|
2018-11-25 03:31:29 -05:00
|
|
|
}
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2019-06-04 17:02:15 -04:00
|
|
|
safe_json_add_string(item, "type", queue_item->type);
|
|
|
|
json_object_object_add(item, "bitrate", json_object_new_int(queue_item->bitrate));
|
|
|
|
json_object_object_add(item, "samplerate", json_object_new_int(queue_item->samplerate));
|
2020-06-30 03:31:10 -04:00
|
|
|
json_object_object_add(item, "channels", json_object_new_int(queue_item->channels));
|
2019-06-04 17:02:15 -04:00
|
|
|
|
2017-11-01 04:47:26 -04:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-10-13 00:57:53 -04:00
|
|
|
queue_tracks_add_artist(const char *id, int pos)
|
2017-11-01 04:47:26 -04:00
|
|
|
{
|
|
|
|
struct query_params query_params;
|
2017-12-27 04:01:36 -05:00
|
|
|
struct player_status status;
|
2018-10-13 00:57:53 -04:00
|
|
|
int count = 0;
|
2017-11-01 04:47:26 -04:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
query_params.type = Q_ITEMS;
|
|
|
|
query_params.sort = S_ALBUM;
|
|
|
|
query_params.idx_type = I_NONE;
|
|
|
|
query_params.filter = db_mprintf("(f.songartistid = %q)", id);
|
2017-11-01 04:47:26 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
player_get_status(&status);
|
2017-11-01 04:47:26 -04:00
|
|
|
|
2018-10-13 00:57:53 -04:00
|
|
|
ret = db_queue_add_by_query(&query_params, status.shuffle, status.item_id, pos, &count, NULL);
|
2017-11-01 04:47:26 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
free(query_params.filter);
|
2017-11-01 04:47:26 -04:00
|
|
|
|
2018-10-13 00:57:53 -04:00
|
|
|
if (ret == 0)
|
|
|
|
return count;
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
return ret;
|
|
|
|
}
|
2017-11-01 04:47:26 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
static int
|
2018-10-13 00:57:53 -04:00
|
|
|
queue_tracks_add_album(const char *id, int pos)
|
2017-12-27 04:01:36 -05:00
|
|
|
{
|
|
|
|
struct query_params query_params;
|
|
|
|
struct player_status status;
|
2018-10-13 00:57:53 -04:00
|
|
|
int count = 0;
|
2017-12-27 04:01:36 -05:00
|
|
|
int ret = 0;
|
2017-11-01 04:47:26 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
2017-11-01 04:47:26 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
query_params.type = Q_ITEMS;
|
|
|
|
query_params.sort = S_ALBUM;
|
|
|
|
query_params.idx_type = I_NONE;
|
|
|
|
query_params.filter = db_mprintf("(f.songalbumid = %q)", id);
|
2017-11-01 04:47:26 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
player_get_status(&status);
|
2017-11-01 04:47:26 -04:00
|
|
|
|
2018-10-13 00:57:53 -04:00
|
|
|
ret = db_queue_add_by_query(&query_params, status.shuffle, status.item_id, pos, &count, NULL);
|
2017-11-01 04:47:26 -04:00
|
|
|
|
|
|
|
free(query_params.filter);
|
|
|
|
|
2018-10-13 00:57:53 -04:00
|
|
|
if (ret == 0)
|
|
|
|
return count;
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
return ret;
|
2017-11-01 04:47:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-10-13 00:57:53 -04:00
|
|
|
queue_tracks_add_track(const char *id, int pos)
|
2017-11-01 04:47:26 -04:00
|
|
|
{
|
2017-12-27 04:01:36 -05:00
|
|
|
struct query_params query_params;
|
|
|
|
struct player_status status;
|
2018-10-13 00:57:53 -04:00
|
|
|
int count = 0;
|
2017-12-27 04:01:36 -05:00
|
|
|
int ret = 0;
|
2017-11-01 04:47:26 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
2017-11-01 04:47:26 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
query_params.type = Q_ITEMS;
|
|
|
|
query_params.sort = S_ALBUM;
|
|
|
|
query_params.idx_type = I_NONE;
|
|
|
|
query_params.filter = db_mprintf("(f.id = %q)", id);
|
2017-11-01 04:47:26 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
player_get_status(&status);
|
|
|
|
|
2018-10-13 00:57:53 -04:00
|
|
|
ret = db_queue_add_by_query(&query_params, status.shuffle, status.item_id, pos, &count, NULL);
|
2017-12-27 04:01:36 -05:00
|
|
|
|
|
|
|
free(query_params.filter);
|
|
|
|
|
2018-10-13 00:57:53 -04:00
|
|
|
if (ret == 0)
|
|
|
|
return count;
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
return ret;
|
2017-11-01 04:47:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-10-13 00:57:53 -04:00
|
|
|
queue_tracks_add_playlist(const char *id, int pos)
|
2017-11-01 04:47:26 -04:00
|
|
|
{
|
2017-12-27 04:01:36 -05:00
|
|
|
struct player_status status;
|
|
|
|
int playlist_id;
|
2018-10-13 00:57:53 -04:00
|
|
|
int count = 0;
|
2017-12-27 04:01:36 -05:00
|
|
|
int ret;
|
2017-11-01 04:47:26 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
ret = safe_atoi32(id, &playlist_id);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "No valid playlist id given '%s'\n", id);
|
2017-11-01 04:47:26 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
2017-11-01 04:47:26 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
player_get_status(&status);
|
|
|
|
|
2018-10-13 00:57:53 -04:00
|
|
|
ret = db_queue_add_by_playlistid(playlist_id, status.shuffle, status.item_id, pos, &count, NULL);
|
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
return count;
|
2017-12-27 04:01:36 -05:00
|
|
|
|
|
|
|
return ret;
|
2017-11-01 04:47:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-12-29 03:51:30 -05:00
|
|
|
queue_tracks_add_byuris(const char *param, int pos, int *total_count)
|
2017-11-01 04:47:26 -04:00
|
|
|
{
|
2020-01-20 17:13:47 -05:00
|
|
|
struct player_status status;
|
2017-12-27 04:01:36 -05:00
|
|
|
char *uris;
|
|
|
|
char *uri;
|
2018-12-31 02:26:40 -05:00
|
|
|
char *ptr;
|
2017-12-27 04:01:36 -05:00
|
|
|
const char *id;
|
2018-10-13 02:30:41 -04:00
|
|
|
int count = 0;
|
2017-12-27 04:01:36 -05:00
|
|
|
int ret = 0;
|
|
|
|
|
2018-12-29 03:51:30 -05:00
|
|
|
*total_count = 0;
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-12-31 02:26:40 -05:00
|
|
|
CHECK_NULL(L_WEB, uris = strdup(param));
|
|
|
|
uri = strtok_r(uris, ",", &ptr);
|
|
|
|
|
|
|
|
if (!uri)
|
2018-12-30 02:33:31 -05:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Empty query parameter 'uris'\n");
|
2018-12-31 02:26:40 -05:00
|
|
|
free(uris);
|
2018-12-30 02:33:31 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
do
|
|
|
|
{
|
2018-10-13 02:30:41 -04:00
|
|
|
count = 0;
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
if (strncmp(uri, "library:artist:", strlen("library:artist:")) == 0)
|
|
|
|
{
|
|
|
|
id = uri + (strlen("library:artist:"));
|
2018-10-13 02:30:41 -04:00
|
|
|
count = queue_tracks_add_artist(id, pos);
|
2017-12-27 04:01:36 -05:00
|
|
|
}
|
|
|
|
else if (strncmp(uri, "library:album:", strlen("library:album:")) == 0)
|
|
|
|
{
|
|
|
|
id = uri + (strlen("library:album:"));
|
2018-10-13 02:30:41 -04:00
|
|
|
count = queue_tracks_add_album(id, pos);
|
2017-12-27 04:01:36 -05:00
|
|
|
}
|
|
|
|
else if (strncmp(uri, "library:track:", strlen("library:track:")) == 0)
|
|
|
|
{
|
|
|
|
id = uri + (strlen("library:track:"));
|
2018-10-13 02:30:41 -04:00
|
|
|
count = queue_tracks_add_track(id, pos);
|
2017-12-27 04:01:36 -05:00
|
|
|
}
|
|
|
|
else if (strncmp(uri, "library:playlist:", strlen("library:playlist:")) == 0)
|
|
|
|
{
|
|
|
|
id = uri + (strlen("library:playlist:"));
|
2018-10-13 02:30:41 -04:00
|
|
|
count = queue_tracks_add_playlist(id, pos);
|
2017-12-27 04:01:36 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-20 17:13:47 -05:00
|
|
|
player_get_status(&status);
|
|
|
|
|
|
|
|
ret = library_queue_item_add(uri, pos, status.shuffle, status.item_id, &count, NULL);
|
2018-03-20 16:53:38 -04:00
|
|
|
if (ret != LIBRARY_OK)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid uri '%s'\n", uri);
|
|
|
|
break;
|
|
|
|
}
|
2018-10-13 02:30:41 -04:00
|
|
|
pos += count;
|
2017-12-27 04:01:36 -05:00
|
|
|
}
|
2018-10-13 02:30:41 -04:00
|
|
|
|
|
|
|
if (pos >= 0)
|
|
|
|
pos += count;
|
2018-12-24 14:26:26 -05:00
|
|
|
|
2018-12-29 03:51:30 -05:00
|
|
|
*total_count += count;
|
2017-12-27 04:01:36 -05:00
|
|
|
}
|
2018-12-31 02:26:40 -05:00
|
|
|
while ((uri = strtok_r(NULL, ",", &ptr)));
|
2017-12-27 04:01:36 -05:00
|
|
|
|
|
|
|
free(uris);
|
|
|
|
|
2018-12-29 03:51:30 -05:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-05-09 15:04:27 -04:00
|
|
|
queue_tracks_add_byexpression(const char *param, int pos, int limit, int *total_count)
|
2018-12-29 03:51:30 -05:00
|
|
|
{
|
|
|
|
char *expression;
|
|
|
|
struct smartpl smartpl_expression;
|
|
|
|
struct query_params query_params;
|
|
|
|
struct player_status status;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
|
|
|
|
|
|
|
query_params.type = Q_ITEMS;
|
2019-02-23 04:44:10 -05:00
|
|
|
query_params.sort = S_NAME;
|
2018-12-29 03:51:30 -05:00
|
|
|
|
|
|
|
memset(&smartpl_expression, 0, sizeof(struct smartpl));
|
|
|
|
expression = safe_asprintf("\"query\" { %s }", param);
|
|
|
|
ret = smartpl_query_parse_string(&smartpl_expression, expression);
|
|
|
|
free(expression);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
query_params.filter = strdup(smartpl_expression.query_where);
|
2019-02-23 04:58:04 -05:00
|
|
|
query_params.order = safe_strdup(smartpl_expression.order);
|
2020-05-09 15:04:27 -04:00
|
|
|
query_params.limit = limit > 0 ? limit : smartpl_expression.limit;
|
2018-12-29 03:51:30 -05:00
|
|
|
free_smartpl(&smartpl_expression, 1);
|
|
|
|
|
|
|
|
player_get_status(&status);
|
|
|
|
|
2020-05-09 12:09:03 -04:00
|
|
|
query_params.idx_type = query_params.limit > 0 ? I_FIRST : I_NONE;
|
|
|
|
|
2018-12-29 03:51:30 -05:00
|
|
|
ret = db_queue_add_by_query(&query_params, status.shuffle, status.item_id, pos, total_count, NULL);
|
|
|
|
|
2019-02-23 04:58:04 -05:00
|
|
|
free_query_params(&query_params, 1);
|
2018-12-29 03:51:30 -05:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jsonapi_reply_queue_tracks_add(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
const char *param_pos;
|
|
|
|
const char *param_uris;
|
|
|
|
const char *param_expression;
|
2019-01-02 03:46:47 -05:00
|
|
|
const char *param;
|
2018-12-29 03:51:30 -05:00
|
|
|
int pos = -1;
|
2020-05-09 15:04:27 -04:00
|
|
|
int limit = -1;
|
2019-01-02 03:46:47 -05:00
|
|
|
bool shuffle;
|
2018-12-29 03:51:30 -05:00
|
|
|
int total_count = 0;
|
|
|
|
json_object *reply;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
|
|
|
|
param_pos = evhttp_find_header(hreq->query, "position");
|
|
|
|
if (param_pos)
|
|
|
|
{
|
|
|
|
if (safe_atoi32(param_pos, &pos) < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid position parameter '%s'\n", param_pos);
|
|
|
|
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTF(E_DBG, L_WEB, "Add tracks starting at position '%d\n", pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
param_uris = evhttp_find_header(hreq->query, "uris");
|
|
|
|
param_expression = evhttp_find_header(hreq->query, "expression");
|
|
|
|
|
|
|
|
if (!param_uris && !param_expression)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Missing query parameter 'uris' or 'expression'\n");
|
|
|
|
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
2019-01-02 03:46:47 -05:00
|
|
|
// if query parameter "clear" is "true", stop playback and clear the queue before adding new queue items
|
|
|
|
param = evhttp_find_header(hreq->query, "clear");
|
|
|
|
if (param && strcmp(param, "true") == 0)
|
|
|
|
{
|
|
|
|
player_playback_stop();
|
|
|
|
db_queue_clear(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if query parameter "shuffle" is present, update the shuffle state before adding new queue items
|
|
|
|
param = evhttp_find_header(hreq->query, "shuffle");
|
|
|
|
if (param)
|
|
|
|
{
|
|
|
|
shuffle = (strcmp(param, "true") == 0);
|
|
|
|
player_shuffle_set(shuffle);
|
|
|
|
}
|
|
|
|
|
2018-12-29 03:51:30 -05:00
|
|
|
if (param_uris)
|
|
|
|
{
|
|
|
|
ret = queue_tracks_add_byuris(param_uris, pos, &total_count);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-09 15:04:27 -04:00
|
|
|
// This overrides the value specified in query
|
|
|
|
param = evhttp_find_header(hreq->query, "limit");
|
|
|
|
if (param)
|
|
|
|
safe_atoi32(param, &limit);
|
|
|
|
ret = queue_tracks_add_byexpression(param_expression, pos, limit, &total_count);
|
2018-12-29 03:51:30 -05:00
|
|
|
}
|
|
|
|
|
2018-12-17 13:03:34 -05:00
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
reply = json_object_new_object();
|
2018-12-29 03:51:30 -05:00
|
|
|
json_object_object_add(reply, "count", json_object_new_int(total_count));
|
2018-12-17 13:03:34 -05:00
|
|
|
|
|
|
|
ret = evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(reply));
|
|
|
|
jparse_free(reply);
|
|
|
|
}
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
2019-01-02 03:46:47 -05:00
|
|
|
// If query parameter "playback" is "start", start playback after successfully adding new items
|
|
|
|
param = evhttp_find_header(hreq->query, "playback");
|
|
|
|
if (param && strcmp(param, "start") == 0)
|
|
|
|
{
|
2019-02-22 07:24:36 -05:00
|
|
|
if ((param = evhttp_find_header(hreq->query, "playback_from_position")))
|
|
|
|
play_item_at_position(param);
|
|
|
|
else
|
|
|
|
player_playback_start();
|
2019-01-02 03:46:47 -05:00
|
|
|
}
|
|
|
|
|
2018-12-17 13:03:34 -05:00
|
|
|
return HTTP_OK;
|
2017-12-27 04:01:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jsonapi_reply_queue_tracks_move(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
uint32_t item_id;
|
|
|
|
uint32_t new_position;
|
|
|
|
const char *param;
|
|
|
|
struct player_status status;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = safe_atou32(hreq->uri_parsed->path_parts[3], &item_id);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "No valid item id given '%s'\n", hreq->uri_parsed->path);
|
|
|
|
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
param = evhttp_find_header(hreq->query, "new_position");
|
|
|
|
if (!param)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Missing parameter 'new_position'\n");
|
|
|
|
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
if (safe_atou32(param, &new_position) < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "No valid item new_position '%s'\n", param);
|
|
|
|
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
player_get_status(&status);
|
|
|
|
ret = db_queue_move_byitemid(item_id, new_position, status.shuffle);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Moving item '%d' to new position %d failed\n", item_id, new_position);
|
|
|
|
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HTTP_NOCONTENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jsonapi_reply_queue_tracks_delete(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
uint32_t item_id;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = safe_atou32(hreq->uri_parsed->path_parts[3], &item_id);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "No valid item id given '%s'\n", hreq->uri_parsed->path);
|
|
|
|
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = db_queue_delete_byitemid(item_id);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HTTP_NOCONTENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jsonapi_reply_queue_clear(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
player_playback_stop();
|
|
|
|
db_queue_clear(0);
|
|
|
|
|
|
|
|
return HTTP_NOCONTENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jsonapi_reply_queue(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
struct query_params query_params;
|
|
|
|
const char *param;
|
|
|
|
uint32_t item_id;
|
2019-05-28 17:40:41 -04:00
|
|
|
uint32_t count;
|
2017-12-27 04:01:36 -05:00
|
|
|
int start_pos, end_pos;
|
2020-02-22 04:03:13 -05:00
|
|
|
int version = 0;
|
2018-03-30 02:56:57 -04:00
|
|
|
char etag[21];
|
2017-12-27 04:01:36 -05:00
|
|
|
struct player_status status;
|
|
|
|
struct db_queue_item queue_item;
|
|
|
|
json_object *reply;
|
|
|
|
json_object *items;
|
|
|
|
json_object *item;
|
|
|
|
int ret = 0;
|
|
|
|
|
2020-02-22 04:03:13 -05:00
|
|
|
db_admin_getint(&version, DB_ADMIN_QUEUE_VERSION);
|
2019-05-28 17:40:41 -04:00
|
|
|
db_queue_get_count(&count);
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-30 02:56:57 -04:00
|
|
|
snprintf(etag, sizeof(etag), "%d", version);
|
|
|
|
if (httpd_request_etag_matches(hreq->req, etag))
|
|
|
|
return HTTP_NOTMODIFIED;
|
|
|
|
|
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
|
|
|
reply = json_object_new_object();
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
json_object_object_add(reply, "version", json_object_new_int(version));
|
2019-05-28 17:40:41 -04:00
|
|
|
json_object_object_add(reply, "count", json_object_new_int((int)count));
|
2017-12-27 04:01:36 -05:00
|
|
|
|
|
|
|
items = json_object_new_array();
|
|
|
|
json_object_object_add(reply, "items", items);
|
|
|
|
|
|
|
|
player_get_status(&status);
|
|
|
|
if (status.shuffle)
|
|
|
|
query_params.sort = S_SHUFFLE_POS;
|
|
|
|
|
|
|
|
param = evhttp_find_header(hreq->query, "id");
|
|
|
|
if (param && safe_atou32(param, &item_id) == 0)
|
|
|
|
{
|
|
|
|
query_params.filter = db_mprintf("id = %d", item_id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
param = evhttp_find_header(hreq->query, "start");
|
|
|
|
if (param && safe_atoi32(param, &start_pos) == 0)
|
|
|
|
{
|
|
|
|
param = evhttp_find_header(hreq->query, "end");
|
|
|
|
if (!param || safe_atoi32(param, &end_pos) != 0)
|
|
|
|
{
|
|
|
|
end_pos = start_pos + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query_params.sort == S_SHUFFLE_POS)
|
|
|
|
query_params.filter = db_mprintf("shuffle_pos >= %d AND shuffle_pos < %d", start_pos, end_pos);
|
|
|
|
else
|
|
|
|
query_params.filter = db_mprintf("pos >= %d AND pos < %d", start_pos, end_pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = db_queue_enum_start(&query_params);
|
|
|
|
if (ret < 0)
|
|
|
|
goto db_start_error;
|
|
|
|
|
|
|
|
while ((ret = db_queue_enum_fetch(&query_params, &queue_item)) == 0 && queue_item.id > 0)
|
|
|
|
{
|
|
|
|
item = queue_item_to_json(&queue_item, status.shuffle);
|
|
|
|
if (!item)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
json_object_array_add(items, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(reply));
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_LOG, L_WEB, "outputs: Couldn't add outputs to response buffer.\n");
|
|
|
|
|
|
|
|
error:
|
|
|
|
db_queue_enum_end(&query_params);
|
|
|
|
db_start_error:
|
|
|
|
jparse_free(reply);
|
|
|
|
free(query_params.filter);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jsonapi_reply_player_repeat(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
const char *param;
|
|
|
|
|
|
|
|
param = evhttp_find_header(hreq->query, "state");
|
|
|
|
if (!param)
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
|
|
|
|
if (strcmp(param, "single") == 0)
|
|
|
|
{
|
|
|
|
player_repeat_set(REPEAT_SONG);
|
|
|
|
}
|
|
|
|
else if (strcmp(param, "all") == 0)
|
|
|
|
{
|
|
|
|
player_repeat_set(REPEAT_ALL);
|
|
|
|
}
|
|
|
|
else if (strcmp(param, "off") == 0)
|
|
|
|
{
|
|
|
|
player_repeat_set(REPEAT_OFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
return HTTP_NOCONTENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jsonapi_reply_player_shuffle(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
const char *param;
|
|
|
|
bool shuffle;
|
|
|
|
|
|
|
|
param = evhttp_find_header(hreq->query, "state");
|
|
|
|
if (!param)
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
|
|
|
|
shuffle = (strcmp(param, "true") == 0);
|
|
|
|
player_shuffle_set(shuffle);
|
|
|
|
|
|
|
|
return HTTP_NOCONTENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jsonapi_reply_player_consume(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
const char *param;
|
|
|
|
bool consume;
|
2017-11-01 04:47:26 -04:00
|
|
|
|
|
|
|
param = evhttp_find_header(hreq->query, "state");
|
|
|
|
if (!param)
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_BADREQUEST;
|
2017-11-01 04:47:26 -04:00
|
|
|
|
|
|
|
consume = (strcmp(param, "true") == 0);
|
|
|
|
player_consume_set(consume);
|
|
|
|
|
2018-02-09 12:30:31 -05:00
|
|
|
return HTTP_NOCONTENT;
|
2017-11-01 04:47:26 -04:00
|
|
|
}
|
|
|
|
|
2019-01-20 07:13:01 -05:00
|
|
|
static int
|
|
|
|
volume_set(int volume, int step)
|
|
|
|
{
|
|
|
|
int new_volume;
|
|
|
|
struct player_status status;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
new_volume = volume;
|
|
|
|
|
|
|
|
if (step != 0)
|
|
|
|
{
|
|
|
|
// Calculate new volume from given step value
|
|
|
|
player_get_status(&status);
|
|
|
|
new_volume = status.volume + step;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we are setting a correct value
|
|
|
|
new_volume = new_volume > 100 ? 100 : new_volume;
|
|
|
|
new_volume = new_volume < 0 ? 0 : new_volume;
|
|
|
|
|
|
|
|
ret = player_volume_set(new_volume);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
output_volume_set(int volume, int step, uint64_t output_id)
|
|
|
|
{
|
|
|
|
int new_volume;
|
2019-01-22 11:47:15 -05:00
|
|
|
struct player_speaker_info speaker_info;
|
2019-01-20 07:13:01 -05:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
new_volume = volume;
|
|
|
|
|
|
|
|
if (step != 0)
|
|
|
|
{
|
|
|
|
// Calculate new output volume from the given step value
|
2020-01-03 13:38:52 -05:00
|
|
|
ret = player_speaker_get_byid(&speaker_info, output_id);
|
2019-01-20 07:13:01 -05:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "No output found for the given output id .\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_volume = speaker_info.absvol + step;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we are setting a correct value
|
|
|
|
new_volume = new_volume > 100 ? 100 : new_volume;
|
|
|
|
new_volume = new_volume < 0 ? 0 : new_volume;
|
|
|
|
|
|
|
|
ret = player_volume_setabs_speaker(output_id, new_volume);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-11-01 04:47:26 -04:00
|
|
|
static int
|
|
|
|
jsonapi_reply_player_volume(struct httpd_request *hreq)
|
|
|
|
{
|
2019-01-20 07:13:01 -05:00
|
|
|
const char *param_volume;
|
|
|
|
const char *param_step;
|
2017-11-01 04:47:26 -04:00
|
|
|
const char *param;
|
|
|
|
uint64_t output_id;
|
|
|
|
int volume;
|
2019-01-20 07:13:01 -05:00
|
|
|
int step;
|
2017-11-01 04:47:26 -04:00
|
|
|
int ret;
|
|
|
|
|
2019-01-20 07:13:01 -05:00
|
|
|
volume = 0;
|
|
|
|
step = 0;
|
2017-11-01 04:47:26 -04:00
|
|
|
|
2019-01-20 07:13:01 -05:00
|
|
|
// Parse and validate parameters
|
|
|
|
param_volume = evhttp_find_header(hreq->query, "volume");
|
|
|
|
if (param_volume)
|
|
|
|
{
|
|
|
|
ret = safe_atoi32(param_volume, &volume);
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
2017-11-01 04:47:26 -04:00
|
|
|
|
2019-01-20 07:13:01 -05:00
|
|
|
param_step = evhttp_find_header(hreq->query, "step");
|
|
|
|
if (param_step)
|
|
|
|
{
|
|
|
|
ret = safe_atoi32(param_step, &step);
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!param_volume && !param_step)
|
|
|
|
|| (param_volume && param_step))
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid parameters for player/volume request. Either 'volume' or 'step' parameter required.\n");
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
2017-11-01 04:47:26 -04:00
|
|
|
|
|
|
|
param = evhttp_find_header(hreq->query, "output_id");
|
|
|
|
if (param)
|
|
|
|
{
|
2019-01-20 07:13:01 -05:00
|
|
|
// Update volume for individual output
|
2017-11-01 04:47:26 -04:00
|
|
|
ret = safe_atou64(param, &output_id);
|
|
|
|
if (ret < 0)
|
2019-01-20 07:13:01 -05:00
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid value for parameter 'output_id'. Output id must be an integer (output_id='%s').\n", param);
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
ret = output_volume_set(volume, step, output_id);
|
2017-11-01 04:47:26 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-20 07:13:01 -05:00
|
|
|
// Update master volume
|
|
|
|
ret = volume_set(volume, step);
|
2017-11-01 04:47:26 -04:00
|
|
|
}
|
|
|
|
|
2018-02-09 12:30:31 -05:00
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_NOCONTENT;
|
2017-11-01 04:47:26 -04:00
|
|
|
}
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
static int
|
|
|
|
jsonapi_reply_library_artists(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
struct query_params query_params;
|
|
|
|
const char *param;
|
|
|
|
enum media_kind media_kind;
|
|
|
|
json_object *reply;
|
|
|
|
json_object *items;
|
|
|
|
int total;
|
|
|
|
int ret = 0;
|
|
|
|
|
2020-02-22 04:03:13 -05:00
|
|
|
if (!is_modified(hreq->req, DB_ADMIN_DB_UPDATE))
|
2018-03-30 02:56:57 -04:00
|
|
|
return HTTP_NOTMODIFIED;
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
media_kind = 0;
|
|
|
|
param = evhttp_find_header(hreq->query, "media_kind");
|
|
|
|
if (param)
|
|
|
|
{
|
|
|
|
media_kind = db_media_kind_enum(param);
|
|
|
|
if (!media_kind)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid media kind '%s'\n", param);
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reply = json_object_new_object();
|
|
|
|
items = json_object_new_array();
|
|
|
|
json_object_object_add(reply, "items", items);
|
|
|
|
|
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
|
|
|
|
|
|
|
ret = query_params_limit_set(&query_params, hreq);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
query_params.type = Q_GROUP_ARTISTS;
|
|
|
|
query_params.sort = S_ARTIST;
|
|
|
|
|
|
|
|
if (media_kind)
|
|
|
|
query_params.filter = db_mprintf("(f.media_kind = %d)", media_kind);
|
|
|
|
|
|
|
|
ret = fetch_artists(&query_params, items, &total);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
json_object_object_add(reply, "total", json_object_new_int(total));
|
|
|
|
json_object_object_add(reply, "offset", json_object_new_int(query_params.offset));
|
|
|
|
json_object_object_add(reply, "limit", json_object_new_int(query_params.limit));
|
|
|
|
|
|
|
|
ret = evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(reply));
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_LOG, L_WEB, "browse: Couldn't add artists to response buffer.\n");
|
|
|
|
|
|
|
|
error:
|
2019-08-28 16:35:29 -04:00
|
|
|
free_query_params(&query_params, 1);
|
2017-12-27 04:01:36 -05:00
|
|
|
jparse_free(reply);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
2018-03-17 15:20:29 -04:00
|
|
|
static int
|
|
|
|
jsonapi_reply_library_artist(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
const char *artist_id;
|
|
|
|
json_object *reply;
|
|
|
|
int ret = 0;
|
|
|
|
|
2020-02-22 04:03:13 -05:00
|
|
|
if (!is_modified(hreq->req, DB_ADMIN_DB_UPDATE))
|
2018-03-30 02:56:57 -04:00
|
|
|
return HTTP_NOTMODIFIED;
|
|
|
|
|
2018-03-17 15:20:29 -04:00
|
|
|
artist_id = hreq->uri_parsed->path_parts[3];
|
|
|
|
|
|
|
|
reply = fetch_artist(artist_id);
|
|
|
|
if (!reply)
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(reply));
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_LOG, L_WEB, "browse: Couldn't add artists to response buffer.\n");
|
|
|
|
|
|
|
|
error:
|
|
|
|
jparse_free(reply);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
static int
|
|
|
|
jsonapi_reply_library_artist_albums(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
struct query_params query_params;
|
|
|
|
const char *artist_id;
|
|
|
|
json_object *reply;
|
|
|
|
json_object *items;
|
|
|
|
int total;
|
|
|
|
int ret = 0;
|
|
|
|
|
2020-02-22 04:03:13 -05:00
|
|
|
if (!is_modified(hreq->req, DB_ADMIN_DB_UPDATE))
|
2018-03-30 02:56:57 -04:00
|
|
|
return HTTP_NOTMODIFIED;
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
artist_id = hreq->uri_parsed->path_parts[3];
|
|
|
|
|
|
|
|
reply = json_object_new_object();
|
|
|
|
items = json_object_new_array();
|
|
|
|
json_object_object_add(reply, "items", items);
|
|
|
|
|
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
|
|
|
|
|
|
|
ret = query_params_limit_set(&query_params, hreq);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
query_params.type = Q_GROUP_ALBUMS;
|
|
|
|
query_params.sort = S_ALBUM;
|
|
|
|
query_params.filter = db_mprintf("(f.songartistid = %q)", artist_id);
|
|
|
|
|
|
|
|
ret = fetch_albums(&query_params, items, &total);
|
|
|
|
free(query_params.filter);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
json_object_object_add(reply, "total", json_object_new_int(total));
|
|
|
|
json_object_object_add(reply, "offset", json_object_new_int(query_params.offset));
|
|
|
|
json_object_object_add(reply, "limit", json_object_new_int(query_params.limit));
|
|
|
|
|
|
|
|
ret = evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(reply));
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_LOG, L_WEB, "browse: Couldn't add albums to response buffer.\n");
|
|
|
|
|
|
|
|
error:
|
|
|
|
jparse_free(reply);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jsonapi_reply_library_albums(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
struct query_params query_params;
|
|
|
|
const char *param;
|
|
|
|
enum media_kind media_kind;
|
|
|
|
json_object *reply;
|
|
|
|
json_object *items;
|
|
|
|
int total;
|
|
|
|
int ret = 0;
|
|
|
|
|
2020-02-22 04:03:13 -05:00
|
|
|
if (!is_modified(hreq->req, DB_ADMIN_DB_UPDATE))
|
2018-03-30 02:56:57 -04:00
|
|
|
return HTTP_NOTMODIFIED;
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
media_kind = 0;
|
|
|
|
param = evhttp_find_header(hreq->query, "media_kind");
|
|
|
|
if (param)
|
|
|
|
{
|
|
|
|
media_kind = db_media_kind_enum(param);
|
|
|
|
if (!media_kind)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid media kind '%s'\n", param);
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reply = json_object_new_object();
|
|
|
|
items = json_object_new_array();
|
|
|
|
json_object_object_add(reply, "items", items);
|
|
|
|
|
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
|
|
|
|
|
|
|
ret = query_params_limit_set(&query_params, hreq);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
query_params.type = Q_GROUP_ALBUMS;
|
|
|
|
query_params.sort = S_ALBUM;
|
|
|
|
|
|
|
|
if (media_kind)
|
|
|
|
query_params.filter = db_mprintf("(f.media_kind = %d)", media_kind);
|
|
|
|
|
|
|
|
ret = fetch_albums(&query_params, items, &total);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
json_object_object_add(reply, "total", json_object_new_int(total));
|
|
|
|
json_object_object_add(reply, "offset", json_object_new_int(query_params.offset));
|
|
|
|
json_object_object_add(reply, "limit", json_object_new_int(query_params.limit));
|
|
|
|
|
|
|
|
ret = evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(reply));
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_LOG, L_WEB, "browse: Couldn't add albums to response buffer.\n");
|
|
|
|
|
|
|
|
error:
|
2019-08-28 16:35:29 -04:00
|
|
|
free_query_params(&query_params, 1);
|
2017-12-27 04:01:36 -05:00
|
|
|
jparse_free(reply);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
2018-03-17 15:20:29 -04:00
|
|
|
static int
|
|
|
|
jsonapi_reply_library_album(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
const char *album_id;
|
|
|
|
json_object *reply;
|
|
|
|
int ret = 0;
|
|
|
|
|
2020-02-22 04:03:13 -05:00
|
|
|
if (!is_modified(hreq->req, DB_ADMIN_DB_UPDATE))
|
2018-03-30 02:56:57 -04:00
|
|
|
return HTTP_NOTMODIFIED;
|
|
|
|
|
2018-03-17 15:20:29 -04:00
|
|
|
album_id = hreq->uri_parsed->path_parts[3];
|
|
|
|
|
|
|
|
reply = fetch_album(album_id);
|
|
|
|
if (!reply)
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(reply));
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_LOG, L_WEB, "browse: Couldn't add artists to response buffer.\n");
|
|
|
|
|
|
|
|
error:
|
|
|
|
jparse_free(reply);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
static int
|
|
|
|
jsonapi_reply_library_album_tracks(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
struct query_params query_params;
|
|
|
|
const char *album_id;
|
|
|
|
json_object *reply;
|
|
|
|
json_object *items;
|
|
|
|
int total;
|
|
|
|
int ret = 0;
|
|
|
|
|
2020-02-22 04:03:13 -05:00
|
|
|
if (!is_modified(hreq->req, DB_ADMIN_DB_MODIFIED))
|
2018-03-30 02:56:57 -04:00
|
|
|
return HTTP_NOTMODIFIED;
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
album_id = hreq->uri_parsed->path_parts[3];
|
|
|
|
|
|
|
|
reply = json_object_new_object();
|
|
|
|
items = json_object_new_array();
|
|
|
|
json_object_object_add(reply, "items", items);
|
|
|
|
|
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
|
|
|
|
|
|
|
ret = query_params_limit_set(&query_params, hreq);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
query_params.type = Q_ITEMS;
|
|
|
|
query_params.sort = S_ALBUM;
|
|
|
|
query_params.filter = db_mprintf("(f.songalbumid = %q)", album_id);
|
|
|
|
|
|
|
|
ret = fetch_tracks(&query_params, items, &total);
|
|
|
|
free(query_params.filter);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
json_object_object_add(reply, "total", json_object_new_int(total));
|
|
|
|
json_object_object_add(reply, "offset", json_object_new_int(query_params.offset));
|
|
|
|
json_object_object_add(reply, "limit", json_object_new_int(query_params.limit));
|
|
|
|
|
|
|
|
ret = evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(reply));
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_LOG, L_WEB, "browse: Couldn't add tracks to response buffer.\n");
|
|
|
|
|
|
|
|
error:
|
|
|
|
jparse_free(reply);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
2020-03-14 16:04:10 -04:00
|
|
|
static int
|
|
|
|
jsonapi_reply_library_album_tracks_put_byid(struct httpd_request *hreq)
|
|
|
|
{
|
2020-03-23 08:12:56 -04:00
|
|
|
const char *param;
|
|
|
|
int64_t album_id;;
|
2020-03-14 16:04:10 -04:00
|
|
|
int ret;
|
|
|
|
|
2020-03-23 08:12:56 -04:00
|
|
|
ret = safe_atoi64(hreq->uri_parsed->path_parts[3], &album_id);
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
2020-03-14 16:04:10 -04:00
|
|
|
|
2020-03-23 08:12:56 -04:00
|
|
|
param = evhttp_find_header(hreq->query, "play_count");
|
|
|
|
if (!param)
|
|
|
|
return HTTP_BADREQUEST;
|
2020-03-14 16:04:10 -04:00
|
|
|
|
2020-03-23 08:12:56 -04:00
|
|
|
if (strcmp(param, "increment") == 0)
|
|
|
|
{
|
|
|
|
db_file_inc_playcount_bysongalbumid(album_id, false);
|
|
|
|
}
|
|
|
|
else if (strcmp(param, "played") == 0)
|
|
|
|
{
|
|
|
|
db_file_inc_playcount_bysongalbumid(album_id, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_WEB, "Ignoring invalid play_count param '%s'\n", param);
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
2020-03-14 16:04:10 -04:00
|
|
|
|
2020-03-23 08:12:56 -04:00
|
|
|
return HTTP_OK;
|
2020-03-14 16:04:10 -04:00
|
|
|
}
|
|
|
|
|
2019-01-22 03:33:04 -05:00
|
|
|
static int
|
|
|
|
jsonapi_reply_library_tracks_get_byid(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
struct query_params query_params;
|
|
|
|
const char *track_id;
|
|
|
|
struct db_media_file_info dbmfi;
|
|
|
|
json_object *reply = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
|
2020-02-22 04:03:13 -05:00
|
|
|
if (!is_modified(hreq->req, DB_ADMIN_DB_MODIFIED))
|
2019-01-22 03:33:04 -05:00
|
|
|
return HTTP_NOTMODIFIED;
|
|
|
|
|
|
|
|
track_id = hreq->uri_parsed->path_parts[3];
|
|
|
|
|
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
|
|
|
|
|
|
|
query_params.type = Q_ITEMS;
|
|
|
|
query_params.filter = db_mprintf("(f.id = %q)", track_id);
|
|
|
|
|
|
|
|
ret = db_query_start(&query_params);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
ret = db_query_fetch_file(&query_params, &dbmfi);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (dbmfi.id == 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Track with id '%s' not found.\n", track_id);
|
|
|
|
ret = -1;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
reply = track_to_json(&dbmfi);
|
|
|
|
|
|
|
|
ret = evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(reply));
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_LOG, L_WEB, "browse: Couldn't add track to response buffer.\n");
|
|
|
|
|
|
|
|
error:
|
|
|
|
db_query_end(&query_params);
|
|
|
|
free(query_params.filter);
|
|
|
|
jparse_free(reply);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jsonapi_reply_library_tracks_put_byid(struct httpd_request *hreq)
|
|
|
|
{
|
2019-01-22 11:43:41 -05:00
|
|
|
int track_id;
|
|
|
|
const char *param;
|
|
|
|
int val;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = safe_atoi32(hreq->uri_parsed->path_parts[3], &track_id);
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
// Update play_count/skip_count
|
|
|
|
param = evhttp_find_header(hreq->query, "play_count");
|
|
|
|
if (param)
|
2020-03-23 08:12:56 -04:00
|
|
|
{
|
|
|
|
if (strcmp(param, "increment") == 0)
|
|
|
|
{
|
|
|
|
db_file_inc_playcount(track_id);
|
|
|
|
}
|
|
|
|
else if (strcmp(param, "reset") == 0)
|
|
|
|
{
|
|
|
|
db_file_reset_playskip_count(track_id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_WEB, "Ignoring invalid play_count value '%s' for track '%d'.\n", param, track_id);
|
|
|
|
}
|
|
|
|
}
|
2019-01-22 11:43:41 -05:00
|
|
|
|
|
|
|
// Update rating
|
|
|
|
param = evhttp_find_header(hreq->query, "rating");
|
|
|
|
if (param)
|
|
|
|
{
|
|
|
|
ret = safe_atoi32(param, &val);
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
|
|
|
|
if (val >= 0 && val <= DB_FILES_RATING_MAX)
|
|
|
|
ret = db_file_rating_update_byid(track_id, val);
|
|
|
|
else
|
|
|
|
DPRINTF(E_WARN, L_WEB, "Ignoring invalid rating value '%d' for track '%d'.\n", val, track_id);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HTTP_OK;
|
2019-01-22 03:33:04 -05:00
|
|
|
}
|
|
|
|
|
2020-04-18 06:30:00 -04:00
|
|
|
static int
|
|
|
|
jsonapi_reply_library_track_playlists(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
struct query_params query_params;
|
|
|
|
json_object *reply;
|
|
|
|
json_object *items;
|
|
|
|
char *path;
|
|
|
|
const char *track_id;
|
|
|
|
int id;
|
|
|
|
int total;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!is_modified(hreq->req, DB_ADMIN_DB_MODIFIED))
|
|
|
|
return HTTP_NOTMODIFIED;
|
|
|
|
|
|
|
|
track_id = hreq->uri_parsed->path_parts[3];
|
|
|
|
if (safe_atoi32(track_id, &id) < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Error converting track id '%s' to int.\n", track_id);
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
path = db_file_path_byid(id);
|
|
|
|
if (!path)
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_WEB, "No file path found for track with id '%s' not found.\n", track_id);
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
reply = json_object_new_object();
|
|
|
|
items = json_object_new_array();
|
|
|
|
json_object_object_add(reply, "items", items);
|
|
|
|
|
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
|
|
|
|
|
|
|
ret = query_params_limit_set(&query_params, hreq);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
query_params.type = Q_FIND_PL;
|
|
|
|
query_params.filter = db_mprintf("filepath = '%q'", path);
|
|
|
|
|
|
|
|
ret = fetch_playlists(&query_params, items, &total);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
json_object_object_add(reply, "total", json_object_new_int(total));
|
|
|
|
json_object_object_add(reply, "offset", json_object_new_int(query_params.offset));
|
|
|
|
json_object_object_add(reply, "limit", json_object_new_int(query_params.limit));
|
|
|
|
|
|
|
|
ret = evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(reply));
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_LOG, L_WEB, "track playlists: Couldn't add playlists to response buffer.\n");
|
|
|
|
|
|
|
|
error:
|
|
|
|
free_query_params(&query_params, 1);
|
|
|
|
jparse_free(reply);
|
|
|
|
free(path);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
static int
|
|
|
|
jsonapi_reply_library_playlists(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
struct query_params query_params;
|
|
|
|
json_object *reply;
|
|
|
|
json_object *items;
|
|
|
|
int total;
|
|
|
|
int ret = 0;
|
|
|
|
|
2020-02-22 04:03:13 -05:00
|
|
|
if (!is_modified(hreq->req, DB_ADMIN_DB_UPDATE))
|
2018-03-30 02:56:57 -04:00
|
|
|
return HTTP_NOTMODIFIED;
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
reply = json_object_new_object();
|
|
|
|
items = json_object_new_array();
|
|
|
|
json_object_object_add(reply, "items", items);
|
|
|
|
|
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
|
|
|
|
|
|
|
ret = query_params_limit_set(&query_params, hreq);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
query_params.type = Q_PL;
|
|
|
|
query_params.sort = S_PLAYLIST;
|
2020-03-08 16:13:19 -04:00
|
|
|
query_params.filter = db_mprintf("(f.type = %d OR f.type = %d OR f.type = %d)", PL_PLAIN, PL_SMART, PL_RSS);
|
2017-12-27 04:01:36 -05:00
|
|
|
|
|
|
|
ret = fetch_playlists(&query_params, items, &total);
|
|
|
|
free(query_params.filter);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
json_object_object_add(reply, "total", json_object_new_int(total));
|
|
|
|
json_object_object_add(reply, "offset", json_object_new_int(query_params.offset));
|
|
|
|
json_object_object_add(reply, "limit", json_object_new_int(query_params.limit));
|
|
|
|
|
|
|
|
ret = evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(reply));
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_LOG, L_WEB, "browse: Couldn't add playlists to response buffer.\n");
|
|
|
|
|
|
|
|
error:
|
|
|
|
jparse_free(reply);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
2018-03-17 15:20:29 -04:00
|
|
|
static int
|
2020-04-17 14:10:58 -04:00
|
|
|
jsonapi_reply_library_playlist_get(struct httpd_request *hreq)
|
2018-03-17 15:20:29 -04:00
|
|
|
{
|
2020-03-08 06:55:33 -04:00
|
|
|
uint32_t playlist_id;
|
|
|
|
json_object *reply = NULL;
|
2018-03-17 15:20:29 -04:00
|
|
|
int ret = 0;
|
|
|
|
|
2020-02-22 04:03:13 -05:00
|
|
|
if (!is_modified(hreq->req, DB_ADMIN_DB_UPDATE))
|
2018-03-30 02:56:57 -04:00
|
|
|
return HTTP_NOTMODIFIED;
|
|
|
|
|
2020-03-08 06:55:33 -04:00
|
|
|
ret = safe_atou32(hreq->uri_parsed->path_parts[3], &playlist_id);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Could not parse playlist id to integer\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (playlist_id == 0)
|
|
|
|
{
|
|
|
|
reply = json_object_new_object();
|
|
|
|
json_object_object_add(reply, "id", json_object_new_int(0));
|
|
|
|
json_object_object_add(reply, "name", json_object_new_string("Playlists"));
|
|
|
|
json_object_object_add(reply, "type", json_object_new_string(db_pl_type_label(PL_FOLDER)));
|
|
|
|
json_object_object_add(reply, "smart_playlist", json_object_new_boolean(false));
|
|
|
|
json_object_object_add(reply, "folder", json_object_new_boolean(true));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reply = fetch_playlist(playlist_id);
|
|
|
|
}
|
2018-03-17 15:20:29 -04:00
|
|
|
|
|
|
|
if (!reply)
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(reply));
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_LOG, L_WEB, "browse: Couldn't add playlist to response buffer.\n");
|
|
|
|
|
|
|
|
error:
|
|
|
|
jparse_free(reply);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
2020-04-17 14:10:58 -04:00
|
|
|
static int
|
|
|
|
playlist_attrib_query_limit_set(int playlist_id, const char *param)
|
|
|
|
{
|
|
|
|
struct playlist_info *pli;
|
|
|
|
int query_limit;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = safe_atoi32(param, &query_limit);
|
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pli = db_pl_fetch_byid(playlist_id);
|
|
|
|
if (!pli)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pli->query_limit = query_limit;
|
|
|
|
|
|
|
|
ret = db_pl_update(pli);
|
|
|
|
|
|
|
|
free_pli(pli, 0);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jsonapi_reply_library_playlist_put(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
uint32_t playlist_id;
|
|
|
|
const char *param;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = safe_atou32(hreq->uri_parsed->path_parts[3], &playlist_id);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Could not parse playlist id to integer\n");
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((param = evhttp_find_header(hreq->query, "query_limit")))
|
|
|
|
ret = playlist_attrib_query_limit_set(playlist_id, param);
|
|
|
|
else
|
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
static int
|
|
|
|
jsonapi_reply_library_playlist_tracks(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
struct query_params query_params;
|
|
|
|
json_object *reply;
|
|
|
|
json_object *items;
|
|
|
|
int playlist_id;
|
|
|
|
int total;
|
|
|
|
int ret = 0;
|
|
|
|
|
2020-08-29 06:40:49 -04:00
|
|
|
// Due to smart playlists possibly changing their tracks between rescans, disable caching in clients
|
|
|
|
httpd_response_not_cachable(hreq->req);
|
2018-03-30 02:56:57 -04:00
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
ret = safe_atoi32(hreq->uri_parsed->path_parts[3], &playlist_id);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "No valid playlist id given '%s'\n", hreq->uri_parsed->path);
|
|
|
|
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
reply = json_object_new_object();
|
|
|
|
items = json_object_new_array();
|
|
|
|
json_object_object_add(reply, "items", items);
|
|
|
|
|
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
|
|
|
|
|
|
|
ret = query_params_limit_set(&query_params, hreq);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
query_params.type = Q_PLITEMS;
|
|
|
|
query_params.id = playlist_id;
|
|
|
|
|
|
|
|
ret = fetch_tracks(&query_params, items, &total);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
json_object_object_add(reply, "total", json_object_new_int(total));
|
|
|
|
json_object_object_add(reply, "offset", json_object_new_int(query_params.offset));
|
|
|
|
json_object_object_add(reply, "limit", json_object_new_int(query_params.limit));
|
|
|
|
|
|
|
|
ret = evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(reply));
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_LOG, L_WEB, "playlist tracks: Couldn't add tracks to response buffer.\n");
|
|
|
|
|
|
|
|
error:
|
2019-08-28 16:35:29 -04:00
|
|
|
free_query_params(&query_params, 1);
|
2017-12-27 04:01:36 -05:00
|
|
|
jparse_free(reply);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
2020-03-21 17:20:59 -04:00
|
|
|
static int
|
|
|
|
jsonapi_reply_library_playlist_delete(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
uint32_t pl_id;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = safe_atou32(hreq->uri_parsed->path_parts[3], &pl_id);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "No valid playlist id given '%s'\n", hreq->uri_parsed->path);
|
|
|
|
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
db_pl_delete(pl_id);
|
|
|
|
|
|
|
|
return HTTP_NOCONTENT;
|
|
|
|
}
|
|
|
|
|
2020-02-23 05:40:39 -05:00
|
|
|
static int
|
|
|
|
jsonapi_reply_library_playlist_playlists(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
struct query_params query_params;
|
|
|
|
json_object *reply;
|
|
|
|
json_object *items;
|
|
|
|
int playlist_id;
|
|
|
|
int total;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!is_modified(hreq->req, DB_ADMIN_DB_MODIFIED))
|
|
|
|
return HTTP_NOTMODIFIED;
|
|
|
|
|
|
|
|
|
|
|
|
ret = safe_atoi32(hreq->uri_parsed->path_parts[3], &playlist_id);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "No valid playlist id given '%s'\n", hreq->uri_parsed->path);
|
|
|
|
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
reply = json_object_new_object();
|
|
|
|
items = json_object_new_array();
|
|
|
|
json_object_object_add(reply, "items", items);
|
|
|
|
|
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
|
|
|
|
|
|
|
ret = query_params_limit_set(&query_params, hreq);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
query_params.type = Q_PL;
|
|
|
|
query_params.sort = S_PLAYLIST;
|
2020-03-08 16:13:19 -04:00
|
|
|
query_params.filter = db_mprintf("f.parent_id = %d AND (f.type = %d OR f.type = %d OR f.type = %d OR f.type = %d)",
|
|
|
|
playlist_id, PL_PLAIN, PL_SMART, PL_RSS, PL_FOLDER);
|
2020-02-23 05:40:39 -05:00
|
|
|
|
|
|
|
ret = fetch_playlists(&query_params, items, &total);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
json_object_object_add(reply, "total", json_object_new_int(total));
|
|
|
|
json_object_object_add(reply, "offset", json_object_new_int(query_params.offset));
|
|
|
|
json_object_object_add(reply, "limit", json_object_new_int(query_params.limit));
|
|
|
|
|
|
|
|
ret = evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(reply));
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_LOG, L_WEB, "playlist tracks: Couldn't add tracks to response buffer.\n");
|
|
|
|
|
|
|
|
error:
|
|
|
|
free_query_params(&query_params, 1);
|
|
|
|
jparse_free(reply);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
2020-03-12 13:10:25 -04:00
|
|
|
static int
|
|
|
|
jsonapi_reply_library_playlist_tracks_put_byid(struct httpd_request *hreq)
|
|
|
|
{
|
2020-03-23 08:12:56 -04:00
|
|
|
const char *param;
|
2020-03-12 13:10:25 -04:00
|
|
|
int playlist_id;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = safe_atoi32(hreq->uri_parsed->path_parts[3], &playlist_id);
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
2020-03-23 08:12:56 -04:00
|
|
|
param = evhttp_find_header(hreq->query, "play_count");
|
|
|
|
if (!param)
|
|
|
|
return HTTP_BADREQUEST;
|
2020-03-12 13:10:25 -04:00
|
|
|
|
2020-03-23 08:12:56 -04:00
|
|
|
if (strcmp(param, "increment") == 0)
|
|
|
|
{
|
|
|
|
db_file_inc_playcount_byplid(playlist_id, false);
|
|
|
|
}
|
|
|
|
else if (strcmp(param, "played") == 0)
|
|
|
|
{
|
|
|
|
db_file_inc_playcount_byplid(playlist_id, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DPRINTF(E_WARN, L_WEB, "Ignoring invalid play_count param '%s'\n", param);
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
2020-03-12 13:10:25 -04:00
|
|
|
|
2020-03-23 08:12:56 -04:00
|
|
|
return HTTP_OK;
|
2020-03-12 13:10:25 -04:00
|
|
|
}
|
|
|
|
|
2019-04-10 15:04:00 -04:00
|
|
|
static int
|
|
|
|
jsonapi_reply_queue_save(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
const char *param;
|
|
|
|
char buf[PATH_MAX+7];
|
2019-05-30 05:13:26 -04:00
|
|
|
char *playlist_name = NULL;
|
|
|
|
int ret = 0;
|
2019-04-10 15:04:00 -04:00
|
|
|
|
|
|
|
if ((param = evhttp_find_header(hreq->query, "name")) == NULL)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid argument, missing 'name'\n");
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!allow_modifying_stored_playlists)
|
|
|
|
{
|
2019-06-01 02:02:46 -04:00
|
|
|
DPRINTF(E_LOG, L_WEB, "Modifying stored playlists is not enabled in the config file\n");
|
2019-04-10 15:04:00 -04:00
|
|
|
return 403;
|
|
|
|
}
|
|
|
|
|
2019-06-01 02:02:46 -04:00
|
|
|
if (access(default_playlist_directory, W_OK) < 0)
|
2019-04-10 15:04:00 -04:00
|
|
|
{
|
2019-06-01 02:02:46 -04:00
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid playlist save directory '%s'\n", default_playlist_directory);
|
2019-04-10 15:04:00 -04:00
|
|
|
return 403;
|
|
|
|
}
|
|
|
|
|
2019-05-30 05:13:26 -04:00
|
|
|
playlist_name = atrim(param);
|
|
|
|
|
|
|
|
if (strlen(playlist_name) < 1) {
|
|
|
|
free(playlist_name);
|
|
|
|
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Empty playlist name parameter is not allowed\n");
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
2019-06-01 02:02:46 -04:00
|
|
|
snprintf(buf, sizeof(buf), "/file:%s/%s", default_playlist_directory, playlist_name);
|
2019-05-30 05:13:26 -04:00
|
|
|
free(playlist_name);
|
2019-04-10 15:04:00 -04:00
|
|
|
|
|
|
|
ret = library_queue_save(buf);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
2018-09-02 03:05:18 -04:00
|
|
|
static int
|
|
|
|
jsonapi_reply_library_genres(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
struct query_params query_params;
|
|
|
|
const char *param;
|
|
|
|
enum media_kind media_kind;
|
|
|
|
json_object *reply;
|
|
|
|
json_object *items;
|
|
|
|
int total;
|
|
|
|
int ret;
|
|
|
|
|
2020-02-22 04:03:13 -05:00
|
|
|
if (!is_modified(hreq->req, DB_ADMIN_DB_UPDATE))
|
2018-09-02 03:05:18 -04:00
|
|
|
return HTTP_NOTMODIFIED;
|
|
|
|
|
|
|
|
media_kind = 0;
|
|
|
|
param = evhttp_find_header(hreq->query, "media_kind");
|
|
|
|
if (param)
|
|
|
|
{
|
|
|
|
media_kind = db_media_kind_enum(param);
|
|
|
|
if (!media_kind)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid media kind '%s'\n", param);
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reply = json_object_new_object();
|
|
|
|
items = json_object_new_array();
|
|
|
|
json_object_object_add(reply, "items", items);
|
|
|
|
|
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
|
|
|
|
|
|
|
ret = query_params_limit_set(&query_params, hreq);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
query_params.type = Q_BROWSE_GENRES;
|
|
|
|
query_params.idx_type = I_NONE;
|
|
|
|
|
|
|
|
if (media_kind)
|
|
|
|
query_params.filter = db_mprintf("(f.media_kind = %d)", media_kind);
|
|
|
|
|
|
|
|
ret = fetch_genres(&query_params, items, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
else
|
|
|
|
total = json_object_array_length(items);
|
|
|
|
|
|
|
|
json_object_object_add(reply, "total", json_object_new_int(total));
|
|
|
|
json_object_object_add(reply, "offset", json_object_new_int(query_params.offset));
|
|
|
|
json_object_object_add(reply, "limit", json_object_new_int(query_params.limit));
|
|
|
|
|
|
|
|
ret = evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(reply));
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_LOG, L_WEB, "browse: Couldn't add genres to response buffer.\n");
|
|
|
|
|
|
|
|
error:
|
|
|
|
jparse_free(reply);
|
|
|
|
free_query_params(&query_params, 1);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
static int
|
2018-04-22 02:40:23 -04:00
|
|
|
jsonapi_reply_library_count(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
const char *param_expression;
|
|
|
|
char *expression;
|
|
|
|
struct smartpl smartpl_expression;
|
|
|
|
struct query_params qp;
|
|
|
|
struct filecount_info fci;
|
|
|
|
json_object *jreply;
|
|
|
|
int ret;
|
|
|
|
|
2020-02-22 04:03:13 -05:00
|
|
|
if (!is_modified(hreq->req, DB_ADMIN_DB_UPDATE))
|
2018-04-27 00:42:57 -04:00
|
|
|
return HTTP_NOTMODIFIED;
|
|
|
|
|
2018-04-22 02:40:23 -04:00
|
|
|
memset(&qp, 0, sizeof(struct query_params));
|
|
|
|
qp.type = Q_COUNT_ITEMS;
|
|
|
|
|
|
|
|
param_expression = evhttp_find_header(hreq->query, "expression");
|
|
|
|
if (param_expression)
|
|
|
|
{
|
|
|
|
memset(&smartpl_expression, 0, sizeof(struct smartpl));
|
|
|
|
expression = safe_asprintf("\"query\" { %s }", param_expression);
|
|
|
|
ret = smartpl_query_parse_string(&smartpl_expression, expression);
|
|
|
|
free(expression);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
|
|
|
|
qp.filter = strdup(smartpl_expression.query_where);
|
|
|
|
free_smartpl(&smartpl_expression, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_NULL(L_WEB, jreply = json_object_new_object());
|
|
|
|
|
|
|
|
ret = db_filecount_get(&fci, &qp);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
json_object_object_add(jreply, "tracks", json_object_new_int(fci.count));
|
|
|
|
json_object_object_add(jreply, "artists", json_object_new_int(fci.artist_count));
|
|
|
|
json_object_object_add(jreply, "albums", json_object_new_int(fci.album_count));
|
|
|
|
json_object_object_add(jreply, "db_playtime", json_object_new_int64((fci.length / 1000)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "library: failed to get count info\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
free(qp.filter);
|
|
|
|
|
|
|
|
CHECK_ERRNO(L_WEB, evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(jreply)));
|
|
|
|
jparse_free(jreply);
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
2018-11-24 06:15:31 -05:00
|
|
|
static int
|
|
|
|
jsonapi_reply_library_files(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
const char *param;
|
|
|
|
int directory_id;
|
|
|
|
json_object *reply;
|
|
|
|
json_object *directories;
|
|
|
|
struct query_params query_params;
|
|
|
|
json_object *tracks;
|
|
|
|
json_object *tracks_items;
|
|
|
|
json_object *playlists;
|
|
|
|
json_object *playlists_items;
|
|
|
|
int total;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
param = evhttp_find_header(hreq->query, "directory");
|
|
|
|
|
|
|
|
directory_id = DIR_FILE;
|
|
|
|
if (param)
|
|
|
|
{
|
|
|
|
directory_id = db_directory_id_bypath(param);
|
|
|
|
if (directory_id <= 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
reply = json_object_new_object();
|
|
|
|
|
|
|
|
// Add sub directories to response
|
|
|
|
directories = json_object_new_array();
|
|
|
|
json_object_object_add(reply, "directories", directories);
|
|
|
|
|
|
|
|
ret = fetch_directories(directory_id, directories);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add tracks to response
|
|
|
|
tracks = json_object_new_object();
|
|
|
|
json_object_object_add(reply, "tracks", tracks);
|
|
|
|
tracks_items = json_object_new_array();
|
|
|
|
json_object_object_add(tracks, "items", tracks_items);
|
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
|
|
|
|
|
|
|
ret = query_params_limit_set(&query_params, hreq);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
query_params.type = Q_ITEMS;
|
2018-12-27 09:29:25 -05:00
|
|
|
query_params.sort = S_VPATH;
|
2018-11-24 06:15:31 -05:00
|
|
|
query_params.filter = db_mprintf("(f.directory_id = %d)", directory_id);
|
|
|
|
|
|
|
|
ret = fetch_tracks(&query_params, tracks_items, &total);
|
|
|
|
free(query_params.filter);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
json_object_object_add(tracks, "total", json_object_new_int(total));
|
|
|
|
json_object_object_add(tracks, "offset", json_object_new_int(query_params.offset));
|
|
|
|
json_object_object_add(tracks, "limit", json_object_new_int(query_params.limit));
|
|
|
|
|
|
|
|
// Add playlists
|
|
|
|
playlists = json_object_new_object();
|
|
|
|
json_object_object_add(reply, "playlists", playlists);
|
|
|
|
playlists_items = json_object_new_array();
|
|
|
|
json_object_object_add(playlists, "items", playlists_items);
|
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
|
|
|
|
|
|
|
ret = query_params_limit_set(&query_params, hreq);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
query_params.type = Q_PL;
|
2019-08-21 16:54:34 -04:00
|
|
|
query_params.sort = S_VPATH;
|
2018-11-24 06:15:31 -05:00
|
|
|
query_params.filter = db_mprintf("(f.directory_id = %d)", directory_id);
|
|
|
|
|
|
|
|
ret = fetch_playlists(&query_params, playlists_items, &total);
|
|
|
|
free(query_params.filter);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
json_object_object_add(playlists, "total", json_object_new_int(total));
|
|
|
|
json_object_object_add(playlists, "offset", json_object_new_int(query_params.offset));
|
|
|
|
json_object_object_add(playlists, "limit", json_object_new_int(query_params.limit));
|
|
|
|
|
|
|
|
// Build JSON response
|
|
|
|
ret = evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(reply));
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_LOG, L_WEB, "browse: Couldn't add directories to response buffer.\n");
|
|
|
|
|
|
|
|
error:
|
|
|
|
jparse_free(reply);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
2020-03-08 16:13:19 -04:00
|
|
|
static int
|
2020-03-21 17:20:59 -04:00
|
|
|
jsonapi_reply_library_add(struct httpd_request *hreq)
|
2020-03-08 16:13:19 -04:00
|
|
|
{
|
|
|
|
const char *url;
|
2020-03-21 17:20:59 -04:00
|
|
|
int ret;
|
2020-03-08 16:13:19 -04:00
|
|
|
|
|
|
|
url = evhttp_find_header(hreq->query, "url");
|
|
|
|
if (!url)
|
|
|
|
{
|
2020-03-21 17:20:59 -04:00
|
|
|
DPRINTF(E_LOG, L_WEB, "Missing URL parameter for library add\n");
|
2020-03-08 16:13:19 -04:00
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
|
2020-03-21 17:20:59 -04:00
|
|
|
ret = library_item_add(url);
|
2020-03-08 16:13:19 -04:00
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
2018-04-22 02:40:23 -04:00
|
|
|
static int
|
|
|
|
search_tracks(json_object *reply, struct httpd_request *hreq, const char *param_query, struct smartpl *smartpl_expression, enum media_kind media_kind)
|
2017-12-27 04:01:36 -05:00
|
|
|
{
|
|
|
|
json_object *type;
|
|
|
|
json_object *items;
|
2018-03-17 08:56:48 -04:00
|
|
|
struct query_params query_params;
|
2017-12-27 04:01:36 -05:00
|
|
|
int total;
|
2018-03-17 08:56:48 -04:00
|
|
|
int ret;
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
type = json_object_new_object();
|
|
|
|
json_object_object_add(reply, "tracks", type);
|
|
|
|
items = json_object_new_array();
|
|
|
|
json_object_object_add(type, "items", items);
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
query_params.type = Q_ITEMS;
|
|
|
|
query_params.sort = S_NAME;
|
|
|
|
|
|
|
|
ret = query_params_limit_set(&query_params, hreq);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (param_query)
|
2017-12-27 04:01:36 -05:00
|
|
|
{
|
2018-04-22 02:40:23 -04:00
|
|
|
if (media_kind)
|
|
|
|
query_params.filter = db_mprintf("(f.title LIKE '%%%q%%' AND f.media_kind = %d)", param_query, media_kind);
|
|
|
|
else
|
|
|
|
query_params.filter = db_mprintf("(f.title LIKE '%%%q%%')", param_query);
|
2018-03-17 08:56:48 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
query_params.filter = strdup(smartpl_expression->query_where);
|
2018-04-15 02:03:42 -04:00
|
|
|
query_params.order = safe_strdup(smartpl_expression->order);
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
if (smartpl_expression->limit > 0)
|
|
|
|
{
|
|
|
|
query_params.idx_type = I_SUB;
|
|
|
|
query_params.limit = smartpl_expression->limit;
|
|
|
|
query_params.offset = 0;
|
|
|
|
}
|
2017-12-27 04:01:36 -05:00
|
|
|
}
|
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
ret = fetch_tracks(&query_params, items, &total);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
json_object_object_add(type, "total", json_object_new_int(total));
|
|
|
|
json_object_object_add(type, "offset", json_object_new_int(query_params.offset));
|
|
|
|
json_object_object_add(type, "limit", json_object_new_int(query_params.limit));
|
|
|
|
|
|
|
|
out:
|
|
|
|
free_query_params(&query_params, 1);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-04-22 02:40:23 -04:00
|
|
|
search_artists(json_object *reply, struct httpd_request *hreq, const char *param_query, struct smartpl *smartpl_expression, enum media_kind media_kind)
|
2018-03-17 08:56:48 -04:00
|
|
|
{
|
|
|
|
json_object *type;
|
|
|
|
json_object *items;
|
|
|
|
struct query_params query_params;
|
|
|
|
int total;
|
|
|
|
int ret;
|
|
|
|
|
2017-12-27 04:01:36 -05:00
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
|
|
|
|
|
|
|
ret = query_params_limit_set(&query_params, hreq);
|
|
|
|
if (ret < 0)
|
2018-03-17 08:56:48 -04:00
|
|
|
goto out;
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
type = json_object_new_object();
|
|
|
|
json_object_object_add(reply, "artists", type);
|
|
|
|
items = json_object_new_array();
|
|
|
|
json_object_object_add(type, "items", items);
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
query_params.type = Q_GROUP_ARTISTS;
|
|
|
|
query_params.sort = S_ARTIST;
|
|
|
|
|
|
|
|
ret = query_params_limit_set(&query_params, hreq);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (param_query)
|
|
|
|
{
|
2018-04-22 02:40:23 -04:00
|
|
|
if (media_kind)
|
|
|
|
query_params.filter = db_mprintf("(f.album_artist LIKE '%%%q%%' AND f.media_kind = %d)", param_query, media_kind);
|
|
|
|
else
|
|
|
|
query_params.filter = db_mprintf("(f.album_artist LIKE '%%%q%%')", param_query);
|
2018-03-17 08:56:48 -04:00
|
|
|
}
|
|
|
|
else
|
2017-12-27 04:01:36 -05:00
|
|
|
{
|
2018-03-17 08:56:48 -04:00
|
|
|
query_params.filter = strdup(smartpl_expression->query_where);
|
|
|
|
query_params.having = safe_strdup(smartpl_expression->having);
|
2018-04-15 02:03:42 -04:00
|
|
|
query_params.order = safe_strdup(smartpl_expression->order);
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
if (smartpl_expression->limit > 0)
|
|
|
|
{
|
|
|
|
query_params.idx_type = I_SUB;
|
|
|
|
query_params.limit = smartpl_expression->limit;
|
|
|
|
query_params.offset = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
ret = fetch_artists(&query_params, items, &total);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
json_object_object_add(type, "total", json_object_new_int(total));
|
|
|
|
json_object_object_add(type, "offset", json_object_new_int(query_params.offset));
|
|
|
|
json_object_object_add(type, "limit", json_object_new_int(query_params.limit));
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
out:
|
|
|
|
free_query_params(&query_params, 1);
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
return ret;
|
|
|
|
}
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
static int
|
2018-04-22 02:40:23 -04:00
|
|
|
search_albums(json_object *reply, struct httpd_request *hreq, const char *param_query, struct smartpl *smartpl_expression, enum media_kind media_kind)
|
2018-03-17 08:56:48 -04:00
|
|
|
{
|
|
|
|
json_object *type;
|
|
|
|
json_object *items;
|
|
|
|
struct query_params query_params;
|
|
|
|
int total;
|
|
|
|
int ret;
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
ret = query_params_limit_set(&query_params, hreq);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
type = json_object_new_object();
|
|
|
|
json_object_object_add(reply, "albums", type);
|
|
|
|
items = json_object_new_array();
|
|
|
|
json_object_object_add(type, "items", items);
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
query_params.type = Q_GROUP_ALBUMS;
|
|
|
|
query_params.sort = S_ALBUM;
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
ret = query_params_limit_set(&query_params, hreq);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
if (param_query)
|
|
|
|
{
|
2018-04-22 02:40:23 -04:00
|
|
|
if (media_kind)
|
|
|
|
query_params.filter = db_mprintf("(f.album LIKE '%%%q%%' AND f.media_kind = %d)", param_query, media_kind);
|
|
|
|
else
|
|
|
|
query_params.filter = db_mprintf("(f.album LIKE '%%%q%%')", param_query);
|
2018-03-17 08:56:48 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
query_params.filter = strdup(smartpl_expression->query_where);
|
|
|
|
query_params.having = safe_strdup(smartpl_expression->having);
|
2018-04-15 02:03:42 -04:00
|
|
|
query_params.order = safe_strdup(smartpl_expression->order);
|
2018-03-17 08:56:48 -04:00
|
|
|
|
|
|
|
if (smartpl_expression->limit > 0)
|
|
|
|
{
|
|
|
|
query_params.idx_type = I_SUB;
|
|
|
|
query_params.limit = smartpl_expression->limit;
|
|
|
|
query_params.offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = fetch_albums(&query_params, items, &total);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
json_object_object_add(type, "total", json_object_new_int(total));
|
|
|
|
json_object_object_add(type, "offset", json_object_new_int(query_params.offset));
|
|
|
|
json_object_object_add(type, "limit", json_object_new_int(query_params.limit));
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
out:
|
|
|
|
free_query_params(&query_params, 1);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
search_playlists(json_object *reply, struct httpd_request *hreq, const char *param_query)
|
|
|
|
{
|
|
|
|
json_object *type;
|
|
|
|
json_object *items;
|
|
|
|
struct query_params query_params;
|
|
|
|
int total;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!param_query)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
memset(&query_params, 0, sizeof(struct query_params));
|
|
|
|
|
|
|
|
ret = query_params_limit_set(&query_params, hreq);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
type = json_object_new_object();
|
|
|
|
json_object_object_add(reply, "playlists", type);
|
|
|
|
items = json_object_new_array();
|
|
|
|
json_object_object_add(type, "items", items);
|
|
|
|
|
|
|
|
query_params.type = Q_PL;
|
|
|
|
query_params.sort = S_PLAYLIST;
|
2020-03-08 16:13:19 -04:00
|
|
|
query_params.filter = db_mprintf("((f.type = %d OR f.type = %d OR f.type = %d) AND f.title LIKE '%%%q%%')", PL_PLAIN, PL_SMART, PL_RSS, param_query);
|
2018-03-17 08:56:48 -04:00
|
|
|
|
|
|
|
ret = fetch_playlists(&query_params, items, &total);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
json_object_object_add(type, "total", json_object_new_int(total));
|
|
|
|
json_object_object_add(type, "offset", json_object_new_int(query_params.offset));
|
|
|
|
json_object_object_add(type, "limit", json_object_new_int(query_params.limit));
|
|
|
|
|
|
|
|
out:
|
|
|
|
free_query_params(&query_params, 1);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jsonapi_reply_search(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
const char *param_type;
|
|
|
|
const char *param_query;
|
|
|
|
const char *param_expression;
|
2018-04-22 02:40:23 -04:00
|
|
|
const char *param_media_kind;
|
|
|
|
enum media_kind media_kind;
|
2018-03-17 08:56:48 -04:00
|
|
|
char *expression;
|
|
|
|
struct smartpl smartpl_expression;
|
|
|
|
json_object *reply;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
reply = NULL;
|
|
|
|
|
|
|
|
param_type = evhttp_find_header(hreq->query, "type");
|
|
|
|
param_query = evhttp_find_header(hreq->query, "query");
|
|
|
|
param_expression = evhttp_find_header(hreq->query, "expression");
|
|
|
|
|
|
|
|
if (!param_type || (!param_query && !param_expression))
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Missing request parameter\n");
|
|
|
|
|
|
|
|
return HTTP_BADREQUEST;
|
2017-12-27 04:01:36 -05:00
|
|
|
}
|
|
|
|
|
2018-04-22 02:40:23 -04:00
|
|
|
media_kind = 0;
|
|
|
|
param_media_kind = evhttp_find_header(hreq->query, "media_kind");
|
|
|
|
if (param_media_kind)
|
|
|
|
{
|
|
|
|
media_kind = db_media_kind_enum(param_media_kind);
|
|
|
|
if (!media_kind)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid media kind '%s'\n", param_media_kind);
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
memset(&smartpl_expression, 0, sizeof(struct smartpl));
|
|
|
|
|
|
|
|
if (param_expression)
|
2017-12-27 04:01:36 -05:00
|
|
|
{
|
2018-03-17 08:56:48 -04:00
|
|
|
expression = safe_asprintf("\"query\" { %s }", param_expression);
|
2018-09-02 03:05:18 -04:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
ret = smartpl_query_parse_string(&smartpl_expression, expression);
|
|
|
|
free(expression);
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
}
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
reply = json_object_new_object();
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
if (strstr(param_type, "track"))
|
|
|
|
{
|
2018-04-22 02:40:23 -04:00
|
|
|
ret = search_tracks(reply, hreq, param_query, &smartpl_expression, media_kind);
|
2017-12-27 04:01:36 -05:00
|
|
|
if (ret < 0)
|
2018-03-17 08:56:48 -04:00
|
|
|
goto error;
|
|
|
|
}
|
2017-12-27 04:01:36 -05:00
|
|
|
|
2018-03-17 08:56:48 -04:00
|
|
|
if (strstr(param_type, "artist"))
|
|
|
|
{
|
2018-04-22 02:40:23 -04:00
|
|
|
ret = search_artists(reply, hreq, param_query, &smartpl_expression, media_kind);
|
2018-03-17 08:56:48 -04:00
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strstr(param_type, "album"))
|
|
|
|
{
|
2018-04-22 02:40:23 -04:00
|
|
|
ret = search_albums(reply, hreq, param_query, &smartpl_expression, media_kind);
|
2018-03-17 08:56:48 -04:00
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strstr(param_type, "playlist") && param_query)
|
|
|
|
{
|
|
|
|
ret = search_playlists(reply, hreq, param_query);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
2017-12-27 04:01:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = evbuffer_add_printf(hreq->reply, "%s", json_object_to_json_string(reply));
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_LOG, L_WEB, "playlist tracks: Couldn't add tracks to response buffer.\n");
|
|
|
|
|
|
|
|
error:
|
|
|
|
jparse_free(reply);
|
2018-03-17 08:56:48 -04:00
|
|
|
free_smartpl(&smartpl_expression, 1);
|
2017-12-27 04:01:36 -05:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
2020-07-11 09:10:11 -04:00
|
|
|
static int
|
|
|
|
jsonapi_reply_library_backup(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
ret = db_backup();
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
if (ret == -2)
|
|
|
|
return HTTP_SERVUNAVAIL; // not enabled by config
|
|
|
|
|
|
|
|
return HTTP_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
static struct httpd_uri_map adm_handlers[] =
|
2017-08-12 13:42:06 -04:00
|
|
|
{
|
2017-12-27 04:01:36 -05:00
|
|
|
{ EVHTTP_REQ_GET, "^/api/config$", jsonapi_reply_config },
|
2019-07-07 03:05:49 -04:00
|
|
|
{ EVHTTP_REQ_GET, "^/api/settings$", jsonapi_reply_settings_get },
|
|
|
|
{ EVHTTP_REQ_GET, "^/api/settings/[A-Za-z0-9_]+$", jsonapi_reply_settings_category_get },
|
|
|
|
{ EVHTTP_REQ_GET, "^/api/settings/[A-Za-z0-9_]+/[A-Za-z0-9_]+$", jsonapi_reply_settings_option_get },
|
|
|
|
{ EVHTTP_REQ_PUT, "^/api/settings/[A-Za-z0-9_]+/[A-Za-z0-9_]+$", jsonapi_reply_settings_option_put },
|
2020-05-15 18:04:37 -04:00
|
|
|
{ EVHTTP_REQ_DELETE, "^/api/settings/[A-Za-z0-9_]+/[A-Za-z0-9_]+$", jsonapi_reply_settings_option_delete },
|
2017-12-27 04:01:36 -05:00
|
|
|
{ EVHTTP_REQ_GET, "^/api/library$", jsonapi_reply_library },
|
2019-06-18 05:15:22 -04:00
|
|
|
{ EVHTTP_REQ_GET |
|
|
|
|
EVHTTP_REQ_PUT, "^/api/update$", jsonapi_reply_update },
|
2019-06-18 04:36:57 -04:00
|
|
|
{ EVHTTP_REQ_PUT, "^/api/rescan$", jsonapi_reply_meta_rescan },
|
2017-12-27 04:01:36 -05:00
|
|
|
{ EVHTTP_REQ_POST, "^/api/spotify-login$", jsonapi_reply_spotify_login },
|
2020-06-29 16:20:29 -04:00
|
|
|
{ EVHTTP_REQ_GET, "^/api/spotify-logout$", jsonapi_reply_spotify_logout },
|
2017-12-27 04:01:36 -05:00
|
|
|
{ EVHTTP_REQ_GET, "^/api/spotify$", jsonapi_reply_spotify },
|
|
|
|
{ EVHTTP_REQ_GET, "^/api/pairing$", jsonapi_reply_pairing_get },
|
2020-03-16 16:03:17 -04:00
|
|
|
{ EVHTTP_REQ_POST, "^/api/pairing$", jsonapi_reply_pairing_pair },
|
2017-12-27 04:01:36 -05:00
|
|
|
{ EVHTTP_REQ_POST, "^/api/lastfm-login$", jsonapi_reply_lastfm_login },
|
|
|
|
{ EVHTTP_REQ_GET, "^/api/lastfm-logout$", jsonapi_reply_lastfm_logout },
|
|
|
|
{ EVHTTP_REQ_GET, "^/api/lastfm$", jsonapi_reply_lastfm },
|
|
|
|
{ EVHTTP_REQ_POST, "^/api/verification$", jsonapi_reply_verification },
|
|
|
|
|
|
|
|
{ EVHTTP_REQ_GET, "^/api/outputs$", jsonapi_reply_outputs },
|
|
|
|
{ EVHTTP_REQ_PUT, "^/api/outputs/set$", jsonapi_reply_outputs_set },
|
|
|
|
{ EVHTTP_REQ_POST, "^/api/select-outputs$", jsonapi_reply_outputs_set }, // deprecated: use "/api/outputs/set"
|
|
|
|
{ EVHTTP_REQ_GET, "^/api/outputs/[[:digit:]]+$", jsonapi_reply_outputs_get_byid },
|
|
|
|
{ EVHTTP_REQ_PUT, "^/api/outputs/[[:digit:]]+$", jsonapi_reply_outputs_put_byid },
|
2019-09-26 02:00:03 -04:00
|
|
|
{ EVHTTP_REQ_PUT, "^/api/outputs/[[:digit:]]+/toggle$", jsonapi_reply_outputs_toggle_byid },
|
2017-12-27 04:01:36 -05:00
|
|
|
|
|
|
|
{ EVHTTP_REQ_GET, "^/api/player$", jsonapi_reply_player },
|
|
|
|
{ EVHTTP_REQ_PUT, "^/api/player/play$", jsonapi_reply_player_play },
|
|
|
|
{ EVHTTP_REQ_PUT, "^/api/player/pause$", jsonapi_reply_player_pause },
|
|
|
|
{ EVHTTP_REQ_PUT, "^/api/player/stop$", jsonapi_reply_player_stop },
|
2019-01-20 06:03:50 -05:00
|
|
|
{ EVHTTP_REQ_PUT, "^/api/player/toggle$", jsonapi_reply_player_toggle },
|
2017-12-27 04:01:36 -05:00
|
|
|
{ EVHTTP_REQ_PUT, "^/api/player/next$", jsonapi_reply_player_next },
|
|
|
|
{ EVHTTP_REQ_PUT, "^/api/player/previous$", jsonapi_reply_player_previous },
|
|
|
|
{ EVHTTP_REQ_PUT, "^/api/player/shuffle$", jsonapi_reply_player_shuffle },
|
|
|
|
{ EVHTTP_REQ_PUT, "^/api/player/repeat$", jsonapi_reply_player_repeat },
|
|
|
|
{ EVHTTP_REQ_PUT, "^/api/player/consume$", jsonapi_reply_player_consume },
|
|
|
|
{ EVHTTP_REQ_PUT, "^/api/player/volume$", jsonapi_reply_player_volume },
|
2018-03-17 15:20:29 -04:00
|
|
|
{ EVHTTP_REQ_PUT, "^/api/player/seek$", jsonapi_reply_player_seek },
|
2017-12-27 04:01:36 -05:00
|
|
|
|
|
|
|
{ EVHTTP_REQ_GET, "^/api/queue$", jsonapi_reply_queue },
|
|
|
|
{ EVHTTP_REQ_PUT, "^/api/queue/clear$", jsonapi_reply_queue_clear },
|
|
|
|
{ EVHTTP_REQ_POST, "^/api/queue/items/add$", jsonapi_reply_queue_tracks_add },
|
|
|
|
{ EVHTTP_REQ_PUT, "^/api/queue/items/[[:digit:]]+$", jsonapi_reply_queue_tracks_move },
|
|
|
|
{ EVHTTP_REQ_DELETE, "^/api/queue/items/[[:digit:]]+$", jsonapi_reply_queue_tracks_delete },
|
2019-04-10 15:04:00 -04:00
|
|
|
{ EVHTTP_REQ_POST, "^/api/queue/save$", jsonapi_reply_queue_save},
|
2017-12-27 04:01:36 -05:00
|
|
|
|
|
|
|
{ EVHTTP_REQ_GET, "^/api/library/playlists$", jsonapi_reply_library_playlists },
|
2020-04-17 14:10:58 -04:00
|
|
|
{ EVHTTP_REQ_GET, "^/api/library/playlists/[[:digit:]]+$", jsonapi_reply_library_playlist_get },
|
|
|
|
{ EVHTTP_REQ_PUT, "^/api/library/playlists/[[:digit:]]+$", jsonapi_reply_library_playlist_put },
|
2017-12-27 04:01:36 -05:00
|
|
|
{ EVHTTP_REQ_GET, "^/api/library/playlists/[[:digit:]]+/tracks$", jsonapi_reply_library_playlist_tracks },
|
2020-03-12 13:10:25 -04:00
|
|
|
{ EVHTTP_REQ_PUT, "^/api/library/playlists/[[:digit:]]+/tracks", jsonapi_reply_library_playlist_tracks_put_byid},
|
2017-12-27 04:01:36 -05:00
|
|
|
// { EVHTTP_REQ_POST, "^/api/library/playlists/[[:digit:]]+/tracks$", jsonapi_reply_library_playlists_tracks },
|
2020-03-21 17:20:59 -04:00
|
|
|
{ EVHTTP_REQ_DELETE, "^/api/library/playlists/[[:digit:]]+$", jsonapi_reply_library_playlist_delete },
|
2020-02-23 05:40:39 -05:00
|
|
|
{ EVHTTP_REQ_GET, "^/api/library/playlists/[[:digit:]]+/playlists", jsonapi_reply_library_playlist_playlists },
|
2017-12-27 04:01:36 -05:00
|
|
|
{ EVHTTP_REQ_GET, "^/api/library/artists$", jsonapi_reply_library_artists },
|
2018-03-17 15:20:29 -04:00
|
|
|
{ EVHTTP_REQ_GET, "^/api/library/artists/[[:digit:]]+$", jsonapi_reply_library_artist },
|
|
|
|
{ EVHTTP_REQ_GET, "^/api/library/artists/[[:digit:]]+/albums$", jsonapi_reply_library_artist_albums },
|
2017-12-27 04:01:36 -05:00
|
|
|
{ EVHTTP_REQ_GET, "^/api/library/albums$", jsonapi_reply_library_albums },
|
2018-03-17 15:20:29 -04:00
|
|
|
{ EVHTTP_REQ_GET, "^/api/library/albums/[[:digit:]]+$", jsonapi_reply_library_album },
|
2017-12-27 04:01:36 -05:00
|
|
|
{ EVHTTP_REQ_GET, "^/api/library/albums/[[:digit:]]+/tracks$", jsonapi_reply_library_album_tracks },
|
2020-03-14 16:04:10 -04:00
|
|
|
{ EVHTTP_REQ_PUT, "^/api/library/albums/[[:digit:]]+/tracks$", jsonapi_reply_library_album_tracks_put_byid },
|
2019-01-22 03:33:04 -05:00
|
|
|
{ EVHTTP_REQ_GET, "^/api/library/tracks/[[:digit:]]+$", jsonapi_reply_library_tracks_get_byid },
|
|
|
|
{ EVHTTP_REQ_PUT, "^/api/library/tracks/[[:digit:]]+$", jsonapi_reply_library_tracks_put_byid },
|
2020-04-18 06:30:00 -04:00
|
|
|
{ EVHTTP_REQ_GET, "^/api/library/tracks/[[:digit:]]+/playlists$", jsonapi_reply_library_track_playlists },
|
2018-09-02 03:05:18 -04:00
|
|
|
{ EVHTTP_REQ_GET, "^/api/library/genres$", jsonapi_reply_library_genres},
|
2018-04-22 02:40:23 -04:00
|
|
|
{ EVHTTP_REQ_GET, "^/api/library/count$", jsonapi_reply_library_count },
|
2018-11-24 06:15:31 -05:00
|
|
|
{ EVHTTP_REQ_GET, "^/api/library/files$", jsonapi_reply_library_files },
|
2020-03-21 17:20:59 -04:00
|
|
|
{ EVHTTP_REQ_POST, "^/api/library/add$", jsonapi_reply_library_add },
|
2020-07-11 09:10:11 -04:00
|
|
|
{ EVHTTP_REQ_PUT, "^/api/library/backup$", jsonapi_reply_library_backup },
|
2017-12-27 04:01:36 -05:00
|
|
|
|
|
|
|
{ EVHTTP_REQ_GET, "^/api/search$", jsonapi_reply_search },
|
2018-02-11 02:53:51 -05:00
|
|
|
|
|
|
|
{ 0, NULL, NULL }
|
2017-08-12 13:42:06 -04:00
|
|
|
};
|
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
|
|
|
|
/* ------------------------------- JSON API --------------------------------- */
|
|
|
|
|
2017-08-12 13:42:06 -04:00
|
|
|
void
|
2017-11-04 16:24:42 -04:00
|
|
|
jsonapi_request(struct evhttp_request *req, struct httpd_uri_parsed *uri_parsed)
|
2017-08-12 13:42:06 -04:00
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
struct httpd_request *hreq;
|
2017-08-12 13:42:06 -04:00
|
|
|
struct evkeyvalq *headers;
|
2018-02-09 12:30:31 -05:00
|
|
|
int status_code;
|
2017-08-12 13:42:06 -04:00
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
DPRINTF(E_DBG, L_WEB, "JSON api request: '%s'\n", uri_parsed->uri);
|
2017-08-12 13:42:06 -04:00
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
if (!httpd_admin_check_auth(req))
|
2017-11-11 15:17:42 -05:00
|
|
|
return;
|
2017-08-12 13:42:06 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
hreq = httpd_request_parse(req, uri_parsed, NULL, adm_handlers);
|
|
|
|
if (!hreq)
|
2017-08-12 13:42:06 -04:00
|
|
|
{
|
2017-11-07 18:32:03 -05:00
|
|
|
DPRINTF(E_LOG, L_WEB, "Unrecognized path '%s' in JSON api request: '%s'\n", uri_parsed->path, uri_parsed->uri);
|
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
httpd_send_error(req, HTTP_BADREQUEST, "Bad Request");
|
2017-08-12 13:42:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
CHECK_NULL(L_WEB, hreq->reply = evbuffer_new());
|
2017-11-04 16:24:42 -04:00
|
|
|
|
2018-02-09 12:30:31 -05:00
|
|
|
status_code = hreq->handler(hreq);
|
2017-08-12 13:42:06 -04:00
|
|
|
|
2018-12-30 02:33:31 -05:00
|
|
|
if (status_code >= 400)
|
|
|
|
DPRINTF(E_LOG, L_WEB, "JSON api request failed with error code %d (%s)\n", status_code, uri_parsed->uri);
|
|
|
|
|
2018-02-09 12:30:31 -05:00
|
|
|
switch (status_code)
|
|
|
|
{
|
|
|
|
case HTTP_OK: /* 200 OK */
|
2018-02-13 14:03:55 -05:00
|
|
|
headers = evhttp_request_get_output_headers(req);
|
2018-02-09 12:30:31 -05:00
|
|
|
evhttp_add_header(headers, "Content-Type", "application/json");
|
2018-02-24 01:15:12 -05:00
|
|
|
httpd_send_reply(req, status_code, "OK", hreq->reply, HTTPD_SEND_NO_GZIP);
|
2018-02-09 12:30:31 -05:00
|
|
|
break;
|
|
|
|
case HTTP_NOCONTENT: /* 204 No Content */
|
2018-02-24 01:15:12 -05:00
|
|
|
httpd_send_reply(req, status_code, "No Content", hreq->reply, HTTPD_SEND_NO_GZIP);
|
2018-02-09 12:30:31 -05:00
|
|
|
break;
|
2018-03-30 02:56:57 -04:00
|
|
|
case HTTP_NOTMODIFIED: /* 304 Not Modified */
|
|
|
|
httpd_send_reply(req, HTTP_NOTMODIFIED, NULL, NULL, HTTPD_SEND_NO_GZIP);
|
|
|
|
break;
|
2017-11-04 16:24:42 -04:00
|
|
|
|
2018-02-09 12:30:31 -05:00
|
|
|
case HTTP_BADREQUEST: /* 400 Bad Request */
|
|
|
|
httpd_send_error(req, status_code, "Bad Request");
|
|
|
|
break;
|
2019-04-10 15:04:00 -04:00
|
|
|
case 403:
|
|
|
|
httpd_send_error(req, status_code, "Forbidden");
|
|
|
|
break;
|
2018-02-09 12:30:31 -05:00
|
|
|
case HTTP_NOTFOUND: /* 404 Not Found */
|
|
|
|
httpd_send_error(req, status_code, "Not Found");
|
|
|
|
break;
|
2020-07-11 09:10:11 -04:00
|
|
|
case HTTP_SERVUNAVAIL: /* 503 */
|
|
|
|
httpd_send_error(req, status_code, "Service Unavailable");
|
|
|
|
break;
|
2018-02-09 12:30:31 -05:00
|
|
|
case HTTP_INTERNAL: /* 500 Internal Server Error */
|
|
|
|
default:
|
|
|
|
httpd_send_error(req, HTTP_INTERNAL, "Internal Server Error");
|
|
|
|
break;
|
|
|
|
}
|
2017-11-04 16:24:42 -04:00
|
|
|
|
2017-11-07 18:32:03 -05:00
|
|
|
evbuffer_free(hreq->reply);
|
|
|
|
free(hreq);
|
2017-08-12 13:42:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-11-04 16:24:42 -04:00
|
|
|
jsonapi_is_request(const char *path)
|
2017-08-12 13:42:06 -04:00
|
|
|
{
|
2017-11-04 16:24:42 -04:00
|
|
|
if (strncmp(path, "/api/", strlen("/api/")) == 0)
|
2017-08-12 13:42:06 -04:00
|
|
|
return 1;
|
2017-11-04 16:24:42 -04:00
|
|
|
if (strcmp(path, "/api") == 0)
|
2017-08-12 13:42:06 -04:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
jsonapi_init(void)
|
|
|
|
{
|
|
|
|
char buf[64];
|
2019-06-01 02:02:46 -04:00
|
|
|
char *temp_path;
|
2017-08-12 13:42:06 -04:00
|
|
|
int i;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
for (i = 0; adm_handlers[i].handler; i++)
|
|
|
|
{
|
|
|
|
ret = regcomp(&adm_handlers[i].preg, adm_handlers[i].regexp, REG_EXTENDED | REG_NOSUB);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
regerror(ret, &adm_handlers[i].preg, buf, sizeof(buf));
|
|
|
|
|
2017-11-04 16:24:42 -04:00
|
|
|
DPRINTF(E_FATAL, L_WEB, "JSON api init failed; regexp error: %s\n", buf);
|
2017-08-12 13:42:06 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-01 02:02:46 -04:00
|
|
|
default_playlist_directory = NULL;
|
2019-04-10 15:04:00 -04:00
|
|
|
allow_modifying_stored_playlists = cfg_getbool(cfg_getsec(cfg, "library"), "allow_modifying_stored_playlists");
|
|
|
|
if (allow_modifying_stored_playlists)
|
|
|
|
{
|
2019-06-01 02:02:46 -04:00
|
|
|
temp_path = cfg_getstr(cfg_getsec(cfg, "library"), "default_playlist_directory");
|
|
|
|
if (temp_path)
|
|
|
|
{
|
|
|
|
// The path in the conf file may have a trailing slash character. Return the realpath like it is done for the library directories.
|
|
|
|
default_playlist_directory = realpath(temp_path, NULL);
|
|
|
|
if (default_playlist_directory)
|
|
|
|
{
|
|
|
|
if (access(default_playlist_directory, W_OK) < 0)
|
|
|
|
DPRINTF(E_WARN, L_WEB, "Non-writable playlist save directory '%s'\n", default_playlist_directory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!default_playlist_directory)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid playlist save directory, disabling modifying stored playlists\n");
|
|
|
|
allow_modifying_stored_playlists = false;
|
|
|
|
}
|
2019-04-10 15:04:00 -04:00
|
|
|
}
|
|
|
|
|
2017-08-12 13:42:06 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
jsonapi_deinit(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; adm_handlers[i].handler; i++)
|
|
|
|
regfree(&adm_handlers[i].preg);
|
2019-06-01 02:02:46 -04:00
|
|
|
|
|
|
|
free(default_playlist_directory);
|
2017-08-12 13:42:06 -04:00
|
|
|
}
|