-
Notifications
You must be signed in to change notification settings - Fork 119
/
fileEntry.go
40 lines (32 loc) · 906 Bytes
/
fileEntry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"io"
"github.com/anacrolix/torrent"
)
// SeekableContent describes an io.ReadSeeker that can be closed as well.
type SeekableContent interface {
io.ReadSeeker
io.Closer
}
// FileEntry helps reading a torrent file.
type FileEntry struct {
*torrent.File
torrent.Reader
}
// Seek seeks to the correct file position, paying attention to the offset.
func (f *FileEntry) Seek(offset int64, whence int) (int64, error) {
return f.Reader.Seek(offset+f.File.Offset(), whence)
}
// NewFileReader sets up a torrent file for streaming reading.
func NewFileReader(f *torrent.File) (SeekableContent, error) {
torrent := f.Torrent()
reader := torrent.NewReader()
// We read ahead 1% of the file continuously.
reader.SetReadahead(f.Length() / 100)
reader.SetResponsive()
_, err := reader.Seek(f.Offset(), io.SeekStart)
return &FileEntry{
File: f,
Reader: reader,
}, err
}