|
| 1 | +package models |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// MatrixToAcrobitsAttachment converts Matrix media event content to an Acrobits Attachment. |
| 11 | +// It handles m.image, m.video, m.audio, and m.file message types. |
| 12 | +func MatrixToAcrobitsAttachment(msgType, body, url, mimeType, filename string, size int64, thumbnailURL, thumbnailMimeType string, thumbnailData []byte) *Attachment { |
| 13 | + attachment := &Attachment{ |
| 14 | + ContentURL: url, |
| 15 | + ContentType: mimeType, |
| 16 | + ContentSize: size, |
| 17 | + Filename: filename, |
| 18 | + Description: body, |
| 19 | + } |
| 20 | + |
| 21 | + // If filename is empty but body is present, use body as filename for non-text content |
| 22 | + if attachment.Filename == "" && body != "" { |
| 23 | + attachment.Filename = body |
| 24 | + } |
| 25 | + |
| 26 | + // Set default content type based on message type if not provided |
| 27 | + if attachment.ContentType == "" { |
| 28 | + switch msgType { |
| 29 | + case "m.image": |
| 30 | + attachment.ContentType = "image/jpeg" |
| 31 | + case "m.video": |
| 32 | + attachment.ContentType = "video/mp4" |
| 33 | + case "m.audio": |
| 34 | + attachment.ContentType = "audio/mpeg" |
| 35 | + default: |
| 36 | + attachment.ContentType = "application/octet-stream" |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + // Add preview/thumbnail if available |
| 41 | + if thumbnailURL != "" || len(thumbnailData) > 0 { |
| 42 | + preview := &AttachmentPreview{} |
| 43 | + if thumbnailMimeType != "" { |
| 44 | + preview.ContentType = thumbnailMimeType |
| 45 | + } else { |
| 46 | + preview.ContentType = "image/jpeg" |
| 47 | + } |
| 48 | + // If we have thumbnail data, encode it as base64 |
| 49 | + // If we only have a URL, store the URL in Content field (client will need to fetch it) |
| 50 | + if len(thumbnailData) > 0 { |
| 51 | + preview.Content = base64.StdEncoding.EncodeToString(thumbnailData) |
| 52 | + } else if thumbnailURL != "" { |
| 53 | + // Store URL as a special marker for clients to fetch |
| 54 | + // This is a workaround since Acrobits expects base64 content |
| 55 | + preview.Content = thumbnailURL |
| 56 | + } |
| 57 | + attachment.Preview = preview |
| 58 | + } |
| 59 | + |
| 60 | + return attachment |
| 61 | +} |
| 62 | + |
| 63 | +// MatrixMediaToFileTransfer converts a Matrix media message to an Acrobits FileTransferMessage. |
| 64 | +// Returns the JSON-encoded file transfer message suitable for sms_text field. |
| 65 | +func MatrixMediaToFileTransfer(msgType, body, url, mimeType, filename string, size int64, thumbnailURL, thumbnailMimeType string) (string, error) { |
| 66 | + attachment := MatrixToAcrobitsAttachment(msgType, body, url, mimeType, filename, size, thumbnailURL, thumbnailMimeType, nil) |
| 67 | + |
| 68 | + ftMsg := &FileTransferMessage{ |
| 69 | + Body: body, |
| 70 | + Attachments: []Attachment{*attachment}, |
| 71 | + } |
| 72 | + |
| 73 | + jsonData, err := json.Marshal(ftMsg) |
| 74 | + if err != nil { |
| 75 | + return "", fmt.Errorf("failed to marshal file transfer message: %w", err) |
| 76 | + } |
| 77 | + |
| 78 | + return string(jsonData), nil |
| 79 | +} |
| 80 | + |
| 81 | +// ParseFileTransferMessage parses a JSON-encoded file transfer message. |
| 82 | +func ParseFileTransferMessage(data string) (*FileTransferMessage, error) { |
| 83 | + var ftMsg FileTransferMessage |
| 84 | + if err := json.Unmarshal([]byte(data), &ftMsg); err != nil { |
| 85 | + return nil, fmt.Errorf("failed to parse file transfer message: %w", err) |
| 86 | + } |
| 87 | + return &ftMsg, nil |
| 88 | +} |
| 89 | + |
| 90 | +// FileTransferToMatrixEventContent converts an Acrobits file transfer message to Matrix event content. |
| 91 | +// Returns the message type, body, and a map suitable for Matrix event content. |
| 92 | +// For messages with multiple attachments, only the first attachment is used (Matrix doesn't support multiple files in one event). |
| 93 | +func FileTransferToMatrixEventContent(ftMsg *FileTransferMessage) (msgType string, content map[string]interface{}, err error) { |
| 94 | + if ftMsg == nil || len(ftMsg.Attachments) == 0 { |
| 95 | + return "", nil, fmt.Errorf("file transfer message has no attachments") |
| 96 | + } |
| 97 | + |
| 98 | + // Use the first attachment |
| 99 | + att := ftMsg.Attachments[0] |
| 100 | + |
| 101 | + // Determine Matrix message type based on content type |
| 102 | + contentType := att.ContentType |
| 103 | + if contentType == "" { |
| 104 | + contentType = "image/jpeg" // Default per Acrobits spec |
| 105 | + } |
| 106 | + |
| 107 | + switch { |
| 108 | + case IsImageContentType(contentType): |
| 109 | + msgType = "m.image" |
| 110 | + case IsVideoContentType(contentType): |
| 111 | + msgType = "m.video" |
| 112 | + case IsAudioContentType(contentType): |
| 113 | + msgType = "m.audio" |
| 114 | + default: |
| 115 | + msgType = "m.file" |
| 116 | + } |
| 117 | + |
| 118 | + // Build the message body |
| 119 | + body := att.Filename |
| 120 | + if body == "" { |
| 121 | + body = ftMsg.Body |
| 122 | + } |
| 123 | + if body == "" { |
| 124 | + body = "attachment" |
| 125 | + } |
| 126 | + |
| 127 | + // Build Matrix event content |
| 128 | + content = map[string]interface{}{ |
| 129 | + "msgtype": msgType, |
| 130 | + "body": body, |
| 131 | + "url": att.ContentURL, |
| 132 | + } |
| 133 | + |
| 134 | + // Add info block |
| 135 | + info := map[string]interface{}{} |
| 136 | + if contentType != "" { |
| 137 | + info["mimetype"] = contentType |
| 138 | + } |
| 139 | + if att.ContentSize > 0 { |
| 140 | + info["size"] = att.ContentSize |
| 141 | + } |
| 142 | + |
| 143 | + // Add thumbnail info if available |
| 144 | + if att.Preview != nil && att.Preview.Content != "" { |
| 145 | + // If preview content looks like a URL, use it as thumbnail_url |
| 146 | + if strings.HasPrefix(att.Preview.Content, "http://") || strings.HasPrefix(att.Preview.Content, "https://") || strings.HasPrefix(att.Preview.Content, "mxc://") { |
| 147 | + info["thumbnail_url"] = att.Preview.Content |
| 148 | + if att.Preview.ContentType != "" { |
| 149 | + info["thumbnail_info"] = map[string]interface{}{ |
| 150 | + "mimetype": att.Preview.ContentType, |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + // Note: Base64 preview content cannot be directly used in Matrix events |
| 155 | + // The client would need to upload it first |
| 156 | + } |
| 157 | + |
| 158 | + if len(info) > 0 { |
| 159 | + content["info"] = info |
| 160 | + } |
| 161 | + |
| 162 | + // Add filename if available |
| 163 | + if att.Filename != "" { |
| 164 | + content["filename"] = att.Filename |
| 165 | + } |
| 166 | + |
| 167 | + return msgType, content, nil |
| 168 | +} |
| 169 | + |
| 170 | +// IsFileTransferContentType checks if the content type is the Acrobits file transfer type. |
| 171 | +func IsFileTransferContentType(contentType string) bool { |
| 172 | + return contentType == FileTransferContentType |
| 173 | +} |
| 174 | + |
| 175 | +// ExtractMatrixMediaInfo extracts media information from Matrix event content. |
| 176 | +// Returns the content URL, mimetype, filename, size, thumbnail URL, and thumbnail mimetype. |
| 177 | +func ExtractMatrixMediaInfo(raw map[string]interface{}) (url, mimeType, filename string, size int64, thumbnailURL, thumbnailMimeType string) { |
| 178 | + // Extract URL |
| 179 | + if u, ok := raw["url"].(string); ok { |
| 180 | + url = u |
| 181 | + } |
| 182 | + |
| 183 | + // Extract filename |
| 184 | + if f, ok := raw["filename"].(string); ok { |
| 185 | + filename = f |
| 186 | + } |
| 187 | + |
| 188 | + // Extract info block |
| 189 | + if info, ok := raw["info"].(map[string]interface{}); ok { |
| 190 | + if mt, ok := info["mimetype"].(string); ok { |
| 191 | + mimeType = mt |
| 192 | + } |
| 193 | + if s, ok := info["size"].(float64); ok { |
| 194 | + size = int64(s) |
| 195 | + } |
| 196 | + // Extract thumbnail info |
| 197 | + if tu, ok := info["thumbnail_url"].(string); ok { |
| 198 | + thumbnailURL = tu |
| 199 | + } |
| 200 | + if ti, ok := info["thumbnail_info"].(map[string]interface{}); ok { |
| 201 | + if tm, ok := ti["mimetype"].(string); ok { |
| 202 | + thumbnailMimeType = tm |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + return |
| 208 | +} |
0 commit comments