]> Git repositories of Nishi - repoview.git/commitdiff
magick works
authorNishi <nishi@nishi.boats>
Thu, 22 Aug 2024 05:25:09 +0000 (05:25 +0000)
committerNishi <nishi@nishi.boats>
Thu, 22 Aug 2024 05:25:09 +0000 (05:25 +0000)
git-svn-id: file:///raid/svn-personal/repoview/trunk@42 7e8b2a19-8934-dd40-8cb3-db22cdd5a80f

CGI/magick.c [new file with mode: 0644]
CGI/main.c
CGI/multipart.c
CGI/rv_magick.h [new file with mode: 0644]
CGI/theme/modern.c
objs.c

diff --git a/CGI/magick.c b/CGI/magick.c
new file mode 100644 (file)
index 0000000..bb45614
--- /dev/null
@@ -0,0 +1,40 @@
+/* $Id$ */
+
+#include "rv_magick.h"
+
+#include "rv_util.h"
+
+#include "../config.h"
+
+#include <stdio.h>
+#include <stdbool.h>
+
+#include <wand/magick_wand.h>
+
+void rv_init_magick(void) {}
+
+bool rv_resize_picture(const char* input, const char* output, char** reason) {
+       MagickWand* wand = NULL;
+       wand = NewMagickWand();
+       if(MagickReadImage(wand, input)) {
+               int width = MagickGetImageWidth(wand);
+               int height = MagickGetImageHeight(wand);
+
+               double scale = 0;
+               if(width >= height) {
+                       scale = (double)256 / width;
+               } else if(width < height) {
+                       scale = (double)256 / height;
+               }
+
+               MagickResizeImage(wand, width * scale, height * scale, LanczosFilter, 1);
+               MagickSetCompressionQuality(wand, 80);
+               MagickWriteImage(wand, output);
+
+               if(wand) DestroyMagickWand(wand);
+               return true;
+       }
+       if(wand) DestroyMagickWand(wand);
+       *reason = rv_strdup("Magick error");
+       return false;
+}
index dd7171e057c3788aa60dbb25e7064565f1a8839e..c03f6823ca542e6755c8e2e0fc281b9735f0b081 100644 (file)
 #include "rv_auth.h"
 #include "rv_multipart.h"
 
+#ifdef USE_GRAPHICSMAGICK
+#include "rv_magick.h"
+#endif
+
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
 
 char* postdata;
