Re-upload files to Telegram instead of exposing a XEP-0363 link
This commit is contained in:
parent
25e8c98c3e
commit
b78779dad0
|
@ -2,6 +2,8 @@ package telegram
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -43,6 +45,16 @@ func (c *Client) getChatMessageLock(chatID int64) *sync.Mutex {
|
||||||
return lock
|
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() {
|
func (c *Client) updateHandler() {
|
||||||
listener := c.client.GetListener()
|
listener := c.client.GetListener()
|
||||||
defer listener.Close()
|
defer listener.Close()
|
||||||
|
@ -111,6 +123,18 @@ func (c *Client) updateHandler() {
|
||||||
uhOh()
|
uhOh()
|
||||||
}
|
}
|
||||||
c.updateAuthorizationState(typedUpdate)
|
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:
|
default:
|
||||||
// log only handled types
|
// log only handled types
|
||||||
continue
|
continue
|
||||||
|
@ -233,3 +257,17 @@ func (c *Client) updateAuthorizationState(update *client.UpdateAuthorizationStat
|
||||||
c.forceClose()
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -5,6 +5,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
osUser "os/user"
|
osUser "os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -832,10 +834,66 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
|
||||||
}
|
}
|
||||||
|
|
||||||
// attach a file
|
// attach a file
|
||||||
var file *client.InputFileRemote
|
var file *client.InputFileLocal
|
||||||
if chatID != 0 && c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) {
|
if chatID != 0 && c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) {
|
||||||
file = &client.InputFileRemote{
|
response, err := http.Get(text)
|
||||||
Id: 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)
|
newlinePos := strings.Index(text, newlineChar)
|
||||||
if newlinePos != -1 {
|
if newlinePos != -1 {
|
||||||
text = text[newlinePos+1:]
|
text = text[newlinePos+1:]
|
||||||
|
} else {
|
||||||
|
text = ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue