From 859b34617eb83ff65f4466d41c6f700368cd65db Mon Sep 17 00:00:00 2001 From: Geri Date: Wed, 3 Apr 2024 15:23:01 +0200 Subject: [PATCH] fix: check quota in chunks --- server/filesystem/compress.go | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/server/filesystem/compress.go b/server/filesystem/compress.go index 82a6af2..c43f0de 100644 --- a/server/filesystem/compress.go +++ b/server/filesystem/compress.go @@ -224,9 +224,35 @@ func (fs *Filesystem) extractStream(ctx context.Context, opts extractStreamOptio } defer f.Close() - // Write our file - if _, err := io.Copy(f, reader); err != nil { - return err + // Read in 4 KB chunks + buf := make([]byte, 4096) + for { + n, err := reader.Read(buf) + if n > 0 { + + // Check quota before writing the chunk + if quotaErr := fs.HasSpaceFor(int64(n)); quotaErr != nil { + return quotaErr + } + + // Write the chunk + if _, writeErr := f.Write(buf[:n]); writeErr != nil { + return writeErr + } + + // Add to quota + fs.addDisk(int64(n)) + } + + if err != nil { + // EOF are expected + if err == io.EOF { + break + } + + // Return any other + return err + } } return nil