gomuks/ui/messages/filemessage.go

191 lines
5.1 KiB
Go
Raw Normal View History

// gomuks - A terminal Matrix client written in Go.
2020-04-19 17:10:14 +02:00
// Copyright (C) 2020 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
2019-01-17 13:13:25 +01:00
// 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
2019-01-17 13:13:25 +01:00
// GNU Affero General Public License for more details.
//
2019-01-17 13:13:25 +01:00
// 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 messages
import (
"bytes"
"fmt"
"image"
2019-01-17 13:13:25 +01:00
"image/color"
2020-04-29 01:45:54 +02:00
"maunium.net/go/mautrix/crypto/attachment"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
2022-04-15 11:53:09 +02:00
"go.mau.fi/mauview"
"go.mau.fi/tcell"
2018-06-01 23:44:21 +02:00
"maunium.net/go/gomuks/config"
"maunium.net/go/gomuks/debug"
2022-04-15 11:53:09 +02:00
ifc "maunium.net/go/gomuks/interface"
"maunium.net/go/gomuks/lib/ansimage"
2020-04-29 01:45:54 +02:00
"maunium.net/go/gomuks/matrix/muksevt"
"maunium.net/go/gomuks/ui/messages/tstring"
)
type FileMessage struct {
2020-04-29 01:45:54 +02:00
Type event.MessageType
Body string
URL id.ContentURI
File *attachment.EncryptedFile
Thumbnail id.ContentURI
ThumbnailFile *attachment.EncryptedFile
2022-04-15 21:30:07 +02:00
eventID id.EventID
imageData []byte
buffer []tstring.TString
matrix ifc.MatrixContainer
}
// NewFileMessage creates a new FileMessage object with the provided values and the default state.
func NewFileMessage(matrix ifc.MatrixContainer, evt *muksevt.Event, displayname string) *UIMessage {
2020-04-19 14:00:49 +02:00
content := evt.Content.AsMessage()
2020-04-29 01:45:54 +02:00
var file, thumbnailFile *attachment.EncryptedFile
if content.File != nil {
file = &content.File.EncryptedFile
content.URL = content.File.URL
}
if content.GetInfo().ThumbnailFile != nil {
thumbnailFile = &content.Info.ThumbnailFile.EncryptedFile
content.Info.ThumbnailURL = content.Info.ThumbnailFile.URL
}
return newUIMessage(evt, displayname, &FileMessage{
2020-04-29 01:45:54 +02:00
Type: content.MsgType,
Body: content.Body,
URL: content.URL.ParseOrIgnore(),
2020-04-29 01:45:54 +02:00
File: file,
Thumbnail: content.GetInfo().ThumbnailURL.ParseOrIgnore(),
2020-04-29 01:45:54 +02:00
ThumbnailFile: thumbnailFile,
2022-04-15 21:30:07 +02:00
eventID: evt.ID,
2020-04-29 01:45:54 +02:00
matrix: matrix,
2019-06-15 00:11:51 +02:00
})
}
func (msg *FileMessage) Clone() MessageRenderer {
data := make([]byte, len(msg.imageData))
copy(data, msg.imageData)
return &FileMessage{
Body: msg.Body,
URL: msg.URL,
Thumbnail: msg.Thumbnail,
imageData: data,
matrix: msg.matrix,
}
}
func (msg *FileMessage) NotificationContent() string {
switch msg.Type {
case event.MsgImage:
return "Sent an image"
case event.MsgAudio:
return "Sent an audio file"
case event.MsgVideo:
return "Sent a video"
case event.MsgFile:
fallthrough
default:
return "Sent a file"
}
}
func (msg *FileMessage) PlainText() string {
return fmt.Sprintf("%s: %s", msg.Body, msg.matrix.GetDownloadURL(msg.URL))
}
func (msg *FileMessage) String() string {
return fmt.Sprintf(`&messages.FileMessage{Body="%s", URL="%s", Thumbnail="%s"}`, msg.Body, msg.URL, msg.Thumbnail)
2019-06-15 00:11:51 +02:00
}
func (msg *FileMessage) DownloadPreview() {
2020-04-29 01:45:54 +02:00
var url id.ContentURI
var file *attachment.EncryptedFile
if !msg.Thumbnail.IsEmpty() {
url = msg.Thumbnail
file = msg.ThumbnailFile
} else if msg.Type == event.MsgImage && !msg.URL.IsEmpty() {
msg.Thumbnail = msg.URL
url = msg.URL
file = msg.File
} else {
return
}
debug.Print("Loading file:", url)
2020-04-29 01:45:54 +02:00
data, err := msg.matrix.Download(url, file)
if err != nil {
debug.Printf("Failed to download file %s: %v", url, err)
return
}
debug.Print("File", url, "loaded.")
msg.imageData = data
}
func (msg *FileMessage) ThumbnailPath() string {
return msg.matrix.GetCachePath(msg.Thumbnail)
}
func (msg *FileMessage) CalculateBuffer(prefs config.UserPreferences, width int, uiMsg *UIMessage) {
if width < 2 {
return
}
if prefs.BareMessageView || prefs.DisableImages || len(msg.imageData) == 0 {
2022-04-15 21:30:07 +02:00
url := msg.matrix.GetDownloadURL(msg.URL)
var urlTString tstring.TString
if prefs.EnableInlineURLs() {
2022-11-13 14:34:21 +01:00
urlTString = tstring.NewStyleTString(url, tcell.StyleDefault.Url(url).UrlId(msg.eventID.String()))
} else {
urlTString = tstring.NewTString(url)
}
2022-04-15 21:30:07 +02:00
text := tstring.NewTString(msg.Body).
Append(": ").
AppendTString(urlTString)
2022-04-15 21:30:07 +02:00
msg.buffer = calculateBufferWithText(prefs, text, width, uiMsg)
return
}
img, _, err := image.DecodeConfig(bytes.NewReader(msg.imageData))
if err != nil {
debug.Print("File could not be decoded:", err)
}
imgWidth := img.Width
if img.Width > width {
imgWidth = width / 3
}
ansFile, err := ansimage.NewScaledFromReader(bytes.NewReader(msg.imageData), 0, imgWidth, color.Black)
if err != nil {
msg.buffer = []tstring.TString{tstring.NewColorTString("Failed to display image", tcell.ColorRed)}
debug.Print("Failed to display image:", err)
return
}
msg.buffer = ansFile.Render()
}
2019-06-15 00:11:51 +02:00
func (msg *FileMessage) Height() int {
2019-06-15 00:11:51 +02:00
return len(msg.buffer)
}
func (msg *FileMessage) Draw(screen mauview.Screen, _ *UIMessage) {
2019-06-15 00:11:51 +02:00
for y, line := range msg.buffer {
line.Draw(screen, 0, y)
}
}