Upload files to "/"

This commit is contained in:
8nl 2025-05-02 14:21:05 +00:00
parent eac855aebb
commit fb751c82d9
2 changed files with 77 additions and 39 deletions

View file

@ -2,10 +2,9 @@
Performance: Performance:
11 sec when processing a 4 sec when processing a
564kb database containing 304kb database containing
1685 files on orange pi 4 lts (cpu freq: 816Mhz) 1841 files on celeron n2840
And 5sec at 1.8GHz
Dependencies: Dependencies:
@ -13,10 +12,10 @@ Dependencies:
* *nix system * *nix system
The db contains a the filename and tags The db contains a the filename, tags and description
exp: exp:
image.png ,photo,sky image.png|photo,sky
cat.png ,animal,photo cat.png|animal,photo|Cute photo
To configure, edit config.h and recompile To configure, edit config.h and recompile

103
img.c
View file

@ -12,6 +12,7 @@
struct DB_STR { struct DB_STR {
char *filename; char *filename;
char *desc;
struct stat stat; struct stat stat;
char stat_filled; char stat_filled;
@ -77,7 +78,7 @@ void copy(const char *src, const char *dst) {
fclose(ofp); fclose(ofp);
} }
void add(char **list, const int size) { void add(char **list, const int size, const char *desc) {
char *name = NULL; char *name = NULL;
char new_path[PATH_MAX + 1]; char new_path[PATH_MAX + 1];
@ -105,17 +106,27 @@ void add(char **list, const int size) {
exit(1); exit(1);
} }
/* Write "filename ,tag1,tag2..." to the db */ /* Write "filename|tag1,tag2...|desc" to the db */
fprintf(fp, "%s ", name); fprintf(fp, "%s|", name);
for (int i = 1; i < size; i++) {
if (strchr(list[i], ',') == NULL || strchr(list[i], ' ') == NULL)
fprintf(fp, ",%s", list[i]);
else if (size > 1) {
fprintf(stderr, "8img: tag '%s' contain ',' or ' '\n", list[i]); for (int i = 1; i < size; i++) {
if (strchr(list[i], ',') == NULL || strchr(list[i], ' ') == NULL)
fprintf(fp, "%s%c", list[i], (i == size - 1) ? '|' : ',');
else
fprintf(stderr, "8img: tag '%s' contain ',' or ' '\n", list[i]);
}
} }
else
fputs("no_context|", fp);
if (desc)
fputs(desc, fp);
fputc('\n', fp); fputc('\n', fp);
free(name); free(name);
fclose(fp); fclose(fp);
} }
@ -132,6 +143,9 @@ void free_db(struct DB_STR *db) {
if (db->filename != NULL) if (db->filename != NULL)
free(db->filename); free(db->filename);
if (db->desc != NULL)
free(db->desc);
if (db->tags != NULL) { if (db->tags != NULL) {
for (size_t i = 0; i < db->size; i++) for (size_t i = 0; i < db->size; i++)
if (db->tags[i] != NULL) if (db->tags[i] != NULL)
@ -172,41 +186,49 @@ struct DB_STR *new_db_value(char *str) {
if (db == NULL) if (db == NULL)
return NULL; return NULL;
/* Filename */ memset(db, 0, sizeof(struct DB_STR));
char *tok = strtok(str, " ");
char *ptr = strrchr(tok, '\n'); char *ptr = strrchr(str, '\n');
if (ptr != NULL) if (ptr != NULL)
*ptr = '\0'; *ptr = '\0';
db->filename = strdup(tok); /* Filename */
if (db->filename == NULL) char *tok_rest = str;
char *tok = strtok_r(tok_rest, "|", &tok_rest);
if (tok != NULL) {
db->filename = strdup(tok);
if (db->filename == NULL)
goto NDV_CLOSE;
}
else
goto NDV_CLOSE; goto NDV_CLOSE;
/* Tags */ /* Tags */
tok = strtok(NULL, " "); tok = strtok_r(tok_rest, "|", &tok_rest);
if (tok == NULL) if (tok != NULL) {
db->tags = NULL; db->size = 1;
else {
ptr = strrchr(tok, '\n');
if (ptr != NULL)
*ptr = '\0';
for (size_t i = 0; i < strlen(tok); i++) for (size_t i = 0; i < strlen(tok); i++)
if (tok[i] == ',' && tok[i + 1]) if (tok[i] == ',' && tok[i + 1])
db->size++; db->size++;
size_t allocated = 0; size_t allocated = 0;
char *tok2 = strtok(tok, ",");
while (tok2 != NULL) { char *tok2_rest = tok;
char *tok2;
while ((tok2 = strtok_r(tok2_rest, ",", &tok2_rest)) != NULL) {
if (append_tag(db, tok2, allocated)) if (append_tag(db, tok2, allocated))
goto NDV_CLOSE; goto NDV_CLOSE;
allocated++; allocated++;
tok2 = strtok(NULL, ",");
} }
} }
/* Desc */
tok = strtok_r(tok_rest, "|", &tok_rest);
if (tok != NULL)
db->desc = strdup(tok);
/* Stats */ /* Stats */
char new_path[PATH_MAX + 1]; char new_path[PATH_MAX + 1];
snprintf(new_path, sizeof(new_path) - 1, "%s/%s", ROOT, db->filename); snprintf(new_path, sizeof(new_path) - 1, "%s/%s", ROOT, db->filename);
@ -269,12 +291,12 @@ RDB_CLOSE:
} }
struct DB_STR *build_html_page(FILE *fp, const char *file, const struct UNIQ_TAGS ut, struct DB_STR *db, const size_t page, const size_t pages) { struct DB_STR *build_html_page(FILE *fp, const char *file, const struct UNIQ_TAGS ut, struct DB_STR *db, const size_t page, const size_t pages) {
fprintf(fp, "<html>\n<head>\n<link rel='stylesheet' href='%s'>\n<title>%s</title>\n</head>\n<body>\n<a href='index.html'><center><img id='logo' src='%s'></center></a>\n<h1 id='desc'>%s<br>%s/p%zu</h1>\n", CSS, TITLE, LOGO, DESC, file, page); fprintf(fp, "<html>\n<head>\n<link rel='stylesheet' href='%s'>\n<title>%s</title>\n<meta charset='UTF-8'>\n</head>\n<body>\n<a href='index.html'> <center> <img id='logo' src='%s'> </center> </a>\n<h1 id='desc'>%s | <a href='%s'>RSS</a><br>%s/p%zu</h1>\n", CSS, TITLE, LOGO, DESC, XML_FILE, file, page);
/* Tags */ /* Tags */
fputs("<center><div class='alltags'>\n<a href='index.html'>all</a>", fp); fputs("<center><div class='alltags'>\n<a href='index.html'>all</a>", fp);
for (size_t i = 0; i < ut.size; i++) for (size_t i = 0; i < ut.size; i++)
fprintf(fp, "<a href='%s.html'> %s</a>", ut.tags[i], ut.tags[i]); fprintf(fp, "<a href='%s.html'> %s</a>", ut.tags[i], ut.tags[i]);
fputs("\n</center></div><br><br>\n", fp); fputs("\n</center></div><br><br>\n", fp);
@ -299,6 +321,9 @@ struct DB_STR *build_html_page(FILE *fp, const char *file, const struct UNIQ_TAG
for (size_t i = 0; i < p->size; i++) for (size_t i = 0; i < p->size; i++)
fprintf(fp, "<a href='%s.html'> %s</a>", p->tags[i], p->tags[i]); fprintf(fp, "<a href='%s.html'> %s</a>", p->tags[i], p->tags[i]);
if (p->desc)
fprintf(fp, " | %s", p->desc);
fputs("\n<br><br><br>\n", fp); fputs("\n<br><br><br>\n", fp);
p = i; p = i;
@ -497,7 +522,7 @@ void rebuild(void) {
} }
void help(void) { void help(void) {
puts("8img [add/rebuild/version]:\n\tadd [img] [tag1] [tag2] [...]\n\trebuild\n\tversion"); puts("8img -[d:] [add/rebuild/version]:\n\tadd [img] [tag1] [tag2]...\n\trebuild\n\tversion\nOptions:\n\t-d STR set description");
exit(0); exit(0);
} }
@ -508,18 +533,32 @@ int main(int argc, char **argv) {
mkdir(ROOT, 0777); mkdir(ROOT, 0777);
srand(getpid()); srand(getpid());
argv++; char *desc = NULL;
argc--;
int opt;
while ((opt = getopt(argc, argv, "d:t:")) != -1) {
switch (opt) {
case 'd':
desc = optarg;
break;
default:
help();
}
}
argv += optind;
argc -= optind;
/* Parse args */ /* Parse args */
if (!strcmp(argv[0], "add") && argc > 1) if (!strcmp(argv[0], "add") && argc > 1)
add(argv + 1, argc - 1); add(argv + 1, argc - 1, desc);
else if (!strcmp(argv[0], "rebuild")) else if (!strcmp(argv[0], "rebuild"))
rebuild(); rebuild();
else if (!strcmp(argv[0], "version")) else if (!strcmp(argv[0], "version"))
puts("8img version: 1.0.2\nWritten under WTFPL License."); puts("8img version: 1.1\nWritten under WTFPL License.");
else else
help(); help();