/// <summary> /// 将生成的webfont资源上传到存储空间里,并返回保存的地址,用于构建@font-face语句 /// </summary> /// <param name="webFontInfo"></param> /// <param name="url">自定义保存路径</param> /// <returns></returns> private FontFaceInfo UploadFontFile(WebFontInfo webFontInfo, string url) { string objectBaseName = ""; // 类似:a/b/font1 if (string.IsNullOrEmpty(url)) { // 使用fontid+ randomid objectBaseName = $"fontface/{webFontInfo.fontId}/{Guid.NewGuid().ToString("N")}/{webFontInfo.nameEn}"; } else { objectBaseName = url; } Console.WriteLine("objectBaseName:" + objectBaseName); string eotObjectName = objectBaseName + ".eot"; string woffObjectName = objectBaseName + ".woff"; string ttfObjectName = objectBaseName + ".ttf"; string woff2ObjectName = objectBaseName + ".woff2"; string eotUrlString = webFontInfo.bytesEot.Length != 0 ? UploadOss(webFontInfo.bytesEot, eotObjectName) : ""; string woffUrlString = webFontInfo.bytesWoff.Length != 0 ? UploadOss(webFontInfo.bytesWoff, woffObjectName) : ""; string woff2UrlString = webFontInfo.bytesWoff2.Length != 0 ? UploadOss(webFontInfo.bytesWoff2, woff2ObjectName) : ""; string ttfUrlString = webFontInfo.bytesTtf.Length != 0 ? UploadOss(webFontInfo.bytesTtf, ttfObjectName) : ""; return(new FontFaceInfo() { eotUrl = eotUrlString, woffUrl = woffUrlString, woff2Url = woff2UrlString, ttfUrl = ttfUrlString, nameEn = webFontInfo.nameEn }); }
/// <summary> /// 传入用户的字体的accesskey和需要处理的文本,然后进行构建字体库 /// </summary> /// <param name="apikey">用户的apikey</param> /// <param name="accessKey">字体的accesskey</param> /// <param name="content">需要处理的文本</param> /// <returns></returns> private async Task <WebFontInfo> BuildFont(string apikey, string accessKey, string content) { FontBuildAccessKeyRequest request = new FontBuildAccessKeyRequest { Apikey = _apiKey, Accesskey = accessKey, Text = content, NeedTtf = true, //如果不需要此格式的文件返回,则此处参数设置为false NeedEot = false, //如果不需要此格式的文件返回,则此处参数设置为false NeedWoff = false, //如果不需要此格式的文件返回,则此处参数设置为false NeedWoff2 = false //如果不需要此格式的文件返回,则此处参数设置为false }; try { var response = await _client.BuildFontForAccessKeyAsync(request); WebFontInfo webFontInfo = new WebFontInfo() { apiKey = apikey, fontId = (int)response.FontId, content = content, fontCheckSum = (long)response.FontChecksum, fontFormatVersion = (int)response.FontFormatVersion, nameEn = response.NameEn, bytesTtf = response.BytesTtf.ToByteArray(), bytesEot = response.BytesEot.ToByteArray(), bytesWoff = response.BytesWoff.ToByteArray(), bytesWoff2 = response.BytesWoff2.ToByteArray() }; //---------------------------打印日志-------------------------- return(webFontInfo); } catch (Exception e) { _logger.Log(LogLevel.Warning, "RPC failed: {0} Desc: {1}", new Object[] { e.Message, e.StackTrace }); throw e; } }