From f4d6d5da8016a76372ad46807926bca13e06dcd4 Mon Sep 17 00:00:00 2001 From: cuteLittleDevil <792192820@qq.com> Date: Mon, 13 Nov 2023 08:29:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dhls=E6=8B=89=E6=B5=81?= =?UTF-8?q?=E6=97=B6=20=E5=9B=A0=E4=B8=BA=E6=B5=81=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E5=B8=A6.=E7=AC=A6=E5=8F=B7=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/base/url.go | 7 +++---- pkg/base/url_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/pkg/base/url.go b/pkg/base/url.go index 0b189c6..162e647 100644 --- a/pkg/base/url.go +++ b/pkg/base/url.go @@ -74,10 +74,9 @@ func (u *UrlContext) GetFileType() string { func (u *UrlContext) calcFilenameAndTypeIfNeeded() { if len(u.filenameWithoutType) == 0 || len(u.fileType) == 0 { - ss := strings.Split(u.LastItemOfPath, ".") - u.filenameWithoutType = ss[0] - if len(ss) > 1 { - u.fileType = ss[1] + if index := strings.LastIndex(u.LastItemOfPath, "."); index != -1 { + u.filenameWithoutType = u.LastItemOfPath[:index] + u.fileType = u.LastItemOfPath[index+1:] } } } diff --git a/pkg/base/url_test.go b/pkg/base/url_test.go index 4175b57..5367fb6 100644 --- a/pkg/base/url_test.go +++ b/pkg/base/url_test.go @@ -10,6 +10,7 @@ package base_test import ( "fmt" + "regexp" "testing" "github.com/q191201771/lal/pkg/base" @@ -295,3 +296,27 @@ func testParseRtmpUrlCase2(t *testing.T) { assert.Equal(t, "rtmp://xxx.net/vhall?vhost=thirdVhost?token=2A317D14t25690", tcUrlFn(ctx)) assert.Equal(t, "138521921", streamNameWithRawQueryFn(ctx)) } + +func FuzzGetFilenameWithoutTypeAndGetFileType(f *testing.F) { + testcases := []string{ + "playlist.m3u8", + "abcABC123.ts", + "192.168.1.68-1699692546341-2.ts", + } + for _, tc := range testcases { + f.Add(tc) + } + re := regexp.MustCompile(`.+\.[^.]+`) + f.Fuzz(func(t *testing.T, orig string) { + if !re.MatchString(orig) { + return + } + tmp := base.UrlContext{LastItemOfPath: orig} + filenameWithoutType := tmp.GetFilenameWithoutType() + fileType := tmp.GetFileType() + if orig != filenameWithoutType+"."+fileType { + t.Errorf("expected: [%s], actual: [%s.%s]", + orig, filenameWithoutType, fileType) + } + }) +}