zjj285156
关注
139654 号成员,2024-04-22 16:46:18 加入
6
个人主页 浏览
2h4m
在线时长
  • 插入本地视频文件上传后,伺服模式下网页端无法播放

    2024-05-06 09:38

    已自行解决。

    结合开源代码和 F12 报错信息,判定鉴权过程出错导致无法播放

    下面是相关的源代码

    func serveCheckAuth(c *gin.Context) {
    	data, err := os.ReadFile(filepath.Join(util.WorkingDir, "stage/auth.html"))
    	if nil != err {
    		logging.LogErrorf("load auth page failed: %s", err)
    		c.Status(500)
    		return
    	}
    
    	tpl, err := template.New("auth").Parse(string(data))
    	if nil != err {
    		logging.LogErrorf("parse auth page failed: %s", err)
    		c.Status(500)
    		return
    	}
    
    	keymapHideWindow := "⌥M"
    	if nil != (*model.Conf.Keymap)["general"] {
    		switch (*model.Conf.Keymap)["general"].(type) {
    		case map[string]interface{}:
    			keymapGeneral := (*model.Conf.Keymap)["general"].(map[string]interface{})
    			if nil != keymapGeneral["toggleWin"] {
    				switch keymapGeneral["toggleWin"].(type) {
    				case map[string]interface{}:
    					toggleWin := keymapGeneral["toggleWin"].(map[string]interface{})
    					if nil != toggleWin["custom"] {
    						keymapHideWindow = toggleWin["custom"].(string)
    					}
    				}
    			}
    		}
    		if "" == keymapHideWindow {
    			keymapHideWindow = "⌥M"
    		}
    	}
    	model := map[string]interface{}{
    		"l0":                     model.Conf.Language(173),
    		"l1":                     model.Conf.Language(174),
    		"l2":                     template.HTML(model.Conf.Language(172)),
    		"l3":                     model.Conf.Language(175),
    		"l4":                     model.Conf.Language(176),
    		"l5":                     model.Conf.Language(177),
    		"l6":                     model.Conf.Language(178),
    		"l7":                     template.HTML(model.Conf.Language(184)),
    		"l8":                     model.Conf.Language(95),
    		"appearanceMode":         model.Conf.Appearance.Mode,
    		"appearanceModeOS":       model.Conf.Appearance.ModeOS,
    		"workspace":              filepath.Base(util.WorkspaceDir),
    		"workspacePath":          util.WorkspaceDir,
    		"keymapGeneralToggleWin": keymapHideWindow,
    		"trayMenuLangs":          util.TrayMenuLangs[util.Lang],
    		"workspaceDir":           util.WorkspaceDir,
    	}
    	buf := &bytes.Buffer{}
    	if err = tpl.Execute(buf, model); nil != err {
    		logging.LogErrorf("execute auth page failed: %s", err)
    		c.Status(500)
    		return
    	}
    	data = buf.Bytes()
    	c.Data(http.StatusOK, "text/html; charset=utf-8", data)
    }
    
    func serveAssets(ginServer *gin.Engine) {
    	ginServer.POST("/upload", model.CheckAuth, model.Upload)
    
    	ginServer.GET("/assets/*path", model.CheckAuth, func(context *gin.Context) {
    		requestPath := context.Param("path")
    		relativePath := path.Join("assets", requestPath)
    		p, err := model.GetAssetAbsPath(relativePath)
    		if nil != err {
    			context.Status(404)
    			return
    		}
    		http.ServeFile(context.Writer, context.Request, p)
    		return
    	})
    	ginServer.GET("/history/*path", model.CheckAuth, func(context *gin.Context) {
    		p := filepath.Join(util.HistoryDir, context.Param("path"))
    		http.ServeFile(context.Writer, context.Request, p)
    		return
    	})
    }
    

    所以问题的解决方法关键在于跳过鉴权过程,于是在本地利用 nginx 反代所有/assets/请求来实现

        server {
            listen       80;
            server_name  localhost;
    
    		location /assets/ {
    			alias D:/SiyuanData/data/assets/;
    			expires 30d; # 设置缓存时间为30天
    			access_log off; # 关闭访问日志
    			add_header Cache-Control "public"; # 允许客户端和代理服务器缓存文件
    			try_files $uri $uri/ =404; # 尝试直接返回请求的文件或目录,如果不存在则返回404错误
    		}
    
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    

    然后在域名网关处利用 nginx 来实现 xxx.com 与 xxx.com/assets 访问不同端口

    实际测试完美,问题解决 🤪