@@ -38,7 +38,21 @@ func (this *StaticController) APP() {
3838func (this * StaticController ) Uploads () {
3939 file := strings .TrimLeft (this .GetString (":splat" ), "./" )
4040 path := strings .ReplaceAll (filepath .Join ("uploads" , file ), "\\ " , "/" )
41- fmt .Println ("===========" , path )
41+
42+ // 签名验证
43+ sign := this .GetString ("sign" )
44+ if ! this .isValidSign (sign , path ) {
45+ // 签名验证不通过,需要再次验证书籍是否是用户的(针对编辑状态)
46+ if ! this .isBookOwner () {
47+ this .Abort ("404" )
48+ return
49+ }
50+ }
51+
52+ if utils .IsSignUsed (sign ) {
53+ this .Abort ("404" )
54+ }
55+
4256 http .ServeFile (this .Ctx .ResponseWriter , this .Ctx .Request , path )
4357}
4458
@@ -62,26 +76,29 @@ func (this *StaticController) ProjectsFile() {
6276
6377 object := filepath .Join ("projects/" , strings .TrimLeft (this .GetString (":splat" ), "./" ))
6478 object = strings .ReplaceAll (object , "\\ " , "/" )
79+
6580 // 不是音频和视频,直接跳转
6681 if ! this .isMedia (object ) {
6782 this .Redirect (this .OssDomain + "/" + object , 302 )
6883 return
6984 }
7085
71- // query := this.Ctx.Request.URL.Query()
7286 // 签名验证
7387 sign := this .GetString ("sign" )
74- if ! this .isValidSign (sign ) {
88+ if ! this .isValidSign (sign , object ) {
7589 // 签名验证不通过,需要再次验证书籍是否是用户的(针对编辑状态)
7690 if ! this .isBookOwner () {
7791 this .Abort ("404" )
7892 return
7993 }
8094 }
8195
82- var expireInSec int64 = 2
96+ if utils .IsSignUsed (sign ) {
97+ this .Abort ("404" )
98+ }
99+
83100 if bucket , err := store .ModelStoreOss .GetBucket (); err == nil {
84- object , _ = bucket .SignURL (object , http .MethodGet , expireInSec )
101+ object , _ = bucket .SignURL (object , http .MethodGet , utils . MediaDuration )
85102 if slice := strings .Split (object , "/" ); len (slice ) > 2 {
86103 object = strings .Join (slice [3 :], "/" )
87104 }
@@ -152,6 +169,10 @@ func (this *StaticController) isBookOwner() (yes bool) {
152169}
153170
154171// 是否是合法的签名(针对音频和视频,签名不可用的时候再验证用户有没有登录,用户登录了再验证用户是不是书籍所有人)
155- func (this * StaticController ) isValidSign (sign string ) bool {
156- return false
172+ func (this * StaticController ) isValidSign (sign , path string ) bool {
173+ signPath , err := utils .ParseSign (sign )
174+ if err != nil {
175+ return false
176+ }
177+ return signPath == path
157178}
0 commit comments