Compare commits
No commits in common. "cf8959b1d738ecf47b8cc86bbaef41ac5c358045" and "3f892331f699720c657007758dc8275516674fdc" have entirely different histories.
cf8959b1d7
...
3f892331f6
2 changed files with 7 additions and 15 deletions
|
|
@ -1,7 +1,6 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"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)
|
||||
return
|
||||
}
|
||||
|
||||
f, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
http.Error(w, "not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
util.DoChunks(f, 4, func (buf []byte) { w.Write(buf) })
|
||||
util.DoChunks(f, func (buf []byte) { w.Write(buf) })
|
||||
}
|
||||
|
||||
func handleArtistAlbumPage(w http.ResponseWriter, req *http.Request) {
|
||||
|
|
@ -166,19 +163,15 @@ func handleArtistAlbumPage(w http.ResponseWriter, req *http.Request) {
|
|||
if musicindex.ArtistExists(artist) {
|
||||
album, ok := musicindex.FindArtist(artist).Albums[albumName]
|
||||
if ok {
|
||||
f, err := os.Open(album.Tarball)
|
||||
contents, err := os.ReadFile(album.Tarball)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, "internal server error: " + err.Error(),
|
||||
http.StatusInternalServerError)
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
w.Header().Set("Content-Disposition", `attachment; filename="` +
|
||||
strings.ReplaceAll(album.Name, `"`, `'`) + `.tar.gz"`)
|
||||
w.Header().Set("Content-Length",
|
||||
fmt.Sprintf("%d", album.TarballSize))
|
||||
util.DoChunks(f, 4 * 1024, func (buf []byte) { w.Write(buf) })
|
||||
w.Write(contents)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,6 @@ func Dirents(dir string) (entries []string, err error) {
|
|||
if err != nil {
|
||||
return nil, errors.New(err.Error())
|
||||
}
|
||||
defer d.Close()
|
||||
|
||||
names, err := d.Readdirnames(0)
|
||||
if err != nil {
|
||||
|
|
@ -40,11 +39,11 @@ func Dirents(dir string) (entries []string, err error) {
|
|||
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
|
||||
bytes, chunks := int64(0), int64(0)
|
||||
r := bufio.NewReader(f)
|
||||
buf := make([]byte, 0, kib * 1024)
|
||||
buf := make([]byte, 0, 4*1024)
|
||||
|
||||
for {
|
||||
var n int
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue