Skip to content

Commit

Permalink
Update internal/hashio/files.go
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Plischke <[email protected]>
  • Loading branch information
tianfeng92 and alexplischke authored Nov 8, 2024
1 parent 80b32f7 commit 828e853
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions internal/hashio/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,19 @@ func SHA256(filename string) (string, error) {
// HashContent computes a SHA-256 hash of the file content combined with extra content,
// and returns the first 16 characters of the hex-encoded hash.
func HashContent(filename string, extra ...string) (string, error) {
h := sha256.New()

file, err := os.Open(filePath)

Check failure on line 30 in internal/hashio/files.go

View workflow job for this annotation

GitHub Actions / test

undefined: filePath

Check failure on line 30 in internal/hashio/files.go

View workflow job for this annotation

GitHub Actions / lint

undefined: filePath

Check failure on line 30 in internal/hashio/files.go

View workflow job for this annotation

GitHub Actions / lint

undefined: filePath

Check failure on line 30 in internal/hashio/files.go

View workflow job for this annotation

GitHub Actions / lint

undefined: filePath
if err != nil {
return "", fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()

fileInfo, err := file.Stat()
if err != nil {
return "", fmt.Errorf("failed to get file info: %w", err)
}

buffer := make([]byte, fileInfo.Size())
if _, err := file.Read(buffer); err != nil {
if _, err := io.Copy(h, file); err != nil {
return "", fmt.Errorf("failed to read file: %w", err)
}

combinedContent := string(buffer) + strings.Join(extraContent, "")
h.Write([]byte(strings.Join(extraContent, "")))

Check failure on line 40 in internal/hashio/files.go

View workflow job for this annotation

GitHub Actions / test

undefined: extraContent

Check failure on line 40 in internal/hashio/files.go

View workflow job for this annotation

GitHub Actions / lint

undefined: extraContent) (typecheck)

Check failure on line 40 in internal/hashio/files.go

View workflow job for this annotation

GitHub Actions / lint

undefined: extraContent (typecheck)

Check failure on line 40 in internal/hashio/files.go

View workflow job for this annotation

GitHub Actions / lint

undefined: extraContent) (typecheck)

hash := sha256.Sum256([]byte(combinedContent))
return fmt.Sprintf("%x", hash)[:15], nil
return fmt.Sprintf("%x", h.Sum(nil))[:16], nil
}

0 comments on commit 828e853

Please sign in to comment.