Resize chat message images

This commit is contained in:
Andrea Maria Piana 2022-05-27 12:21:02 +01:00
parent 869942c05e
commit d25c0fa1b8
2 changed files with 30 additions and 3 deletions

View File

@ -38,7 +38,7 @@ import (
type testTimeSource struct{}
func (t *testTimeSource) GetCurrentTime() uint64 {
return uint64(time.Now().Unix())
return uint64(time.Now().Unix()) * 1000
}
const (
@ -379,7 +379,7 @@ func defaultNodeConfig(installationID string) (*params.NodeConfig, error) {
}
nodeConfig.Name = "StatusIM"
nodeConfig.Rendezvous = true
nodeConfig.Rendezvous = false
clusterConfig, err := params.LoadClusterConfigFromFleet("eth.prod")
if err != nil {
return nil, err
@ -496,6 +496,7 @@ func buildMessage(chat *protocol.Chat, count int) *common.Message {
}
clock, timestamp := chat.NextClockAndTimestamp(&testTimeSource{})
clock += uint64(count)
message := &common.Message{}
message.Text = fmt.Sprintf("test message %d", count)
message.ChatId = chat.ID

View File

@ -72,6 +72,10 @@ const (
publicChat chatContext = "public-chat"
privateChat chatContext = "private-chat"
maxChatMessageImageSize = 400000
resizeTargetImageSize = 350000
idealTargetImageSize = 50000
)
const messageResendMinDelay = 30
@ -2403,6 +2407,7 @@ func (m *Messenger) sendChatMessage(ctx context.Context, message *common.Message
message.DisplayName = displayName
if len(message.ImagePath) != 0 {
file, err := os.Open(message.ImagePath)
if err != nil {
return nil, err
@ -2412,8 +2417,29 @@ func (m *Messenger) sendChatMessage(ctx context.Context, message *common.Message
payload, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
img, err := userimage.Decode(message.ImagePath)
if err != nil {
return nil, err
}
bb := bytes.NewBuffer([]byte{})
err = userimage.EncodeToLimits(bb, img, userimage.DimensionLimits{Ideal: idealTargetImageSize, Max: resizeTargetImageSize})
if err != nil {
return nil, err
}
// We keep the smallest one
if len(payload) > len(bb.Bytes()) {
payload = bb.Bytes()
}
if len(payload) > maxChatMessageImageSize {
return nil, errors.New("image too large")
}
image := protobuf.ImageMessage{
Payload: payload,
Type: images.ImageType(payload),