Skip to content

Commit 5829e6c

Browse files
committed
refactor(serverHandler/archive): extract archive func
1 parent ed19071 commit 5829e6c

File tree

3 files changed

+55
-51
lines changed

3 files changed

+55
-51
lines changed

src/serverHandler/archive.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import (
99
"strings"
1010
)
1111

12+
type archiveCallback func(f *os.File, fInfo os.FileInfo, relPath string) error
13+
1214
func (h *handler) visitFs(
1315
initFsPath, rawRequestPath, relPath string,
14-
callback func(*os.File, os.FileInfo, string) error,
16+
callback archiveCallback,
1517
) {
1618
aliasedFsPath, hasAlias := h.aliases[rawRequestPath]
1719

@@ -95,6 +97,34 @@ func (h *handler) visitFs(
9597
}
9698
}
9799

100+
func (h *handler) archive(
101+
w http.ResponseWriter,
102+
r *http.Request,
103+
pageData *responseData,
104+
fileSuffix string,
105+
contentType string,
106+
cbWriteFile archiveCallback,
107+
) {
108+
targetFilename := pageData.ItemName + fileSuffix
109+
writeArchiveHeader(w, contentType, targetFilename)
110+
111+
if !needResponseBody(r.Method) {
112+
return
113+
}
114+
115+
h.visitFs(
116+
path.Clean(h.root+pageData.handlerReqPath),
117+
pageData.rawReqPath,
118+
"",
119+
func(f *os.File, fInfo os.FileInfo, relPath string) error {
120+
go h.logArchive(targetFilename, relPath, r)
121+
err := cbWriteFile(f, fInfo, relPath)
122+
h.errHandler.LogError(err)
123+
return err
124+
},
125+
)
126+
}
127+
98128
func writeArchiveHeader(w http.ResponseWriter, contentType, filename string) {
99129
filename = url.PathEscape(filename)
100130

src/serverHandler/archiveTar.go

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io"
77
"net/http"
88
"os"
9-
"path"
109
)
1110

1211
func writeTar(tw *tar.Writer, f *os.File, fInfo os.FileInfo, archivePath string) error {
@@ -55,22 +54,14 @@ func (h *handler) tar(w http.ResponseWriter, r *http.Request, pageData *response
5554
h.errHandler.LogError(err)
5655
}()
5756

58-
filename := pageData.ItemName + ".tar"
59-
writeArchiveHeader(w, "application/octet-stream", filename)
60-
61-
if !needResponseBody(r.Method) {
62-
return
63-
}
64-
65-
h.visitFs(
66-
path.Clean(h.root+pageData.handlerReqPath),
67-
pageData.rawReqPath,
68-
"",
69-
func(f *os.File, fInfo os.FileInfo, relPath string) (err error) {
70-
go h.logArchive(filename, relPath, r)
71-
err = writeTar(tw, f, fInfo, relPath)
72-
h.errHandler.LogError(err)
73-
return
57+
h.archive(
58+
w,
59+
r,
60+
pageData,
61+
".tar",
62+
"application/octet-stream",
63+
func(f *os.File, fInfo os.FileInfo, relPath string) error {
64+
return writeTar(tw, f, fInfo, relPath)
7465
},
7566
)
7667
}
@@ -91,22 +82,14 @@ func (h *handler) tgz(w http.ResponseWriter, r *http.Request, pageData *response
9182
h.errHandler.LogError(err)
9283
}()
9384

94-
filename := pageData.ItemName + ".tar.gz"
95-
writeArchiveHeader(w, "application/octet-stream", filename)
96-
97-
if !needResponseBody(r.Method) {
98-
return
99-
}
100-
101-
h.visitFs(
102-
path.Clean(h.root+pageData.handlerReqPath),
103-
pageData.rawReqPath,
104-
"",
105-
func(f *os.File, fInfo os.FileInfo, relPath string) (err error) {
106-
go h.logArchive(filename, relPath, r)
107-
err = writeTar(tw, f, fInfo, relPath)
108-
h.errHandler.LogError(err)
109-
return
85+
h.archive(
86+
w,
87+
r,
88+
pageData,
89+
".tar.gz",
90+
"application/octet-stream",
91+
func(f *os.File, fInfo os.FileInfo, relPath string) error {
92+
return writeTar(tw, f, fInfo, relPath)
11093
},
11194
)
11295
}

src/serverHandler/archiveZip.go

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

1110
func writeZip(zw *zip.Writer, f *os.File, fInfo os.FileInfo, archivePath string) error {
@@ -45,22 +44,14 @@ func (h *handler) zip(w http.ResponseWriter, r *http.Request, pageData *response
4544
h.errHandler.LogError(err)
4645
}()
4746

48-
filename := pageData.ItemName + ".zip"
49-
writeArchiveHeader(w, "application/zip", filename)
50-
51-
if !needResponseBody(r.Method) {
52-
return
53-
}
54-
55-
h.visitFs(
56-
path.Clean(h.root+pageData.handlerReqPath),
57-
pageData.rawReqPath,
58-
"",
59-
func(f *os.File, fInfo os.FileInfo, relPath string) (err error) {
60-
go h.logArchive(filename, relPath, r)
61-
err = writeZip(zipWriter, f, fInfo, relPath)
62-
h.errHandler.LogError(err)
63-
return
47+
h.archive(
48+
w,
49+
r,
50+
pageData,
51+
".zip",
52+
"application/zip",
53+
func(f *os.File, fInfo os.FileInfo, relPath string) error {
54+
return writeZip(zipWriter, f, fInfo, relPath)
6455
},
6556
)
6657
}

0 commit comments

Comments
 (0)