// Copyright (c) 2026 Xessenger // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. package bridgekit import ( "bytes" "context" "encoding/json" "fmt" "path" "strings" "github.com/rs/zerolog" "maunium.net/go/mautrix/bridgev2/matrix" "maunium.net/go/mautrix/crypto/attachment" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" ) // pictureRecord is what the bridge keeps about a picture bridgev2 knows by its MXC. type pictureRecord struct { // File is the picture itself when it was uploaded encrypted (File.URL is then the MXC). One with // another URL is a copy every encrypted room once shared, which nothing reads any more. File *event.EncryptedFileInfo `json:"file,omitempty"` Mime string `json:"mime,omitempty"` // Plain is a plaintext copy of a picture uploaded encrypted, made for a room that is not. Plain id.ContentURIString `json:"plain,omitempty"` } // picture returns the record kept about a picture bridgev2 knows by mxc, nil when there is none. The // record holds the keys of encrypted copies, so the store keeps it sealed. func (k *Kit) picture(ctx context.Context, mxc id.ContentURIString) (*pictureRecord, error) { data, err := k.store.picture(ctx, mxc) if err != nil || data == nil { return nil, err } var record pictureRecord if err = json.Unmarshal(data, &record); err != nil { return nil, err } return &record, nil } func (k *Kit) putPicture(ctx context.Context, mxc id.ContentURIString, record *pictureRecord) error { data, err := json.Marshal(record) if err != nil { return err } return k.store.putPicture(ctx, mxc, data) } // pictureUpload reports whether an upload without a room is a profile or room picture: bridgev2 names // those "avatar" with the picture's extension (Avatar.Reupload). Anything else uploaded without a room // is meant to be read without a key, as Matrix has it, like LINE's reaction icons and inline emoji. func pictureUpload(fileName string) bool { return strings.TrimSuffix(fileName, path.Ext(fileName)) == "avatar" } // uploadEncryptedPicture uploads a picture encrypted, as the bridge bot so that the upload does not // say whose picture it is, and returns the encrypted file's MXC for bridgev2 to keep. func (k *Kit) uploadEncryptedPicture(ctx context.Context, data []byte, mimeType string) (id.ContentURIString, *event.EncryptedFileInfo, error) { file, err := k.encryptAndUpload(ctx, data) if err != nil { return "", nil, err } if err = k.putPicture(ctx, file.URL, &pictureRecord{File: file, Mime: mimeType}); err != nil { return "", nil, err } return file.URL, nil, nil } func (k *Kit) encryptAndUpload(ctx context.Context, data []byte) (*event.EncryptedFileInfo, error) { ciphertext := bytes.Clone(data) file := attachment.NewEncryptedFile() file.EncryptInPlace(ciphertext) resp, err := k.conn.Bot.UploadBytes(ctx, ciphertext, "application/octet-stream") if err != nil { return nil, fmt.Errorf("failed to upload an encrypted file: %w", err) } return &event.EncryptedFileInfo{EncryptedFile: *file, URL: resp.ContentURI.CUString()}, nil } // slotPicture returns the encrypted copy of a picture bridgev2 knows by source that a room's encrypted // state slot shows (meta.go), nil for none: the copy the slot was given last when it is of the same // picture, otherwise a new copy made for the slot alone. A copy is never given to another slot, nor // again once its slot was given another picture or none: the API deletes a copy once no row names it // (mediarefs.go), and may still be deleting one a slot let go of while the homeserver serves it. The // picture itself is never shown, so the API never deletes bridgev2's picture, which the network gives // again only once it changes. A slot whose picture cannot be copied shows none. func (k *Kit) slotPicture(ctx context.Context, roomID id.RoomID, slot string, source id.ContentURIString) (*event.EncryptedFileInfo, error) { if source != "" { kept, data, err := k.store.slotPicture(ctx, roomID, slot) if err != nil { return nil, err } else if kept == source { var file event.EncryptedFileInfo if err = json.Unmarshal(data, &file); err != nil { return nil, err } return &file, nil } file, err := k.copyPicture(ctx, source) if err == nil { if data, err = json.Marshal(file); err == nil { err = k.store.putSlotPicture(ctx, roomID, slot, source, data) } return file, err } zerolog.Ctx(ctx).Err(err).Stringer("room_id", roomID).Msg("Failed to copy a picture for encrypted metadata, sending it without one") } return nil, k.store.deleteSlotPicture(ctx, roomID, slot) } // copyPicture makes an encrypted copy of a picture bridgev2 knows by mxc, from the picture itself // (decrypted when it was uploaded encrypted). func (k *Kit) copyPicture(ctx context.Context, mxc id.ContentURIString) (*event.EncryptedFileInfo, error) { record, err := k.picture(ctx, mxc) if err != nil { return nil, err } uri, err := mxc.Parse() if err != nil { return nil, err } data, err := k.conn.Bot.DownloadBytes(ctx, uri) if err != nil { return nil, fmt.Errorf("failed to download picture: %w", err) } if record != nil && record.File != nil && record.File.URL == mxc { if err = record.File.DecryptInPlace(data); err != nil { return nil, fmt.Errorf("failed to decrypt picture: %w", err) } } return k.encryptAndUpload(ctx, data) } // plaintextPicture returns what a room that is not encrypted shows for a picture bridgev2 knows by // mxc: mxc itself, unless the picture was uploaded encrypted, in which case a plaintext copy made // once and uploaded as intent. func (k *Kit) plaintextPicture(ctx context.Context, intent *matrix.ASIntent, mxc id.ContentURIString) (id.ContentURIString, error) { if mxc == "" || !k.box.Ready() { return mxc, nil } record, err := k.picture(ctx, mxc) if err != nil { return "", err } else if record == nil || record.File == nil || record.File.URL != mxc { return mxc, nil } else if record.Plain != "" { return record.Plain, nil } uri, err := mxc.Parse() if err != nil { return "", err } data, err := k.conn.Bot.DownloadBytes(ctx, uri) if err != nil { return "", fmt.Errorf("failed to download encrypted picture: %w", err) } if err = record.File.DecryptInPlace(data); err != nil { return "", fmt.Errorf("failed to decrypt picture: %w", err) } mimeType := record.Mime if mimeType == "" { mimeType = "application/octet-stream" } resp, err := intent.Matrix.UploadBytes(ctx, data, mimeType) if err != nil { return "", fmt.Errorf("failed to upload plaintext copy of picture: %w", err) } record.Plain = resp.ContentURI.CUString() if err = k.putPicture(ctx, mxc, record); err != nil { return "", err } return record.Plain, nil }