Last active
August 29, 2015 14:27
-
-
Save rafaelmartins/b3838a7f2afeb3df5dac to your computer and use it in GitHub Desktop.
balde - image upload example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This is a simple (and naive) example of image upload using balde. It saves | |
* the image in a temporary directory with the SHA1 checksum as the file name. | |
* Also, it supports only JPEG files, and the validation is very weak. It's | |
* just an usage example. :) | |
* | |
* After uploading the image, the client will be redirected to a page with | |
* the image, from the 'picture' endpoint, that shows the last uploaded iamge. | |
* | |
* To build it: | |
* $ gcc -o upload upload.c $(pkg-config --libs --cflags balde) | |
* | |
* To run it: | |
* $ ./upload -s | |
* | |
* License: LGPL-2.1 (http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html) | |
*/ | |
#include <balde.h> | |
balde_response_t* | |
main_view(balde_app_t *app, balde_request_t *request) | |
{ | |
balde_session_open(app, request); | |
balde_response_t *response = balde_make_response( | |
"<html>\n<body>\n<h1>Upload your picture</h1>\n"); | |
gchar *tmp, *url; | |
if (balde_session_get(request, "picture")) { | |
url = balde_app_url_for(app, request, "picture", FALSE); | |
tmp = g_strdup_printf( | |
"<h2>Current picture</h2>\n<img src=\"%s\" />\n<hr />\n", url); | |
g_free(url); | |
balde_response_append_body(response, tmp); | |
g_free(tmp); | |
} | |
url = balde_app_url_for(app, request, "upload", FALSE); | |
tmp = g_strdup_printf( | |
"<form action=\"%s\" method=\"POST\" enctype=\"multipart/form-data\">\n" | |
" <input type=\"file\" name=\"picture\" />\n" | |
" <input type=\"submit\" />\n" | |
"</form>\n", url); | |
g_free(url); | |
balde_response_append_body(response, tmp); | |
g_free(tmp); | |
balde_session_save(request, response); | |
return response; | |
} | |
balde_response_t* | |
upload_view(balde_app_t *app, balde_request_t *request) | |
{ | |
balde_session_open(app, request); | |
balde_response_t *response = NULL; | |
const balde_file_t *file = balde_request_get_file(request, "picture"); | |
if (file == NULL) { | |
response = balde_abort(app, 404); | |
goto clean; | |
} | |
if (g_strcmp0(file->type, "image/jpeg") != 0) { | |
response = balde_abort(app, 403); | |
goto clean; | |
} | |
gchar *hash = g_compute_checksum_for_string(G_CHECKSUM_SHA1, | |
file->content->str, file->content->len); | |
gchar *filename = g_strdup_printf("%s.jpg", hash); | |
g_free(hash); | |
gchar *filepath = balde_file_save_to_disk(file, g_get_tmp_dir(), filename); | |
g_free(filename); | |
balde_session_set(request, "picture", filepath); | |
g_free(filepath); | |
response = balde_abort(app, 301); | |
gchar *url = balde_app_url_for(app, request, "main", FALSE); | |
balde_response_set_header(response, "location", url); | |
g_free(url); | |
clean: | |
balde_session_save(request, response); | |
return response; | |
} | |
balde_response_t* | |
picture_view(balde_app_t *app, balde_request_t *request) | |
{ | |
balde_session_open(app, request); | |
balde_response_t *response = NULL; | |
const gchar *filepath = balde_session_get(request, "picture"); | |
if (filepath == NULL) { | |
response = balde_abort(app, 404); | |
goto clean; | |
} | |
gchar *contents; | |
gsize len; | |
if (!g_file_get_contents(filepath, &contents, &len, NULL)) { | |
response = balde_abort(app, 404); | |
goto clean; | |
} | |
response = balde_make_response_len(contents, len); | |
g_free(contents); | |
balde_response_set_header(response, "content-type", "image/jpeg"); | |
clean: | |
balde_session_save(request, response); | |
return response; | |
} | |
int | |
main(int argc, char **argv) | |
{ | |
balde_app_t *app = balde_app_init(); | |
balde_app_set_config(app, "SECRET_KEY", "bola"); | |
balde_app_add_url_rule(app, "main", "/", BALDE_HTTP_GET, main_view); | |
balde_app_add_url_rule(app, "upload", "/upload/", BALDE_HTTP_POST, | |
upload_view); | |
balde_app_add_url_rule(app, "picture", "/picture/", BALDE_HTTP_GET, | |
picture_view); | |
balde_app_run(app, argc, argv); | |
balde_app_free(app); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment