Skip to content

Commit 103794d

Browse files
committed
feat: dry
Signed-off-by: abzcoding <[email protected]>
1 parent a40d109 commit 103794d

File tree

3 files changed

+5
-27
lines changed

3 files changed

+5
-27
lines changed

http.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ func NewHTTPDownloader(url string, par int, skipTLS bool, proxyServer string, bw
9595
Printf("Download target size: %.1f GB\n", sizeInMb/1024)
9696
}
9797

98-
parsed, err = stdurl.Parse(url)
99-
FatalCheck(err)
100-
file := filepath.Base(strings.TrimRight(parsed.Path, "/\\"))
98+
file := TaskFromURL(url)
10199

102100
ret := new(HTTPDownloader)
103101
ret.rate = 0
@@ -131,9 +129,7 @@ func partCalculate(par int64, len int64, url string) []Part {
131129
to = len
132130
}
133131

134-
parsed, err := stdurl.Parse(url)
135-
FatalCheck(err)
136-
file := filepath.Base(strings.TrimRight(parsed.Path, "/\\"))
132+
file := TaskFromURL(url)
137133

138134
folder := FolderOf(url)
139135
if err := MkdirIfNotExist(folder); err != nil {

main.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@ import (
66
"io"
77
"os"
88
"os/signal"
9-
"path/filepath"
109
"runtime"
1110
"syscall"
1211

13-
stdurl "net/url"
14-
"strings"
15-
1612
"github.com/imkira/go-task"
1713
)
1814

@@ -111,15 +107,9 @@ func Execute(url string, state *State, conn int, skiptls bool, proxy string, bwL
111107
if state == nil {
112108
downloader = NewHTTPDownloader(url, conn, skiptls, proxy, bwLimit)
113109
} else {
114-
// Extract the last path from the URL, excluding parameters.
115-
// eg: URL_ADDRESS.com/path/to/file?param=value -> file
116-
parsed, err := stdurl.Parse(state.URL)
117-
FatalCheck(err)
118-
cleanFile := filepath.Base(strings.TrimRight(parsed.Path, "/\\"))
119-
120110
downloader = &HTTPDownloader{
121111
url: state.URL,
122-
file: cleanFile,
112+
file: TaskFromURL(state.URL),
123113
par: int64(len(state.Parts)),
124114
parts: state.Parts,
125115
resumable: true,
@@ -154,13 +144,7 @@ func Execute(url string, state *State, conn int, skiptls bool, proxy string, bwL
154144
Warnf("Interrupted, but the download is not resumable. Exiting silently.\n")
155145
}
156146
} else {
157-
// Extract the last path from the URL, excluding parameters.
158-
// eg: URL_ADDRESS.com/path/to/file?param=value -> file
159-
parsed, err := stdurl.Parse(url)
160-
FatalCheck(err)
161-
filename := filepath.Base(strings.TrimRight(parsed.Path, "/\\"))
162-
163-
err = JoinFile(files, filename)
147+
err := JoinFile(files, TaskFromURL(url))
164148
FatalCheck(err)
165149
err = os.RemoveAll(FolderOf(url))
166150
FatalCheck(err)

util.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,9 @@ func DisplayProgressBar() bool {
5555
func FolderOf(urlStr string) string {
5656
safePath := filepath.Join(os.Getenv("HOME"), dataFolder)
5757

58-
parsedURL, err := url.Parse(urlStr)
59-
FatalCheck(err)
6058
// Extract the last path from the URL, excluding parameters.
6159
// eg: URL_ADDRESS.com/path/to/file?param=value -> file
62-
cleanPath := filepath.Base(strings.TrimRight(parsedURL.Path, "/\\"))
60+
cleanPath := TaskFromURL(urlStr)
6361

6462
fullQualifyPath, err := filepath.Abs(filepath.Join(os.Getenv("HOME"), dataFolder, cleanPath))
6563
FatalCheck(err)

0 commit comments

Comments
 (0)