修复文章发布报错

This commit is contained in:
2026-08-20 01:11:43 +08:00
parent 14f2e8b5c1
commit 758f072494
17 changed files with 644 additions and 69 deletions
+26 -1
View File
@@ -119,7 +119,32 @@ func (c *TenantSiteController) resolveTemplateCode(tid uint64) string {
func (c *TenantSiteController) writeHTML(status int, html string) {
c.Ctx.Output.SetStatus(status)
c.Ctx.Output.Header("Content-Type", "text/html; charset=utf-8")
c.Ctx.WriteString(html)
c.Ctx.WriteString(c.forceHTTPSStorageURLs(html))
}
// forceHTTPSStorageURLs 访客以 HTTPS 访问站点时,把页面中存储(CDN)域名的
// http:// 资源升级为 https://,避免混合内容(Mixed Content)被浏览器拦截。
// 仅替换存储配置里那个域名,不动正文里的其他外部链接。
func (c *TenantSiteController) forceHTTPSStorageURLs(html string) string {
proto := c.Ctx.Input.Header("X-Forwarded-Proto")
if proto == "" && c.Ctx.Input.IsSecure() {
proto = "https"
}
if proto != "https" {
return html
}
cfg, err := models.GetStorageConfig()
if err != nil || strings.TrimSpace(cfg.QiniuDomain) == "" {
return html
}
host := strings.TrimSpace(cfg.QiniuDomain)
host = strings.TrimPrefix(host, "http://")
host = strings.TrimPrefix(host, "https://")
host = strings.TrimRight(host, "/")
if host == "" {
return html
}
return strings.ReplaceAll(html, "http://"+host, "https://"+host)
}
// render 解析并输出模板文件