aboutsummaryrefslogtreecommitdiff
path: root/telegram/handlers.go
diff options
context:
space:
mode:
authorBohdan Horbeshko <bodqhrohro@gmail.com>2022-05-15 19:59:54 +0300
committerBohdan Horbeshko <bodqhrohro@gmail.com>2022-05-15 19:59:54 +0300
commitb78779dad0c78479654941ecb43a3d0a30e83689 (patch)
tree4a295273609134a8f2d535983f5d28917ad491b5 /telegram/handlers.go
parent25e8c98c3ea2e9249416e70cea6c9818757ddff2 (diff)
Re-upload files to Telegram instead of exposing a XEP-0363 link
Diffstat (limited to 'telegram/handlers.go')
-rw-r--r--telegram/handlers.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/telegram/handlers.go b/telegram/handlers.go
index 45f10fd..9b86ac7 100644
--- a/telegram/handlers.go
+++ b/telegram/handlers.go
@@ -2,6 +2,8 @@ package telegram
import (
"fmt"
+ "os"
+ "path/filepath"
"strconv"
"strings"
"sync"
@@ -43,6 +45,16 @@ func (c *Client) getChatMessageLock(chatID int64) *sync.Mutex {
return lock
}
+func (c *Client) cleanTempFile(path string) {
+ os.Remove(path)
+
+ dir := filepath.Dir(path)
+ dirName := filepath.Base(dir)
+ if strings.HasPrefix(dirName, "telegabber-") {
+ os.Remove(dir)
+ }
+}
+
func (c *Client) updateHandler() {
listener := c.client.GetListener()
defer listener.Close()
@@ -111,6 +123,18 @@ func (c *Client) updateHandler() {
uhOh()
}
c.updateAuthorizationState(typedUpdate)
+ case client.TypeUpdateMessageSendSucceeded:
+ typedUpdate, ok := update.(*client.UpdateMessageSendSucceeded)
+ if !ok {
+ uhOh()
+ }
+ c.updateMessageSendSucceeded(typedUpdate)
+ case client.TypeUpdateMessageSendFailed:
+ typedUpdate, ok := update.(*client.UpdateMessageSendFailed)
+ if !ok {
+ uhOh()
+ }
+ c.updateMessageSendFailed(typedUpdate)
default:
// log only handled types
continue
@@ -233,3 +257,17 @@ func (c *Client) updateAuthorizationState(update *client.UpdateAuthorizationStat
c.forceClose()
}
}
+
+// clean uploaded files
+func (c *Client) updateMessageSendSucceeded(update *client.UpdateMessageSendSucceeded) {
+ file, _ := c.contentToFile(update.Message.Content)
+ if file != nil && file.Local != nil {
+ c.cleanTempFile(file.Local.Path)
+ }
+}
+func (c *Client) updateMessageSendFailed(update *client.UpdateMessageSendFailed) {
+ file, _ := c.contentToFile(update.Message.Content)
+ if file != nil && file.Local != nil {
+ c.cleanTempFile(file.Local.Path)
+ }
+}