6
6
"path/filepath"
7
7
8
8
"github.com/simulot/immich-go/internal/fshelper/debugfiles"
9
+ "github.com/simulot/immich-go/internal/fshelper/hash"
9
10
"github.com/simulot/immich-go/internal/fshelper/osfs"
10
11
"github.com/simulot/immich-go/internal/loghelper"
11
12
)
@@ -20,8 +21,12 @@ type CacheReader struct {
20
21
// NewCacheReader creates a new CacheReader from an io.ReadCloser
21
22
// When the reader is an os.File, it will be used directly
22
23
// 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 ) {
24
27
var err error
28
+ var sha1Hash string
29
+
25
30
c := & CacheReader {}
26
31
if f , ok := rc .(osfs.OSFS ); ok {
27
32
c .name = f .Name ()
@@ -42,22 +47,24 @@ func NewCacheReader(name string, rc io.ReadCloser) (*CacheReader, error) {
42
47
}
43
48
c .tmpFile , err = os .CreateTemp (d , "immich-go_*" )
44
49
if err != nil {
45
- return nil , err
50
+ return nil , "" , err
46
51
}
47
52
debugfiles .TrackOpenFile (c .tmpFile , c .tmpFile .Name ())
48
53
c .name = c .tmpFile .Name ()
54
+
49
55
// 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 )))
51
58
if err != nil {
52
59
c .tmpFile .Close ()
53
60
_ = os .Remove (c .name )
54
- return nil , err
61
+ return nil , "" , err
55
62
}
56
63
rc .Close ()
57
64
debugfiles .TrackCloseFile (rc )
58
65
c .shouldRemove = true
59
66
}
60
- return c , err
67
+ return c , sha1Hash , err
61
68
}
62
69
63
70
// OpenFile creates a new file handler based on the temporary file
0 commit comments