Fix chunk manager

This commit is contained in:
Ben Sarmiento
2023-11-14 16:55:24 +01:00
parent 16505ec33f
commit 4da9416bec
4 changed files with 24 additions and 25 deletions

View File

@@ -1,9 +1,9 @@
package chunk
import (
"crypto/sha256"
"encoding/binary"
"fmt"
"hash/fnv"
"os"
"github.com/debridmediamanager.com/zurg/internal/config"
@@ -58,8 +58,8 @@ func NewManager(
checkThreads,
loadThreads,
maxChunks int,
t *torrent.TorrentManager,
c config.ConfigInterface) (*Manager, error) {
torMgr *torrent.TorrentManager,
cfg config.ConfigInterface) (*Manager, error) {
pageSize := int64(os.Getpagesize())
if chunkSize < pageSize {
@@ -82,7 +82,7 @@ func NewManager(
return nil, err
}
downloader, err := NewDownloader(loadThreads, storage, chunkSize, t, c)
downloader, err := NewDownloader(loadThreads, storage, chunkSize, torMgr)
if nil != err {
return nil, err
}
@@ -149,12 +149,17 @@ func buildRequestID(object *torrent.File, offset int64) (id RequestID) {
if fileID == "" {
fileID = object.Path
}
hash := sha256.Sum256([]byte(fileID))
hash := hashStringToFh(fileID)
copy(id[:16], hash[:16])
binary.BigEndian.PutUint64(id[16:], uint64(offset))
return
}
func hashStringToFh(s string) []byte {
hasher := fnv.New64a()
return hasher.Sum([]byte(s))
}
func (m *Manager) requestChunk(object *torrent.File, offset, size int64, sequence int, preload bool, response chan Response) {
chunkOffset := offset % m.ChunkSize
offsetStart := offset - chunkOffset