Convert www_files to tree style

This commit is contained in:
Andrew Pamment 2018-10-22 10:47:20 +10:00
parent 947d4ab37f
commit d4e80de397
3 changed files with 168 additions and 55 deletions

View File

@ -296,8 +296,14 @@ char *www_create_link(int dir, int sub, int fid) {
}
char *www_files_display_listing(int dir, int sub) {
static const char *sql = "select id, filename, description, size, dlcount, uploaddate from files where approved=1 ORDER BY filename";
stralloc page = EMPTY_STRALLOC;
struct www_tag *page = www_tag_new(NULL, "");
struct www_tag *cur_tag;
struct www_tag *child_tag;
struct www_tag *child_child_tag;
struct www_tag *child_child_child_tag;
struct www_tag *child_child_child_child_tag;
struct www_tag *child_child_child_child_child_tag;
//stralloc page = EMPTY_STRALLOC;
char pathbuf[PATH_MAX];
sqlite3 *db = NULL;
sqlite3_stmt *res = NULL;
@ -312,43 +318,107 @@ char *www_files_display_listing(int dir, int sub) {
struct file_sub *fsub = ptr_vector_get(&fdir->file_subs, sub);
assert(fsub != NULL);
stralloc_copys(&page, "<div class=\"content-header\"><h2>Files: ");
stralloc_cats(&page, fdir->name);
stralloc_cats(&page, " - ");
stralloc_cats(&page, fsub->name);
stralloc_cats(&page, "</h2></div>\n");
cur_tag = www_tag_new("div", NULL);
www_tag_add_attrib(cur_tag, "class", "content-header");
www_tag_add_child(page, cur_tag);
child_tag = www_tag_new("h2", NULL);
www_tag_add_child(cur_tag, child_tag);
child_child_tag = www_tag_new(NULL, "Files: ");
www_tag_add_child(child_tag, child_child_tag);
child_child_tag = www_tag_new(NULL, fdir->name);
www_tag_add_child(child_tag, child_child_tag);
child_child_tag = www_tag_new(NULL, " - ");
www_tag_add_child(child_tag, child_child_tag);
child_child_tag = www_tag_new(NULL, fsub->name);
www_tag_add_child(child_tag, child_child_tag);
snprintf(pathbuf, sizeof pathbuf, "%s/%s.sq3", conf.bbs_path, fsub->database);
rc = sqlite3_open(pathbuf, &db);
if (rc != SQLITE_OK) {
free(page.s);
www_tag_destroy(page);
return NULL;
}
sqlite3_busy_timeout(db, 5000);
rc = sqlite3_prepare_v2(db, sql, -1, &res, 0);
if (rc != SQLITE_OK) {
sqlite3_close(db);
free(page.s);
www_tag_destroy(page);
return NULL;
}
stralloc_cats(&page, "<table class=\"fileentry\"><thead><tr><td>Filename</td><td>Size</td><td>Description</td></tr></thead><tbody>\n");
cur_tag = www_tag_new("table", NULL);
www_tag_add_attrib(cur_tag, "class", "fileentry");
www_tag_add_child(page, cur_tag);
child_tag = www_tag_new("thead", NULL);
www_tag_add_child(cur_tag, child_tag);
child_child_tag = www_tag_new("tr", NULL);
www_tag_add_child(child_tag, child_child_tag);
child_child_child_tag = www_tag_new("td", NULL);
www_tag_add_child(child_child_tag, child_child_child_tag);
child_child_child_child_tag = www_tag_new(NULL, "Filename");
www_tag_add_child(child_child_child_tag, child_child_child_child_tag);
child_child_child_tag = www_tag_new("td", NULL);
www_tag_add_child(child_child_tag, child_child_child_tag);
child_child_child_child_tag = www_tag_new(NULL, "Size");
www_tag_add_child(child_child_child_tag, child_child_child_child_tag);
child_child_child_tag = www_tag_new("td", NULL);
www_tag_add_child(child_child_tag, child_child_child_tag);
child_child_child_child_tag = www_tag_new(NULL, "Description");
www_tag_add_child(child_child_child_tag, child_child_child_child_tag);
child_tag = www_tag_new("tbody", NULL);
www_tag_add_child(cur_tag, child_tag);
while (sqlite3_step(res) == SQLITE_ROW) {
char *filename = strdup((char *)sqlite3_column_text(res, 1));
char *base_filename = basename(filename);
stralloc_cats(&page, "<tr><td class=\"filename\"><a href=\"");
stralloc_cats(&page, conf.www_url);
stralloc_cats(&page, "files/areas/");
stralloc_cat_long(&page, dir);
stralloc_append1(&page, '/');
stralloc_cat_long(&page, sub);
stralloc_append1(&page, '/');
www_encode(&page, base_filename);
stralloc_cats(&page, "\">");
stralloc_cats(&page, base_filename);
stralloc_cats(&page, "</a></td>");
child_child_tag = www_tag_new("tr", NULL);
www_tag_add_child(child_tag, child_child_tag);
child_child_child_tag = www_tag_new("td", NULL);
www_tag_add_attrib(child_child_child_tag, "class", "filename");
www_tag_add_child(child_child_tag, child_child_child_tag);
child_child_child_child_tag = www_tag_new("a", NULL);
www_tag_add_child(child_child_child_tag, child_child_child_child_tag);
stralloc url = EMPTY_STRALLOC;
stralloc_cats(&url, conf.www_url);
stralloc_cats(&url, "files/areas/");
stralloc_cat_long(&url, dir);
stralloc_append1(&url, '/');
stralloc_cat_long(&url, sub);
stralloc_append1(&url, '/');
www_encode(&url, base_filename);
stralloc_0(&url);
www_tag_add_attrib(child_child_child_child_tag, "href", url.s);
free(url.s);
child_child_child_child_child_tag = www_tag_new(NULL, base_filename);
www_tag_add_child(child_child_child_child_tag, child_child_child_child_child_tag);
int size = sqlite3_column_int(res, 3);
stralloc_cats(&page, "<td class=\"filesize\">");
child_child_child_tag = www_tag_new("td", NULL);
www_tag_add_attrib(child_child_child_tag, "class", "filesize");
www_tag_add_child(child_child_tag, child_child_child_tag);
int c = 'b';
if (size > 1024) {
size /= 1024;
@ -362,64 +432,92 @@ char *www_files_display_listing(int dir, int sub) {
size /= 1024;
c = 'G';
}
stralloc_cat_long(&page, size);
stralloc_append1(&page, c);
stralloc_cats(&page, "</td>");
stralloc size_str = EMPTY_STRALLOC;
stralloc_cat_long(&size_str, size);
stralloc_append1(&size_str, c);
stralloc_cats(&page, "<td class=\"filedesc\">");
child_child_child_child_tag = www_tag_new(NULL, size_str.s);
www_tag_add_child(child_child_child_tag, child_child_child_child_tag);
free(size_str.s);
child_child_child_tag = www_tag_new("td", NULL);
www_tag_add_attrib(child_child_child_tag, "class", "filedesc");
www_tag_add_child(child_child_tag, child_child_child_tag);
char *description = strdup((char *)sqlite3_column_text(res, 2));
for (char *p = description; *p != '\0'; ++p) {
if (*p == '\n')
*p = '\r';
}
struct www_tag *aha_out = www_tag_new(NULL, "");
aha(description, aha_out);
char *aha_data = www_tag_unwravel(aha_out);
stralloc_cats(&page, aha_data);
free(aha_data);
aha(description, child_child_child_tag);
free(description);
free(filename);
stralloc_cats(&page, "</td></tr>\n");
}
stralloc_cats(&page, "</tbody></table>\n");
stralloc_0(&page);
sqlite3_finalize(res);
sqlite3_close(db);
return page.s;
return www_tag_unwravel(page);
}
char *www_files_areas() {
stralloc page = EMPTY_STRALLOC;
struct www_tag *page = www_tag_new(NULL, "");
struct www_tag *cur_tag;
struct www_tag *child_tag;
struct www_tag *child_child_tag;
cur_tag = www_tag_new("div", NULL);
www_tag_add_attrib(cur_tag, "class", "content-header");
www_tag_add_child(page, cur_tag);
child_tag = www_tag_new("h2", NULL);
www_tag_add_child(cur_tag, child_tag);
child_child_tag = www_tag_new(NULL, "File Directories");
www_tag_add_child(child_tag, child_child_tag);
stralloc_copys(&page, "<div class=\"content-header\"><h2>File Directories</h2></div>\n");
for (size_t i = 0; i < ptr_vector_len(&conf.file_directories); ++i) {
struct file_directory *dir = ptr_vector_get(&conf.file_directories, i);
if (!dir->display_on_web)
continue;
stralloc_cats(&page, "<div class=\"conference-list-item\">");
stralloc_cats(&page, dir->name);
stralloc_cats(&page, "</div>\n");
cur_tag = www_tag_new("div", NULL);
www_tag_add_attrib(cur_tag, "class", "conference-list-item");
www_tag_add_child(page, cur_tag);
child_tag = www_tag_new(NULL, dir->name);
www_tag_add_child(cur_tag, child_tag);
for (size_t j = 0; j < ptr_vector_len(&dir->file_subs); ++j) {
struct file_sub *sub = ptr_vector_get(&dir->file_subs, j);
stralloc_cats(&page, "<div class=\"area-list-item\"><a href=\"");
stralloc_cats(&page, conf.www_url);
stralloc_cats(&page, "files/areas/");
stralloc_cat_long(&page, i);
stralloc_append1(&page, '/');
stralloc_cat_long(&page, j);
stralloc_cats(&page, "/\">");
stralloc_cats(&page, sub->name);
stralloc_cats(&page, "</a></div>\n");
cur_tag = www_tag_new("div", NULL);
www_tag_add_attrib(cur_tag, "class", "area-list-item");
www_tag_add_child(page, cur_tag);
child_tag = www_tag_new("a", NULL);
stralloc url = EMPTY_STRALLOC;
stralloc_cats(&url, conf.www_url);
stralloc_cats(&url, "files/areas/");
stralloc_cat_long(&url, i);
stralloc_append1(&url, '/');
stralloc_cat_long(&url, j);
stralloc_0(&url);
www_tag_add_attrib(child_tag, "href", url.s);
free(url.s);
www_tag_add_child(cur_tag, child_tag);
child_child_tag = www_tag_new(NULL, sub->name);
www_tag_add_child(child_tag, child_child_tag);
}
}
stralloc_0(&page);
return page.s;
return www_tag_unwravel(page);
}
char *www_files_get_from_area(int dir, int sub, char *clean_file) {

View File

@ -581,9 +581,23 @@ void www_tag_add_child(struct www_tag *tag, struct www_tag *child) {
ptr_vector_append(&tag->children, child);
}
char *www_tag_destroy(struct www_tag *tag) {
while (tag->children.len > 0) {
struct www_tag *child = ptr_vector_del(&tag->children, 0);
www_tag_destroy(child);
}
if (tag->tag != NULL) {
ptr_vector_apply(&tag->attribs, free);
destroy_ptr_vector(&tag->attribs);
ptr_vector_apply(&tag->values, free);
destroy_ptr_vector(&tag->values);
}
destroy_ptr_vector(&tag->children);
}
char *www_tag_unwravel(struct www_tag *tag) {
stralloc thedata = EMPTY_STRALLOC;
int children = tag->children.len;
while (tag->children.len > 0) {
struct www_tag *child = ptr_vector_del(&tag->children, 0);
if (child->children.len > 0) {

View File

@ -17,6 +17,7 @@ extern struct www_tag *www_tag_new(char *tag, char *data);
extern void www_tag_add_attrib(struct www_tag *tag, char *attrib, char *value);
extern struct www_tag *www_tag_duplicate(struct www_tag *oldtag);
extern void www_tag_add_child(struct www_tag *tag, struct www_tag *child);
extern char *www_tag_destroy(struct www_tag *tag);
extern char *www_tag_unwravel(struct www_tag *tag);
#endif
#endif