[MOD] Add width/height information when possible

This commit is contained in:
LDA 2024-09-30 06:11:31 +02:00
commit d989331716
5 changed files with 103 additions and 3 deletions

View file

@ -79,7 +79,7 @@ MatrixCreateNickChange(char *nick)
return map;
}
HashMap *
MatrixCreateMedia(char *mxc, char *body, char *mime)
MatrixCreateMedia(char *mxc, char *body, char *mime, FileInfo *info)
{
HashMap *map;
char *mime_type = NULL, *matrix_type = NULL;
@ -120,6 +120,11 @@ MatrixCreateMedia(char *mxc, char *body, char *mime)
map = HashMapCreate();
JsonSet(map, JsonValueString(mime), 2, "info", "mimetype");
if (info && info->width && info->height)
{
JsonSet(map, JsonValueInteger(info->width), 2, "info", "w");
JsonSet(map, JsonValueInteger(info->height), 2, "info", "h");
}
HashMapSet(map, "msgtype", JsonValueString(matrix_type));
HashMapSet(map, "mimetype", JsonValueString(mime));
HashMapSet(map, "body", JsonValueString(body));

66
src/FileInfo.c Normal file
View file

@ -0,0 +1,66 @@
#include <FileInfo.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Str.h>
#include <string.h>
#include <stdlib.h>
static int
GetField(XMLElement *elem)
{
XMLElement *child;
if (!elem || ArraySize(elem->children) != 1)
{
return 0;
}
child = ArrayGet(elem->children, 0);
return strtol(child->data, NULL, 10);
}
FileInfo *
FileInfoFromXMPP(XMLElement *stanza)
{
FileInfo *info;
XMLElement *reference, *sims, *file;
if (!stanza)
{
return NULL;
}
reference = XMLookForTKV(stanza,
"reference", "xmlns", "urn:xmpp:reference:0"
);
sims = XMLookForTKV(reference,
"media-sharing", "xmlns", "urn:xmpp:sims:1"
);
file = XMLookForTKV(sims,
"file", "xmlns", "urn:xmpp:jingle:apps:file-transfer:5"
);
if (!file)
{
return NULL;
}
info = Malloc(sizeof(*info));
info->width = GetField(XMLookForUnique(file, "width"));
info->height = GetField(XMLookForUnique(file, "height"));
info->size = GetField(XMLookForUnique(file, "size"));
return info;
}
void
FileInfoFree(FileInfo *info)
{
if (!info)
{
return;
}
Free(info);
}

View file

@ -419,10 +419,13 @@ end_error:
if (oob_data)
{
FileInfo *info = FileInfoFromXMPP(stanza);
mxc = ASReupload(args->config, oob_data->data, &mime);
if (mxc)
{
content = MatrixCreateMedia(mxc, data->data, mime);
content = MatrixCreateMedia(
mxc, data->data, mime, info
);
HashMapSet(content,
"at.kappach.at.parsee.external",
@ -436,6 +439,7 @@ end_error:
);
Free(mxc);
}
FileInfoFree(info);
Free(mime);
}
}

23
src/include/FileInfo.h Normal file
View file

@ -0,0 +1,23 @@
#ifndef PARSEE_FILEINFO_H
#define PARSEE_FILEINFO_H
#include "XML.h"
typedef struct FileInfo {
int width;
int height;
int size;
} FileInfo;
/** Grab file metadata through SIMS.
* ----------------
* Returns: File information(SIMS)[HEAP]
* Thrasher: FileInfoFree */
extern FileInfo * FileInfoFromXMPP(XMLElement *stanza);
/** Frees all information related with file metadata.
* --------------
* Thrashes: info */
extern void FileInfoFree(FileInfo *info);
#endif

View file

@ -3,6 +3,8 @@
#include <Cytoplasm/Json.h>
#include "FileInfo.h"
/* A simple representation of everything. It is not as complex as
* Telodendria's CommonID parser, simply because Parsee does not
* need such complexity. */
@ -33,7 +35,7 @@ extern HashMap * MatrixCreateReact(char *event, char *body);
extern HashMap * MatrixCreateReplace(char *event, char *body);
/* Creates the content for a media file. */
extern HashMap * MatrixCreateMedia(char *mxc, char *body, char *mime);
extern HashMap * MatrixCreateMedia(char *mxc, char *body, char *mime, FileInfo *info);
/* Creates the content for a m.room.name state event */
extern HashMap * MatrixCreateNameState(char *name);