Esempio n. 1
0
        /// <summary>
        /// 净化富文本
        /// </summary>
        /// <param name="html">HTML 代码</param>
        /// <returns>净化后的 HTML 代码</returns>
        public static string SanitizeRichText(string html)
        {
            Config.HtmlEncoder     = new HtmlEncoderNone();
            Config.OutputFormatter = OutputFormatters.HtmlEncodingNone;
            var sanitizer = new HtmlSanitizer(
                new[]
            {
                "br", "span", "a", "img", "b", "strong", "i", "strike", "u", "p", "blockquote", "h1", "hr",
                "comment", "spoiler", "table", "colgroup", "col", "thead", "tr", "th", "tbody", "td"
            },
                null,
                new[] { "src", "alt", "width", "height", "data-non-image", "href", "style" },
                null,
                new[] { "text-align" });
            var dom = CQ.Create(sanitizer.Sanitize(html));

            foreach (var img in dom["img"])
            {
                if (string.IsNullOrWhiteSpace(img.Attributes["src"]))
                {
                    img.Remove();
                }
                else
                {
                    var fileName = UpyunProvider.ExtractFileName(img.Attributes["src"]);
                    if (string.IsNullOrWhiteSpace(fileName))
                    {
                        continue;
                    }
                    img.Attributes["article-image-src"] = $"keylol://{fileName}";
                    img.RemoveAttribute("src");
                }
            }
            return(dom.Render());
        }
Esempio n. 2
0
        /// <summary>
        /// 如果图片属于自有存储,则提取出来替换 Schema,否则返回原图
        /// </summary>
        /// <param name="coverImage">封面图</param>
        /// <returns>净化后的封面图</returns>
        public static string SanitizeCoverImage(string coverImage)
        {
            var fileName = UpyunProvider.ExtractFileName(coverImage);

            return(string.IsNullOrWhiteSpace(fileName) ? coverImage : $"keylol://{fileName}");
        }
Esempio n. 3
0
        private async Task <string> UploadFromUrlAsync([NotNull] string url)
        {
            try
            {
                url = Regex.Replace(url, @"^\/\/", "http://");
                if (!Regex.IsMatch(url, @"^https?:\/\/"))
                {
                    url = $"http://{url}";
                }
                var request = WebRequest.CreateHttp(url);
                request.Referer   = url;
                request.UserAgent =
                    "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36";
                request.Accept = "image/webp,image/*,*/*;q=0.8";
                request.Headers["Accept-Language"] = "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4";
                request.Timeout          = 10000;
                request.ReadWriteTimeout = 60000;
                using (var response = await request.GetResponseAsync())
                    using (var ms = new MemoryStream(response.ContentLength > 0 ? (int)response.ContentLength : 0))
                    {
                        var extension = MimeTypeToFileExtension(response.ContentType?.Split(';')[0]);
                        if (extension == null) // 不支持的类型
                        {
                            _logger.Warn($"Unsupported MIME type: {url}");
                            return(null);
                        }
                        if (response.ContentLength > UpyunProvider.MaxImageSize)
                        {
                            _logger.Warn($"Image (Content-Length) is too large: {url}");
                            return(null);
                        }
                        var responseStream = response.GetResponseStream();
                        if (responseStream == null)
                        {
                            _logger.Warn($"Null response stream: {url}");
                            return(null);
                        }
                        await responseStream.CopyToAsync(ms);

                        var fileData = ms.ToArray();
                        if (fileData.Length <= 0)
                        {
                            _logger.Warn($"Empty response stream: {url}");
                            return(null);
                        }
                        if (fileData.Length > UpyunProvider.MaxImageSize)
                        {
                            _logger.Warn($"Image (response stream length) is too large: {url}");
                            return(null);
                        }
                        var name = await UpyunProvider.UploadFile(fileData, extension);

                        if (string.IsNullOrWhiteSpace(name))
                        {
                            _logger.Warn($"Upload failed: {url}");
                            return(null);
                        }
                        return($"keylol://{name}");
                    }
            }
            catch (Exception e)
            {
                _logger.Warn($"Download failed: {url}", e);
                return(null);
            }
        }