commit 2fca692

uint  ·  2026-01-31 04:33:18 +0000 UTC
parent 9f73103
add metadata: mtime, name, kind
3 files changed,  +49, -3
+1, -1
1@@ -812,7 +812,7 @@ int http_handle(int c)
2 		const char* type = mime_from_path(rel);
3 
4 		struct json j;
5-		if (json_meta(&j, &tmp, (size_t)st.st_size, type) < 0) {
6+		if (json_meta(&j, &tmp, (size_t)st.st_size, (long)st.st_mtime, type) < 0) {
7 			reply_text(c, hdr, HTTP_500, "json failed\n");
8 			return -1;
9 		}
+1, -1
1@@ -14,7 +14,7 @@ struct json {
2 
3 void json_free(struct json* j);
4 int json_library(struct json* j, const struct library* l);
5-int json_meta(struct json* j, const struct item* it, size_t size, const char* type);
6+int json_meta(struct json* j, const struct item* it, size_t size, long mtime, const char* type);
7 
8 #endif /* JSON_H */
9 
+47, -1
 1@@ -7,13 +7,24 @@
 2 #include "json.h"
 3 #include "log.h"
 4 
 5+static const char* json_basename(const char* path);
 6 static int json_grow(struct json* j, size_t need);
 7 static int json_hex64(struct json* j, uint64_t v);
 8+static const char* json_kind_from_type(const char* type);
 9 static int json_putc(struct json* j, const char c);
10 static int json_putn(struct json* j, const char* s, size_t n);
11 static int json_puts(struct json* j, const char* s);
12 static int json_string(struct json* j, const char* s);
13 
14+static const char* json_basename(const char* path)
15+{
16+	if (!path)
17+		return "";
18+
19+	const char* p = strrchr(path, '/');
20+	return p ? (p + 1) : path;
21+}
22+
23 static int json_grow(struct json* j, size_t need)
24 {
25 	if (j->len + need <= j->cap)
26@@ -49,6 +60,21 @@ static int json_hex64(struct json* j, uint64_t v)
27 	return json_string(j, hex);
28 }
29 
30+static const char* json_kind_from_type(const char* type)
31+{
32+	if (!type || type[0] == '\0')
33+		return "other";
34+
35+	if (strncmp(type, "audio/", 6) == 0)
36+		return "audio";
37+	if (strncmp(type, "video/", 6) == 0)
38+		return "video";
39+	if (strncmp(type, "image/", 6) == 0)
40+		return "image";
41+
42+	return "other";
43+}
44+
45 static int json_putc(struct json* j, char c)
46 {
47 	return json_putn(j, &c, 1);
48@@ -183,7 +209,7 @@ fail:
49 	return -1;
50 }
51 
52-int json_meta(struct json* j, const struct item* it, size_t size, const char* type)
53+int json_meta(struct json* j, const struct item* it, size_t size, long mtime, const char* type)
54 {
55 	memset(j, 0, sizeof(*j));
56 
57@@ -197,6 +223,12 @@ int json_meta(struct json* j, const struct item* it, size_t size, const char* ty
58 	if (json_string(j, it->path) < 0)
59 		goto fail;
60 
61+	/* basename */
62+	if (json_puts(j, ",\"name\":") < 0)
63+		goto fail;
64+	if (json_string(j, json_basename(it->path)) < 0)
65+		goto fail;
66+
67 	if (json_puts(j, ",\"size\":") < 0)
68 		goto fail;
69 
70@@ -206,11 +238,25 @@ int json_meta(struct json* j, const struct item* it, size_t size, const char* ty
71 	if (json_puts(j, tmp) < 0)
72 		goto fail;
73 
74+
75+	/* mtime (epoch seconds) */
76+	if (json_puts(j, ",\"mtime\":") < 0)
77+		goto fail;
78+	snprintf(tmp, sizeof(tmp), "%ld", mtime);
79+	if (json_puts(j, tmp) < 0)
80+		goto fail;
81+
82+	/* formats */
83 	if (json_puts(j, ",\"type\":") < 0)
84 		goto fail;
85 	if (json_string(j, type) < 0)
86 		goto fail;
87 
88+	if (json_puts(j, ",\"kind\":") < 0)
89+		goto fail;
90+	if (json_string(j, json_kind_from_type(type)) < 0)
91+		goto fail;
92+
93 	if (json_putc(j, '}') < 0)
94 		goto fail;
95