Compare commits

..

No commits in common. "cf8959b1d738ecf47b8cc86bbaef41ac5c358045" and "3f892331f699720c657007758dc8275516674fdc" have entirely different histories.

2 changed files with 7 additions and 15 deletions

View file

@ -1,7 +1,6 @@
package server package server
import ( import (
"fmt"
"log" "log"
"os" "os"
"regexp" "regexp"
@ -103,14 +102,12 @@ func serveMediaDirectory(w http.ResponseWriter, req *http.Request) {
http.Error(w, "is a directory; maybe you are looking for " + path, http.StatusForbidden) http.Error(w, "is a directory; maybe you are looking for " + path, http.StatusForbidden)
return return
} }
f, err := os.Open(filePath) f, err := os.Open(filePath)
if err != nil { if err != nil {
http.Error(w, "not found", http.StatusNotFound) http.Error(w, "not found", http.StatusNotFound)
return return
} }
defer f.Close() util.DoChunks(f, func (buf []byte) { w.Write(buf) })
util.DoChunks(f, 4, func (buf []byte) { w.Write(buf) })
} }
func handleArtistAlbumPage(w http.ResponseWriter, req *http.Request) { func handleArtistAlbumPage(w http.ResponseWriter, req *http.Request) {
@ -166,19 +163,15 @@ 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 {
f, err := os.Open(album.Tarball) contents, err := os.ReadFile(album.Tarball)
if err != nil { if err != nil {
http.Error(w, "internal server error: " + err.Error(), http.Error(w, err.Error(), http.StatusNotFound)
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.Write(contents)
fmt.Sprintf("%d", album.TarballSize))
util.DoChunks(f, 4 * 1024, func (buf []byte) { w.Write(buf) })
return return
} }
} }

View file

@ -26,7 +26,6 @@ func Dirents(dir string) (entries []string, err error) {
if err != nil { if err != nil {
return nil, errors.New(err.Error()) return nil, errors.New(err.Error())
} }
defer d.Close()
names, err := d.Readdirnames(0) names, err := d.Readdirnames(0)
if err != nil { if err != nil {
@ -40,11 +39,11 @@ func Dirents(dir string) (entries []string, err error) {
return return
} }
func DoChunks(f io.Reader, kib uint, fun func (buf []byte)) (err error) { func DoChunks(f io.Reader, fun func (buf []byte)) (err error) {
err = nil err = nil
bytes, chunks := int64(0), int64(0) bytes, chunks := int64(0), int64(0)
r := bufio.NewReader(f) r := bufio.NewReader(f)
buf := make([]byte, 0, kib * 1024) buf := make([]byte, 0, 4*1024)
for { for {
var n int var n int