teldrive/internal/reader/decrypted-reader.go

112 lines
2.1 KiB
Go
Raw Normal View History

2023-12-08 05:46:06 +08:00
package reader
import (
"context"
"io"
"github.com/divyam234/teldrive/internal/crypt"
"github.com/divyam234/teldrive/pkg/types"
"github.com/gotd/td/telegram"
)
type decrpytedReader struct {
ctx context.Context
parts []types.Part
2024-03-20 01:01:56 +08:00
ranges []types.Range
2023-12-08 05:46:06 +08:00
pos int
client *telegram.Client
reader io.ReadCloser
2024-03-10 22:40:09 +08:00
limit int64
err error
encryptionKey string
2023-12-08 05:46:06 +08:00
}
func NewDecryptedReader(
ctx context.Context,
client *telegram.Client,
parts []types.Part,
2024-03-20 01:01:56 +08:00
start, end int64,
encryptionKey string) (io.ReadCloser, error) {
2023-12-08 05:46:06 +08:00
r := &decrpytedReader{
ctx: ctx,
parts: parts,
client: client,
2024-03-20 01:01:56 +08:00
limit: end - start + 1,
ranges: calculatePartByteRanges(start, end, parts[0].DecryptedSize),
encryptionKey: encryptionKey,
2023-12-08 05:46:06 +08:00
}
res, err := r.nextPart()
if err != nil {
return nil, err
}
r.reader = res
return r, nil
}
func (r *decrpytedReader) Read(p []byte) (n int, err error) {
2024-03-10 22:40:09 +08:00
if r.err != nil {
return 0, r.err
}
if r.limit <= 0 {
return 0, io.EOF
}
2023-12-08 05:46:06 +08:00
n, err = r.reader.Read(p)
2024-03-10 22:40:09 +08:00
if err == nil {
r.limit -= int64(n)
}
if err == io.EOF {
if r.limit > 0 {
err = nil
}
2023-12-08 05:46:06 +08:00
r.pos++
2024-03-22 14:46:12 +08:00
if r.pos < len(r.ranges) {
2024-03-20 01:01:56 +08:00
r.reader, err = r.nextPart()
2023-12-08 05:46:06 +08:00
}
}
2024-03-10 22:40:09 +08:00
r.err = err
return
2023-12-08 05:46:06 +08:00
}
func (r *decrpytedReader) Close() (err error) {
if r.reader != nil {
err = r.reader.Close()
r.reader = nil
return err
}
return nil
}
func (r *decrpytedReader) nextPart() (io.ReadCloser, error) {
2024-03-20 01:01:56 +08:00
location := r.parts[r.ranges[r.pos].PartNo].Location
start := r.ranges[r.pos].Start
end := r.ranges[r.pos].End
salt := r.parts[r.ranges[r.pos].PartNo].Salt
cipher, _ := crypt.NewCipher(r.encryptionKey, salt)
return cipher.DecryptDataSeek(r.ctx,
2023-12-08 05:46:06 +08:00
func(ctx context.Context,
underlyingOffset,
underlyingLimit int64) (io.ReadCloser, error) {
var end int64
if underlyingLimit >= 0 {
2024-03-20 01:01:56 +08:00
end = min(r.parts[r.ranges[r.pos].PartNo].Size-1, underlyingOffset+underlyingLimit-1)
2023-12-08 05:46:06 +08:00
}
2024-03-20 01:01:56 +08:00
return newTGReader(r.ctx, r.client, location, underlyingOffset, end)
}, start, end-start+1)
2023-12-08 05:46:06 +08:00
}