aboutsummaryrefslogtreecommitdiff
path: root/telegram/utils.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/utils.go
parent25e8c98c3ea2e9249416e70cea6c9818757ddff2 (diff)
Re-upload files to Telegram instead of exposing a XEP-0363 link
Diffstat (limited to 'telegram/utils.go')
-rw-r--r--telegram/utils.go66
1 files changed, 63 insertions, 3 deletions
diff --git a/telegram/utils.go b/telegram/utils.go
index f1f7f52..1248a54 100644
--- a/telegram/utils.go
+++ b/telegram/utils.go
@@ -5,6 +5,8 @@ import (
"fmt"
"github.com/pkg/errors"
"io"
+ "io/ioutil"
+ "net/http"
"os"
osUser "os/user"
"path/filepath"
@@ -832,10 +834,66 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
}
// attach a file
- var file *client.InputFileRemote
+ var file *client.InputFileLocal
if chatID != 0 && c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) {
- file = &client.InputFileRemote{
- Id: text,
+ response, err := http.Get(text)
+ if err != nil {
+ gateway.SendMessage(
+ returnJid,
+ strconv.FormatInt(chatID, 10),
+ fmt.Sprintf("Failed to fetch the uploaded file: %s", err.Error()),
+ c.xmpp,
+ )
+ return nil
+ }
+ if response != nil && response.Body != nil {
+ defer response.Body.Close()
+
+ if response.StatusCode != 200 {
+ gateway.SendMessage(
+ returnJid,
+ strconv.FormatInt(chatID, 10),
+ fmt.Sprintf("Received status code %v", response.StatusCode),
+ c.xmpp,
+ )
+ return nil
+ }
+
+ tempDir, err := ioutil.TempDir("", "telegabber-*")
+ if err != nil {
+ gateway.SendMessage(
+ returnJid,
+ strconv.FormatInt(chatID, 10),
+ fmt.Sprintf("Failed to create a temporary directory: %s", err.Error()),
+ c.xmpp,
+ )
+ return nil
+ }
+ tempFile, err := os.Create(filepath.Join(tempDir, filepath.Base(text)))
+ if err != nil {
+ gateway.SendMessage(
+ returnJid,
+ strconv.FormatInt(chatID, 10),
+ fmt.Sprintf("Failed to create a temporary file: %s", err.Error()),
+ c.xmpp,
+ )
+ return nil
+ }
+
+ _, err = io.Copy(tempFile, response.Body)
+ if err != nil {
+ gateway.SendMessage(
+ returnJid,
+ strconv.FormatInt(chatID, 10),
+ fmt.Sprintf("Failed to write a temporary file: %s", err.Error()),
+ c.xmpp,
+ )
+ return nil
+ }
+
+ file = &client.InputFileLocal{
+ Path: tempFile.Name(),
+ }
}
}
@@ -844,6 +902,8 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
newlinePos := strings.Index(text, newlineChar)
if newlinePos != -1 {
text = text[newlinePos+1:]
+ } else {
+ text = ""
}
}