From 32245397fdc322066bd3c9dd96e1c7c74edec21c Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Thu, 10 Mar 2016 17:56:19 +0100 Subject: [PATCH] Add the FileInfo struct --- src/matrix-types.vala | 50 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/matrix-types.vala b/src/matrix-types.vala index 24fe21f..f5671f5 100644 --- a/src/matrix-types.vala +++ b/src/matrix-types.vala @@ -291,6 +291,56 @@ namespace Matrix { ANSWER; /// call answer } + /** + * Information about the file referred to in a URL. + */ + public struct FileInfo { + int? size; + string? mimetype; + + public void + set_from_json(Json.Node json_data) + { + size = null; + mimetype = null; + + var root = json_data.get_object(); + Json.Node? node; + + if ((node = root.get_member("size")) != null) { + size = (int)node.get_int(); + } else if (Config.DEBUG) { + warning("size is missing from a FileInfo"); + } + + if ((node = root.get_member("mimetype")) != null) { + mimetype = node.get_string(); + } else if (Config.DEBUG) { + warning("mimetype is missing from a FileInfo"); + } + } + + public Json.Node + get_json_node() + throws Matrix.Error + { + if ((size == null) + || (mimetype == null)) { + throw new Matrix.Error.INCOMPLETE( + "Won't generate a FileInfo without all fields set."); + } + + var node = new Json.Node(Json.NodeType.OBJECT); + var obj = new Json.Object(); + node.set_object(obj); + + obj.set_int_member("size", size); + obj.set_string_member("mimetype", mimetype); + + return node; + } + } + public struct ImageInfo { int? size; int? height;