2018-10-07 15:54:38 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 Espen Jürgensen <espenjurgensen@gmail.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
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2022-12-20 16:59:36 -05:00
|
|
|
#include "httpd_internal.h"
|
2018-10-07 15:54:38 -04:00
|
|
|
#include "logger.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "player.h"
|
|
|
|
#include "artwork.h"
|
|
|
|
|
|
|
|
static int
|
|
|
|
request_process(struct httpd_request *hreq, uint32_t *max_w, uint32_t *max_h)
|
|
|
|
{
|
|
|
|
const char *param;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*max_w = 0;
|
|
|
|
*max_h = 0;
|
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
param = httpd_query_value_find(hreq->query, "maxwidth");
|
2018-10-07 15:54:38 -04:00
|
|
|
if (param)
|
|
|
|
{
|
|
|
|
ret = safe_atou32(param, max_w);
|
|
|
|
if (ret < 0)
|
2022-12-21 13:11:03 -05:00
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid width in request: '%s'\n", hreq->uri);
|
2018-10-07 15:54:38 -04:00
|
|
|
}
|
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
param = httpd_query_value_find(hreq->query, "maxheight");
|
2018-10-07 15:54:38 -04:00
|
|
|
if (param)
|
|
|
|
{
|
|
|
|
ret = safe_atou32(param, max_h);
|
|
|
|
if (ret < 0)
|
2022-12-21 13:11:03 -05:00
|
|
|
DPRINTF(E_LOG, L_WEB, "Invalid height in request: '%s'\n", hreq->uri);
|
2018-10-07 15:54:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
response_process(struct httpd_request *hreq, int format)
|
|
|
|
{
|
|
|
|
if (format == ART_FMT_PNG)
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_header_add(hreq->out_headers, "Content-Type", "image/png");
|
2018-10-07 15:54:38 -04:00
|
|
|
else if (format == ART_FMT_JPEG)
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_header_add(hreq->out_headers, "Content-Type", "image/jpeg");
|
2018-10-07 15:54:38 -04:00
|
|
|
else
|
|
|
|
return HTTP_NOCONTENT;
|
|
|
|
|
|
|
|
return HTTP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
artworkapi_reply_nowplaying(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
uint32_t max_w;
|
|
|
|
uint32_t max_h;
|
|
|
|
uint32_t id;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = request_process(hreq, &max_w, &max_h);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
|
2019-02-16 13:34:36 -05:00
|
|
|
ret = player_playing_now(&id);
|
2018-10-07 15:54:38 -04:00
|
|
|
if (ret != 0)
|
|
|
|
return HTTP_NOTFOUND;
|
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
ret = artwork_get_item(hreq->out_body, id, max_w, max_h, 0);
|
2018-10-07 15:54:38 -04:00
|
|
|
|
|
|
|
return response_process(hreq, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
artworkapi_reply_item(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
uint32_t max_w;
|
|
|
|
uint32_t max_h;
|
|
|
|
uint32_t id;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = request_process(hreq, &max_w, &max_h);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
ret = safe_atou32(hreq->path_parts[2], &id);
|
2018-10-07 15:54:38 -04:00
|
|
|
if (ret != 0)
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
ret = artwork_get_item(hreq->out_body, id, max_w, max_h, 0);
|
2018-10-07 15:54:38 -04:00
|
|
|
|
|
|
|
return response_process(hreq, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
artworkapi_reply_group(struct httpd_request *hreq)
|
|
|
|
{
|
|
|
|
uint32_t max_w;
|
|
|
|
uint32_t max_h;
|
|
|
|
uint32_t id;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = request_process(hreq, &max_w, &max_h);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
ret = safe_atou32(hreq->path_parts[2], &id);
|
2018-10-07 15:54:38 -04:00
|
|
|
if (ret != 0)
|
|
|
|
return HTTP_BADREQUEST;
|
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
ret = artwork_get_group(hreq->out_body, id, max_w, max_h, 0);
|
2018-10-07 15:54:38 -04:00
|
|
|
|
|
|
|
return response_process(hreq, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct httpd_uri_map artworkapi_handlers[] =
|
|
|
|
{
|
2022-12-21 13:11:03 -05:00
|
|
|
{ HTTPD_METHOD_GET, "^/artwork/nowplaying$", artworkapi_reply_nowplaying },
|
|
|
|
{ HTTPD_METHOD_GET, "^/artwork/item/[[:digit:]]+$", artworkapi_reply_item },
|
|
|
|
{ HTTPD_METHOD_GET, "^/artwork/group/[[:digit:]]+$", artworkapi_reply_group },
|
2018-10-07 15:54:38 -04:00
|
|
|
{ 0, NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* ------------------------------- API --------------------------------- */
|
2022-12-20 16:59:36 -05:00
|
|
|
|
|
|
|
static void
|
|
|
|
artworkapi_request(struct httpd_request *hreq)
|
2018-10-07 15:54:38 -04:00
|
|
|
{
|
|
|
|
int status_code;
|
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
if (!httpd_admin_check_auth(hreq))
|
2018-10-07 15:54:38 -04:00
|
|
|
return;
|
|
|
|
|
2022-12-20 16:59:36 -05:00
|
|
|
if (!hreq->handler)
|
2018-10-07 15:54:38 -04:00
|
|
|
{
|
2022-12-20 16:59:36 -05:00
|
|
|
DPRINTF(E_LOG, L_WEB, "Unrecognized path in artwork api request: '%s'\n", hreq->uri);
|
2018-10-07 15:54:38 -04:00
|
|
|
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_send_error(hreq, HTTP_BADREQUEST, "Bad Request");
|
2018-10-07 15:54:38 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_code = hreq->handler(hreq);
|
|
|
|
|
|
|
|
switch (status_code)
|
|
|
|
{
|
|
|
|
case HTTP_OK: /* 200 OK */
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send_reply(hreq, status_code, "OK", HTTPD_SEND_NO_GZIP);
|
2018-10-07 15:54:38 -04:00
|
|
|
break;
|
|
|
|
case HTTP_NOCONTENT: /* 204 No Content */
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send_reply(hreq, status_code, "No Content", HTTPD_SEND_NO_GZIP);
|
2018-10-07 15:54:38 -04:00
|
|
|
break;
|
|
|
|
case HTTP_NOTMODIFIED: /* 304 Not Modified */
|
2023-01-27 17:13:46 -05:00
|
|
|
httpd_send_reply(hreq, HTTP_NOTMODIFIED, NULL, HTTPD_SEND_NO_GZIP);
|
2018-10-07 15:54:38 -04:00
|
|
|
break;
|
|
|
|
case HTTP_BADREQUEST: /* 400 Bad Request */
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_send_error(hreq, status_code, "Bad Request");
|
2018-10-07 15:54:38 -04:00
|
|
|
break;
|
|
|
|
case HTTP_NOTFOUND: /* 404 Not Found */
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_send_error(hreq, status_code, "Not Found");
|
2018-10-07 15:54:38 -04:00
|
|
|
break;
|
|
|
|
case HTTP_INTERNAL: /* 500 Internal Server Error */
|
|
|
|
default:
|
2022-12-21 13:11:03 -05:00
|
|
|
httpd_send_error(hreq, HTTP_INTERNAL, "Internal Server Error");
|
2018-10-07 15:54:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 16:59:36 -05:00
|
|
|
struct httpd_module httpd_artworkapi =
|
2018-10-07 15:54:38 -04:00
|
|
|
{
|
2022-12-20 16:59:36 -05:00
|
|
|
.name = "Artwork API",
|
|
|
|
.type = MODULE_ARTWORKAPI,
|
2022-12-21 13:11:03 -05:00
|
|
|
.logdomain = L_WEB,
|
2022-12-20 16:59:36 -05:00
|
|
|
.subpaths = { "/artwork/", NULL },
|
|
|
|
.handlers = artworkapi_handlers,
|
|
|
|
.request = artworkapi_request,
|
|
|
|
};
|