WebUI: add a JSON helper function

Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
Nicola Murino
2024-02-04 18:16:10 +01:00
parent c23d779280
commit e5836c8118
7 changed files with 239 additions and 120 deletions

View File

@@ -299,6 +299,41 @@ func renderAPIDirContents(w http.ResponseWriter, r *http.Request, contents []os.
render.JSON(w, r, results)
}
func streamData(w io.Writer, data []byte) {
b := bytes.NewBuffer(data)
_, err := io.CopyN(w, b, int64(len(data)))
if err != nil {
panic(http.ErrAbortHandler)
}
}
func streamJSONArray(w http.ResponseWriter, chunkSize int, dataGetter func(limit, offset int) ([]byte, int, error)) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Accept-Ranges", "none")
w.WriteHeader(http.StatusOK)
streamData(w, []byte("["))
offset := 0
for {
data, count, err := dataGetter(chunkSize, offset)
if err != nil {
panic(http.ErrAbortHandler)
}
if count == 0 {
break
}
if offset > 0 {
streamData(w, []byte(","))
}
streamData(w, data[1:len(data)-1])
if count < chunkSize {
break
}
offset += count
}
streamData(w, []byte("]"))
}
func getCompressedFileName(username string, files []string) string {
if len(files) == 1 {
name := path.Base(files[0])