From 8f07f6f4d7e91a9e4b7164a39759907fac5fb8a1 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sat, 1 Jul 2023 22:06:01 +0800 Subject: Refactor: encapsulate custom url manipulation function into object method. --- common/myurl/urlparse.go | 27 +++++++++++++++++++++++++++ common/myurl/urlparse_test.go | 32 ++++++++++++++++++++++++++++++++ common/urlparse.go | 21 --------------------- common/urlparse_test.go | 32 -------------------------------- recording/runner.go | 3 ++- 5 files changed, 61 insertions(+), 54 deletions(-) create mode 100644 common/myurl/urlparse.go create mode 100644 common/myurl/urlparse_test.go delete mode 100644 common/urlparse.go delete mode 100644 common/urlparse_test.go diff --git a/common/myurl/urlparse.go b/common/myurl/urlparse.go new file mode 100644 index 0000000..69695d1 --- /dev/null +++ b/common/myurl/urlparse.go @@ -0,0 +1,27 @@ +package myurl + +import ( + "errors" + "net/url" + "strings" +) + +type Url string + +func (o Url) Url() string { + return string(o) +} + +// FileExtension returns file extension of file name from this url. +// copied from https://elisegev.medium.com/get-a-file-extension-from-a-url-in-golang-5061d4a298a +func (o Url) FileExtension() (string, error) { + u, err := url.Parse(o.Url()) + if err != nil { + return "", err + } + pos := strings.LastIndex(u.Path, ".") + if pos == -1 { + return "", errors.New("couldn't find a period to indicate a file extension") + } + return u.Path[pos+1 : len(u.Path)], nil +} diff --git a/common/myurl/urlparse_test.go b/common/myurl/urlparse_test.go new file mode 100644 index 0000000..b7df7bd --- /dev/null +++ b/common/myurl/urlparse_test.go @@ -0,0 +1,32 @@ +package myurl + +import "testing" + +func TestGetFileExtensionFromUrl(t *testing.T) { + tests := []struct { + Expected string + Actual string + }{ + {Expected: "html"}, + {Expected: "htm"}, + {Expected: "flv"}, + } + var err error + tests[0].Actual, err = Url("http://www.example.com/index.html").FileExtension() + if err != nil { + t.Fatalf("GetFileExtensionFromUrl: %v", err) + } + tests[1].Actual, err = Url("https://www.example.com/index.htm").FileExtension() + if err != nil { + t.Fatalf("GetFileExtensionFromUrl: %v", err) + } + tests[2].Actual, err = Url("https://www.example.com/video.flv?a=1&b=2flv").FileExtension() + if err != nil { + t.Fatalf("GetFileExtensionFromUrl: %v", err) + } + for i, tc := range tests { + if tc.Expected != tc.Actual { + t.Fatalf("Test %v failed: %v", i, tc) + } + } +} diff --git a/common/urlparse.go b/common/urlparse.go deleted file mode 100644 index dc72cee..0000000 --- a/common/urlparse.go +++ /dev/null @@ -1,21 +0,0 @@ -package common - -import ( - "errors" - "net/url" - "strings" -) - -// GetFileExtensionFromUrl -// copied from https://elisegev.medium.com/get-a-file-extension-from-a-url-in-golang-5061d4a298a -func GetFileExtensionFromUrl(rawUrl string) (string, error) { - u, err := url.Parse(rawUrl) - if err != nil { - return "", err - } - pos := strings.LastIndex(u.Path, ".") - if pos == -1 { - return "", errors.New("couldn't find a period to indicate a file extension") - } - return u.Path[pos+1 : len(u.Path)], nil -} diff --git a/common/urlparse_test.go b/common/urlparse_test.go deleted file mode 100644 index 9ed4449..0000000 --- a/common/urlparse_test.go +++ /dev/null @@ -1,32 +0,0 @@ -package common - -import "testing" - -func TestGetFileExtensionFromUrl(t *testing.T) { - tests := []struct { - Expected string - Actual string - }{ - {Expected: "html"}, - {Expected: "htm"}, - {Expected: "flv"}, - } - var err error - tests[0].Actual, err = GetFileExtensionFromUrl("http://www.example.com/index.html") - if err != nil { - t.Fatalf("GetFileExtensionFromUrl: %v", err) - } - tests[1].Actual, err = GetFileExtensionFromUrl("https://www.example.com/index.htm") - if err != nil { - t.Fatalf("GetFileExtensionFromUrl: %v", err) - } - tests[2].Actual, err = GetFileExtensionFromUrl("https://www.example.com/video.flv?a=1&b=2flv") - if err != nil { - t.Fatalf("GetFileExtensionFromUrl: %v", err) - } - for i, tc := range tests { - if tc.Expected != tc.Actual { - t.Fatalf("Test %v failed: %v", i, tc) - } - } -} diff --git a/recording/runner.go b/recording/runner.go index 8f892fa..fe7aa21 100644 --- a/recording/runner.go +++ b/recording/runner.go @@ -12,6 +12,7 @@ import ( "fmt" "github.com/keuin/slbr/bilibili" "github.com/keuin/slbr/common" + "github.com/keuin/slbr/common/myurl" "github.com/keuin/slbr/logging" "github.com/samber/mo" "io" @@ -277,7 +278,7 @@ func record( var extName string // the real extension name (without renaming) - originalExtName := mo.TupleToResult(common.GetFileExtensionFromUrl(streamSource.URL)).OrElse("flv") + originalExtName := mo.TupleToResult(myurl.Url(streamSource.URL).FileExtension()).OrElse("flv") if task.Download.UseSpecialExtNameBeforeFinishing { extName = kSpecialExtName -- cgit v1.2.3