Handle downloaded files

This commit is contained in:
bodqhrohro 2019-12-04 00:17:36 +02:00
parent 0c0c8e777a
commit 8949290c52

View file

@ -1,7 +1,10 @@
package telegram package telegram
import ( import (
"crypto/sha256"
"fmt" "fmt"
"os"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -75,6 +78,12 @@ func (c *Client) updateHandler() {
uhOh() uhOh()
} }
c.updateDeleteMessages(typedUpdate) c.updateDeleteMessages(typedUpdate)
case client.TypeUpdateFile:
typedUpdate, ok := update.(*client.UpdateFile)
if !ok {
uhOh()
}
c.updateFile(typedUpdate)
default: default:
// log only handled types // log only handled types
continue continue
@ -85,17 +94,20 @@ func (c *Client) updateHandler() {
} }
} }
// new user discovered
func (c *Client) updateUser(update *client.UpdateUser) { func (c *Client) updateUser(update *client.UpdateUser) {
cache.users[update.User.Id] = update.User cache.users[update.User.Id] = update.User
show, status := userStatusToText(update.User.Status) show, status := userStatusToText(update.User.Status)
c.processStatusUpdate(update.User.Id, status, show) c.processStatusUpdate(update.User.Id, status, show)
} }
// user status changed
func (c *Client) updateUserStatus(update *client.UpdateUserStatus) { func (c *Client) updateUserStatus(update *client.UpdateUserStatus) {
show, status := userStatusToText(update.Status) show, status := userStatusToText(update.Status)
c.processStatusUpdate(update.UserId, status, show, gateway.SPImmed(false)) c.processStatusUpdate(update.UserId, status, show, gateway.SPImmed(false))
} }
// new chat discovered
func (c *Client) updateNewChat(update *client.UpdateNewChat) { func (c *Client) updateNewChat(update *client.UpdateNewChat) {
if update.Chat != nil && update.Chat.Photo != nil && update.Chat.Photo.Small != nil { if update.Chat != nil && update.Chat.Photo != nil && update.Chat.Photo.Small != nil {
_, err := c.client.DownloadFile(&client.DownloadFileRequest{ _, err := c.client.DownloadFile(&client.DownloadFileRequest{
@ -135,6 +147,7 @@ func (c *Client) updateNewChat(update *client.UpdateNewChat) {
} }
} }
// message received
func (c *Client) updateNewMessage(update *client.UpdateNewMessage) { func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
// ignore self outgoing messages // ignore self outgoing messages
if update.Message.IsOutgoing && if update.Message.IsOutgoing &&
@ -186,6 +199,7 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
gateway.SendMessage(c.jid, strconv.Itoa(int(update.Message.ChatId)), text, c.xmpp) gateway.SendMessage(c.jid, strconv.Itoa(int(update.Message.ChatId)), text, c.xmpp)
} }
// message content updated
func (c *Client) updateMessageContent(update *client.UpdateMessageContent) { func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
if update.NewContent.MessageContentType() == client.TypeMessageText { if update.NewContent.MessageContentType() == client.TypeMessageText {
textContent := update.NewContent.(*client.MessageText) textContent := update.NewContent.(*client.MessageText)
@ -194,9 +208,31 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
} }
} }
// message(s) deleted
func (c *Client) updateDeleteMessages(update *client.UpdateDeleteMessages) { func (c *Client) updateDeleteMessages(update *client.UpdateDeleteMessages) {
if update.IsPermanent { if update.IsPermanent {
text := "✗ " + strings.Join(int64SliceToStringSlice(update.MessageIds), ",") text := "✗ " + strings.Join(int64SliceToStringSlice(update.MessageIds), ",")
gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp) gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp)
} }
} }
// file downloaded
func (c *Client) updateFile(update *client.UpdateFile) {
// not really
if !update.File.Local.IsDownloadingCompleted {
return
}
err := os.Symlink(
update.File.Local.Path,
fmt.Sprintf(
"%s/%s%s",
c.content.Path,
fmt.Sprintf("%x", sha256.Sum256([]byte(update.File.Remote.Id))),
filepath.Ext(update.File.Local.Path),
),
)
if err != nil {
log.Errorf("Error creating symlink: %v", err)
}
}