Skip to content

Commit

Permalink
experimental rendering for all sticker types
Browse files Browse the repository at this point in the history
  • Loading branch information
koenigskraut committed Aug 4, 2023
1 parent 3d6e04e commit b13e675
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 29 deletions.
88 changes: 74 additions & 14 deletions cmd/webapp/downloader.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
}
}
37 changes: 31 additions & 6 deletions cmd/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<title>Sticker Tagger App</title>
<script src="https://telegram.org/js/telegram-web-app.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bodymovin/5.12.2/lottie.min.js"></script>
<style>

.text {
Expand Down Expand Up @@ -62,11 +63,19 @@
transform: scale(1.2);
}

.sortable-chosen svg {
transform: scale(1.2) !important;
}

.sortable-fallback svg {
transform: scale(1.2) !important;
}

.sortable-unchosen {
transform: scale(0.9);
transform: scale(0.9) !important;
}

img {
.square-img {
transition: transform 150ms ease;
-webkit-transition: transform 150ms ease;
-o-transition: transform 150ms ease;
Expand Down Expand Up @@ -151,7 +160,9 @@
let webApp = Telegram.WebApp;
let container = null;
let stickerNumber = 0;
const stickerTypes = {"webp": "s", "tgs": "l", "webm": "v"};
webApp.MainButton.text = "Сохранить порядок";
let animations = {};

function registerContainer() {
container = document.getElementById("gridDemo");
Expand Down Expand Up @@ -262,10 +273,24 @@
let elem = document.createElement("div");
elem.className = "grid-square";
elem.id = "elem" + String(stickerNumber+1);
let elemChild = document.createElement("img");
elemChild.className = "square-img";
elemChild.src = "/stickers/" + ev.data;
elem.appendChild(elemChild);
if (ev.data[0] === stickerTypes.tgs) {
animations[elem.id] = bodymovin.loadAnimation({
container: elem,
renderer: 'svg',
loop: false,
autoplay: false,
path: "/stickers/" + ev.data.substring(1),
rendererSettings: {
className: 'square-img',
progressiveLoad: true,
}
});
} else {
let elemChild = document.createElement("img");
elemChild.src = "/stickers/" + ev.data.substring(1);
elemChild.className = "square-img";
elem.appendChild(elemChild);
}
container.appendChild(elem)
stickerNumber++;
};
Expand Down
17 changes: 8 additions & 9 deletions cmd/webapp/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@ func handleWS(writer http.ResponseWriter, request *http.Request) {
return
}
recentStickers, _ := dbUser.RecentStickers()
locations := make([]*tg.InputDocumentFileLocation, 0, len(recentStickers))
locations := make([]InputDocumentMimeTyped, 0, len(recentStickers))
for _, r := range recentStickers {
// TODO handle other sticker types
if r.Sticker.Type != db.MimeTypeWebp {
continue
}
locations = append(locations, &tg.InputDocumentFileLocation{
ID: r.Sticker.DocumentID,
AccessHash: r.Sticker.AccessHash,
FileReference: r.Sticker.FileReference,
locations = append(locations, InputDocumentMimeTyped{
mimeType: r.Sticker.Type,
doc: &tg.InputDocumentFileLocation{
ID: r.Sticker.DocumentID,
AccessHash: r.Sticker.AccessHash,
FileReference: r.Sticker.FileReference,
},
})
}
fmt.Println(locations)
Expand Down

0 comments on commit b13e675

Please sign in to comment.