| 1 |
namespace Xmpp.Xep.FileMetadataElement { |
| 2 |
public const string NS_URI = "urn:xmpp:file:metadata:0"; |
| 3 |
|
| 4 |
public class FileMetadata { |
| 5 |
public string? name { get; set; } |
| 6 |
public string? mime_type { get; set; } |
| 7 |
public int64 size { get; set; default=-1; } |
| 8 |
public string? desc { get; set; } |
| 9 |
public DateTime? date { get; set; } |
| 10 |
public int width { get; set; default=-1; } // Width of image in pixels |
| 11 |
public int height { get; set; default=-1; } // Height of image in pixels |
| 12 |
public Gee.List<CryptographicHashes.Hash> hashes = new Gee.ArrayList<CryptographicHashes.Hash>(); |
| 13 |
public int64 length { get; set; default=-1; } // Length of audio/video in milliseconds |
| 14 |
public Gee.List<Xep.JingleContentThumbnails.Thumbnail> thumbnails = new Gee.ArrayList<Xep.JingleContentThumbnails.Thumbnail>(); |
| 15 |
|
| 16 |
public StanzaNode to_stanza_node() { |
| 17 |
StanzaNode node = new StanzaNode.build("file", NS_URI).add_self_xmlns(); |
| 18 |
|
| 19 |
if (this.name != null) { |
| 20 |
node.put_node(new StanzaNode.build("name", NS_URI).put_node(new StanzaNode.text(this.name))); |
| 21 |
} |
| 22 |
if (this.mime_type != null) { |
| 23 |
// TODO remove the media_type node, it was implemented by accident and temporary provides backwards-compatibility |
| 24 |
node.put_node(new StanzaNode.build("media_type", NS_URI).put_node(new StanzaNode.text(this.mime_type))); |
| 25 |
node.put_node(new StanzaNode.build("media-type", NS_URI).put_node(new StanzaNode.text(this.mime_type))); |
| 26 |
} |
| 27 |
if (this.size != -1) { |
| 28 |
node.put_node(new StanzaNode.build("size", NS_URI).put_node(new StanzaNode.text(this.size.to_string()))); |
| 29 |
} |
| 30 |
if (this.date != null) { |
| 31 |
node.put_node(new StanzaNode.build("date", NS_URI).put_node( |
| 32 |
new StanzaNode.text(DateTimeProfiles.to_datetime(this.date)) |
| 33 |
)); |
| 34 |
} |
| 35 |
if (this.desc != null) { |
| 36 |
node.put_node(new StanzaNode.build("desc", NS_URI).put_node(new StanzaNode.text(this.desc))); |
| 37 |
} |
| 38 |
if (this.width != -1) { |
| 39 |
node.put_node(new StanzaNode.build("width", NS_URI).put_node(new StanzaNode.text(this.width.to_string()))); |
| 40 |
} |
| 41 |
if (this.height != -1) { |
| 42 |
node.put_node(new StanzaNode.build("height", NS_URI).put_node(new StanzaNode.text(this.height.to_string()))); |
| 43 |
} |
| 44 |
if (this.length != -1) { |
| 45 |
node.put_node(new StanzaNode.build("length", NS_URI).put_node(new StanzaNode.text(this.length.to_string()))); |
| 46 |
} |
| 47 |
foreach (var hash in hashes) { |
| 48 |
node.put_node(hash.to_stanza_node()); |
| 49 |
} |
| 50 |
foreach (Xep.JingleContentThumbnails.Thumbnail thumbnail in this.thumbnails) { |
| 51 |
node.put_node(thumbnail.to_stanza_node()); |
| 52 |
} |
| 53 |
return node; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
public static FileMetadata? get_file_metadata(StanzaNode node) { |
| 58 |
StanzaNode? file_node = node.get_subnode("file", Xep.FileMetadataElement.NS_URI); |
| 59 |
if (file_node == null) return null; |
| 60 |
|
| 61 |
FileMetadata metadata = new FileMetadata(); |
| 62 |
|
| 63 |
StanzaNode? name_node = file_node.get_subnode("name"); |
| 64 |
if (name_node != null && name_node.get_string_content() != null) { |
| 65 |
metadata.name = name_node.get_string_content(); |
| 66 |
} |
| 67 |
|
| 68 |
StanzaNode? desc_node = file_node.get_subnode("desc"); |
| 69 |
if (desc_node != null && desc_node.get_string_content() != null) { |
| 70 |
metadata.desc = desc_node.get_string_content(); |
| 71 |
} |
| 72 |
// TODO remove media_type handling, it was implemented by accident and temporary provides backwards-compatibility |
| 73 |
StanzaNode? mime_node_bad = file_node.get_subnode("media_type"); |
| 74 |
if (mime_node_bad != null && mime_node_bad.get_string_content() != null) { |
| 75 |
metadata.mime_type = mime_node_bad.get_string_content(); |
| 76 |
} |
| 77 |
StanzaNode? mime_node = file_node.get_subnode("media-type"); |
| 78 |
if (mime_node != null && mime_node.get_string_content() != null) { |
| 79 |
metadata.mime_type = mime_node.get_string_content(); |
| 80 |
} |
| 81 |
StanzaNode? size_node = file_node.get_subnode("size"); |
| 82 |
if (size_node != null && size_node.get_string_content() != null) { |
| 83 |
metadata.size = int64.parse(size_node.get_string_content()); |
| 84 |
} |
| 85 |
StanzaNode? date_node = file_node.get_subnode("date"); |
| 86 |
if (date_node != null && date_node.get_string_content() != null) { |
| 87 |
metadata.date = DateTimeProfiles.parse_string(date_node.get_string_content()); |
| 88 |
} |
| 89 |
StanzaNode? width_node = file_node.get_subnode("width"); |
| 90 |
if (width_node != null && width_node.get_string_content() != null) { |
| 91 |
metadata.width = int.parse(width_node.get_string_content()); |
| 92 |
} |
| 93 |
StanzaNode? height_node = file_node.get_subnode("height"); |
| 94 |
if (height_node != null && height_node.get_string_content() != null) { |
| 95 |
metadata.height = int.parse(height_node.get_string_content()); |
| 96 |
} |
| 97 |
StanzaNode? length_node = file_node.get_subnode("length"); |
| 98 |
if (length_node != null && length_node.get_string_content() != null) { |
| 99 |
metadata.length = int.parse(length_node.get_string_content()); |
| 100 |
} |
| 101 |
metadata.thumbnails = Xep.JingleContentThumbnails.get_thumbnails(file_node); |
| 102 |
metadata.hashes = CryptographicHashes.get_hashes(file_node); |
| 103 |
return metadata; |
| 104 |
} |
| 105 |
} |