Skip to content
This repository was archived by the owner on Jun 7, 2024. It is now read-only.

Commit 1437e06

Browse files
committed
[get] support single file download
1 parent fa37e2d commit 1437e06

File tree

1 file changed

+42
-17
lines changed

1 file changed

+42
-17
lines changed

fgit.go

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func get(url, fpath string) {
219219

220220
func downloadFile(url, fpath string) {
221221
urlSplit := strings.Split(url, "/")
222-
filename := urlSplit[len(urlSplit) - 1]
222+
filename := urlSplit[len(urlSplit)-1]
223223
if fpath == "" {
224224
downloadFile(url, filename)
225225
}
@@ -247,23 +247,48 @@ func downloadFile(url, fpath string) {
247247

248248
}
249249
}
250-
startDown:
251-
newUrl := strings.Replace(url, "https://github.com", "https://download.fastgit.org", -1)
252-
if newUrl != url {
253-
fmt.Println("Redirect ->", newUrl)
250+
251+
if strings.HasPrefix(url, "https://github.com") {
252+
query := strings.Replace(url, "https://github.com", "", -1)
253+
querySplit := strings.Split(query, "/")
254+
if len(querySplit) <= 3 {
255+
goto startDown
254256
}
255-
fmt.Println("Downloading...")
256-
resp, err := http.Get(newUrl)
257-
checkErr(err, "Http.Get create failed", 1)
258-
defer resp.Body.Close()
257+
// Source -> fastgitorg/fgit-go/blob/master/fgit.go
258+
// Target -> fastgitorg/fgit-go/master/fgit.go
259+
if querySplit[2] == "blob" {
260+
url = "https://raw.fastgit.org/"
261+
for _i, _s := range querySplit {
262+
if _i != 2 {
263+
// not /blob/
264+
if _i == len(querySplit)-1 {
265+
url += _s
266+
} else {
267+
url += _s + "/"
268+
}
269+
}
270+
}
271+
fmt.Println("Redirect ->", url)
272+
}
273+
}
259274

260-
out, err := os.Create(fpath)
261-
checkErr(err, "File create failed", 1)
262-
defer out.Close()
263-
_, err = io.Copy(out, resp.Body)
264-
checkErr(err, "io.Copy failed!", 1)
265-
fmt.Println("Finished.")
266-
os.Exit(0)
275+
startDown:
276+
newUrl := strings.Replace(url, "https://github.com", "https://download.fastgit.org", -1)
277+
if newUrl != url {
278+
fmt.Println("Redirect ->", newUrl)
279+
}
280+
fmt.Println("Downloading...")
281+
resp, err := http.Get(newUrl)
282+
checkErr(err, "Http.Get create failed", 1)
283+
defer resp.Body.Close()
284+
285+
out, err := os.Create(fpath)
286+
checkErr(err, "File create failed", 1)
287+
defer out.Close()
288+
_, err = io.Copy(out, resp.Body)
289+
checkErr(err, "io.Copy failed!", 1)
290+
fmt.Println("Finished.")
291+
os.Exit(0)
267292
}
268293

269294
func isDir(path string) bool {
@@ -275,7 +300,7 @@ func isDir(path string) bool {
275300
}
276301

277302
func isExists(path string) bool {
278-
_, err := os.Stat(path) //os.Stat获取文件信息
303+
_, err := os.Stat(path)
279304
if err != nil {
280305
if os.IsExist(err) {
281306
return true

0 commit comments

Comments
 (0)