Compare commits
4 commits
3f892331f6
...
cf8959b1d7
| Author | SHA1 | Date | |
|---|---|---|---|
| cf8959b1d7 | |||
| 58b6b1618d | |||
| 06f296639e | |||
| f02730dc6b |
2 changed files with 15 additions and 7 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
@ -102,12 +103,14 @@ 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
|
||||||
}
|
}
|
||||||
util.DoChunks(f, func (buf []byte) { w.Write(buf) })
|
defer f.Close()
|
||||||
|
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) {
|
||||||
|
|
@ -163,15 +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.Write(contents)
|
w.Header().Set("Content-Length",
|
||||||
|
fmt.Sprintf("%d", album.TarballSize))
|
||||||
|
util.DoChunks(f, 4 * 1024, func (buf []byte) { w.Write(buf) })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@ 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 {
|
||||||
|
|
@ -39,11 +40,11 @@ func Dirents(dir string) (entries []string, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func DoChunks(f io.Reader, fun func (buf []byte)) (err error) {
|
func DoChunks(f io.Reader, kib uint, 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, 4*1024)
|
buf := make([]byte, 0, kib * 1024)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
var n int
|
var n int
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue