/// <summary> /// 删除表情分组 /// </summary> /// <param name="directoryName">表情分类目录名</param> public void DeleteEmoticonCategory(string directoryName) { if (string.IsNullOrEmpty(directoryName)) { return; } EmotionCategory category = GetEmotionCategory(directoryName); if (category == null) { return; } if (Directory.Exists(category.PhysicalDirectoryPath)) { DirectoryInfo dir = new DirectoryInfo(category.PhysicalDirectoryPath); //将文件夹内的内容设置为可写 foreach (var fp in dir.GetFiles()) { File.SetAttributes(fp.FullName, System.IO.FileAttributes.Normal); } Directory.Delete(category.PhysicalDirectoryPath, true); _categories.RemoveAll(n => n.DirectoryName == category.DirectoryName); emotionRepository.DeleteByEntityId(category.DirectoryName); } }
/// <summary> /// 更新表情分类 /// </summary> /// <param name="emotionCategory">表情分类</param> public void UpdateEmotionCategory(EmotionCategory emotionCategory) { emotionRepository.Update(emotionCategory); //加载表情 if (emotionCategory.IsEnabled && (emotionCategory.Emotions == null || emotionCategory.Emotions.Count() == 0)) { LoadEmoticons(emotionCategory); } }
/// <summary> /// 创建实体示例 /// </summary> /// <param name="directoryName">表情包目录名</param> public static EmotionCategory New(string directoryName) { EmotionCategory category = new EmotionCategory(); if (!string.IsNullOrEmpty(directoryName)) { category.DirectoryName = directoryName; } return(category); }
/// <summary> /// 根据CategoryID获取EmotionCategory /// </summary> /// <param name="directoryName">表情分类目录名</param> public EmotionCategory GetEmotionCategory(string directoryName) { if (_categories != null) { EmotionCategory category = _categories.FirstOrDefault(n => n.DirectoryName == directoryName); if (category != null && category.Emotions.Count == 0) { LoadEmoticons(category); } return(category); } return(null); }
/// <summary> /// 组装表情分类实体 /// </summary> /// <param name="directoryName">目录名</param> /// <param name="fullPath">表情包目录</param> private void PopulateEmotionCategory(string directoryName, string fullPath) { if (string.IsNullOrEmpty(directoryName)) { return; } string configFullName = fullPath + "\\" + _emoticonConfigName; if (!File.Exists(configFullName)) { Directory.Delete(fullPath, true); throw new ExceptionFacade("找不到文件:" + configFullName + ",您上传的不是表情包"); } XElement document = XElement.Load(configFullName); if (document == null) { Directory.Delete(fullPath, true); return; } EmotionCategory category; if (!emotionRepository.Exists(directoryName)) { category = EmotionCategory.New(directoryName); int maxDisplayOrder = emotionRepository.GetAll().Max(n => n.DisplayOrder); category.DisplayOrder = maxDisplayOrder + 1; emotionRepository.Insert(category); } else { category = emotionRepository.Get(directoryName); } category.InitPropertyValue(document, fullPath); if (category != null) { if (_categories.Where(n => n.DirectoryName == category.DirectoryName).Count() == 0) { _categories.Add(category); } if (category.IsEnabled) { PopulateEmotion(category, document); } } }
/// <summary> /// 加载表情分类下的表情 /// </summary> /// <param name="emotionCategory">表情分类</param> public void LoadEmoticons(EmotionCategory emotionCategory) { if (emotionCategory == null) { return; } XElement document = XElement.Load(emotionCategory.PhysicalDirectoryPath + "\\" + _emoticonConfigName); if (document != null) { //为启用的分类加载表情 PopulateEmotion(emotionCategory, document); } }
/// <summary> /// 组装表情实体 /// </summary> /// <param name="category">表情分类</param> /// <param name="categoryElement">表情分类配置节点</param> private void PopulateEmotion(EmotionCategory category, XElement categoryElement) { string emoticonPath = WebUtility.ResolveUrl(_emotionSettings.EmoticonPath); IEnumerable <XElement> emotionsElements = categoryElement.Elements(); foreach (var emotionElement in emotionsElements) { Emotion emotion = Emotion.New(emotionElement); emotion.FormatedCode = string.Format("[{0}{1}]", !string.IsNullOrEmpty(category.CodePrefix) ? category.CodePrefix + ":" : string.Empty, emotion.Code); emotion.ImageUrl = string.Format("{0}/{1}/{2}", emoticonPath, category.DirectoryName, emotion.FileName); if (category.Emotions.Where(n => n.Code == emotion.Code).Count() == 0) { category.Emotions.Add(emotion); } PrepareTransformsInfo(category); } }
/// <summary> /// 将所有表情写入集合中供下面的转换 /// </summary> /// <param name="category">表情分类</param> private void PrepareTransformsInfo(EmotionCategory category) { if (category == null || category.Emotions == null) { return; } foreach (Emotion emotion in category.Emotions) { string smileyPattern = emotion.FormatedCode; string replacePattern = string.Format("<img src=\"{0}\" title=\"{1}\" alt=\"{1}\" />", emotion.ImageUrl, emotion.Description); maxWordLength = Math.Max(maxWordLength, smileyPattern.Length); minWordLength = Math.Min(minWordLength, smileyPattern.Length); foreach (char c in smileyPattern) { allCharCheck[c] = true; } emoctionDictionary[smileyPattern] = replacePattern; } }