|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Newtonsoft.Json; |
| 4 | + |
| 5 | +namespace Nest |
| 6 | +{ |
| 7 | + [JsonConverter(typeof(AttachmentConverter))] |
| 8 | + public class Attachment |
| 9 | + { |
| 10 | + /// <summary> |
| 11 | + /// The author. |
| 12 | + /// </summary> |
| 13 | + [JsonProperty("author")] |
| 14 | + public string Author { get; set; } |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// The base64 encoded content. Can be explicitly set |
| 18 | + /// </summary> |
| 19 | + [JsonProperty("content")] |
| 20 | + public string Content { get; set; } |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// The length of the content before text extraction. |
| 24 | + /// </summary> |
| 25 | + [JsonProperty("content_length")] |
| 26 | + public long? ContentLength { get; set; } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// The content type of the attachment. Can be explicitly set |
| 30 | + /// </summary> |
| 31 | + [JsonProperty("content_type")] |
| 32 | + public string ContentType { get; set; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// The date of the attachment. |
| 36 | + /// </summary> |
| 37 | + [JsonProperty("date")] |
| 38 | + public DateTime? Date { get; set; } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// The keywords in the attachment. |
| 42 | + /// </summary> |
| 43 | + [JsonProperty("keywords")] |
| 44 | + public string Keywords { get; set; } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// The language of the attachment. Can be explicitly set. This requires |
| 48 | + /// <see cref="DetectLanguage"/> to be set to <c>true</c> |
| 49 | + /// </summary> |
| 50 | + [JsonProperty("language")] |
| 51 | + public string Language { get; set; } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Detect the language of the attachment. Language detection is |
| 55 | + /// disabled by default. |
| 56 | + /// </summary> |
| 57 | + [JsonProperty("detect_language")] |
| 58 | + public bool? DetectLanguage { get; set; } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// The name of the attachment. Can be explicitly set |
| 62 | + /// </summary> |
| 63 | + [JsonProperty("name")] |
| 64 | + public string Name { get; set; } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// The title of the attachment. |
| 68 | + /// </summary> |
| 69 | + [JsonProperty("title")] |
| 70 | + public string Title { get; set; } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Determines how many characters are extracted when indexing the content. |
| 74 | + /// By default, 100000 characters are extracted when indexing the content. |
| 75 | + /// -1 can be set to extract all text, but note that all the text needs to be |
| 76 | + /// allowed to be represented in memory |
| 77 | + /// </summary> |
| 78 | + [JsonProperty("indexed_chars")] |
| 79 | + public long? IndexedCharacters { get; set; } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Whether the attachment contains explicit metadata in addition to the |
| 83 | + /// content. Used at indexing time to determine the serialized form of the |
| 84 | + /// attachment. |
| 85 | + /// </summary> |
| 86 | + [JsonIgnore] |
| 87 | + public bool ContainsMetadata => !Name.IsNullOrEmpty() || |
| 88 | + !ContentType.IsNullOrEmpty() || |
| 89 | + !Language.IsNullOrEmpty() || |
| 90 | + DetectLanguage.HasValue || |
| 91 | + IndexedCharacters.HasValue; |
| 92 | + } |
| 93 | + |
| 94 | + internal class AttachmentConverter : JsonConverter |
| 95 | + { |
| 96 | + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
| 97 | + { |
| 98 | + var attachment = (Attachment)value; |
| 99 | + if (!attachment.ContainsMetadata) |
| 100 | + { |
| 101 | + writer.WriteValue(attachment.Content); |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + writer.WriteStartObject(); |
| 106 | + writer.WritePropertyName("_content"); |
| 107 | + writer.WriteValue(attachment.Content); |
| 108 | + |
| 109 | + if (!string.IsNullOrEmpty(attachment.Name)) |
| 110 | + { |
| 111 | + writer.WritePropertyName("_name"); |
| 112 | + writer.WriteValue(attachment.Name); |
| 113 | + } |
| 114 | + |
| 115 | + if (!string.IsNullOrEmpty(attachment.ContentType)) |
| 116 | + { |
| 117 | + writer.WritePropertyName("_content_type"); |
| 118 | + writer.WriteValue(attachment.ContentType); |
| 119 | + } |
| 120 | + |
| 121 | + if (!string.IsNullOrEmpty(attachment.Language)) |
| 122 | + { |
| 123 | + writer.WritePropertyName("_language"); |
| 124 | + writer.WriteValue(attachment.Language); |
| 125 | + } |
| 126 | + |
| 127 | + if (attachment.DetectLanguage.HasValue) |
| 128 | + { |
| 129 | + writer.WritePropertyName("_detect_language"); |
| 130 | + writer.WriteValue(attachment.DetectLanguage.Value); |
| 131 | + } |
| 132 | + |
| 133 | + if (attachment.IndexedCharacters.HasValue) |
| 134 | + { |
| 135 | + writer.WritePropertyName("_indexed_chars"); |
| 136 | + writer.WriteValue(attachment.IndexedCharacters.Value); |
| 137 | + } |
| 138 | + |
| 139 | + writer.WriteEndObject(); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) |
| 144 | + { |
| 145 | + if (reader.TokenType == JsonToken.String) |
| 146 | + { |
| 147 | + return new Attachment { Content = (string)reader.Value }; |
| 148 | + } |
| 149 | + |
| 150 | + if (reader.TokenType == JsonToken.StartObject) |
| 151 | + { |
| 152 | + var attachment = new Attachment(); |
| 153 | + while (reader.Read()) |
| 154 | + { |
| 155 | + if (reader.TokenType == JsonToken.PropertyName) |
| 156 | + { |
| 157 | + var propertyName = (string)reader.Value; |
| 158 | + switch (propertyName) |
| 159 | + { |
| 160 | + case "_content": |
| 161 | + attachment.Content = reader.ReadAsString(); |
| 162 | + break; |
| 163 | + case "_name": |
| 164 | + attachment.Name = reader.ReadAsString(); |
| 165 | + break; |
| 166 | + case "_content_type": |
| 167 | + attachment.ContentType = reader.ReadAsString(); |
| 168 | + break; |
| 169 | + case "_language": |
| 170 | + attachment.Language = reader.ReadAsString(); |
| 171 | + break; |
| 172 | + case "_detect_language": |
| 173 | + attachment.DetectLanguage = reader.ReadAsBoolean(); |
| 174 | + break; |
| 175 | + case "_indexed_chars": |
| 176 | + reader.Read(); |
| 177 | + attachment.IndexedCharacters = (long?)reader.Value; |
| 178 | + break; |
| 179 | + } |
| 180 | + } |
| 181 | + if (reader.TokenType == JsonToken.EndObject) |
| 182 | + { |
| 183 | + break; |
| 184 | + } |
| 185 | + } |
| 186 | + return attachment; |
| 187 | + } |
| 188 | + return null; |
| 189 | + } |
| 190 | + |
| 191 | + public override bool CanConvert(Type objectType) => objectType == typeof(Attachment); |
| 192 | + } |
| 193 | +} |
0 commit comments