mirror of
https://github.com/truewhile/MeBox.git
synced 2026-11-16 04:44:23 +08:00
优化
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/truewhile/MeBox/internal/service/reader"
|
||||
)
|
||||
|
||||
func TestReaderContentOptions(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
for _, tc := range []struct {
|
||||
query string
|
||||
want reader.ContentOptions
|
||||
}{
|
||||
{"", reader.ContentOptions{}},
|
||||
{"?media=direct", reader.ContentOptions{MediaDirect: true}},
|
||||
{"?media=proxy", reader.ContentOptions{ForceProxy: true}},
|
||||
{"?audio=native", reader.ContentOptions{NativeAudio: true}},
|
||||
{"?audio=native&media=direct", reader.ContentOptions{NativeAudio: true, MediaDirect: true}},
|
||||
{"?audio=native&media=proxy", reader.ContentOptions{NativeAudio: true, ForceProxy: true}},
|
||||
{"?audio=%20NaTiVe%20&media=%20DiReCt%20", reader.ContentOptions{NativeAudio: true, MediaDirect: true}},
|
||||
{"?audio=native&media=unknown", reader.ContentOptions{NativeAudio: true}},
|
||||
{"?audio=browser&media=proxy", reader.ContentOptions{ForceProxy: true}},
|
||||
{"?audio=unknown&media=direct", reader.ContentOptions{MediaDirect: true}},
|
||||
} {
|
||||
t.Run(tc.query, func(t *testing.T) {
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
c.Request = httptest.NewRequest(http.MethodGet, "/api/reader/books/book/content"+tc.query, nil)
|
||||
if got := readerContentOptions(c); got != tc.want {
|
||||
t.Fatalf("options = %+v, want %+v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -2023,19 +2023,23 @@ func readerLocalAudioHandler(svc *service.Container) gin.HandlerFunc {
|
||||
|
||||
// readerContentOptions 从查询参数解析正文下发的可选项。
|
||||
//
|
||||
// 目前只有 `media` 一个维度:
|
||||
// - 不传(网页端):媒体全部走服务端签名代理,与历史行为一致;
|
||||
// - `media=direct`:书源开启直连时保留上游地址,由客户端自取;
|
||||
// - `media=proxy`:本次强制代理,给客户端直连失败时的退路。
|
||||
// - `media=proxy`:本次强制代理,给客户端直连失败时的退路;
|
||||
// - `audio=native`:由客户端原生解码音频,不走本地有声书的浏览器转码。
|
||||
//
|
||||
// audio 与 media 独立,原生解码同样可以走签名代理。
|
||||
func readerContentOptions(c *gin.Context) reader.ContentOptions {
|
||||
opts := reader.ContentOptions{
|
||||
NativeAudio: strings.EqualFold(strings.TrimSpace(c.Query("audio")), "native"),
|
||||
}
|
||||
switch strings.ToLower(strings.TrimSpace(c.Query("media"))) {
|
||||
case "direct":
|
||||
return reader.ContentOptions{MediaDirect: true}
|
||||
opts.MediaDirect = true
|
||||
case "proxy":
|
||||
return reader.ContentOptions{ForceProxy: true}
|
||||
default:
|
||||
return reader.ContentOptions{}
|
||||
opts.ForceProxy = true
|
||||
}
|
||||
return opts
|
||||
}
|
||||
|
||||
// readerBookContentHandler 书架维度正文(服务端应用替换净化规则)。
|
||||
|
||||
Reference in New Issue
Block a user