NEW: tour views are stored in/read from STORAGEDIR/tours/

this separates manually created views from those submitted
	automatically from the apps, and solves the problem of having to
	make viewsdir writeable by the process owner of ot-recorder,
	which I find is a cleaner solution.
This commit is contained in:
Jan-Piet Mens
2022-08-05 20:16:34 +02:00
parent 42d08e3cd1
commit 577dc56d8d
5 changed files with 54 additions and 10 deletions

View File

@@ -244,7 +244,7 @@ This section lists the most important options of the Recorder with their long na
`--docroot` overrides the compile-time setting of the HTTP document root.
`--viewsdir` overrides the path to the JSON views, which defaults to `<docroot>/views`.
`--viewsdir` overrides the path to the JSON views, which defaults to `<docroot>/views`. (Note that for the experimental _tours_ functionality the directory for the _tour views_ is in `<STORAGEDIR>/tours`.)
`--lua-script` specifies the path to the Lua script. If not given, Lua support is disabled.

18
http.c
View File

@@ -139,8 +139,17 @@ static JsonNode *loadview(struct udata *ud, const char *viewname)
view = json_mkobject();
if (json_copy_from_file(view, UB(fpath)) != TRUE) {
#ifdef WITH_SHARES
/* Now try second possibility */
utstring_renew(fpath);
utstring_printf(fpath, "%s/%s.json", toursdir(), viewname);
if (json_copy_from_file(view, UB(fpath)) != TRUE) {
#endif
json_delete(view);
return (NULL);
#ifdef WITH_SHARES
}
#endif
}
return (view);
@@ -962,8 +971,17 @@ static int view(struct mg_connection *conn, const char *viewname)
debug(ud, "page file=%s", UB(fpath));
if ((fp = fopen(UB(fpath), "r")) == NULL) {
#ifdef WITH_SHARES
utstring_renew(fpath);
utstring_printf(fpath, "%s/%s", toursdir(), j->string_);
debug(ud, "page file=%s", UB(fpath));
if ((fp = fopen(UB(fpath), "r")) == NULL) {
#endif
json_delete(view);
return send_status(conn, 404, "Cannot open view page");
#ifdef WITH_SHARES
}
#endif
}
mg_send_header(conn, "Content-type", "text/html");

View File

@@ -460,11 +460,8 @@ void do_request(struct udata *ud, UT_string *username, UT_string *device, char *
json_append_member(o, "uuid", json_mkstring(uuid));
json_append_member(o, "url", json_mkstring(UB(url)));
snprintf(path, sizeof(path), "%s/%s.json", ud->viewsdir, uuid);
olog(LOG_DEBUG, "New share %s for %s/%s", uuid, UB(username), UB(device));
if ((fp = fopen(path, "w")) != NULL) {
snprintf(path, sizeof(path), "%s.json", uuid);
if ((fp = tourfile(ud, path, "w")) != NULL) {
char *js = json_stringify(o, " ");
fprintf(fp, "%s\n", js);
free(js);
@@ -503,7 +500,6 @@ void do_request(struct udata *ud, UT_string *username, UT_string *device, char *
} else if (strcmp(request_type, "shares") == 0) {
//FILE *fp;
JsonNode *arr, *o;
char path[BUFSIZ];
DIR *dirp;
@@ -517,7 +513,7 @@ void do_request(struct udata *ud, UT_string *username, UT_string *device, char *
arr = json_mkarray();
if ((dirp = opendir(ud->viewsdir)) != NULL) {
if ((dirp = opendir(toursdir())) != NULL) {
while ((dp = readdir(dirp)) != NULL) {
char *fn = dp->d_name;
@@ -529,7 +525,7 @@ void do_request(struct udata *ud, UT_string *username, UT_string *device, char *
continue;
o = json_mkobject();
snprintf(path, sizeof(path), "%s/%s", ud->viewsdir, fn);
snprintf(path, sizeof(path), "%s/%s", toursdir(), fn);
if (json_copy_from_file(o, path) == false) {
olog(LOG_ERR, "Can't copy JSON from %s", path);
json_delete(o);
@@ -578,7 +574,7 @@ void do_request(struct udata *ud, UT_string *username, UT_string *device, char *
olog(LOG_DEBUG, "Unshare %s for %s/%s", r->string_, UB(username), UB(device));
snprintf(path, sizeof(path), "%s/%s.json", ud->viewsdir, r->string_);
snprintf(path, sizeof(path), "%s/%s.json", toursdir(), r->string_);
if (access(path, R_OK) < 0) {
olog(LOG_ERR, "Can't find share %s: %m", r->string_);
}

28
util.c
View File

@@ -642,4 +642,32 @@ char *uuid4()
return (uustr);
}
char *toursdir()
{
static UT_string *path = NULL;
utstring_renew(path);
utstring_printf(path, "%s/tours", STORAGEDIR);
return (UB(path));
}
FILE *tourfile(struct udata *ud, char *filename, char *mode)
{
static UT_string *path = NULL;
utstring_renew(path);
utstring_printf(path, "%s", toursdir());
ut_clean(path);
if (mkpath(UB(path)) < 0) {
olog(LOG_ERR, "Cannot create directory at %s: %m", UB(path));
return (NULL);
}
utstring_printf(path, "/%s", filename);
return (fopen(UB(path), mode));
}
#endif

2
util.h
View File

@@ -42,6 +42,8 @@ void chomp(char *s);
double number(JsonNode *j, char *element);
#ifdef WITH_SHARES
char *uuid4(void);
char *toursdir(void);
FILE *tourfile(struct udata *ud, char *filename, char *mode);
#endif
#endif