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 := "" + ti.Title + " " + "" + humanize.Bytes(uint64(ti.TorrentSize)) + "\n\n" + "" + ti.Hash + "" 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 = "" + ti.Title + " " + "" + humanize.Bytes(uint64(ti.TorrentSize)) + "\n\n" + "" + ti.Hash + "\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 }