From d4cf884730b856998ad6f2c3e4c6a10a7ea845bb Mon Sep 17 00:00:00 2001 From: William Chong Date: Fri, 31 May 2024 02:32:19 +0800 Subject: [PATCH] [preprocessor/folder] optimize memory usage for hash calculation --- preprocessor/folder/file.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/preprocessor/folder/file.go b/preprocessor/folder/file.go index 0fa3e74..942fd44 100644 --- a/preprocessor/folder/file.go +++ b/preprocessor/folder/file.go @@ -30,19 +30,23 @@ func getFileMetadata(filePath string) (map[string]any, error) { return nil, err } + buffer := make([]byte, 512) + n, err := file.Read(buffer) + if err != nil { + return nil, err + } + mediaType := http.DetectContentType(buffer[:n]) + file.Seek(0, 0) + sha := sha256.New() md := md5.New() blake := blake3.New(32, nil) - tee := io.TeeReader(file, sha) - tee = io.TeeReader(tee, md) - tee = io.TeeReader(tee, blake) - - bytes, err := io.ReadAll(tee) + writers := io.MultiWriter(sha, md, blake) + _, err = io.Copy(writers, file) if err != nil { return nil, err } - mediaType := http.DetectContentType(bytes) return map[string]any{ "sha256": hex.EncodeToString(sha.Sum(nil)),