master
parent
eb432b6ea0
commit
c66f92924d
@ -0,0 +1,73 @@
|
||||
#include "emoji.h"
|
||||
|
||||
#include "http/server/request.h"
|
||||
#include "format.h"
|
||||
|
||||
#include "model/server.h"
|
||||
#include "model/emoji.h"
|
||||
|
||||
#include "view/api/Emoji.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
|
||||
// Route: /api/v1/custom_emojis
|
||||
bool route_custom_emojis( struct http_request* req )
|
||||
{
|
||||
http_request_send_headers( req, 200, "application/json", true );
|
||||
FILE* f = http_request_get_response_body( req );
|
||||
fprintf( f, "[" );
|
||||
DIR* d = opendir( "data/emoji" );
|
||||
struct dirent* ent;
|
||||
bool first = true;
|
||||
while( ent = readdir(d) ) {
|
||||
if( ent->d_name[0] != '.' && !index(ent->d_name,' ') ) {
|
||||
if( !first ) {
|
||||
fprintf( f, "," );
|
||||
} else {
|
||||
fprintf( f, "\n" );
|
||||
}
|
||||
char* filename = strdup(ent->d_name);
|
||||
char* remain = NULL;
|
||||
char* shortcode = strtok_r(filename, ".", &remain );
|
||||
|
||||
struct emoji e;
|
||||
e.shortcode = shortcode;
|
||||
e.url = aformat( "https://%s/emoji/%s", g_server_name, shortcode );
|
||||
api_Emoji_write( &e, f, 1 );
|
||||
free(e.url);
|
||||
free(filename);
|
||||
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
closedir(d);
|
||||
|
||||
fprintf( f, "\n]" );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const char* mime_type_for_filename( const char* filename );
|
||||
|
||||
bool route_emoji_asset( struct http_request* req )
|
||||
{
|
||||
char* id_str = http_request_route_get_dir_or_file(req);
|
||||
char* filename = filename_for_shortcode( id_str );
|
||||
free(id_str);
|
||||
|
||||
if( !filename ) { return false; }
|
||||
|
||||
const char* mime_type = mime_type_for_filename( filename );
|
||||
char fs_path[512];
|
||||
snprintf( fs_path, 512, "data/emoji/%s", filename );
|
||||
free(filename);
|
||||
|
||||
return http_request_send_file( req, fs_path, mime_type );
|
||||
}
|
||||
|
@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
struct http_request;
|
||||
|
||||
bool route_custom_emojis( struct http_request* req );
|
||||
bool route_emoji_asset( struct http_request* req );
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit c2f69469881dbc5b4a4cd976f5d58d7700f83ccb
|
||||
Subproject commit 49765d54359b3537465ea432ba92fa955cf4a385
|
@ -0,0 +1,42 @@
|
||||
#include "emoji.h"
|
||||
|
||||
#include "json/layout.h"
|
||||
|
||||
#include <dirent.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define OBJ_TYPE struct emoji
|
||||
struct json_object_field emoji_layout[] = {
|
||||
JSON_FIELD_STRING( url, true ),
|
||||
JSON_FIELD_STRING( shortcode, true ),
|
||||
JSON_FIELD_END,
|
||||
};
|
||||
#undef OBJ_TYPE
|
||||
JSON_FIELD_TYPE_OBJECT_LAYOUT_WITH_DEFAULTS( emoji );
|
||||
|
||||
char* filename_for_shortcode( const char* shortcode )
|
||||
{
|
||||
int len = strlen(shortcode);
|
||||
|
||||
DIR* d = opendir( "data/emoji" );
|
||||
struct dirent* ent;
|
||||
char* filename = NULL;
|
||||
while( ent = readdir(d) ) {
|
||||
if( 0 == strncmp( shortcode, ent->d_name, len ) && ent->d_name[len] == '.' ) {
|
||||
closedir(d);
|
||||
return strdup(ent->d_name);
|
||||
}
|
||||
}
|
||||
closedir(d);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void emoji_free( struct emoji* e )
|
||||
{
|
||||
if( !e ) { return; }
|
||||
|
||||
free(e->shortcode);
|
||||
free(e->url);
|
||||
free(e);
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "json/layout.h"
|
||||
|
||||
char* filename_for_shortcode( const char* shortcode );
|
||||
|
||||
struct emoji
|
||||
{
|
||||
char* shortcode;
|
||||
char* url;
|
||||
};
|
||||
|
||||
void emoji_free( struct emoji* e );
|
||||
|
||||
extern struct json_object_field emoji_layout[];
|
||||
extern struct json_field_type emoji_type;
|
||||
|
@ -0,0 +1,46 @@
|
||||
#include "Emoji.h"
|
||||
|
||||
#include "json/layout.h"
|
||||
|
||||
#include "model/emoji.h"
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define OBJ_TYPE struct emoji
|
||||
struct json_object_field api_Emoji_layout[] = {
|
||||
JSON_FIELD_FIXED_STRING( category, "", false ),
|
||||
JSON_FIELD_STRING( shortcode, true ),
|
||||
JSON_FIELD_STRING( url, true ),
|
||||
{
|
||||
.key = "static_url",
|
||||
.offset = offsetof( OBJ_TYPE, url ),
|
||||
.type = &json_field_string,
|
||||
},
|
||||
JSON_FIELD_FIXED_BOOL( visible_in_picker, true ),
|
||||
JSON_FIELD_END,
|
||||
};
|
||||
#undef OBJ_TYPE
|
||||
|
||||
void api_Emoji_write( struct emoji* e, FILE* f, int indent )
|
||||
{
|
||||
struct json_writer jw = {
|
||||
.f = f,
|
||||
.indentation = "\t",
|
||||
.indent = indent,
|
||||
};
|
||||
|
||||
if( !e ) {
|
||||
fprintf( f, "null" );
|
||||
return;
|
||||
}
|
||||
|
||||
json_write_pretty_object_layout( &jw, api_Emoji_layout, e );
|
||||
}
|
||||
|
||||
struct json_field_type api_Emoji_type = {
|
||||
.reader = json_field_object_type_reader,
|
||||
.writer = json_field_object_type_writer,
|
||||
.size = sizeof(struct emoji*),
|
||||
.layout = api_Emoji_layout,
|
||||
};
|
||||
|
@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
struct emoji;
|
||||
void api_Emoji_write( struct emoji* e, FILE* f, int indent );
|
||||
|
Loading…
Reference in new issue