Finish basic file upload support. Fixes #62

This commit is contained in:
Tulir Asokan 2020-09-02 02:12:09 +03:00
parent 1abf902392
commit 0aeedd2c43
8 changed files with 292 additions and 100 deletions

3
go.mod
View File

@ -23,8 +23,9 @@ require (
golang.org/x/image v0.0.0-20200430140353-33d19683fad8
golang.org/x/net v0.0.0-20200602114024-627f9648deb9
gopkg.in/toast.v1 v1.0.0-20180812000517-0a84660828b2
gopkg.in/vansante/go-ffprobe.v2 v2.0.2
gopkg.in/yaml.v2 v2.3.0
maunium.net/go/mautrix v0.7.4
maunium.net/go/mautrix v0.7.5
maunium.net/go/mauview v0.1.1
maunium.net/go/tcell v0.2.0
)

4
go.sum
View File

@ -102,6 +102,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/toast.v1 v1.0.0-20180812000517-0a84660828b2/go.mod h1:s1Sn2yZos05Qfs7NKt867Xe18emOmtsO3eAKbDaon0o=
gopkg.in/vansante/go-ffprobe.v2 v2.0.2 h1:DdxSfFnlqeawPIVbIQEI6LR6OQHQNR7tNgWb2mWuC4w=
gopkg.in/vansante/go-ffprobe.v2 v2.0.2/go.mod h1:qF0AlAjk7Nqzqf3y333Ly+KxN3cKF2JqA3JT5ZheUGE=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
@ -125,6 +127,8 @@ maunium.net/go/mautrix v0.7.3 h1:yC287SXL0pTZzAtpGIvwtlxPHZsWMuq2DmI5/POTKFE=
maunium.net/go/mautrix v0.7.3/go.mod h1:Va/74MijqaS0DQ3aUqxmFO54/PMfr1LVsCOcGRHbYmo=
maunium.net/go/mautrix v0.7.4 h1:MDjrvDyHcu5ozKAa80ohcXmYAXZTHgHxrhiERtvkEdY=
maunium.net/go/mautrix v0.7.4/go.mod h1:Va/74MijqaS0DQ3aUqxmFO54/PMfr1LVsCOcGRHbYmo=
maunium.net/go/mautrix v0.7.5 h1:3MFayZGekrPVL0xogwqeX50UdQnKqGkd9w1c1HQoyYo=
maunium.net/go/mautrix v0.7.5/go.mod h1:Va/74MijqaS0DQ3aUqxmFO54/PMfr1LVsCOcGRHbYmo=
maunium.net/go/mauview v0.1.1 h1:wfTXyPx3LGAGpTskh+UbBv/QItUWnEpaneHmywoYnfY=
maunium.net/go/mauview v0.1.1/go.mod h1:3QBUiuLct9moP1LgDhCGIg0Ovxn38Bd2sGndnUOuj4o=
maunium.net/go/tcell v0.2.0 h1:1Q0kN3wCOGAIGu1r3QHADsjSUOPDylKREvCv3EzJpVg=

View File

