server: write tarball in 4 MiB chunks

This commit is contained in:
Jeremy Baxter 2026-01-05 01:32:55 +13:00
parent 58b6b1618d
commit cf8959b1d7

View file

@ -166,17 +166,19 @@ func handleArtistAlbumPage(w http.ResponseWriter, req *http.Request) {
if musicindex.ArtistExists(artist) { if musicindex.ArtistExists(artist) {
album, ok := musicindex.FindArtist(artist).Albums[albumName] album, ok := musicindex.FindArtist(artist).Albums[albumName]
if ok { if ok {
contents, err := os.ReadFile(album.Tarball) f, err := os.Open(album.Tarball)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusNotFound) http.Error(w, "internal server error: " + err.Error(),
http.StatusInternalServerError)
return return
} }
defer f.Close()
w.Header().Set("Content-Disposition", `attachment; filename="` + w.Header().Set("Content-Disposition", `attachment; filename="` +
strings.ReplaceAll(album.Name, `"`, `'`) + `.tar.gz"`) strings.ReplaceAll(album.Name, `"`, `'`) + `.tar.gz"`)
w.Header().Set("Content-Length", w.Header().Set("Content-Length",
fmt.Sprintf("%d", album.TarballSize)) fmt.Sprintf("%d", album.TarballSize))
w.Write(contents) util.DoChunks(f, 4 * 1024, func (buf []byte) { w.Write(buf) })
return return
} }
} }