From db0126968a1b426f0d2bc90906e225def67619c3 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sat, 20 Jun 2020 11:48:45 +0200 Subject: [PATCH] Improve memory usage of dump by not loading all files in memory prior to adding them to the zip --- pkg/files/dump.go | 14 ++++++-------- pkg/modules/dump/dump.go | 13 +++++++++++-- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/pkg/files/dump.go b/pkg/files/dump.go index 154f9f66..379a4f7d 100644 --- a/pkg/files/dump.go +++ b/pkg/files/dump.go @@ -16,27 +16,25 @@ package files -import "bytes" +import ( + "io" +) // Dump dumps all saved files // This only includes the raw files, no db entries. -func Dump() (allFiles map[int64][]byte, err error) { +func Dump() (allFiles map[int64]io.ReadCloser, err error) { files := []*File{} err = x.Find(&files) if err != nil { return } - allFiles = make(map[int64][]byte, len(files)) + allFiles = make(map[int64]io.ReadCloser, len(files)) for _, file := range files { if err := file.LoadFileByID(); err != nil { return nil, err } - var buf bytes.Buffer - if _, err := buf.ReadFrom(file.File); err != nil { - return nil, err - } - allFiles[file.ID] = buf.Bytes() + allFiles[file.ID] = file.File } return diff --git a/pkg/modules/dump/dump.go b/pkg/modules/dump/dump.go index 648e9ec7..e0647dc6 100644 --- a/pkg/modules/dump/dump.go +++ b/pkg/modules/dump/dump.go @@ -80,11 +80,20 @@ func Dump(filename string) error { if err != nil { return fmt.Errorf("error saving file: %s", err) } - for fid, fcontent := range allFiles { - err = writeBytesToZip("files/"+strconv.FormatInt(fid, 10), fcontent, dumpWriter) + for fid, file := range allFiles { + header := &zip.FileHeader{ + Name: "files/" + strconv.FormatInt(fid, 10), + Method: compressionUsed, + } + w, err := dumpWriter.CreateHeader(header) + if err != nil { + return err + } + _, err = io.Copy(w, file) if err != nil { return fmt.Errorf("error writing file %d: %s", fid, err) } + _ = file.Close() } log.Infof("Dumped files")