Skip to content

Commit 2548753

Browse files
committed
小程序/APP音视频链接加密
1 parent 8662206 commit 2548753

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

change.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@
3636
- [x] 如果开启了OSS云存储,则将音视频上传上去的同时将音视频设置为私有
3737
- [x] 支持音频和视频播放管理
3838
- [x] 管理后台,恢复和优化附件管理功能
39-
- [ ] 如果管理员或者用户是书籍项目所有人,则音视频链接支持直链播放,否则对音视频链接进行一定的防盗链处理
39+
- [x] 如果管理员或者用户是书籍项目所有人,则音视频链接支持直链播放,否则对音视频链接进行一定的防盗链处理
4040
- [x] 优化程序内的 `cmd` 执行
4141
- [x] 内容阅读页面音频视频播放功能
4242
- [x] 音频和视频播放倍速控制
4343
- [x] 视频画中画播放
4444
- [x] 禁止音频和视频直接下载
4545
- [x] 增加和升级API,使小程序和APP支持音频和视频播放,以及图片放大预览
46+
- [x] 小程序/APP返回的音视频内容进行加密
4647
- [x] 优化`html2json`仓库,解析`HTML`内容,使小程序支持音频和视频播放功能,以及图片放大预览功能
4748
- [x] BookStack 依赖检测,以便程序可以正常使用完整功能进行工作,检测项:chrome、puppeteer、git、calibre
4849

controllers/DocumentController.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,11 @@ func (this *DocumentController) Read() {
301301
src, _ := sel.Attr("src")
302302
if !(strings.HasPrefix(src, "https://") || strings.HasPrefix(src, "http://")) {
303303
sign, _ := utils.GenerateSign(src, time.Duration(utils.MediaDuration)*time.Second)
304-
src = src + "?sign=" + sign
304+
if strings.Contains(src, "?") {
305+
src = src + "&sign=" + sign
306+
} else {
307+
src = src + "?sign=" + sign
308+
}
305309
}
306310
if item == "video" {
307311
sel.BeforeHtml(fmt.Sprintf(videoBoxFmt, title, poster, src, title))

controllers/api/CommonController.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/TruthHun/html2json/html2json"
1313

14+
"github.com/TruthHun/BookStack/models/store"
1415
"github.com/TruthHun/BookStack/oauth"
1516

1617
"github.com/PuerkitoBio/goquery"
@@ -914,6 +915,7 @@ func (this *CommonController) handleReleaseV2(release, bookIdentify string) inte
914915
utils.HandleSVG(query, bookIdentify)
915916
query.Find(".reference-link").Remove()
916917
query.Find(".header-link").Remove()
918+
release, _ = query.Html()
917919

918920
nodes, err := html2json.NewDefault().Parse(release, models.GetAPIStaticDomain())
919921
if err != nil {
@@ -933,7 +935,34 @@ func (this *CommonController) handleReleaseV3(release, bookIdentify string) inte
933935
utils.HandleSVG(query, bookIdentify)
934936
query.Find(".reference-link").Remove()
935937
query.Find(".header-link").Remove()
938+
medias := []string{"audio", "video"}
939+
for _, tag := range medias {
940+
query.Find(tag).Each(func(idx int, sel *goquery.Selection) {
941+
src, ok := sel.Attr("src")
942+
if ok && !(strings.HasPrefix(src, "https://") || strings.HasPrefix(src, "http://")) {
943+
if utils.StoreType == utils.StoreOss { // OSS 云存储,则使用OSS签名,否则使用本地存储的链接签名
944+
if bucket, err := store.ModelStoreOss.GetBucket(); err == nil {
945+
src = strings.TrimLeft(src, "/")
946+
src, _ = bucket.SignURL(src, http.MethodGet, utils.MediaDuration)
947+
if slice := strings.Split(src, "/"); len(slice) > 2 {
948+
src = strings.Join(slice[3:], "/")
949+
}
950+
}
951+
} else {
952+
if sign, err := utils.GenerateSign(src, time.Duration(utils.MediaDuration)); err == nil {
953+
if strings.Contains(src, "?") {
954+
src = src + "&sign=" + sign
955+
} else {
956+
src = src + "?sign=" + sign
957+
}
958+
}
959+
}
960+
}
961+
sel.SetAttr("src", src)
962+
})
963+
}
936964

965+
release, _ = query.Html()
937966
nodes, err := html2json.NewDefault().ParseByByteV2([]byte(release), models.GetAPIStaticDomain())
938967
if err != nil {
939968
beego.Error(err)

0 commit comments

Comments
 (0)