using System; using System.IO; using System.Collections.Generic; using LeanCloud.Storage; namespace LeanCloud.Realtime { /// /// 文件消息 /// public class LCIMFileMessage : LCIMTextMessage { /// /// 文件 /// public LCFile File { get; set; } /// /// 文件大小 /// public int Size { get; private set; } /// /// 文件扩展名 /// public string Format { get; private set; } /// /// 文件链接 /// 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."); } if (string.IsNullOrEmpty(File.ObjectId)) { throw new Exception("File MUST be saved before sent."); } Dictionary fileData = new Dictionary { { MessageDataObjectIdKey, File.ObjectId } }; // 链接 if (!string.IsNullOrEmpty(File.Url)) { fileData[MessageDataUrlKey] = File.Url; } // 元数据 Dictionary metaData = new Dictionary(); // 文件名 if (!string.IsNullOrEmpty(File.Name)) { metaData[MessageDataMetaNameKey] = File.Name; } // 文件扩展名 string format = null; if (File.MetaData != null && File.MetaData.TryGetValue(MessageDataMetaFormatKey, out object f)) { // 优先使用用户设置值 format = f as string; } else if (File.Name != null && !string.IsNullOrEmpty(Path.GetExtension(File.Name))) { // 根据文件名推测 format = Path.GetExtension(File.Name)?.Replace(".", string.Empty); } else if (File.Url != null && !string.IsNullOrEmpty(Path.GetExtension(File.Url))) { // 根据 url 推测 format = Path.GetExtension(File.Url)?.Replace(".", string.Empty); } if (!string.IsNullOrEmpty(format)) { metaData[MessageDataMetaFormatKey] = format; } // 文件大小 if (File.MetaData != null && File.MetaData.TryGetValue(MessageDataMetaSizeKey, out object size)) { metaData[MessageDataMetaSizeKey] = size; } fileData[MessageDataMetaDataKey] = metaData; 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 == null) { return; } 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 == null) { return; } if (File.MetaData.TryGetValue(MessageDataMetaNameKey, out object name)) { File.Name = name as string; } if (File.MetaData.TryGetValue(MessageDataMetaSizeKey, out object size) && int.TryParse(size as string, out int s)) { Size = s; } if (File.MetaData.TryGetValue(MessageDataMetaFormatKey, out object format)) { Format = format as string; } } } } } } }