diff options
| author | arf20 <aruizfernandez05@gmail.com> | 2025-12-02 21:36:03 +0100 |
|---|---|---|
| committer | arf20 <aruizfernandez05@gmail.com> | 2025-12-02 21:36:03 +0100 |
| commit | e3fd95c7a599aedb8798ab27813d2457d99be3b3 (patch) | |
| tree | 1a31c4ecc5072e90e84b4c90097f25fc4d28ce64 | |
| parent | f982fb95044581435e31c765487b45d5ee963519 (diff) | |
| download | arfnet2-search-e3fd95c7a599aedb8798ab27813d2457d99be3b3.tar.gz arfnet2-search-e3fd95c7a599aedb8798ab27813d2457d99be3b3.zip | |
generating results
| -rw-r--r-- | config.c | 10 | ||||
| -rw-r--r-- | config.h | 3 | ||||
| -rw-r--r-- | index.c | 9 | ||||
| -rw-r--r-- | index.htm.tmpl | 92 | ||||
| -rw-r--r-- | main.c | 52 | ||||
| -rwxr-xr-x | search | bin | 47088 -> 47776 bytes | |||
| -rw-r--r-- | search.cfg | 3 |
7 files changed, 143 insertions, 26 deletions
@@ -28,7 +28,7 @@ #include <errno.h> unsigned short port = 0; -char *tmpl_path = NULL, *root = NULL; +char *tmpl_path = NULL, *root = NULL, *subdir = NULL; int config_load(const char *conf_path) @@ -39,10 +39,6 @@ config_load(const char *conf_path) return -1; } - //fseek(cfgf , 0, SEEK_END); - //size_t cfgsize = ftell(cfgf); - //rewind(cfgf); - printf("config:\n"); char line[256]; @@ -75,6 +71,10 @@ config_load(const char *conf_path) value[strlen(value) - 1] = '\0'; root = strdup(value); printf("\troot: %s\n", root); + } else if (strcmp(line, "subdir") == 0) { + value[strlen(value) - 1] = '\0'; + subdir = strdup(value); + printf("\tsubdir: %s\n", subdir); } else { fprintf(stderr, "[config] unknown key: %s\n", line); continue; @@ -31,8 +31,7 @@ /* config */ extern unsigned short port; -extern char *tmpl_path; -extern char *root; +extern char *tmpl_path, *root, *subdir; int config_load(const char *conf_path); @@ -171,6 +171,7 @@ index_recurse(size_t size, const char *dir, int examine, size_t rootlen) /* stat it */ node_data_t *data = malloc(sizeof(node_data_t)); + memset(data, 0, sizeof(node_data_t)); data->name = strdup(de->d_name); data->path = strdup(&path[rootlen]); if (stat(path, &data->stat) < 0) { @@ -182,11 +183,13 @@ index_recurse(size_t size, const char *dir, int examine, size_t rootlen) /* examine */ if (examine) { - data->mime = magic_file(magic_cookie, path); - if (!data->mime) + const char *mime = magic_file(magic_cookie, path); + if (!mime) { fprintf(stderr, "[index] error magic_file() %s: %s\n", path, magic_error(magic_cookie)); - } + } else + data->mime = strdup(mime); + } /* recurse */ map_t *child = NULL; diff --git a/index.htm.tmpl b/index.htm.tmpl index fd21578..fdb1b58 100644 --- a/index.htm.tmpl +++ b/index.htm.tmpl @@ -5,10 +5,12 @@ <link rel="stylesheet" type="text/css" href="https://arf20.com/style.css"> <title>ARFNET</title> <style> +.searchform { + margin-bottom: 2em; +} + .box { - margin: auto; width: 50%; - padding-top: 20vh; min-width: 400px; display: block; } @@ -34,6 +36,23 @@ border: none; } +.label { + display: inline-block; + width: 200px; +} + +.collapse-title { + font-weight: bold; +} + +.sort-left { + display: inline-block; +} + +.sort-right { + display: inline-block; +} + .result { margin-left: 1em; margin-bottom: 1em; @@ -45,9 +64,26 @@ margin: 0; } +.mime { + font-size: 10pt; + margin-left: 10pt; +} + .path { } + +.attrib { + float: right; +} + +.size { + +} + +.time { + margin-left: 2em; +} </style> </head> @@ -59,12 +95,54 @@ <main> <h2 class="center">Search</h2> <p>Search all of the ARFNET content fast</p> - <div class="box"> - <form class="form-inline" action="/query" method="get"> + <form class="searchform" action="/query" method="get"> + <div class="box form-inline"> <input class="input" type="text" name="query" value="%s"> - <button type="submit">Search</button> - </form> - </div> + <button type="submit">Search</button><br> + </div> + <div> + <details> + <summary class="collapse-title">Advanced</summary> + <input type="radio" id="substr" name="type" value="substr"> + <label for="substr">substring</label> + <input type="radio" id="substr_nocase" name="type" value="substr_nocase"> + <label for="substr_nocase">case insensitive substring</label> + <input type="radio" id="exact" name="type" value="exact"> + <label for="exact">exact</label> + <input type="radio" id="regex" name="type" value="regex"> + <label for="regex">regex</label> + </details> + <details> + <summary class="collapse-title">Filtering</summary> + <label class="label" for="mtime_start">Timeframe start</label> + <input type="date" id="mtime_start" name="filter_mtime_start"><br> + <label class="label" for="mtime_end">Timeframe end</label> + <input type="date" id="mtime_end" name="filter_mtime_end"><br> + <label class="label" for="size_start">Size lower bound</label> + <input type="text" id="size_start" name="filter_size_start"><br> + <label class="label" for="size_end">Size upper bound</label> + <input type="text" id="size_end" name="filter_size_end"><br> + </details> + <details> + <summary class="collapse-title">Sorting</summary> + <div class="sort-left"> + <input type="radio" name="sort" id="name" value="name"> + <label for="name">name</label><br> + <input type="radio" name="sort" id="time" value="time"> + <label for="time">time</label><br> + <input type="radio" name="sort" id="size" value="size"> + <label for="size">size</label><br> + </div> + <div class="sort-right"> + <input type="radio" name="sort_dir" id="asc" value="asc"> + <label for="asc">ascending</label><br> + <input type="radio" name="sort_dir" id="desc" value="desc"> + <label for="desc">descending</label><br> + </div> + </details> + </div> + </form> + <hr> %s </main> </body> @@ -42,21 +42,48 @@ static char *index_format_template = NULL; static index_t g_index = NULL; static const char *result_html_template = - "<div class=\"result\"><p class=\"name\">%s</p><a class=\"path\">%s</a></div>\n"; + "<div class=\"result\">\n" + "<span class=\"name\">%s</span>""<super class=\"mime\">%s</super><br>\n" + "<a class=\"path\" href=\"%s\">%s</a><div class=\"attrib\"><span class=\"size\">%s</span><span class=\"time\">%s</span></div><br>\n" + "</div>\n"; +static const char * +sizestr(size_t size) +{ + static char buf[32]; + if (size < 1024) + snprintf(buf, 32, "%ld B", size); + else if (size < 1024LL * 1024LL) + snprintf(buf, 32, "%.2f KiB", (float)size/1024.f); + else if (size < 1024LL * 1024LL * 1024LL) + snprintf(buf, 32, "%.2f MiB", (float)size/(1024.f*1024.f)); + else if (size < 1024LL * 1024LL * 1024LL * 1024LL) + snprintf(buf, 32, "%.2f GiB", (float)size/(1024.f*1024.f*1024.f)); + else if (size < 1024LL * 1024LL * 1024LL * 1024LL * 1024LL) + snprintf(buf, 32, "%.2f TiB", (float)size/(1024.f*1024.f*1024.f*1024.f)); + return buf; +} static const char * generate_results_html(results_t *results) { - static char buff[65535]; + static char buff[65535], timebuf[256], urlbuf[4096]; char *pos = buff; for (int i = 0; i < results->size; i++) { + const node_data_t *data = results->results[i]; + struct tm *tm_mtim = gmtime(&data->stat.st_mtim.tv_sec); + strftime(timebuf, 256, "%Y-%m-%d %H:%M:%S", tm_mtim); + + snprintf(urlbuf, 4096, "%s%s", subdir, data->path); + pos += snprintf(pos, 65535 - (pos - buff), result_html_template, - results->results[i]->name, - results->results[i]->path + data->name, + data->mime ? data->mime : "", + urlbuf, data->path, + sizestr(data->stat.st_size), timebuf ); } @@ -103,15 +130,22 @@ enum MHD_Result answer_to_connection( const char *query = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "query"); - results_t *results = index_lookup(g_index, LOOKUP_SUBSTR, query); + results_t *results = NULL; + if (g_index) + results = index_lookup(g_index, LOOKUP_SUBSTR, query); - snprintf(buff, BUFF_SIZE, index_format_template, query, - generate_results_html(results)); + if (results) + snprintf(buff, BUFF_SIZE, index_format_template, query, + generate_results_html(results)); + else + snprintf(buff, BUFF_SIZE, index_format_template, query, + "indexing in progress... try again later"); response = MHD_create_response_from_buffer(strlen(buff), (void*)buff, MHD_RESPMEM_PERSISTENT); - results_destroy(results); + if (results) + results_destroy(results); printf("%d\n", 200); ret = MHD_queue_response(connection, MHD_HTTP_OK, response); @@ -171,7 +205,7 @@ int main() { printf("[%s] [index] indexeding started...\n", timestr); - g_index = index_new(INIT_MAP_CAPACITY, root, 0); + g_index = index_new(INIT_MAP_CAPACITY, root, 1); time_now = time(NULL); tm_now = gmtime(&time_now); Binary files differ@@ -9,3 +9,6 @@ template=index.htm.tmpl # root root=/home/arf20/projects/arfminesweeper +# http subdirectory for file links +subdir=/files/ + |
