From b13e6757a123bbc53fec7f624e268d31f8abee7e Mon Sep 17 00:00:00 2001 From: koenigskraut <87582318+koenigskraut@users.noreply.github.com> Date: Fri, 4 Aug 2023 16:13:56 +0300 Subject: [PATCH] experimental rendering for all sticker types --- cmd/webapp/downloader.go | 88 +++++++++++++++++++++++++++++++++------- cmd/webapp/index.html | 37 ++++++++++++++--- cmd/webapp/ws.go | 17 ++++---- 3 files changed, 113 insertions(+), 29 deletions(-) diff --git a/cmd/webapp/downloader.go b/cmd/webapp/downloader.go index c5bc6a8..89cc0fc 100644 --- a/cmd/webapp/downloader.go +++ b/cmd/webapp/downloader.go @@ -1,12 +1,16 @@ package main import ( + "compress/gzip" "context" "fmt" "github.com/gotd/td/telegram/downloader" "github.com/gotd/td/tg" + "github.com/koenigskraut/piktagbot/database" + "io" "log" "os" + "os/exec" "strconv" "sync" ) @@ -47,8 +51,13 @@ func (dm *downloadedMap) peek(documentID int64) bool { var downloaded = downloadedMap{m: make(map[int64]bool)} +type InputDocumentMimeTyped struct { + mimeType database.MimeType + doc *tg.InputDocumentFileLocation +} + type receiveFiles struct { - files []*tg.InputDocumentFileLocation + files []InputDocumentMimeTyped ch chan string } @@ -57,20 +66,71 @@ var downloadChan = make(chan *receiveFiles) func downloadLoop(ch chan *receiveFiles, ctx context.Context, tgClient *tg.Client) { d := downloader.NewDownloader() for r := range ch { - go func(rr *receiveFiles) { - for _, file := range rr.files { - if !downloaded.peek(file.ID) { - _, err := d.Download(tgClient, file). - ToPath(ctx, fmt.Sprintf("%s/%d", stickerPath, file.ID)) - if err != nil { - rr.ch <- "404" - continue - } - downloaded.add(file.ID) + go downloadAll(ctx, tgClient, d, r) + } +} + +const prefix = `slv` + +func downloadAll(ctx context.Context, api *tg.Client, d *downloader.Downloader, r *receiveFiles) { + defer close(r.ch) + for _, file := range r.files { + if !downloaded.peek(file.doc.ID) { + webpPath := fmt.Sprintf("%s/%d", stickerPath, file.doc.ID) + switch file.mimeType { + case database.MimeTypeWebp: + _, err := d.Download(api, file.doc).ToPath(ctx, webpPath) + if err != nil { + r.ch <- "404" + continue + } + case database.MimeTypeWebm: + webmPath := fmt.Sprintf("%s/webm/%d", stickerPath, file.doc.ID) + _, err := d.Download(api, file.doc).ToPath(ctx, webmPath) + if err != nil { + r.ch <- "404" + continue } - rr.ch <- strconv.FormatInt(file.ID, 10) + // yeah.. i know + err = exec.Command("ffmpeg", "-y", "-vcodec", "libvpx-vp9", "-i", webmPath, + "-vframes", "1", "-f", "webp", webpPath).Run() + if err != nil { + r.ch <- "404" + continue + } + case database.MimeTypeTgs: + tgsPath := fmt.Sprintf("%s/tgs/%d.tgs", stickerPath, file.doc.ID) + _, err := d.Download(api, file.doc).ToPath(ctx, tgsPath) + if err != nil { + r.ch <- "404" + continue + } + fileIn, err := os.Open(tgsPath) + if err != nil { + r.ch <- "404" + continue + } + reader, err := gzip.NewReader(fileIn) + if err != nil { + r.ch <- "404" + continue + } + jsonPath := fmt.Sprintf("%s/%d", stickerPath, file.doc.ID) + fileOut, err := os.Create(jsonPath) + if err != nil { + r.ch <- "404" + continue + } + if _, err := io.Copy(fileOut, reader); err != nil { + r.ch <- "404" + continue + } + fileOut.Close() + reader.Close() + fileIn.Close() } - close(rr.ch) - }(r) + downloaded.add(file.doc.ID) + } + r.ch <- string(prefix[file.mimeType]) + strconv.FormatInt(file.doc.ID, 10) } } diff --git a/cmd/webapp/index.html b/cmd/webapp/index.html index 59b310a..d3c78eb 100644 --- a/cmd/webapp/index.html +++ b/cmd/webapp/index.html @@ -5,6 +5,7 @@