+char* nocache;
 
 int main() {
        srand(time(NULL));
+       nocache = malloc(512);
+       sprintf(nocache, "?time=%llu", (unsigned long long)time(NULL));
        rv_check_sanity();
        rv_init_db();
+       rv_init_magick();
        rv_parse_query(getenv("QUERY_STRING"));
        rv_save_query('Q');
        postdata = malloc(1);
index 0f3f3c4d461ccfbeac73c62e869ce7be2e1760a5..917cfa7a68af48fe655dad73f333e18eea6fe732 100644 (file)
@@ -39,6 +39,9 @@ void rv_parse_multipart(unsigned char* buffer, char* boundary, unsigned long lon
        unsigned long long fstart = 0;
        char* b = rv_strcat3("--", boundary, "\r");
        char* eb = rv_strcat3("--", boundary, "--\r");
+       multipart_entries = malloc(sizeof(*multipart_entries));
+       multipart_entries[0] = NULL;
+       char* name = NULL;
        for(i = 0;; i++) {
                if(i == length || buffer[i] == '\n') {
                        char* line = malloc(i - incr + 1);
@@ -52,6 +55,23 @@ void rv_parse_multipart(unsigned char* buffer, char* boundary, unsigned long lon
                                        unsigned long long fend = i - strlen(line) - 2;
                                        char* data = buffer + fstart;
                                        unsigned long long datalen = fend - fstart;
+                                       struct multipart_entry* entry = malloc(sizeof(*entry));
+                                       entry->length = datalen;
+                                       entry->data = malloc(datalen);
+                                       entry->name = rv_strdup(name);
+                                       unsigned long long j;
+                                       for(j = 0; j < datalen; j++) entry->data[j] = data[j];
+
+                                       struct multipart_entry** old_entries = multipart_entries;
+                                       for(j = 0; old_entries[j] != NULL; j++)
+                                               ;
+                                       multipart_entries = malloc(sizeof(*multipart_entries) * (j + 2));
+                                       for(j = 0; old_entries[j] != NULL; j++) {
+                                               multipart_entries[j] = old_entries[j];
+                                       }
+                                       multipart_entries[j] = entry;
+                                       multipart_entries[j + 1] = NULL;
+                                       free(old_entries);
                                }
                                phase = 0;
                                if(strcmp(eb, line) == 0) {
@@ -60,12 +80,64 @@ void rv_parse_multipart(unsigned char* buffer, char* boundary, unsigned long lon
                                }
                        } else if(phase == 0) {
                                line[strlen(line) - 1] = 0;
-                               fprintf(stderr, "%s\n", line);
+                               int j;
+                               for(j = 0; line[j] != 0; j++) {
+                                       if(line[j] == ':') {
+                                               line[j] = 0;
+                                               char* value = "";
+                                               j++;
+                                               for(; line[j] != 0; j++) {
+                                                       if(line[j] != ' ' && line[j] != '\t') {
+                                                               value = line + j;
+                                                               break;
+                                                       }
+                                               }
+                                               if(strcasecmp(line, "Content-Disposition") == 0) {
+                                                       int j;
+                                                       int incrval = 0;
+                                                       for(j = 0;; j++) {
+                                                               if(value[j] == ';' || value[j] == 0) {
+                                                                       char oldc = value[j];
+                                                                       value[j] = 0;
+                                                                       char* kv = value + incrval;
+                                                                       j++;
+                                                                       for(; value[j] != 0; j++) {
+                                                                               if(value[j] != ' ' && value[j] != '\t') break;
+                                                                       }
+                                                                       incrval = j;
+                                                                       int k;
+                                                                       for(k = 0; kv[k] != 0; k++) {
+                                                                               if(kv[k] == '=') {
+                                                                                       kv[k] = 0;
+                                                                                       if(strcmp(kv, "name") == 0) {
+                                                                                               if(name != NULL) free(name);
+                                                                                               name = malloc(strlen(kv + k + 1) + 1);
+                                                                                               char* nkv = kv + k + 1;
+                                                                                               int l = 0;
+                                                                                               int incrn = 0;
+                                                                                               for(l = 0; nkv[l] != 0; l++) {
+                                                                                                       if(nkv[l] != '"') {
+                                                                                                               name[incrn++] = nkv[l];
+                                                                                                       }
+                                                                                               }
+                                                                                               name[incrn] = 0;
+                                                                                       }
+                                                                                       break;
+                                                                               }
+                                                                       }
+                                                                       if(oldc == 0) break;
+                                                               }
+                                                       }
+                                               }
+                                               break;
+                                       }
+                               }
                        }
                        free(line);
                        incr = i + 1;
                        if(i == length) break;
                }
        }
+       if(name != NULL) free(name);
        free(b);
 }
diff --git a/CGI/rv_magick.h b/CGI/rv_magick.h
new file mode 100644 (file)
index 0000000..3fcdf21
--- /dev/null
@@ -0,0 +1,11 @@
+/* $Id$ */
+
+#ifndef __RV_MAGICK_H__
+#define __RV_MAGICK_H__
+
+#include <stdbool.h>
+
+void rv_init_magick(void);
+bool rv_resize_picture(const char* input, const char* output, char** reason);
+
+#endif
index df0bd9bc73a21e526b0e1bd23048909c708bfe27..e6ba41ba69dcca01f260d0a54b70cde3f2c8e7bf 100644 (file)
@@ -7,6 +7,7 @@
 #include "rv_auth.h"
 #include "rv_db.h"
 #include "rv_repo.h"
+#include "rv_multipart.h"
 
 #include "../../config.h"
 
 #include "rv_avatar.h"
 #endif
 
+#ifdef USE_GRAPHICSMAGICK
+#include "rv_magick.h"
+#endif
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
+extern char* nocache;
 extern char* buffer;
 void add_data(char** data, const char* txt);
 void render_stuff();
@@ -289,7 +295,7 @@ void render_page(void) {
                                bool reject = false;
                                char* name = rv_get_query("username");
                                for(i = 0; name[i] != 0; i++) {
-                                       if(name[i] == REPO_USER_DELIM || name[i] == '#' || name[i] == '\\' || name[i] == '/') {
+                                       if(name[i] == REPO_USER_DELIM || name[i] == '#' || name[i] == '\\' || name[i] == '/' || name[i] == ':' || name[i] == '\n' || name[i] == '\r') {
                                                char cbuf[2];
                                                cbuf[0] = REPO_USER_DELIM;
                                                cbuf[1] = 0;
@@ -388,7 +394,9 @@ void render_page(void) {
                        add_data(&page, WWW_AVATAR_ROOT);
                        add_data(&page, "/");
                        add_data(&page, user);
-                       add_data(&page, ".png\" alt=\"Your Icon\"></a>");
+                       add_data(&page, ".png");
+                       add_data(&page, nocache);
+                       add_data(&page, "\" alt=\"Your Icon\" width=\"50%\"></a>");
                        add_data(&page, "<form action=\"");
                        add_data(&page, INSTANCE_ROOT);
                        add_data(&page, "/?page=uploadpfp\" method=\"POST\" enctype=\"multipart/form-data\">\n");
@@ -396,6 +404,40 @@ void render_page(void) {
                        add_data(&page, "       <input type=\"submit\" value=\"Upload\">\n");
                        add_data(&page, "</form>\n");
                }
+#endif
+#ifdef USE_AVATAR
+       } else if(strcmp(query, "uploadpfp") == 0) {
+               title = rv_strdup("Uploading Profile Picture Result");
+               page = rv_strdup("");
+               if(user == NULL) {
+                       add_data(&page, "It looks like you are not logged in.<br>Want to <a href=\"");
+                       add_data(&page, INSTANCE_ROOT);
+                       add_data(&page, "/?page=login\">log in</a>?\n");
+               } else if(rv_get_multipart("pfp") == NULL) {
+                       add_data(&page, "Invalid Form.");
+               } else {
+                       struct multipart_entry* entry = rv_get_multipart("pfp");
+                       char* tmp = rv_strcat3(AVATAR_ROOT, "/", user);
+                       char* path = rv_strcat(tmp, ".tmp");
+                       char* outpath = rv_strcat(tmp, ".png");
+                       free(tmp);
+                       FILE* f = fopen(path, "wb");
+                       fwrite(entry->data, 1, entry->length, f);
+                       fclose(f);
+                       char* reason;
+                       if(rv_resize_picture(path, outpath, &reason)) {
+                               add_data(&page, "Uploaded the profile picture successfully.\n");
+                       } else {
+                               add_data(&page, "Failed to upload the profile picture.<br><code>\n");
+                               char* esc = html_escape(reason);
+                               add_data(&page, esc);
+                               free(esc);
+                               add_data(&page, "</code>\n");
+                               free(reason);
+                       }
+                       free(path);
+                       free(outpath);
+               }
 #endif
        } else if(strcmp(query, "myrepo") == 0) {
                title = rv_strdup("My Repositories");
@@ -451,7 +493,7 @@ void render_page(void) {
                        bool reject = false;
                        char* name = rv_get_query("name");
                        for(i = 0; name[i] != 0; i++) {
-                               if(name[i] == REPO_USER_DELIM || name[i] == '#' || name[i] == '\\' || name[i] == '/') {
+                               if(name[i] == REPO_USER_DELIM || name[i] == '#' || name[i] == '\\' || name[i] == '/' || name[i] == ':' || name[i] == '\n' || name[i] == '\r') {
                                        char cbuf[2];
                                        cbuf[0] = REPO_USER_DELIM;
                                        cbuf[1] = 0;
diff --git a/objs.c b/objs.c
index d097a9518aab1d706e26abf9b311dd24eca9cf8a..c6b6d3e19c8ba4d8cf8a55b5b4a480fe460f594c 100644 (file)
--- a/objs.c
+++ b/objs.c
@@ -21,6 +21,10 @@ int main() {
        printf("enscript.o");
 #endif
        printf(" ");
+#if defined(USE_AVATAR)
+       printf("magick.o");
+#endif
+       printf(" ");
 #if defined(USE_AVATAR)
        printf("avatar.o");
 #endif