mirror of
https://git.sr.ht/~sircmpwn/tokidoki
synced 2025-12-12 06:07:22 +01:00
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:
parent
fe0a0d0d00
commit
665b206709
4 changed files with 89 additions and 54 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue