Skip to content

Commit 63f2cbb

Browse files
committed
send img
1 parent c921d00 commit 63f2cbb

File tree

3 files changed

+87
-6
lines changed

3 files changed

+87
-6
lines changed

pkg/connector/handlematrix.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"maunium.net/go/mautrix/bridgev2/database"
99
"maunium.net/go/mautrix/bridgev2/networkid"
1010

11+
"go.mau.fi/util/ptr"
12+
1113
"go.mau.fi/mautrix-googlechat/pkg/gchatmeow/proto"
1214
)
1315

@@ -27,10 +29,37 @@ func (c *GChatClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.Mat
2729
return nil, err
2830
}
2931

30-
res, err := c.client.CreateTopic(ctx, &proto.CreateTopicRequest{
32+
plainGroupId := groupId.GetDmId().DmId
33+
if plainGroupId == nil {
34+
plainGroupId = groupId.GetSpaceId().SpaceId
35+
}
36+
37+
req := &proto.CreateTopicRequest{
3138
GroupId: groupId,
3239
TextBody: &msg.Content.Body,
33-
})
40+
}
41+
42+
if msg.Content.MsgType.IsMedia() {
43+
data, err := c.userLogin.Bridge.Bot.DownloadMedia(ctx, msg.Content.URL, msg.Content.File)
44+
if err != nil {
45+
return nil, err
46+
}
47+
metadata, err := c.client.UploadFile(ctx, data, *plainGroupId, msg.Content.FileName, msg.Content.Info.MimeType)
48+
if err != nil {
49+
return nil, err
50+
}
51+
req.Annotations = []*proto.Annotation{
52+
{
53+
Type: ptr.Ptr(proto.AnnotationType_UPLOAD_METADATA),
54+
ChipRenderType: ptr.Ptr(proto.Annotation_RENDER),
55+
Metadata: &proto.Annotation_UploadMetadata{
56+
UploadMetadata: metadata,
57+
},
58+
},
59+
}
60+
}
61+
62+
res, err := c.client.CreateTopic(ctx, req)
3463
if err != nil {
3564
return nil, err
3665
}

pkg/gchatmeow/api.go

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package gchatmeow
22

33
import (
44
"context"
5+
"encoding/base64"
6+
"errors"
57
"fmt"
68
"net/http"
79
"net/url"
@@ -46,7 +48,7 @@ func (c *Client) gcRequest(ctx context.Context, endpoint string, requestPB proto
4648
return err
4749
}
4850

49-
if err := pb.Unmarshal(res, responsePB); err != nil {
51+
if err := pb.Unmarshal(res.Body, responsePB); err != nil {
5052
return fmt.Errorf("failed to decode Protocol Buffer response: %v", err)
5153
}
5254

@@ -63,7 +65,7 @@ func (c *Client) baseRequest(
6365
headers http.Header,
6466
params url.Values,
6567
method string,
66-
) ([]byte, error) {
68+
) (*FetchResponse, error) {
6769
if headers == nil {
6870
headers = http.Header{}
6971
}
@@ -88,7 +90,7 @@ func (c *Client) baseRequest(
8890
return nil, err
8991
}
9092

91-
return res.Body, nil
93+
return res, nil
9294
}
9395

9496
func (c *Client) getSelfUserStatus(ctx context.Context) (*proto.GetSelfUserStatusResponse, error) {
@@ -145,3 +147,49 @@ func (c *Client) GetGroup(ctx context.Context, request *proto.GetGroupRequest) (
145147
err := c.gcRequest(ctx, "get_group", request, response)
146148
return response, err
147149
}
150+
151+
func (c *Client) UploadFile(ctx context.Context, data []byte, groupId string, fileName string, mimeType string) (*proto.UploadMetadata, error) {
152+
headers := http.Header{
153+
"x-goog-upload-protocol": {"resumable"},
154+
"x-goog-upload-command": {"start"},
155+
"x-goog-upload-content-length": {string(len(data))},
156+
"x-goog-upload-content-type": {mimeType},
157+
"x-goog-upload-file-name": {fileName},
158+
}
159+
res, err := c.baseRequest(
160+
ctx, uploadURL, "", "", nil,
161+
headers, url.Values{"group_id": []string{groupId}},
162+
http.MethodPost,
163+
)
164+
if err != nil {
165+
return nil, err
166+
}
167+
newUploadURL := res.Headers.Get("x-goog-upload-url")
168+
if newUploadURL == "" {
169+
return nil, errors.New("image upload failed: can not acquire an upload url")
170+
}
171+
172+
headers = http.Header{
173+
"x-goog-upload-command": {"upload, finalize"},
174+
"x-goog-upload-protocol": {"resumable"},
175+
"x-goog-upload-offset": {"0"},
176+
}
177+
res, err = c.baseRequest(
178+
ctx, newUploadURL, "", "",
179+
data, headers, nil,
180+
http.MethodPut,
181+
)
182+
if err != nil {
183+
return nil, err
184+
}
185+
body, err := base64.StdEncoding.DecodeString(string(res.Body))
186+
if err != nil {
187+
return nil, err
188+
}
189+
uploadMetadata := &proto.UploadMetadata{}
190+
err = pb.Unmarshal(body, uploadMetadata)
191+
if err != nil {
192+
return nil, err
193+
}
194+
return uploadMetadata, nil
195+
}

pkg/gchatmeow/session.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,11 @@ func (s *Session) FetchRaw(ctx context.Context, method, urlStr string, params ur
148148
}
149149

150150
if params != nil {
151-
parsedURL.RawQuery = params.Encode()
151+
query := parsedURL.Query()
152+
for key, value := range params {
153+
query.Set(key, value[0])
154+
}
155+
parsedURL.RawQuery = query.Encode()
152156
}
153157

154158
req, err := http.NewRequestWithContext(ctx, method, parsedURL.String(), bytes.NewReader(data))

0 commit comments

Comments
 (0)