/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="msg"></param> /// <returns></returns> public static DAO.MessageContentInfo ToDao(this MessageContentInfo obj, DAO.Message msg) { if (obj == null) { return(null); } #region Validate parameters if (msg == null) { throw new ArgumentNullException("msg"); } #endregion var dao = new DAO.MessageContentInfo(); dao.Comment = obj.Comment; dao.FileSize = obj.FileSize; dao.Length = obj.Length; dao.LINK = obj.LINK; dao.Message = msg; dao.Name = obj.Name; dao.Type = obj.Type; return(dao); }
/// <summary> /// /// </summary> /// <param name="dao"></param> /// <returns></returns> public static MessageContentInfo ToObj(this DAO.MessageContentInfo dao) { if (dao == null) { return(null); } var obj = new MessageContentInfo(); obj.Comment = dao.Comment; obj.FileSize = dao.FileSize; obj.Length = dao.Length; obj.LINK = dao.LINK; obj.MessageLINK = dao.Message.LINK; obj.Name = dao.Name; obj.Type = dao.Type; return(obj); }
/// <summary> /// Сохранить контент сообщения. /// </summary> /// <param name="content"></param> public virtual void SaveMessageContent(MessageContent content) { #region Validate parameters if (content == null) { throw new ArgumentNullException("content"); } #endregion MessageContentInfo contentInfo = content.ContentInfo(); ContentType contentType = contentInfo.ContentType(); Encoding encoding = contentType.Encoding(); using (UnitOfWork work = BeginWork()) { int msgLink = content.MessageLINK.Value; var msg = work.Get <DAO.Message>(msgLink); if (msg == null) { throw new MessageNotFoundException(msgLink); } DAO.MessageContentInfo dao = contentInfo.ToDao(msg); if (dao.LINK == 0) { work.Save(dao); } else { work.Update <DAO.MessageContentInfo>(ref dao); } if (content.Value != null) { using (MessageContentStreamBase stream = ConstructMessageContentStream(dao.LINK, work, DataStreamMode.WRITE, encoding)) { stream.WriteTimeout = this.ExecuteTimeout; var buffer = new char[this.bufferSize]; int charsReaded; do { charsReaded = content.Value.Read(buffer, 0, buffer.Length); if (charsReaded > 0) { stream.Write(buffer, 0, charsReaded); } } while (charsReaded > 0); if (dao.Length == null) { dao.Length = (int)stream.Length; work.Update <DAO.MessageContentInfo>(ref dao); } work.End(); } } else { work.End(); } contentInfo = dao.ToObj(); content.ApplyInfo(contentInfo); } }