@ -32,6 +32,14 @@ type Relation struct {
Event *muksevt.Event
}
type UploadedMediaInfo struct {
*mautrix.RespMediaUpload
EncryptionInfo *attachment.EncryptedFile
MsgType event.MessageType
Name string
Info *event.FileInfo
}
type MatrixContainer interface {
Client() *mautrix.Client
Preferences() *config.UserPreferences
@ -46,6 +54,7 @@ type MatrixContainer interface {
SendPreferencesToMatrix()
PrepareMarkdownMessage(roomID id.RoomID, msgtype event.MessageType, text, html string, relation *Relation) *muksevt.Event
PrepareMediaMessage(room *rooms.Room, path string, relation *Relation) (*muksevt.Event, error)
SendEvent(evt *muksevt.Event) (id.EventID, error)
Redact(roomID id.RoomID, eventID id.EventID, reason string) error
SendTyping(roomID id.RoomID, typing bool)
@ -60,8 +69,7 @@ type MatrixContainer interface {
GetRoom(roomID id.RoomID) *rooms.Room
GetOrCreateRoom(roomID id.RoomID) *rooms.Room
SendImage(roomID id.RoomID, body string, url id.ContentURI)
UploadMedia(data mautrix.ReqUploadMedia) (*id.ContentURI, error)
UploadMedia(path string, encrypt bool) (*UploadedMediaInfo, error)
Download(uri id.ContentURI, file *attachment.EncryptedFile) ([]byte, error)
DownloadToDisk(uri id.ContentURI, file *attachment.EncryptedFile, target string) (string, error)
GetDownloadURL(uri id.ContentURI) string

View File

@ -819,6 +819,28 @@ func (c *Container) MarkRead(roomID id.RoomID, eventID id.EventID) {
}()
}
func (c *Container) PrepareMediaMessage(room *rooms.Room, path string, rel *ifc.Relation) (*muksevt.Event, error) {
resp, err := c.UploadMedia(path, room.Encrypted)
if err != nil {
return nil, err
}
content := event.MessageEventContent{
MsgType: resp.MsgType,
Body: resp.Name,
Info: resp.Info,
}
if resp.EncryptionInfo != nil {
content.File = &event.EncryptedFileInfo{
EncryptedFile: *resp.EncryptionInfo,
URL: resp.ContentURI.CUString(),
}
} else {
content.URL = resp.ContentURI.CUString()
}
return c.prepareEvent(room.ID, &content, rel), nil
}
func (c *Container) PrepareMarkdownMessage(roomID id.RoomID, msgtype event.MessageType, text, html string, rel *ifc.Relation) *muksevt.Event {
var content event.MessageEventContent
if html != "" {
@ -833,8 +855,12 @@ func (c *Container) PrepareMarkdownMessage(roomID id.RoomID, msgtype event.Messa
content.MsgType = msgtype
}
return c.prepareEvent(roomID, &content, rel)
}
func (c *Container) prepareEvent(roomID id.RoomID, content *event.MessageEventContent, rel *ifc.Relation) *muksevt.Event {
if rel != nil && rel.Type == event.RelReplace {
contentCopy := content
contentCopy := *content
content.NewContent = &contentCopy
content.Body = "* " + content.Body
if len(content.FormattedBody) > 0 {
@ -855,7 +881,7 @@ func (c *Container) PrepareMarkdownMessage(roomID id.RoomID, msgtype event.Messa
Type: event.EventMessage,
Timestamp: time.Now().UnixNano() / 1e6,
RoomID: roomID,
Content: event.Content{Parsed: &content},
Content: event.Content{Parsed: content},
Unsigned: event.Unsigned{TransactionID: txnID},
})
localEcho.Gomuks.OutgoingState = muksevt.StateLocalEcho
@ -905,18 +931,60 @@ func (c *Container) SendEvent(evt *muksevt.Event) (id.EventID, error) {
return resp.EventID, nil
}
func (c *Container) SendImage(roomID id.RoomID, body string, url id.ContentURI) {
func (c *Container) UploadMedia(path string, encrypt bool) (*ifc.UploadedMediaInfo, error) {
var err error
path, err = filepath.Abs(path)
if err != nil {
return nil, errors.Wrap(err, "failed to get absolute path")
}
func (c *Container) UploadMedia(data mautrix.ReqUploadMedia) (*id.ContentURI, error) {
resp, err := c.client.UploadMedia(data)
msgtype, info, err := getMediaInfo(path)
if err != nil {
return nil, err
}
file, err := os.Open(path)
if err != nil {
return nil, errors.Wrap(err, "failed to open file")
}
stat, err := file.Stat()
if err != nil {
return nil, errors.Wrap(err, "failed to get file info")
}
uploadFileName := stat.Name()
uploadMimeType := info.MimeType
var content io.Reader
var encryptionInfo *attachment.EncryptedFile
if encrypt {
uploadMimeType = "application/octet-stream"
uploadFileName = ""
encryptionInfo = attachment.NewEncryptedFile()
content = encryptionInfo.EncryptStream(file)
} else {
content = file
}
resp, err := c.client.UploadMedia(mautrix.ReqUploadMedia{
Content: content,
ContentLength: stat.Size(),
ContentType: uploadMimeType,
FileName: uploadFileName,
})
if err != nil {
return nil, err
}
return &resp.ContentURI, nil
return &ifc.UploadedMediaInfo{
RespMediaUpload: resp,
EncryptionInfo: encryptionInfo,
Name: stat.Name(),
MsgType: msgtype,
Info: &info,
}, nil
}
func (c *Container) sendTypingAsync(roomID id.RoomID, typing bool, timeout int64) {

106
matrix/mediainfo.go Normal file
View File

@ -0,0 +1,106 @@
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2020 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package matrix
import (
"context"
"fmt"
"image"
"os"
"strings"
"time"
"github.com/gabriel-vasile/mimetype"
"github.com/pkg/errors"
"gopkg.in/vansante/go-ffprobe.v2"
"maunium.net/go/gomuks/debug"
"maunium.net/go/mautrix/event"
)
func getImageInfo(path string) (event.FileInfo, error) {
var info event.FileInfo
file, err := os.Open(path)
if err != nil {
return info, errors.Wrap(err, "failed to open image to get info")
}
cfg, _, err := image.DecodeConfig(file)
if err != nil {
return info, errors.Wrap(err, "failed to get image info")
}
info.Width = cfg.Width
info.Height = cfg.Height
return info, nil
}
func getFFProbeInfo(mimeClass, path string) (msgtype event.MessageType, info event.FileInfo, err error) {
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFn()
var probedInfo *ffprobe.ProbeData
probedInfo, err = ffprobe.ProbeURL(ctx, path)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("failed to get %s info with ffprobe", mimeClass))
return
}
if mimeClass == "audio" {
msgtype = event.MsgAudio
stream := probedInfo.FirstAudioStream()
if stream != nil {
info.Duration = int(stream.DurationTs)
}
} else {
msgtype = event.MsgVideo
stream := probedInfo.FirstVideoStream()
if stream != nil {
info.Duration = int(stream.DurationTs)
info.Width = stream.Width
info.Height = stream.Height
}
}
return
}
func getMediaInfo(path string) (msgtype event.MessageType, info event.FileInfo, err error) {
var mime *mimetype.MIME
mime, err = mimetype.DetectFile(path)
if err != nil {
err = errors.Wrap(err, "failed to get content type")
return
}
mimeClass := strings.SplitN(mime.String(), "/", 2)[0]
switch mimeClass {
case "image":
msgtype = event.MsgImage
info, err = getImageInfo(path)
if err != nil {
debug.Printf("Failed to get image info for %s: %v", err)
err = nil
}
case "audio", "video":
msgtype, info, err = getFFProbeInfo(mimeClass, path)
if err != nil {
debug.Printf("Failed to get ffprobe info for %s: %v", err)
err = nil
}
default:
msgtype = event.MsgFile
}
info.MimeType = mime.String()
return
}

58
ui/autocomplete.go Normal file
View File

@ -0,0 +1,58 @@
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2020 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package ui
import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
)
func autocompleteFile(cmd *CommandAutocomplete) (completions []string, newText string) {
inputPath, err := filepath.Abs(cmd.RawArgs)
if err != nil {
return
}
var searchNamePrefix, searchDir string
if strings.HasSuffix(cmd.RawArgs, "/") {
searchDir = inputPath
} else {
searchNamePrefix = filepath.Base(inputPath)
searchDir = filepath.Dir(inputPath)
}
files, err := ioutil.ReadDir(searchDir)
if err != nil {
return
}
for _, file := range files {
name := file.Name()
if !strings.HasPrefix(name, searchNamePrefix) || (name[0] == '.' && searchNamePrefix == "") {
continue
}
fullPath := filepath.Join(searchDir, name)
if file.IsDir() {
fullPath += "/"
}
completions = append(completions, fullPath)
}
if len(completions) == 1 {
newText = fmt.Sprintf("/%s %s", cmd.OrigCommand, completions[0])
}
return
}

View File

@ -20,7 +20,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math"
"os"
"path/filepath"
@ -34,7 +33,6 @@ import (
"time"
"unicode"
"github.com/gabriel-vasile/mimetype"
"github.com/lucasb-eyer/go-colorful"
"github.com/pkg/errors"
"github.com/russross/blackfriday/v2"
@ -179,40 +177,6 @@ func cmdRedact(cmd *Command) {
cmd.Room.StartSelecting(SelectRedact, strings.Join(cmd.Args, " "))
}
func autocompleteFile(cmd *CommandAutocomplete) (completions []string, newText string) {
inputPath, err := filepath.Abs(cmd.RawArgs)
if err != nil {
return
}
var searchNamePrefix, searchDir string
if strings.HasSuffix(cmd.RawArgs, "/") {
searchDir = inputPath
} else {
searchNamePrefix = filepath.Base(inputPath)
searchDir = filepath.Dir(inputPath)
}
files, err := ioutil.ReadDir(searchDir)
if err != nil {
return
}
for _, file := range files {
name := file.Name()
if !strings.HasPrefix(name, searchNamePrefix) || (name[0] == '.' && searchNamePrefix == "") {
continue
}
fullPath := filepath.Join(searchDir, name)
if file.IsDir() {
fullPath += "/"
}
completions = append(completions, fullPath)
}
if len(completions) == 1 {
newText = fmt.Sprintf("/%s %s", cmd.OrigCommand, completions[0])
}
return
}
func cmdDownload(cmd *Command) {
cmd.Room.StartSelecting(SelectDownload, strings.Join(cmd.Args, " "))
}
@ -229,30 +193,7 @@ func cmdUpload(cmd *Command) {
return
}
ftype, err := mimetype.DetectFile(path)
if err != nil {
cmd.Reply("Failed to get conetnt type: %v", err)
return
}
fi, err := os.Stat(path)
if err != nil {
cmd.Reply("Failed to stat file: %v", err)
return
}
reader, err := os.Open(path)
if err != nil {
cmd.Reply("Failed to open file: %v", err)
return
}
cmd.Room.UploadMedia(mautrix.ReqUploadMedia{
reader,
fi.Size(),
ftype.String(),
filepath.Base(cmd.RawArgs),
})
go cmd.Room.SendMessageMedia(path)
}
func cmdOpen(cmd *Command) {

View File

@ -31,17 +31,16 @@ import (
"maunium.net/go/mauview"
"maunium.net/go/tcell"
"maunium.net/go/gomuks/lib/util"
"maunium.net/go/mautrix/crypto/attachment"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/crypto/attachment"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
"maunium.net/go/gomuks/config"
"maunium.net/go/gomuks/debug"
ifc "maunium.net/go/gomuks/interface"
"maunium.net/go/gomuks/interface"
"maunium.net/go/gomuks/lib/open"
"maunium.net/go/gomuks/lib/util"
"maunium.net/go/gomuks/matrix/muksevt"
"maunium.net/go/gomuks/matrix/rooms"
"maunium.net/go/gomuks/ui/messages"
@ -762,25 +761,46 @@ func (view *RoomView) SendMessage(msgtype event.MessageType, text string) {
view.SendMessageHTML(msgtype, text, "")
}
func (view *RoomView) getRelationForNewEvent() *ifc.Relation {
if view.editing != nil {
return &ifc.Relation{
Type: event.RelReplace,
Event: view.editing,
}
} else if view.replying != nil {
return &ifc.Relation{
Type: event.RelReference,
Event: view.replying,
}
}
return nil
}
func (view *RoomView) SendMessageHTML(msgtype event.MessageType, text, html string) {
defer debug.Recover()
debug.Print("Sending message", msgtype, text, "to", view.Room.ID)
if !view.config.Preferences.DisableEmojis {
text = emoji.Sprint(text)
}
var rel *ifc.Relation
if view.editing != nil {
rel = &ifc.Relation{
Type: event.RelReplace,
Event: view.editing,
}
} else if view.replying != nil {
rel = &ifc.Relation{
Type: event.RelReference,
Event: view.replying,
}
}
rel := view.getRelationForNewEvent()
evt := view.parent.matrix.PrepareMarkdownMessage(view.Room.ID, msgtype, text, html, rel)
view.addLocalEcho(evt)
}
func (view *RoomView) SendMessageMedia(path string) {
defer debug.Recover()
debug.Print("Sending media at", path, "to", view.Room.ID)
rel := view.getRelationForNewEvent()
evt, err := view.parent.matrix.PrepareMediaMessage(view.Room, path, rel)
if err != nil {
view.AddServiceMessage(fmt.Sprintf("Failed to upload media: %v", err))
view.parent.parent.Render()
return
}
view.addLocalEcho(evt)
}
func (view *RoomView) addLocalEcho(evt *muksevt.Event) {
msg := view.parseEvent(evt.SomewhatDangerousCopy())
view.content.AddMessage(msg, AppendMessage)
view.ClearAllContext()
@ -806,20 +826,6 @@ func (view *RoomView) SendMessageHTML(msgtype event.MessageType, text, html stri
}
}
func (view *RoomView) SendImage(body string, url id.ContentURI) {
// evt := view.parent.matrix.SendImage(view.Room.ID, body, url)
}
func (view *RoomView) UploadMedia(data mautrix.ReqUploadMedia) {
contentUri, err := view.parent.matrix.UploadMedia(data)
if err != nil {
view.AddServiceMessage(fmt.Sprintf("Failed to upload file: %v", err))
return
}
view.AddServiceMessage(fmt.Sprintf("ContentURI: %v", contentUri))
}
func (view *RoomView) MessageView() *MessageView {
return view.content
}