parent
6d3b608925
commit
d80b5e1e62
@ -0,0 +1,70 @@
|
||||
#include "media.h"
|
||||
|
||||
#include "json/layout.h"
|
||||
#include "format.h"
|
||||
|
||||
#include "model/server.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#define OBJ_TYPE struct media
|
||||
static struct json_object_field media_layout[] = {
|
||||
JSON_FIELD_STRING( content_type, true ),
|
||||
JSON_FIELD_END,
|
||||
};
|
||||
#undef OBJ_TYPE
|
||||
|
||||
struct media* media_from_id( int id )
|
||||
{
|
||||
if( id == 0 ) { return NULL; }
|
||||
char filename[512];
|
||||
FILE* f = fopen( format( filename, sizeof(filename), "data/media/%d.json", id ), "r" );
|
||||
if( !f ) { return NULL; }
|
||||
|
||||
struct media* m;
|
||||
m = malloc(sizeof(*m));
|
||||
memset(m,0,sizeof(*m));
|
||||
m->id = id;
|
||||
|
||||
if( !json_read_object_layout_from_FILE( f, media_layout, m ) ) {
|
||||
media_free(m);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
struct media* media_from_local_uri( const char* uri )
|
||||
{
|
||||
if( 0 != strncmp( "https://", uri, 8 ) ) { return NULL; }
|
||||
uri += 8;
|
||||
|
||||
int server_name_length = strlen(g_server_name);
|
||||
if( 0 != strncmp( g_server_name, uri, server_name_length ) ) { return NULL; }
|
||||
uri += server_name_length;
|
||||
|
||||
if( 0 != strncmp( "/media/", uri, 7 ) ) { return NULL; }
|
||||
uri += 7;
|
||||
|
||||
// Note: zero is never a valid status id
|
||||
int id = atoi(uri);
|
||||
if( id == 0 ) { return NULL; }
|
||||
|
||||
return media_from_id(id);
|
||||
}
|
||||
void media_free( struct media* m )
|
||||
{
|
||||
if( !m ) { return; }
|
||||
|
||||
free(m->content_type);
|
||||
free(m);
|
||||
}
|
||||
void media_save( struct media* m )
|
||||
{
|
||||
assert( m );
|
||||
assert( m->id );
|
||||
|
||||
char filename[512];
|
||||
json_write_object_layout_to_file( format(filename,sizeof(filename),"data/media/%d.json", m->id ), "\t", media_layout, m );
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
struct media
|
||||
{
|
||||
int id;
|
||||
char* content_type;
|
||||
};
|
||||
|
||||
struct media* media_from_id( int id );
|
||||
struct media* media_from_local_uri( const char* uri );
|
||||
void media_free( struct media* m );
|
||||
void media_save( struct media* m );
|
||||
|
@ -0,0 +1,65 @@
|
||||
#include "Attachement.h"
|
||||
|
||||
#include "json/layout.h"
|
||||
#include "format.h"
|
||||
|
||||
#include "model/server.h"
|
||||
#include "model/media.h"
|
||||
|
||||
bool int_to_string_callback( void* field_data, bool is_read, char** res );
|
||||
bool preview_url_string_callback( void* field_data, bool is_read, char** res )
|
||||
{
|
||||
struct media* m = field_data;
|
||||
if( !is_read ) {
|
||||
*res = aformat( "https://%s/media/%d/preview", g_server_name, m->id );
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool url_string_callback( void* field_data, bool is_read, char** res )
|
||||
{
|
||||
struct media* m = field_data;
|
||||
if( !is_read ) {
|
||||
*res = aformat( "https://%s/media/%d/blob", g_server_name, m->id );
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#define OBJ_TYPE struct media
|
||||
static struct json_object_field api_Attachment_layout[] = {
|
||||
{
|
||||
.key = "id",
|
||||
.offset = offsetof( OBJ_TYPE, id ),
|
||||
.type = &json_field_callback_string,
|
||||
.string_callback = int_to_string_callback,
|
||||
},
|
||||
JSON_FIELD_FIXED_STRING( type, "image", true ),
|
||||
{
|
||||
.key = "preview_url",
|
||||
.offset = 0,
|
||||
.required = true,
|
||||
.type = &json_field_callback_string,
|
||||
.string_callback = preview_url_string_callback,
|
||||
},
|
||||
{
|
||||
.key = "url",
|
||||
.offset = 0,
|
||||
.required = true,
|
||||
.type = &json_field_callback_string,
|
||||
.string_callback = url_string_callback,
|
||||
},
|
||||
JSON_FIELD_END,
|
||||
};
|
||||
#undef OBJ_TYPE
|
||||
|
||||
void api_Attachement_write( struct media* m, FILE* f, int indent )
|
||||
{
|
||||
struct json_writer jw = {
|
||||
.f = f,
|
||||
.indentation = "\t",
|
||||
.indent = indent,
|
||||
};
|
||||
|
||||
json_write_pretty_object_layout( &jw, api_Attachment_layout, m );
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
struct media;
|
||||
|
||||
void api_Attachement_write( struct media* m, FILE* f, int indent );
|
||||
|
Loading…
Reference in new issue