Skip to content

Commit 7ed9cfd

Browse files
committed
fix
1 parent 00b4a95 commit 7ed9cfd

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

service/messages.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ func (s *MessageService) SendMessage(ctx context.Context, req *models.SendMessag
240240

241241
matrixURL := ""
242242
uploadSuccess := true
243+
fileSize := 0
244+
243245
if acrobitsURL != "" {
244246
logger.Debug().Str("content_url", acrobitsURL).Msg("downloading attachment from Acrobits")
245247
fileData, err := s.downloadFile(ctx, acrobitsURL)
@@ -252,8 +254,31 @@ func (s *MessageService) SendMessage(ctx context.Context, req *models.SendMessag
252254
}
253255
uploadSuccess = false
254256
} else {
257+
fileSize = len(fileData)
258+
259+
// Detect content type from file data to ensure correct display
260+
detectedMime := http.DetectContentType(fileData)
261+
// Strip parameters (e.g. "; charset=utf-8")
262+
if idx := strings.Index(detectedMime, ";"); idx != -1 {
263+
detectedMime = detectedMime[:idx]
264+
}
265+
266+
// Use detected mime if it's an image or if original is missing/generic
267+
if strings.HasPrefix(detectedMime, "image/") || mimetype == "" || mimetype == "application/octet-stream" {
268+
mimetype = detectedMime
269+
270+
// Update msgType based on new mimetype
271+
if models.IsImageContentType(mimetype) {
272+
msgType = "m.image"
273+
} else if models.IsVideoContentType(mimetype) {
274+
msgType = "m.video"
275+
} else if models.IsAudioContentType(mimetype) {
276+
msgType = "m.audio"
277+
}
278+
}
279+
255280
// Upload to Matrix content repository
256-
logger.Debug().Str("content_url", acrobitsURL).Int("size", len(fileData)).Msg("uploading attachment to Matrix content repository")
281+
logger.Debug().Str("content_url", acrobitsURL).Int("size", fileSize).Str("mimetype", mimetype).Msg("uploading attachment to Matrix content repository")
257282
uploadedURL, err := s.matrixClient.UploadMedia(ctx, senderMatrix, mimetype, fileData)
258283
if err != nil {
259284
logger.Warn().Err(err).Str("content_url", acrobitsURL).Msg("failed to upload attachment to Matrix, falling back to text message")
@@ -290,12 +315,17 @@ func (s *MessageService) SendMessage(ctx context.Context, req *models.SendMessag
290315
// Set info block if present
291316
if info, ok := rawContent["info"].(map[string]interface{}); ok {
292317
content.Info = &event.FileInfo{}
293-
if mimetype, ok := info["mimetype"].(string); ok {
294-
content.Info.MimeType = mimetype
295-
}
296-
if size, ok := info["size"].(int64); ok {
318+
319+
// Use the resolved mimetype
320+
content.Info.MimeType = mimetype
321+
322+
// Use actual file size if available, otherwise fallback to info
323+
if fileSize > 0 {
324+
content.Info.Size = fileSize
325+
} else if size, ok := info["size"].(int64); ok {
297326
content.Info.Size = int(size)
298327
}
328+
299329
// Handle thumbnail info
300330
if thumbnailURL, ok := info["thumbnail_url"].(string); ok {
301331
content.Info.ThumbnailURL = id.ContentURIString(thumbnailURL)

0 commit comments

Comments
 (0)