From 63552e17dee3ff895ad83a8119f83701cb8c02fd Mon Sep 17 00:00:00 2001 From: oneRain Date: Tue, 28 Apr 2020 11:12:25 +0800 Subject: [PATCH] * LCIMFileMessage.cs: * LCIMAudioMessage.cs: * LCIMImageMessage.cs: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * LCIMVideoMessage.cs: chore: 完善富媒体消息 --- Realtime/Message/LCIMAudioMessage.cs | 7 ++- Realtime/Message/LCIMFileMessage.cs | 72 +++++++++++++++++++++------- Realtime/Message/LCIMImageMessage.cs | 16 +++++-- Realtime/Message/LCIMVideoMessage.cs | 22 +++++---- 4 files changed, 85 insertions(+), 32 deletions(-) diff --git a/Realtime/Message/LCIMAudioMessage.cs b/Realtime/Message/LCIMAudioMessage.cs index ead335e..830459f 100644 --- a/Realtime/Message/LCIMAudioMessage.cs +++ b/Realtime/Message/LCIMAudioMessage.cs @@ -24,7 +24,8 @@ namespace LeanCloud.Realtime { Dictionary data = base.Encode(); Dictionary fileData = data[MessageFileKey] as Dictionary; Dictionary metaData = fileData[MessageDataMetaDataKey] as Dictionary; - if (File.MetaData.TryGetValue(MessageDataMetaDurationKey, out object duration)) { + if (File.MetaData != null && + File.MetaData.TryGetValue(MessageDataMetaDurationKey, out object duration)) { metaData[MessageDataMetaDurationKey] = duration; } return data; @@ -32,6 +33,10 @@ namespace LeanCloud.Realtime { internal override void Decode(Dictionary msgData) { base.Decode(msgData); + + if (File.MetaData == null) { + return; + } if (File.MetaData.TryGetValue(MessageDataMetaDurationKey, out object duration) && double.TryParse(duration as string, out double d)) { Duration = d; diff --git a/Realtime/Message/LCIMFileMessage.cs b/Realtime/Message/LCIMFileMessage.cs index 27c82b5..0cdf93c 100644 --- a/Realtime/Message/LCIMFileMessage.cs +++ b/Realtime/Message/LCIMFileMessage.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Collections.Generic; using LeanCloud.Storage; @@ -18,21 +19,14 @@ namespace LeanCloud.Realtime { /// 文件大小 /// public int Size { - get { - if (int.TryParse(File.MetaData[MessageDataMetaSizeKey] as string, out int size)) { - return size; - } - return 0; - } + get; private set; } /// - /// 文件类型 + /// 文件扩展名 /// public string Format { - get { - return File.MimeType; - } + get; private set; } /// @@ -58,18 +52,47 @@ namespace LeanCloud.Realtime { 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 }, - { MessageDataUrlKey, File.Url }, - { MessageDataMetaDataKey, new Dictionary { - { MessageDataMetaNameKey, File.Name }, - { MessageDataMetaFormatKey, File.MimeType } - } } + { MessageDataObjectIdKey, File.ObjectId } }; - if (File.MetaData.TryGetValue(MessageDataMetaSizeKey, out object size)) { - Dictionary metaData = fileData[MessageDataMetaDataKey] as Dictionary; + // 链接 + 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; @@ -80,6 +103,9 @@ namespace LeanCloud.Realtime { 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; @@ -88,9 +114,19 @@ namespace LeanCloud.Realtime { } 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; + } } } } diff --git a/Realtime/Message/LCIMImageMessage.cs b/Realtime/Message/LCIMImageMessage.cs index 7d116d2..5bc4144 100644 --- a/Realtime/Message/LCIMImageMessage.cs +++ b/Realtime/Message/LCIMImageMessage.cs @@ -31,17 +31,23 @@ namespace LeanCloud.Realtime { Dictionary data = base.Encode(); Dictionary fileData = data[MessageFileKey] as Dictionary; Dictionary metaData = fileData[MessageDataMetaDataKey] as Dictionary; - if (File.MetaData.TryGetValue(MessageDataMetaWidthKey, out object width)) { - metaData[MessageDataMetaWidthKey] = width; - } - if (File.MetaData.TryGetValue(MessageDataMetaHeightKey, out object height)) { - metaData[MessageDataMetaHeightKey] = height; + if (File.MetaData != null) { + if (File.MetaData.TryGetValue(MessageDataMetaWidthKey, out object width)) { + metaData[MessageDataMetaWidthKey] = width; + } + if (File.MetaData.TryGetValue(MessageDataMetaHeightKey, out object height)) { + metaData[MessageDataMetaHeightKey] = height; + } } return data; } internal override void Decode(Dictionary msgData) { base.Decode(msgData); + + if (File.MetaData == null) { + return; + } if (File.MetaData.TryGetValue(MessageDataMetaWidthKey, out object width) && int.TryParse(width as string, out int w)) { Width = w; diff --git a/Realtime/Message/LCIMVideoMessage.cs b/Realtime/Message/LCIMVideoMessage.cs index 09f6a91..fb86337 100644 --- a/Realtime/Message/LCIMVideoMessage.cs +++ b/Realtime/Message/LCIMVideoMessage.cs @@ -38,20 +38,26 @@ namespace LeanCloud.Realtime { Dictionary data = base.Encode(); Dictionary fileData = data[MessageFileKey] as Dictionary; Dictionary metaData = fileData[MessageDataMetaDataKey] as Dictionary; - if (File.MetaData.TryGetValue(MessageDataMetaWidthKey, out object width)) { - metaData[MessageDataMetaWidthKey] = width; - } - if (File.MetaData.TryGetValue(MessageDataMetaHeightKey, out object height)) { - metaData[MessageDataMetaHeightKey] = height; - } - if (File.MetaData.TryGetValue(MessageDataMetaDurationKey, out object duration)) { - metaData[MessageDataMetaDurationKey] = duration; + if (File.MetaData != null) { + if (File.MetaData.TryGetValue(MessageDataMetaWidthKey, out object width)) { + metaData[MessageDataMetaWidthKey] = width; + } + if (File.MetaData.TryGetValue(MessageDataMetaHeightKey, out object height)) { + metaData[MessageDataMetaHeightKey] = height; + } + if (File.MetaData.TryGetValue(MessageDataMetaDurationKey, out object duration)) { + metaData[MessageDataMetaDurationKey] = duration; + } } return data; } internal override void Decode(Dictionary msgData) { base.Decode(msgData); + + if (File.MetaData == null) { + return; + } if (File.MetaData.TryGetValue(MessageDataMetaWidthKey, out object width) && int.TryParse(width as string, out int w)) { Width = w;