Skip to content

Commit 72a0c60

Browse files
committed
feat: update NewCacheReader to return SHA1 checksum along with CacheReader
1 parent 184c9ea commit 72a0c60

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

internal/fshelper/cachereader/cachereader.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"path/filepath"
77

88
"github.com/simulot/immich-go/internal/fshelper/debugfiles"
9+
"github.com/simulot/immich-go/internal/fshelper/hash"
910
"github.com/simulot/immich-go/internal/fshelper/osfs"
1011
"github.com/simulot/immich-go/internal/loghelper"
1112
)
@@ -20,8 +21,12 @@ type CacheReader struct {
2021
// NewCacheReader creates a new CacheReader from an io.ReadCloser
2122
// When the reader is an os.File, it will be used directly
2223
// Otherwise, the content will be copied into a temporary file, and the original reader will be closed
23-
func NewCacheReader(name string, rc io.ReadCloser) (*CacheReader, error) {
24+
//
25+
// The Checksum is computed on the fly
26+
func NewCacheReader(name string, rc io.ReadCloser) (*CacheReader, string, error) {
2427
var err error
28+
var sha1Hash string
29+
2530
c := &CacheReader{}
2631
if f, ok := rc.(osfs.OSFS); ok {
2732
c.name = f.Name()
@@ -42,22 +47,24 @@ func NewCacheReader(name string, rc io.ReadCloser) (*CacheReader, error) {
4247
}
4348
c.tmpFile, err = os.CreateTemp(d, "immich-go_*")
4449
if err != nil {
45-
return nil, err
50+
return nil, "", err
4651
}
4752
debugfiles.TrackOpenFile(c.tmpFile, c.tmpFile.Name())
4853
c.name = c.tmpFile.Name()
54+
4955
// be sure to copy the reader content into the temporary file
50-
_, err = io.Copy(c.tmpFile, rc)
56+
// and compute the SHA1 checksum on the fly
57+
sha1Hash, err = hash.Base64Encode(hash.GetSHA1Hash(io.TeeReader(rc, c.tmpFile)))
5158
if err != nil {
5259
c.tmpFile.Close()
5360
_ = os.Remove(c.name)
54-
return nil, err
61+
return nil, "", err
5562
}
5663
rc.Close()
5764
debugfiles.TrackCloseFile(rc)
5865
c.shouldRemove = true
5966
}
60-
return c, err
67+
return c, sha1Hash, err
6168
}
6269

6370
// OpenFile creates a new file handler based on the temporary file

internal/fshelper/cachereader/cacheredear_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func Test_NewReaderAtOnBuffer(t *testing.T) {
2525
}
2626

2727
b := makeBuffer(0, 4096)
28-
cr, err := NewCacheReader("test", io.NopCloser(bytes.NewReader(b)))
28+
cr, _, err := NewCacheReader("test", io.NopCloser(bytes.NewReader(b)))
2929
if err != nil {
3030
t.Fatalf("NewCacheReader() error = %v", err)
3131
}
@@ -92,7 +92,7 @@ func Test_NewReaderAtOnFile(t *testing.T) {
9292
return
9393
}
9494

95-
cr, err := NewCacheReader("test", f)
95+
cr, _, err := NewCacheReader("test", f)
9696
if err != nil {
9797
t.Fatalf("NewCacheReader() error = %v", err)
9898
}

0 commit comments

Comments
 (0)