/// <summary> /// 保存资源文件(存放于OSS上) /// </summary> /// <param name="modelId"></param> /// <param name="resourceFile"></param> /// <returns></returns> internal bool SaveModelResource(int modelId, IFormFile file) { byte[] data = file.ToArray(); ViewModel.ResourceFile resource = new ViewModel.ResourceFile(file, data); if (modelId == 0) { return(this.FaildMessage("模型编号错误")); } if (string.IsNullOrEmpty(resource.Name)) { return(this.FaildMessage("没有上传内容")); } using (DbExecutor db = NewExecutor()) { ViewModel model = new ViewModel() { ID = modelId }.Info(db); if (model == null) { return(this.FaildMessage("模型编号错误")); } if (model.ResourceFiles.ContainsKey(resource.Name)) { model.ResourceFiles[resource.Name] = resource; } else { model.ResourceFiles.Add(resource.Name, resource); } model.Resources = JsonConvert.SerializeObject(model.ResourceFiles); // 上传文件至于OSS if (!OSSAgent.Upload(new OSSSetting(SettingAgent.Instance().GetSetting(SystemSetting.SettingType.CDNOSS)), resource.Path, data, new ObjectMetadata(), out string message)) { return(this.FaildMessage(message)); } return(model.Update(db, t => t.Resources) == 1); } }
/// <summary> /// 发布模型 /// </summary> /// <param name="modelId"></param> /// <returns></returns> internal bool PublishModel(int modelId) { ViewModel model = this.GetModelInfo(modelId); if (model == null) { return(this.FaildMessage("模型错误")); } Language[] languages = SettingAgent.Instance().GetSetting(SystemSetting.SettingType.Language).Split(',').Select(t => t.ToEnum <Language>()).ToArray(); if (languages.Length == 0) { return(this.FaildMessage("没有配置语言包")); } string translateUrl = SettingAgent.Instance().GetSetting(SystemSetting.SettingType.Translate); if (string.IsNullOrEmpty(translateUrl)) { return(this.FaildMessage("没有配置语言包接口地址")); } //#1 找出样式文件中需要替换的资源路径 model.Style = Regex.Replace(model.Style, @"url\((?<Resource>[^\)]+?)\)", t => { string resource = t.Groups["Resource"].Value; if (model.ResourceFiles.ContainsKey(resource)) { return($"url({model.ResourceFiles[resource]})"); } return(t.Value); }); using (DbExecutor db = NewExecutor()) { model.Update(db, t => t.Style); } //#2 发布页面的各个语言版本 { Regex regex = new Regex(@"~.+?~", RegexOptions.Multiline); //#2.1 找出单词 Dictionary <string, bool> words = new Dictionary <string, bool>(); foreach (Match match in regex.Matches(model.Page)) { string word = match.Value; if (!words.ContainsKey(word)) { words.Add(word, true); } } //#2.2 发送到翻译API,获得语言包 Dictionary <string, Dictionary <Language, string> > dic = Translate.Get(words.Select(t => t.Key).ToArray(), translateUrl, languages); foreach (Language language in languages) { int total = 0; int count = 0; string content = regex.Replace(model.Page, t => { total++; if (dic.ContainsKey(t.Value) && dic[t.Value].ContainsKey(language)) { count++; return(dic[t.Value][language]); } return(t.Value.Replace("~", string.Empty)); }); string path = $"html/{ Encryption.toMD5Short(Encryption.toMD5(content)) }.html"; if (!OSSAgent.Upload(new OSSSetting(SettingAgent.Instance().GetSetting(SystemSetting.SettingType.CDNOSS)), path, Encoding.UTF8.GetBytes(content), new ObjectMetadata() { ContentType = "text/html", ContentEncoding = "utf-8", ContentDisposition = "inline", CacheControl = "only-if-cached", ExpirationTime = DateTime.Now.AddYears(1), }, out string message)) { return(this.FaildMessage(message)); } ViewContent viewContent = new ViewContent() { Language = language, ModelID = modelId, Path = path, Translate = total == 0 ? 1M : count / (decimal)total }; this.SaveModelContent(viewContent); } } return(true); }