Skip to content

Commit ed19071

Browse files
committed
refactor(serverHandler/archive): refine codes
1 parent 320f25f commit ed19071

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

src/serverHandler/archive.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func (h *handler) visitFs(
1313
initFsPath, rawRequestPath, relPath string,
14-
callback func(*os.File, os.FileInfo, string),
14+
callback func(*os.File, os.FileInfo, string) error,
1515
) {
1616
aliasedFsPath, hasAlias := h.aliases[rawRequestPath]
1717

@@ -39,7 +39,9 @@ func (h *handler) visitFs(
3939
}
4040

4141
if len(relPath) > 0 {
42-
callback(f, fInfo, relPath)
42+
if callback(f, fInfo, relPath) != nil {
43+
return
44+
}
4345
}
4446

4547
if fInfo.IsDir() {

src/serverHandler/archiveTar.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"io"
77
"net/http"
88
"os"
9-
"runtime"
9+
"path"
1010
)
1111

1212
func writeTar(tw *tar.Writer, f *os.File, fInfo os.FileInfo, archivePath string) error {
@@ -36,7 +36,7 @@ func writeTar(tw *tar.Writer, f *os.File, fInfo os.FileInfo, archivePath string)
3636
return err
3737
}
3838

39-
if size == 0 || f == nil {
39+
if size == 0 || f == nil || fInfo.IsDir() {
4040
return nil
4141
}
4242

@@ -63,15 +63,14 @@ func (h *handler) tar(w http.ResponseWriter, r *http.Request, pageData *response
6363
}
6464

6565
h.visitFs(
66-
h.root+pageData.handlerReqPath,
66+
path.Clean(h.root+pageData.handlerReqPath),
6767
pageData.rawReqPath,
6868
"",
69-
func(f *os.File, fInfo os.FileInfo, relPath string) {
69+
func(f *os.File, fInfo os.FileInfo, relPath string) (err error) {
7070
go h.logArchive(filename, relPath, r)
71-
err := writeTar(tw, f, fInfo, relPath)
72-
if h.errHandler.LogError(err) {
73-
runtime.Goexit()
74-
}
71+
err = writeTar(tw, f, fInfo, relPath)
72+
h.errHandler.LogError(err)
73+
return
7574
},
7675
)
7776
}
@@ -100,15 +99,14 @@ func (h *handler) tgz(w http.ResponseWriter, r *http.Request, pageData *response
10099
}
101100

102101
h.visitFs(
103-
h.root+pageData.handlerReqPath,
102+
path.Clean(h.root+pageData.handlerReqPath),
104103
pageData.rawReqPath,
105104
"",
106-
func(f *os.File, fInfo os.FileInfo, relPath string) {
105+
func(f *os.File, fInfo os.FileInfo, relPath string) (err error) {
107106
go h.logArchive(filename, relPath, r)
108-
err := writeTar(tw, f, fInfo, relPath)
109-
if h.errHandler.LogError(err) {
110-
runtime.Goexit()
111-
}
107+
err = writeTar(tw, f, fInfo, relPath)
108+
h.errHandler.LogError(err)
109+
return
112110
},
113111
)
114112
}

src/serverHandler/archiveZip.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"io"
66
"net/http"
77
"os"
8-
"runtime"
8+
"path"
99
)
1010

1111
func writeZip(zw *zip.Writer, f *os.File, fInfo os.FileInfo, archivePath string) error {
@@ -26,7 +26,7 @@ func writeZip(zw *zip.Writer, f *os.File, fInfo os.FileInfo, archivePath string)
2626
return err
2727
}
2828

29-
if size == 0 || f == nil {
29+
if size == 0 || f == nil || fInfo.IsDir() {
3030
return nil
3131
}
3232

@@ -53,15 +53,14 @@ func (h *handler) zip(w http.ResponseWriter, r *http.Request, pageData *response
5353
}
5454

5555
h.visitFs(
56-
h.root+pageData.handlerReqPath,
56+
path.Clean(h.root+pageData.handlerReqPath),
5757
pageData.rawReqPath,
5858
"",
59-
func(f *os.File, fInfo os.FileInfo, relPath string) {
59+
func(f *os.File, fInfo os.FileInfo, relPath string) (err error) {
6060
go h.logArchive(filename, relPath, r)
61-
err := writeZip(zipWriter, f, fInfo, relPath)
62-
if h.errHandler.LogError(err) {
63-
runtime.Goexit()
64-
}
61+
err = writeZip(zipWriter, f, fInfo, relPath)
62+
h.errHandler.LogError(err)
63+
return
6564
},
6665
)
6766
}

0 commit comments

Comments
 (0)