Skip to content

Commit

Permalink
zerolog: remove instances of using global log
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <[email protected]>
  • Loading branch information
sumnerevans committed Dec 30, 2023
1 parent 03b7c38 commit f526e90
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
12 changes: 5 additions & 7 deletions pkg/signalmeow/attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package signalmeow

import (
"bytes"
"context"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
Expand All @@ -28,8 +29,7 @@ import (
"io"
"math"

"github.com/rs/zerolog/log"

"github.com/rs/zerolog"
signalpb "go.mau.fi/mautrix-signal/pkg/signalmeow/protobuf"
"go.mau.fi/mautrix-signal/pkg/signalmeow/web"
)
Expand Down Expand Up @@ -101,7 +101,8 @@ type attachmentV3UploadAttributes struct {
SignedUploadLocation string `json:"signedUploadLocation"`
}

func encryptAndUploadAttachment(device *Device, body []byte, mimeType, filename string) (*signalpb.AttachmentPointer, error) {
func encryptAndUploadAttachment(ctx context.Context, device *Device, body []byte, mimeType, filename string) (*signalpb.AttachmentPointer, error) {
log := zerolog.Ctx(ctx)
keys := make([]byte, 64) // combined AES and MAC keys
randBytes(keys)
plaintextLength := uint32(len(body))
Expand Down Expand Up @@ -208,10 +209,7 @@ func aesDecrypt(key, ciphertext []byte) ([]byte, error) {
}

if len(ciphertext)%aes.BlockSize != 0 {
log.Debug().
Int("length", len(ciphertext)%aes.BlockSize).
Msg("aesDecrypt ciphertext not multiple of AES blocksize")
return nil, errors.New("ciphertext not multiple of AES blocksize")
return nil, fmt.Errorf("ciphertext not multiple of AES blocksize (%d extra bytes)", len(ciphertext)%aes.BlockSize)
}

iv := ciphertext[:aes.BlockSize]
Expand Down
4 changes: 2 additions & 2 deletions pkg/signalmeow/sending.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,8 @@ func AddExpiryToDataMessage(content *SignalContent, expiresInSeconds uint32) {
content.DataMessage.ExpireTimer = proto.Uint32(expiresInSeconds)
}

func UploadAttachment(d *Device, image []byte, mimeType string, filename string) (*AttachmentPointer, error) {
ap, err := encryptAndUploadAttachment(d, image, mimeType, filename)
func UploadAttachment(ctx context.Context, d *Device, image []byte, mimeType string, filename string) (*AttachmentPointer, error) {
ap, err := encryptAndUploadAttachment(ctx, d, image, mimeType, filename)
return (*AttachmentPointer)(ap), err
}

Expand Down
10 changes: 5 additions & 5 deletions portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ func (portal *Portal) convertMatrixMessage(ctx context.Context, sender *User, ev
if err != nil {
return nil, err
}
attachmentPointer, err := signalmeow.UploadAttachment(sender.SignalDevice, convertedImage, newMimeType, fileName)
attachmentPointer, err := signalmeow.UploadAttachment(ctx, sender.SignalDevice, convertedImage, newMimeType, fileName)
if err != nil {
return nil, err
}
Expand All @@ -727,7 +727,7 @@ func (portal *Portal) convertMatrixMessage(ctx context.Context, sender *User, ev
if err != nil {
return nil, err
}
attachmentPointer, err := signalmeow.UploadAttachment(sender.SignalDevice, convertedSticker, newMimeType, content.FileName)
attachmentPointer, err := signalmeow.UploadAttachment(ctx, sender.SignalDevice, convertedSticker, newMimeType, content.FileName)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -765,7 +765,7 @@ func (portal *Portal) convertMatrixMessage(ctx context.Context, sender *User, ev
if err != nil {
return nil, err
}
attachmentPointer, err := signalmeow.UploadAttachment(sender.SignalDevice, convertedVideo, newMimeType, fileName)
attachmentPointer, err := signalmeow.UploadAttachment(ctx, sender.SignalDevice, convertedVideo, newMimeType, fileName)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -793,7 +793,7 @@ func (portal *Portal) convertMatrixMessage(ctx context.Context, sender *User, ev
mime = "audio/aac"
fileName += ".m4a"
}
attachmentPointer, err := signalmeow.UploadAttachment(sender.SignalDevice, data, mime, fileName)
attachmentPointer, err := signalmeow.UploadAttachment(ctx, sender.SignalDevice, data, mime, fileName)
if err != nil {
return nil, err
}
Expand All @@ -814,7 +814,7 @@ func (portal *Portal) convertMatrixMessage(ctx context.Context, sender *User, ev
if err != nil {
return nil, err
}
attachmentPointer, err := signalmeow.UploadAttachment(sender.SignalDevice, file, content.GetInfo().MimeType, fileName)
attachmentPointer, err := signalmeow.UploadAttachment(ctx, sender.SignalDevice, file, content.GetInfo().MimeType, fileName)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit f526e90

Please sign in to comment.