API endpoint photo/

This commit is contained in:
Jan-Piet Mens
2015-10-21 14:37:51 +02:00
parent 42feb5ee65
commit 08d7f055c0
4 changed files with 45 additions and 1 deletions

View File

@@ -442,6 +442,9 @@ The reported timestamp was the time at which this cache entry was made. Note tha
Requires POST method and _user_. This is currently incomplete; it simply writes a key into LMDB consisting of "blockme user".
#### `photo`
Requires GET method and _user_, and will return the `image/png` 40x40px photograph of a user if available in `STORAGEDIR/photos/`.
#### `kill`
@@ -499,7 +502,7 @@ This is invoked when the _recorder_ stops, which it doesn't really do unless you
This function is invoked at every location publish processed by the _recorder_. Your function is passed three arguments:
1. _topic_ is the topic published to (e.g. `owntracks/jane/phone`)
2. _type_ is the type of MQTT message. This is the `_type` in our JSON messages (e.g. `location`) or `"unknown"`.
2. _type_ is the type of MQTT message. This is the `_type` in our JSON messages (e.g. `location`, `cmd`, `transition`, ...) or `"unknown"`.
3. _location_ is a [Lua table](http://www.lua.org/pil/2.5.html) (associative array) with all the elements obtained in the JSON message. In the case of _type_ being `location`, we also add country code (`cc`) and the location's address (`addr`) unless reverse-geo lookups have been disabled in _recorder_.
Assume the following small example Lua script in `example.lua`:

28
http.c
View File

@@ -23,6 +23,7 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include "json.h"
@@ -445,6 +446,28 @@ static int dispatch(struct mg_connection *conn, const char *uri)
return (MG_TRUE);
}
/* /api/0/photo/?user=yyyy */
static int photo(struct mg_connection *conn)
{
char *u, *userphoto;
if ((u = field(conn, "user")) == NULL) {
mg_send_status(conn, 416);
mg_printf_data(conn, "missing username\n");
return (MG_TRUE);
}
if (((userphoto = storage_userphoto(u)) != NULL) && access(userphoto, R_OK) == 0) {
free(u);
mg_send_file(conn, userphoto, NULL);
return (MG_MORE);
}
free(u);
mg_send_status(conn, 404);
mg_printf_data(conn, "%s unreadable\n", userphoto);
return (MG_TRUE);
}
int ev_handler(struct mg_connection *conn, enum mg_event ev)
{
struct udata *ud = (struct udata *)conn->server_param;
@@ -494,11 +517,16 @@ int ev_handler(struct mg_connection *conn, enum mg_event ev)
return monitor(conn);
}
if (strncmp(conn->uri, "/api/0/photo/", strlen("/api/0/photo/")) == 0) {
return photo(conn);
}
if (strncmp(conn->uri, API_PREFIX, strlen(API_PREFIX)) == 0) {
return dispatch(conn, conn->uri + strlen(API_PREFIX) - 1);
}
if (!strcmp(conn->request_method, "POST")) {
if (!strcmp(conn->uri, "/block")) {

View File

@@ -1193,3 +1193,15 @@ void xml_output(JsonNode *json, output_type otype, JsonNode *fields, void (*func
json_delete(inttypes);
}
char *storage_userphoto(char *username)
{
static char path[BUFSIZ];
if (!username || !*username)
return (NULL);
snprintf(path, sizeof(path), "%s/photos/%s/%s.png", STORAGEDIR, username, username);
return (path);
}

View File

@@ -49,5 +49,6 @@ void storage_init(int revgeo);
void storage_gcache_dump(char *lmdbname);
void storage_gcache_load(char *lmdbname);
void xml_output(JsonNode *json, output_type otype, JsonNode *fields, void (*func)(char *s, void *param), void *param);
char *storage_userphoto(char *username);
#endif