Skip to content

Commit 8f2fb81

Browse files
committed
fix
1 parent 7ed9cfd commit 8f2fb81

File tree

3 files changed

+109
-3
lines changed

3 files changed

+109
-3
lines changed

matrix/client.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,26 @@ func (mc *MatrixClient) SetPusher(ctx context.Context, userID id.UserID, req *mo
281281
Msg("matrix: pusher set successfully")
282282
return nil
283283
}
284+
285+
// ResolveMXC converts an MXC URI (mxc://server/mediaId) to a downloadable HTTP URL.
286+
// It uses the configured homeserver URL to construct the download link.
287+
func (mc *MatrixClient) ResolveMXC(mxcURI string) string {
288+
if !strings.HasPrefix(mxcURI, "mxc://") {
289+
return mxcURI
290+
}
291+
292+
// Parse the MXC URI
293+
// Format: mxc://<server-name>/<media-id>
294+
trimmed := strings.TrimPrefix(mxcURI, "mxc://")
295+
parts := strings.SplitN(trimmed, "/", 2)
296+
if len(parts) != 2 {
297+
return mxcURI
298+
}
299+
serverName := parts[0]
300+
mediaID := parts[1]
301+
302+
// Construct the download URL
303+
// Format: /_matrix/media/v3/download/<server-name>/<media-id>
304+
baseURL := strings.TrimSuffix(mc.homeserverURL, "/")
305+
return fmt.Sprintf("%s/_matrix/media/v3/download/%s/%s", baseURL, serverName, mediaID)
306+
}

models/filetransfer_convert.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ func FileTransferToMatrixEventContent(ftMsg *FileTransferMessage) (msgType strin
131131
"url": att.ContentURL,
132132
}
133133

134+
// Include filename when present so clients can render proper disposition
135+
if att.Filename != "" {
136+
content["filename"] = att.Filename
137+
}
138+
134139
// Add info block
135140
info := map[string]interface{}{}
136141
if contentType != "" {

service/messages.go

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,18 +441,96 @@ func (s *MessageService) FetchMessages(ctx context.Context, req *models.FetchMes
441441

442442
logger.Debug().Str("event_id", string(evt.ID)).Str("room_id", string(eventRoomID)).Msg("processing message event")
443443

444-
body := ""
444+
// Extract content fields
445+
var msgType string
446+
var body string
447+
var url string
448+
var info map[string]interface{}
449+
var filename string
450+
451+
if t, ok := evt.Content.Raw["msgtype"].(string); ok {
452+
msgType = t
453+
}
445454
if b, ok := evt.Content.Raw["body"].(string); ok {
446455
body = b
447456
}
457+
if fn, ok := evt.Content.Raw["filename"].(string); ok {
458+
filename = fn
459+
}
460+
if u, ok := evt.Content.Raw["url"].(string); ok {
461+
url = u
462+
}
463+
if i, ok := evt.Content.Raw["info"].(map[string]interface{}); ok {
464+
info = i
465+
}
466+
448467
sms := models.SMS{
449468
SMSID: string(evt.ID),
450469
SendingDate: time.UnixMilli(evt.Timestamp).UTC().Format(time.RFC3339),
451-
SMSText: body,
452-
ContentType: "text/plain",
453470
StreamID: string(roomID),
454471
}
455472

473+
// Check if it's a media message
474+
if msgType == "m.image" || msgType == "m.video" || msgType == "m.audio" || msgType == "m.file" {
475+
// Convert to Acrobits file transfer format
476+
// Resolve MXC URI to HTTP URL
477+
httpURL := s.matrixClient.ResolveMXC(url)
478+
if httpURL == "" {
479+
logger.Warn().Str("event_id", string(evt.ID)).Msg("media event missing URL, falling back to text")
480+
sms.SMSText = body
481+
sms.ContentType = "text/plain"
482+
} else {
483+
// Extract metadata
484+
mimetype := ""
485+
var size int64
486+
thumbnailURL := ""
487+
thumbnailMime := ""
488+
489+
if info != nil {
490+
if m, ok := info["mimetype"].(string); ok {
491+
mimetype = m
492+
}
493+
if sVal, ok := info["size"].(float64); ok { // JSON numbers are float64
494+
size = int64(sVal)
495+
} else if sVal, ok := info["size"].(int64); ok {
496+
size = sVal
497+
} else if sVal, ok := info["size"].(int); ok {
498+
size = int64(sVal)
499+
}
500+
if tURL, ok := info["thumbnail_url"].(string); ok {
501+
thumbnailURL = s.matrixClient.ResolveMXC(tURL)
502+
}
503+
if tInfo, ok := info["thumbnail_info"].(map[string]interface{}); ok {
504+
if tm, ok := tInfo["mimetype"].(string); ok {
505+
thumbnailMime = tm
506+
}
507+
}
508+
}
509+
510+
// Convert to JSON
511+
// Prefer explicit filename if present; fall back to body
512+
effectiveFilename := filename
513+
if effectiveFilename == "" {
514+
effectiveFilename = body
515+
}
516+
517+
ftJSON, err := models.MatrixMediaToFileTransfer(msgType, body, httpURL, mimetype, effectiveFilename, size, thumbnailURL, thumbnailMime)
518+
if err != nil {
519+
logger.Warn().Err(err).Msg("failed to convert matrix media to file transfer")
520+
// Fallback to text
521+
sms.SMSText = body
522+
sms.ContentType = "text/plain"
523+
} else {
524+
sms.SMSText = ftJSON
525+
sms.ContentType = models.FileTransferContentType
526+
}
527+
}
528+
} else {
529+
// Regular text message
530+
sms.SMSText = body
531+
sms.ContentType = "text/plain"
532+
}
533+
456534
// Determine if I sent the message
457535
senderMatrixID := string(evt.Sender)
458536
isSent := isSentBy(senderMatrixID, string(userID))

0 commit comments

Comments
 (0)