storage: streamline ETag calculation

This commit introduces some helpers so that ETags can be calculated at
the same time that files get read or written. Besides looking nicer, it
should also help reduce lock contention around file access, as files do
not need to be opened twice anymore.
This commit is contained in:
Conrad Hoffmann 2024-11-07 17:50:09 +01:00
parent fe0a0d0d00
commit 665b206709
4 changed files with 89 additions and 54 deletions

View file

@ -1,8 +1,6 @@
package storage
import (
"crypto/sha1"
"encoding/base64"
"fmt"
"io"
"os"
@ -144,11 +142,10 @@ func etagForFile(path string) (string, error) {
}
defer f.Close()
h := sha1.New()
if _, err := io.Copy(h, f); err != nil {
src := NewETagReader(f)
if _, err := io.ReadAll(src); err != nil {
return "", err
}
csum := h.Sum(nil)
return base64.StdEncoding.EncodeToString(csum[:]), nil
return src.ETag(), nil
}