using System; using System.Collections.Generic; using LeanCloud.Storage; namespace LeanCloud.Realtime { /// /// 文件消息 /// public class LCIMFileMessage : LCIMTextMessage { /// /// 文件 /// public LCFile File { get; set; } /// /// 文件大小 /// public int Size { get { if (int.TryParse(File.MetaData[MessageDataMetaSizeKey] as string, out int size)) { return size; } return 0; } } /// /// 文件类型 /// public string Format { get { return File.MimeType; } } /// /// 文件链接 /// public string Url { get { return File.Url; } } internal LCIMFileMessage() : base() { } public LCIMFileMessage(LCFile file) : base() { File = file; } public override int MessageType => FileMessageType; internal override Dictionary Encode() { if (File == null) { throw new Exception("File MUST NOT be null before sent."); } Dictionary fileData = new Dictionary { { MessageDataObjectIdKey, File.ObjectId }, { MessageDataUrlKey, File.Url }, { MessageDataMetaDataKey, new Dictionary { { MessageDataMetaNameKey, File.Name }, { MessageDataMetaFormatKey, File.MimeType } } } }; if (File.MetaData.TryGetValue(MessageDataMetaSizeKey, out object size)) { Dictionary metaData = fileData[MessageDataMetaDataKey] as Dictionary; metaData[MessageDataMetaSizeKey] = size; } Dictionary data = base.Encode(); data[MessageFileKey] = fileData; return data; } internal override void Decode(Dictionary msgData) { base.Decode(msgData); if (msgData.TryGetValue(MessageFileKey, out object fileDataObject)) { Dictionary fileData = fileDataObject as Dictionary; if (fileData.TryGetValue(MessageDataObjectIdKey, out object objectIdObject)) { string objectId = objectIdObject as string; File = LCObject.CreateWithoutData(LCFile.CLASS_NAME, objectId) as LCFile; if (fileData.TryGetValue(MessageDataUrlKey, out object url)) { File.Url = url as string; } if (fileData.TryGetValue(MessageDataMetaDataKey, out object metaData)) { File.MetaData = metaData as Dictionary; if (File.MetaData.TryGetValue(MessageDataMetaNameKey, out object name)) { File.Name = name as string; } } } } } } }