using LeanCloud; using LeanCloud.Storage.Internal; using LeanCloud.Realtime.Internal; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace LeanCloud.Realtime { /// /// 图像消息 /// [AVIMMessageClassName("_AVIMImageMessage")] [AVIMTypedMessageTypeInt(-2)] public class AVIMImageMessage : AVIMFileMessage { } /// /// File message. /// [AVIMMessageClassName("_AVIMFileMessage")] [AVIMTypedMessageTypeInt(-6)] public class AVIMFileMessage : AVIMMessageDecorator { /// /// Initializes a new instance of the class. /// public AVIMFileMessage() : base(new AVIMTypedMessage()) { } /// /// Initializes a new instance of the class. /// /// Message. public AVIMFileMessage(AVIMTypedMessage message) : base(message) { } /// /// Gets or sets the file. /// /// The file. public AVFile File { get; set; } /// /// Froms the URL. /// /// The URL. /// URL. /// The 1st type parameter. public static T FromUrl(string url) where T : AVIMFileMessage, new() { return FromUrl(string.Empty.Random(8), url, null); } /// /// From the URL. /// /// The URL. /// File name. /// External URL. /// Text title. /// Custom attributes. /// The 1st type parameter. public static T FromUrl(string fileName, string externalUrl, string textTitle, IDictionary customAttributes = null) where T : AVIMFileMessage, new() { T message = new T(); message.File = new AVFile(fileName, externalUrl); message.TextContent = textTitle; message.MergeCustomAttributes(customAttributes); return message; } /// /// From the stream. /// /// The stream. /// File name. /// Data. /// MIME type. /// Text title. /// Meta data. /// Custom attributes. /// The 1st type parameter. public static T FromStream(string fileName, Stream data, string mimeType, string textTitle, IDictionary metaData, IDictionary customAttributes = null) where T : AVIMFileMessage, new() { T message = new T(); message.File = new AVFile(fileName, data, mimeType, metaData); message.TextContent = textTitle; message.MergeCustomAttributes(customAttributes); return message; } /// /// Encodes the decorator. /// /// The decorator. public override IDictionary EncodeDecorator() { if (File.Url == null) throw new InvalidOperationException("File.Url can not be null before it can be sent."); File.MetaData["name"] = File.Name; File.MetaData["format"] = File.MimeType; var fileData = new Dictionary { { "url", File.Url.ToString()}, { "objId", File.ObjectId }, { "metaData", File.MetaData } }; return new Dictionary { { AVIMProtocol.LCFILE, fileData } }; } /// /// Deserialize the specified msgStr. /// /// The deserialize. /// Message string. public override IAVIMMessage Deserialize(string msgStr) { var msg = Json.Parse(msgStr) as IDictionary; var fileData = msg[AVIMProtocol.LCFILE] as IDictionary; string mimeType = null; string url = null; string name = null; string objId = null; IDictionary metaData = null; if (fileData != null) { object metaDataObj = null; if (fileData.TryGetValue("metaData", out metaDataObj)) { metaData = metaDataObj as IDictionary; object fileNameObj = null; if (metaData != null) { if (metaData.TryGetValue("name", out fileNameObj)) { name = fileNameObj.ToString(); } } object mimeTypeObj = null; if (metaData != null) { if (metaData.TryGetValue("format", out mimeTypeObj)) { if (mimeTypeObj != null) mimeType = mimeTypeObj.ToString(); } } } object objIdObj = null; if (fileData.TryGetValue("objId", out objIdObj)) { if (objIdObj != null) objId = objIdObj.ToString(); } object urlObj = null; if (fileData.TryGetValue("url", out urlObj)) { url = urlObj.ToString(); } File = AVFile.CreateWithData(objId, name, url, metaData); } return base.Deserialize(msgStr); } } /// /// Location message. /// [AVIMMessageClassName("_AVIMMessageClassName")] [AVIMTypedMessageTypeInt(-5)] public class AVIMLocationMessage : AVIMMessageDecorator { /// /// Initializes a new instance of the class. /// public AVIMLocationMessage() : base(new AVIMTypedMessage()) { } /// /// Gets or sets the location. /// /// The location. public AVGeoPoint Location { get; set; } public AVIMLocationMessage(AVGeoPoint location) : this() { Location = location; } /// /// Encodes the decorator. /// /// The decorator. public override IDictionary EncodeDecorator() { var locationData = new Dictionary { { "longitude", Location.Longitude}, { "latitude", Location.Latitude } }; return new Dictionary { { AVIMProtocol.LCLOC, locationData } }; } /// /// Deserialize the specified msgStr. /// /// The deserialize. /// Message string. public override IAVIMMessage Deserialize(string msgStr) { var msg = Json.Parse(msgStr) as IDictionary; var locationData = msg[AVIMProtocol.LCLOC] as IDictionary; if (locationData != null) { Location = new AVGeoPoint(double.Parse(locationData["latitude"].ToString()), double.Parse(locationData["longitude"].ToString())); } return base.Deserialize(msgStr); } } }