Move code used for sending a file from a file URI into discordutil
This commit is contained in:
parent
49ef544bcd
commit
835aaa6aa3
|
@ -3,11 +3,14 @@ package discordutil
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/Bios-Marcel/discordgo"
|
||||
|
||||
"github.com/Bios-Marcel/cordless/times"
|
||||
"github.com/Bios-Marcel/cordless/util/files"
|
||||
)
|
||||
|
||||
// MentionsCurrentUserExplicitly checks whether the message contains any
|
||||
|
@ -212,3 +215,20 @@ func MessageToPlainText(message *discordgo.Message) string {
|
|||
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
// ResolveFilePathAndSendFile will attempt to resolve the message and see if
|
||||
// it points to a file on the users harddrive. If so, it's sent to the given
|
||||
// channel using it's basename as the discord filename.
|
||||
func ResolveFilePathAndSendFile(session *discordgo.Session, message, targetChannelID string) error {
|
||||
path, pathError := files.ToAbsolutePath(message)
|
||||
if pathError != nil {
|
||||
return pathError
|
||||
}
|
||||
data, readError := ioutil.ReadFile(path)
|
||||
if readError != nil {
|
||||
return readError
|
||||
}
|
||||
reader := bytes.NewBuffer(data)
|
||||
_, sendError := session.ChannelFileSend(targetChannelID, filepath.Base(message), reader)
|
||||
return sendError
|
||||
}
|
||||
|
|
18
ui/window.go
18
ui/window.go
|
@ -3,7 +3,6 @@ package ui
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
@ -1260,22 +1259,7 @@ func (window *Window) TrySendMessage(targetChannel *discordgo.Channel, message s
|
|||
if button == yesButton {
|
||||
window.messageInput.SetText("")
|
||||
go func() {
|
||||
path, pathError := files.ToAbsolutePath(message)
|
||||
if pathError != nil {
|
||||
window.app.QueueUpdateDraw(func() {
|
||||
window.ShowErrorDialog(pathError.Error())
|
||||
})
|
||||
return
|
||||
}
|
||||
data, readError := ioutil.ReadFile(path)
|
||||
if readError != nil {
|
||||
window.app.QueueUpdateDraw(func() {
|
||||
window.ShowErrorDialog(readError.Error())
|
||||
})
|
||||
return
|
||||
}
|
||||
reader := bytes.NewBuffer(data)
|
||||
_, sendError := window.session.ChannelFileSend(targetChannel.ID, filepath.Base(message), reader)
|
||||
sendError := discordutil.ResolveFilePathAndSendFile(window.session, message, targetChannel.ID)
|
||||
if sendError != nil {
|
||||
window.app.QueueUpdateDraw(func() {
|
||||
window.ShowErrorDialog(sendError.Error())
|
||||
|
|
Loading…
Reference in New Issue