diff options
author | fiaxh <git@lightrise.org> | 2024-11-02 22:24:59 +0100 |
---|---|---|
committer | fiaxh <git@lightrise.org> | 2024-11-15 14:40:08 -0600 |
commit | 79f792e090330a05753f9edb27332a946eb0840d (patch) | |
tree | 5a6f1ad3ac0af0beea44ca9e83e7a9b052263025 /xmpp-vala/src/module/xep/0446_file_metadata_element.vala | |
parent | aaf4542e6208460c305db4be36b15dc832ddc95a (diff) | |
download | dino-79f792e090330a05753f9edb27332a946eb0840d.tar.gz dino-79f792e090330a05753f9edb27332a946eb0840d.zip |
Fix and improve stateless file-sharing
Diffstat (limited to 'xmpp-vala/src/module/xep/0446_file_metadata_element.vala')
-rw-r--r-- | xmpp-vala/src/module/xep/0446_file_metadata_element.vala | 128 |
1 files changed, 52 insertions, 76 deletions
diff --git a/xmpp-vala/src/module/xep/0446_file_metadata_element.vala b/xmpp-vala/src/module/xep/0446_file_metadata_element.vala index d7fbb06f..56b3d379 100644 --- a/xmpp-vala/src/module/xep/0446_file_metadata_element.vala +++ b/xmpp-vala/src/module/xep/0446_file_metadata_element.vala @@ -1,23 +1,24 @@ -using Xmpp.Xep.CryptographicHashes; - namespace Xmpp.Xep.FileMetadataElement { public const string NS_URI = "urn:xmpp:file:metadata:0"; public class FileMetadata { - public string name { get; set; } + public string? name { get; set; } public string? mime_type { get; set; } public int64 size { get; set; default=-1; } public string? desc { get; set; } public DateTime? date { get; set; } public int width { get; set; default=-1; } // Width of image in pixels - public int height { get; set; default=-1; } // Height of image in pixels - public CryptographicHashes.Hashes hashes = new CryptographicHashes.Hashes(); - public int64 length { get; set; default=-1; } // Length of audio/video in milliseconds - public Gee.List<Xep.JingleContentThumbnails.Thumbnail> thumbnails = new Gee.ArrayList<Xep.JingleContentThumbnails.Thumbnail>(); + public int height { get; set; default=-1; } // Height of image in pixels + public Gee.List<CryptographicHashes.Hash> hashes = new Gee.ArrayList<CryptographicHashes.Hash>(); + public int64 length { get; set; default=-1; } // Length of audio/video in milliseconds + public Gee.List<Xep.JingleContentThumbnails.Thumbnail> thumbnails = new Gee.ArrayList<Xep.JingleContentThumbnails.Thumbnail>(); public StanzaNode to_stanza_node() { - StanzaNode node = new StanzaNode.build("file", NS_URI).add_self_xmlns() - .put_node(new StanzaNode.build("name", NS_URI).put_node(new StanzaNode.text(this.name))); + StanzaNode node = new StanzaNode.build("file", NS_URI).add_self_xmlns(); + + if (this.name != null) { + node.put_node(new StanzaNode.build("name", NS_URI).put_node(new StanzaNode.text(this.name))); + } if (this.mime_type != null) { node.put_node(new StanzaNode.build("media_type", NS_URI).put_node(new StanzaNode.text(this.mime_type))); } @@ -39,82 +40,57 @@ namespace Xmpp.Xep.FileMetadataElement { if (this.length != -1) { node.put_node(new StanzaNode.build("length", NS_URI).put_node(new StanzaNode.text(this.length.to_string()))); } - node.sub_nodes.add_all(this.hashes.to_stanza_nodes()); + foreach (var hash in hashes) { + node.put_node(hash.to_stanza_node()); + } foreach (Xep.JingleContentThumbnails.Thumbnail thumbnail in this.thumbnails) { node.put_node(thumbnail.to_stanza_node()); } return node; } + } - public void add_to_message(MessageStanza message) { - StanzaNode node = this.to_stanza_node(); - printerr("Attaching file metadata:\n"); - printerr("%s\n", node.to_ansi_string(true)); - message.stanza.put_node(node); - } + public static FileMetadata? get_file_metadata(StanzaNode node) { + StanzaNode? file_node = node.get_subnode("file", Xep.FileMetadataElement.NS_URI); + if (file_node == null) return null; - public static FileMetadata? from_stanza_node(StanzaNode node) { - FileMetadata metadata = new FileMetadata(); - // TODO: null checks on final values - StanzaNode? name_node = node.get_subnode("name"); - if (name_node == null || name_node.get_string_content() == null) { - return null; - } else { - metadata.name = name_node.get_string_content(); - } - StanzaNode? desc_node = node.get_subnode("desc"); - if (desc_node != null && desc_node.get_string_content() != null) { - metadata.desc = desc_node.get_string_content(); - } - StanzaNode? mime_node = node.get_subnode("media_type"); - if (mime_node != null && mime_node.get_string_content() != null) { - metadata.mime_type = mime_node.get_string_content(); - } - StanzaNode? size_node = node.get_subnode("size"); - if (size_node != null && size_node.get_string_content() != null) { - metadata.size = int64.parse(size_node.get_string_content()); - } - StanzaNode? date_node = node.get_subnode("date"); - if (date_node != null && date_node.get_string_content() != null) { - metadata.date = new DateTime.from_iso8601(date_node.get_string_content(), null); - } - StanzaNode? width_node = node.get_subnode("width"); - if (width_node != null && width_node.get_string_content() != null) { - metadata.width = int.parse(width_node.get_string_content()); - } - StanzaNode? height_node = node.get_subnode("height"); - if (height_node != null && height_node.get_string_content() != null) { - metadata.height = int.parse(height_node.get_string_content()); - } - StanzaNode? length_node = node.get_subnode("length"); - if (length_node != null && length_node.get_string_content() != null) { - metadata.length = int.parse(length_node.get_string_content()); - } - foreach (StanzaNode thumbnail_node in node.get_subnodes(Xep.JingleContentThumbnails.STANZA_NAME, Xep.JingleContentThumbnails.NS_URI)) { - Xep.JingleContentThumbnails.Thumbnail? thumbnail = Xep.JingleContentThumbnails.Thumbnail.from_stanza_node(thumbnail_node); - if (thumbnail != null) { - metadata.thumbnails.add(thumbnail); - } - } - metadata.hashes = new CryptographicHashes.Hashes.from_stanza_subnodes(node); - return metadata; + FileMetadata metadata = new FileMetadata(); + + StanzaNode? name_node = file_node.get_subnode("name"); + if (name_node != null && name_node.get_string_content() != null) { + metadata.name = name_node.get_string_content(); } - public static FileMetadata? from_message(MessageStanza message) { - StanzaNode? node = message.stanza.get_subnode("file", NS_URI); - if (node == null) { - return null; - } - printerr("Parsing metadata from message:\n"); - printerr("%s\n", node.to_xml()); - FileMetadata metadata = FileMetadata.from_stanza_node(node); - if (metadata != null) { - printerr("Parsed metadata:\n"); - printerr("%s\n", metadata.to_stanza_node().to_ansi_string(true)); - } else { - printerr("Failed to parse metadata!\n"); - } - return FileMetadata.from_stanza_node(node); + StanzaNode? desc_node = file_node.get_subnode("desc"); + if (desc_node != null && desc_node.get_string_content() != null) { + metadata.desc = desc_node.get_string_content(); + } + StanzaNode? mime_node = file_node.get_subnode("media_type"); + if (mime_node != null && mime_node.get_string_content() != null) { + metadata.mime_type = mime_node.get_string_content(); + } + StanzaNode? size_node = file_node.get_subnode("size"); + if (size_node != null && size_node.get_string_content() != null) { + metadata.size = int64.parse(size_node.get_string_content()); + } + StanzaNode? date_node = file_node.get_subnode("date"); + if (date_node != null && date_node.get_string_content() != null) { + metadata.date = new DateTime.from_iso8601(date_node.get_string_content(), null); + } + StanzaNode? width_node = file_node.get_subnode("width"); + if (width_node != null && width_node.get_string_content() != null) { + metadata.width = int.parse(width_node.get_string_content()); + } + StanzaNode? height_node = file_node.get_subnode("height"); + if (height_node != null && height_node.get_string_content() != null) { + metadata.height = int.parse(height_node.get_string_content()); + } + StanzaNode? length_node = file_node.get_subnode("length"); + if (length_node != null && length_node.get_string_content() != null) { + metadata.length = int.parse(length_node.get_string_content()); } + metadata.thumbnails = Xep.JingleContentThumbnails.get_thumbnails(file_node); + metadata.hashes = CryptographicHashes.get_hashes(file_node); + return metadata; } } |