616c6b1c62
Release Docker multi arch / docker (push) Has been cancelled
Test Install Script / Test Script Syntax (push) Has been cancelled
Test Install Script / Test on almalinux-10 (default) (push) Has been cancelled
Test Install Script / Test on almalinux-10 (root) (push) Has been cancelled
Test Install Script / Test on almalinux-8 (default) (push) Has been cancelled
Test Install Script / Test on almalinux-8 (root) (push) Has been cancelled
Test Install Script / Test on almalinux-9 (default) (push) Has been cancelled
Test Install Script / Test on almalinux-9 (root) (push) Has been cancelled
Test Install Script / Test on amazonlinux-2 (default) (push) Has been cancelled
Test Install Script / Test on amazonlinux-2 (root) (push) Has been cancelled
Test Install Script / Test on debian-11 (default) (push) Has been cancelled
Test Install Script / Test on debian-11 (root) (push) Has been cancelled
Test Install Script / Test on debian-12 (default) (push) Has been cancelled
Test Install Script / Test on debian-12 (root) (push) Has been cancelled
Test Install Script / Test on debian-13 (default) (push) Has been cancelled
Test Install Script / Test on debian-13 (root) (push) Has been cancelled
Test Install Script / Test on fedora-latest (default) (push) Has been cancelled
Test Install Script / Test on fedora-latest (root) (push) Has been cancelled
Test Install Script / Test on rocky-10 (default) (push) Has been cancelled
Test Install Script / Test on rocky-10 (root) (push) Has been cancelled
Test Install Script / Test on rocky-8 (default) (push) Has been cancelled
Test Install Script / Test on rocky-8 (root) (push) Has been cancelled
Test Install Script / Test on rocky-9 (default) (push) Has been cancelled
Test Install Script / Test on rocky-9 (root) (push) Has been cancelled
Test Install Script / Test on ubuntu-22.04 (default) (push) Has been cancelled
Test Install Script / Test on ubuntu-22.04 (root) (push) Has been cancelled
Test Install Script / Test on ubuntu-24.04 (default) (push) Has been cancelled
Test Install Script / Test on ubuntu-24.04 (root) (push) Has been cancelled
99 lines
2.0 KiB
Go
99 lines
2.0 KiB
Go
package upload
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"path/filepath"
|
|
|
|
"github.com/anacrolix/torrent"
|
|
|
|
sets "server/settings"
|
|
"server/log"
|
|
"server/tgbot/config"
|
|
"server/torr"
|
|
"server/torr/state"
|
|
"server/torr/storage/torrstor"
|
|
)
|
|
|
|
var ERR_STOPPED = errors.New("stopped")
|
|
|
|
type TorrFile struct {
|
|
hash string
|
|
name string
|
|
wrk *Worker
|
|
offset int64
|
|
size int64
|
|
id int
|
|
|
|
reader *torrstor.Reader
|
|
}
|
|
|
|
func NewTorrFile(wrk *Worker, stFile *state.TorrentFileStat) (*TorrFile, error) {
|
|
uid := int64(0)
|
|
if wrk.c != nil && wrk.c.Sender() != nil {
|
|
uid = wrk.c.Sender().ID
|
|
}
|
|
if config.Cfg != nil && config.Cfg.HostTG != "" && stFile.Length > 2*1024*1024*1024 {
|
|
return nil, errors.New(tr(uid, "upload_file_too_large_2gb"))
|
|
}
|
|
if (config.Cfg == nil || config.Cfg.HostTG == "") && stFile.Length > 50*1024*1024 {
|
|
return nil, errors.New(tr(uid, "upload_file_too_large_50mb"))
|
|
}
|
|
|
|
tf := new(TorrFile)
|
|
tf.hash = wrk.torrentHash
|
|
tf.name = filepath.Base(stFile.Path)
|
|
tf.wrk = wrk
|
|
tf.size = stFile.Length
|
|
|
|
t := torr.GetTorrent(wrk.torrentHash)
|
|
t.WaitInfo()
|
|
|
|
files := t.Files()
|
|
var file *torrent.File
|
|
for _, tfile := range files {
|
|
if tfile.Path() == stFile.Path {
|
|
file = tfile
|
|
break
|
|
}
|
|
}
|
|
if file == nil {
|
|
return nil, fmt.Errorf("file with id %v not found", stFile.Id)
|
|
}
|
|
if int64(sets.MaxSize) > 0 && file.Length() > int64(sets.MaxSize) {
|
|
log.TLogln("tg upload err size", file.DisplayPath(), "max", sets.MaxSize)
|
|
return nil, fmt.Errorf("file size exceeded max allowed %d bytes", sets.MaxSize)
|
|
}
|
|
|
|
reader := t.NewReader(file)
|
|
if reader == nil {
|
|
return nil, errors.New("cannot create torrent reader")
|
|
}
|
|
if sets.BTsets != nil && sets.BTsets.ResponsiveMode {
|
|
reader.SetResponsive()
|
|
}
|
|
tf.reader = reader
|
|
|
|
return tf, nil
|
|
}
|
|
|
|
func (t *TorrFile) Read(p []byte) (n int, err error) {
|
|
if t.wrk.isCancelled {
|
|
return 0, ERR_STOPPED
|
|
}
|
|
n, err = t.reader.Read(p)
|
|
t.offset += int64(n)
|
|
return
|
|
}
|
|
|
|
func (t *TorrFile) Remaining() int64 {
|
|
return t.size - t.offset
|
|
}
|
|
|
|
func (t *TorrFile) Close() {
|
|
if t.reader != nil {
|
|
t.reader.Close()
|
|
t.reader = nil
|
|
}
|
|
}
|