mirror of
https://mau.dev/mautrix/go.git
synced 2026-03-14 14:25:53 +01:00
Fix content URI parsing
This commit is contained in:
parent
6790dda123
commit
a14b55c544
3 changed files with 38 additions and 17 deletions
|
|
@ -634,7 +634,7 @@ func (cli *Client) SendImage(roomID id.RoomID, body string, url id.ContentURI) (
|
|||
return cli.SendMessageEvent(roomID, event.EventMessage, event.MessageEventContent{
|
||||
MsgType: event.MsgImage,
|
||||
Body: body,
|
||||
URL: url.CUString(),
|
||||
URL: url,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -644,7 +644,7 @@ func (cli *Client) SendVideo(roomID id.RoomID, body string, url id.ContentURI) (
|
|||
return cli.SendMessageEvent(roomID, event.EventMessage, event.MessageEventContent{
|
||||
MsgType: event.MsgVideo,
|
||||
Body: body,
|
||||
URL: url.CUString(),
|
||||
URL: url,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -71,9 +71,9 @@ type MessageEventContent struct {
|
|||
FormattedBody string `json:"formatted_body,omitempty"`
|
||||
|
||||
// Extra fields for media types
|
||||
URL id.ContentURIString `json:"url,omitempty"`
|
||||
Info *FileInfo `json:"info,omitempty"`
|
||||
File *EncryptedFileInfo `json:"file,omitempty"`
|
||||
URL id.ContentURI `json:"url,omitempty"`
|
||||
Info *FileInfo `json:"info,omitempty"`
|
||||
File *EncryptedFileInfo `json:"file,omitempty"`
|
||||
|
||||
// Edits and relations
|
||||
NewContent *MessageEventContent `json:"m.new_content,omitempty"`
|
||||
|
|
@ -103,24 +103,24 @@ func (content *MessageEventContent) GetInfo() *FileInfo {
|
|||
|
||||
type EncryptedFileInfo struct {
|
||||
attachment.EncryptedFile
|
||||
URL id.ContentURIString
|
||||
URL id.ContentURI
|
||||
}
|
||||
|
||||
type FileInfo struct {
|
||||
MimeType string `json:"mimetype,omitempty"`
|
||||
ThumbnailInfo *FileInfo `json:"thumbnail_info,omitempty"`
|
||||
ThumbnailURL id.ContentURIString `json:"thumbnail_url,omitempty"`
|
||||
ThumbnailFile *EncryptedFileInfo `json:"thumbnail_file,omitempty"`
|
||||
Width int `json:"-"`
|
||||
Height int `json:"-"`
|
||||
MimeType string `json:"mimetype,omitempty"`
|
||||
ThumbnailInfo *FileInfo `json:"thumbnail_info,omitempty"`
|
||||
ThumbnailURL id.ContentURI `json:"thumbnail_url,omitempty"`
|
||||
ThumbnailFile *EncryptedFileInfo `json:"thumbnail_file,omitempty"`
|
||||
Width int `json:"-"`
|
||||
Height int `json:"-"`
|
||||
Duration int `json:"-"`
|
||||
Size int `json:"-"`
|
||||
Size int `json:"-"`
|
||||
}
|
||||
|
||||
type serializableFileInfo struct {
|
||||
MimeType string `json:"mimetype,omitempty"`
|
||||
ThumbnailInfo *serializableFileInfo `json:"thumbnail_info,omitempty"`
|
||||
ThumbnailURL id.ContentURIString `json:"thumbnail_url,omitempty"`
|
||||
ThumbnailURL id.ContentURI `json:"thumbnail_url,omitempty"`
|
||||
|
||||
Width json.Number `json:"w,omitempty"`
|
||||
Height json.Number `json:"h,omitempty"`
|
||||
|
|
|
|||
|
|
@ -7,12 +7,16 @@
|
|||
package id
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var InvalidContentURI = errors.New("invalid Matrix content URI")
|
||||
var (
|
||||
InvalidContentURI = errors.New("invalid Matrix content URI")
|
||||
InputNotJSONString = errors.New("input doesn't look like a JSON string")
|
||||
)
|
||||
|
||||
// ContentURIString is a string that's expected to be a Matrix content URI.
|
||||
// It's useful for delaying the parsing of the content URI to move errors from the event content
|
||||
|
|
@ -51,8 +55,25 @@ func ParseContentURI(uri string) (parsed ContentURI, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
var mxcBytes = []byte("mxc://")
|
||||
|
||||
func ParseContentURIBytes(uri []byte) (parsed ContentURI, err error) {
|
||||
if !bytes.HasPrefix(uri, mxcBytes) {
|
||||
err = InvalidContentURI
|
||||
} else if index := bytes.IndexRune(uri[6:], '/'); index == -1 || index == len(uri)-7 {
|
||||
err = InvalidContentURI
|
||||
} else {
|
||||
parsed.Homeserver = string(uri[6 : 6+index])
|
||||
parsed.FileID = string(uri[6+index+1:])
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (uri *ContentURI) UnmarshalJSON(raw []byte) (err error) {
|
||||
parsed, err := ParseContentURI(string(raw))
|
||||
if len(raw) < 2 || raw[0] != '"' || raw[len(raw)-1] != '"' {
|
||||
return InputNotJSONString
|
||||
}
|
||||
parsed, err := ParseContentURIBytes(raw[1:len(raw)-1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -74,4 +95,4 @@ func (uri *ContentURI) CUString() ContentURIString {
|
|||
|
||||
func (uri *ContentURI) IsEmpty() bool {
|
||||
return len(uri.Homeserver) == 0 || len(uri.FileID) == 0
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue