Files
TorrServerJellyfin/server/tgbot/files.go
2025-03-22 00:14:29 +03:00

84 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package tgbot
import (
"github.com/dustin/go-humanize"
tele "gopkg.in/telebot.v4"
"path/filepath"
"server/settings"
"server/torr"
"server/web"
"strconv"
"time"
)
func files(c tele.Context) error {
args := c.Args()
msg, err := c.Bot().Send(c.Sender(), "Подключение к торренту...")
t := torr.GetTorrent(args[1])
if t == nil {
c.Edit(msg, "Torrent not connected: "+args[1])
return nil
}
if err == nil {
go func() {
for !t.WaitInfo() {
time.Sleep(time.Second)
t = torr.GetTorrent(args[1])
}
c.Bot().Delete(msg)
host := settings.PubIPv4
if host == "" {
ips := web.GetLocalIps()
if len(ips) == 0 {
host = "127.0.0.1"
} else {
host = ips[0]
}
}
t = torr.GetTorrent(args[1])
ti := t.Status()
txt := "<b>" + ti.Title + "</b> " +
"<i>" + humanize.Bytes(uint64(ti.TorrentSize)) + "</i>\n\n" +
"<code>" + ti.Hash + "</code>"
filesKbd := &tele.ReplyMarkup{}
var files []tele.Row
i := len(txt)
for _, f := range ti.FileStats {
btn := filesKbd.Data("#"+strconv.Itoa(f.Id)+": "+humanize.Bytes(uint64(f.Length))+"\n"+filepath.Base(f.Path), "upload", ti.Hash, strconv.Itoa(f.Id))
link := filesKbd.URL("Ссылка", "http://"+host+":"+settings.Port+"/stream/"+filepath.Base(f.Path)+"?link="+t.Hash().HexString()+"&index="+strconv.Itoa(f.Id)+"&play")
files = append(files, filesKbd.Row(btn, link))
if i+len(txt) > 1024 || len(files) > 99 {
filesKbd := &tele.ReplyMarkup{}
filesKbd.Inline(files...)
c.Send(txt, filesKbd)
files = files[:0]
i = len(txt)
}
i += len(btn.Text + link.Text)
}
if len(files) > 0 {
filesKbd.Inline(files...)
c.Send(txt, filesKbd)
}
if len(files) > 1 {
txt = "<b>" + ti.Title + "</b> " +
"<i>" + humanize.Bytes(uint64(ti.TorrentSize)) + "</i>\n\n" +
"<code>" + ti.Hash + "</code>\n\n" +
"Чтобы скачать несколько файлов, ответьте на это сообщение, с какого файла скачать по какой, пример: 2-12\n\n" +
"Скачать все файлы? Всего:" + strconv.Itoa(len(ti.FileStats))
files = files[:0]
files = append(files, filesKbd.Row(filesKbd.Data("Скачать все файлы", "uploadall", ti.Hash)))
filesKbd.Inline(files...)
c.Send(txt, filesKbd)
}
}()
}
return err
}