File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change 1
1
package assets
2
2
3
3
import (
4
+ "errors"
4
5
"fmt"
5
6
"log/slog"
6
7
"path"
7
8
"time"
8
9
9
10
"github.com/simulot/immich-go/internal/fshelper"
10
11
"github.com/simulot/immich-go/internal/fshelper/cachereader"
12
+ "github.com/simulot/immich-go/internal/fshelper/hash"
11
13
)
12
14
13
15
/*
@@ -161,3 +163,28 @@ func (a *Asset) MergeTags(t2 []Tag) {
161
163
}
162
164
}
163
165
}
166
+
167
+ // GetChecksum returns the checksum of the asset.
168
+ // If the checksum is already set, it returns it. Otherwise, it computes it.
169
+ // Use this method to get the checksum of an asset.
170
+ func (a * Asset ) GetChecksum () (string , error ) {
171
+ if a .Checksum != "" {
172
+ return a .Checksum , nil
173
+ }
174
+ if a .File .FS () == nil {
175
+ return "" , errors .New ("no file to compute checksum" )
176
+ }
177
+
178
+ f , err := a .File .Open ()
179
+ if err != nil {
180
+ return "" , err
181
+ }
182
+ defer f .Close ()
183
+
184
+ sha1Hash , err := hash .Base64Encode (hash .GetSHA1Hash (f ))
185
+ if err != nil {
186
+ return "" , err
187
+ }
188
+ a .Checksum = sha1Hash
189
+ return a .Checksum , nil
190
+ }
Original file line number Diff line number Diff line change @@ -22,11 +22,12 @@ func (a *Asset) OpenFile() (osfs.OSFS, error) {
22
22
}
23
23
debugfiles .TrackOpenFile (f , a .File .FullName ())
24
24
// Create a cache reader from the FS.File
25
- cr , err := cachereader .NewCacheReader (a .File .FullName (), f )
25
+ cr , sha1 , err := cachereader .NewCacheReader (a .File .FullName (), f )
26
26
if err != nil {
27
27
return nil , err
28
28
}
29
29
a .cacheReader = cr
30
+ a .Checksum = sha1
30
31
}
31
32
return a .cacheReader .OpenFile ()
32
33
}
You can’t perform that action at this time.
0 commit comments