Skip to content

Commit

Permalink
Add a function to get the opensub hash of a file
Browse files Browse the repository at this point in the history
  • Loading branch information
gregdel authored and PouuleT committed Nov 21, 2024
1 parent ff7d966 commit fb1c071
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lib/file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package polochon

import (
"encoding/binary"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -198,6 +199,49 @@ func (f *File) MovieThumbPath() string {
return filepath.Join(path.Dir(f.Path), "/poster.jpg")
}

// OpensubHash implements the opensubtitles hash functions:
// https://trac.opensubtitles.org/projects/opensubtitles/wiki/HashSourceCodes
func (f *File) OpensubHash() (uint64, error) {
const hashChunkSize = 65536 // 64k
const hashBufSize = 8 // 8 bytes

if f.Size < hashChunkSize {
return 0, fmt.Errorf("polochon: file to small to be hashed")
}

file, err := os.Open(f.Path)
if err != nil {
return 0, err
}
defer file.Close()

hash := uint64(f.Size)

buf := make([]byte, hashBufSize)
parts := hashChunkSize / 8
for _, offset := range []int64{0, f.Size - hashChunkSize} {
_, err := file.Seek(offset, 0)
if err != nil {
return 0, err
}

for i := 0; i < parts; i++ {
n, err := file.Read(buf)
if err != nil {
return 0, err
}

if n != hashBufSize {
return 0, fmt.Errorf("polochon: failed to read all bytes %d/%d", n, hashBufSize)
}

hash += binary.LittleEndian.Uint64(buf)
}
}

return hash, nil
}

// removeExt returns file path without the extension
func removeExt(filepath string) string {
// Extension
Expand Down

0 comments on commit fb1c071

Please sign in to comment.