gmessages/libgm/upload.go

250 lines
8.6 KiB
Go
Raw Normal View History

2023-06-30 11:05:33 +00:00
package libgm
2023-06-30 09:54:08 +00:00
import (
2023-07-17 13:43:34 +00:00
"bytes"
"encoding/base64"
"errors"
"io"
"net/http"
"strconv"
"strings"
2023-07-17 13:43:34 +00:00
"github.com/google/uuid"
"google.golang.org/protobuf/proto"
2023-06-30 09:54:08 +00:00
"go.mau.fi/mautrix-gmessages/libgm/crypto"
2023-07-17 13:51:31 +00:00
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
2023-07-17 13:43:34 +00:00
"go.mau.fi/mautrix-gmessages/libgm/util"
2023-06-30 09:54:08 +00:00
)
2023-07-15 16:48:26 +00:00
type MediaType struct {
2023-06-30 09:54:08 +00:00
Extension string
Format string
2023-07-17 13:51:31 +00:00
Type gmproto.MediaFormats
2023-06-30 09:54:08 +00:00
}
var MimeToMediaType = map[string]MediaType{
2023-07-17 13:51:31 +00:00
"image/jpeg": {Extension: "jpeg", Type: gmproto.MediaFormats_IMAGE_JPEG},
"image/jpg": {Extension: "jpg", Type: gmproto.MediaFormats_IMAGE_JPG},
"image/png": {Extension: "png", Type: gmproto.MediaFormats_IMAGE_PNG},
"image/gif": {Extension: "gif", Type: gmproto.MediaFormats_IMAGE_GIF},
"image/wbmp": {Extension: "wbmp", Type: gmproto.MediaFormats_IMAGE_WBMP},
"image/bmp": {Extension: "bmp", Type: gmproto.MediaFormats_IMAGE_X_MS_BMP},
"image/x-ms-bmp": {Extension: "bmp", Type: gmproto.MediaFormats_IMAGE_X_MS_BMP},
"video/mp4": {Extension: "mp4", Type: gmproto.MediaFormats_VIDEO_MP4},
"video/3gpp2": {Extension: "3gpp2", Type: gmproto.MediaFormats_VIDEO_3G2},
"video/3gpp": {Extension: "3gpp", Type: gmproto.MediaFormats_VIDEO_3GPP},
"video/webm": {Extension: "webm", Type: gmproto.MediaFormats_VIDEO_WEBM},
"video/x-matroska": {Extension: "mkv", Type: gmproto.MediaFormats_VIDEO_MKV},
"audio/aac": {Extension: "aac", Type: gmproto.MediaFormats_AUDIO_AAC},
"audio/amr": {Extension: "amr", Type: gmproto.MediaFormats_AUDIO_AMR},
"audio/mp3": {Extension: "mp3", Type: gmproto.MediaFormats_AUDIO_MP3},
"audio/mpeg": {Extension: "mpeg", Type: gmproto.MediaFormats_AUDIO_MPEG},
"audio/mpg": {Extension: "mpg", Type: gmproto.MediaFormats_AUDIO_MPG},
"audio/mp4": {Extension: "mp4", Type: gmproto.MediaFormats_AUDIO_MP4},
"audio/mp4-latm": {Extension: "latm", Type: gmproto.MediaFormats_AUDIO_MP4_LATM},
"audio/3gpp": {Extension: "3gpp", Type: gmproto.MediaFormats_AUDIO_3GPP},
"audio/ogg": {Extension: "ogg", Type: gmproto.MediaFormats_AUDIO_OGG},
"text/vcard": {Extension: "vcard", Type: gmproto.MediaFormats_TEXT_VCARD},
"application/pdf": {Extension: "pdf", Type: gmproto.MediaFormats_APP_PDF},
"text/plain": {Extension: "txt", Type: gmproto.MediaFormats_APP_TXT},
"text/html": {Extension: "html", Type: gmproto.MediaFormats_APP_HTML},
"application/msword": {Extension: "doc", Type: gmproto.MediaFormats_APP_DOC},
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": {Extension: "docx", Type: gmproto.MediaFormats_APP_DOCX},
"application/vnd.openxmlformats-officedocument.presentationml.presentation": {Extension: "pptx", Type: gmproto.MediaFormats_APP_PPTX},
"application/vnd.ms-powerpoint": {Extension: "ppt", Type: gmproto.MediaFormats_APP_PPT},
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": {Extension: "xlsx", Type: gmproto.MediaFormats_APP_XLSX},
"application/vnd.ms-excel": {Extension: "xls", Type: gmproto.MediaFormats_APP_XLS},
"application/vnd.android.package-archive": {Extension: "apk", Type: gmproto.MediaFormats_APP_APK},
"application/zip": {Extension: "zip", Type: gmproto.MediaFormats_APP_ZIP},
"application/java-archive": {Extension: "jar", Type: gmproto.MediaFormats_APP_JAR},
"text/x-calendar": {Extension: "vcs", Type: gmproto.MediaFormats_CAL_TEXT_VCALENDAR},
"text/calendar": {Extension: "ics", Type: gmproto.MediaFormats_CAL_TEXT_CALENDAR},
"image": {Type: gmproto.MediaFormats_IMAGE_UNSPECIFIED},
"video": {Type: gmproto.MediaFormats_VIDEO_UNSPECIFIED},
"audio": {Type: gmproto.MediaFormats_AUDIO_UNSPECIFIED},
"application": {Type: gmproto.MediaFormats_APP_UNSPECIFIED},
"text": {Type: gmproto.MediaFormats_APP_TXT},
2023-06-30 09:54:08 +00:00
}
2023-07-17 13:51:31 +00:00
var FormatToMediaType = map[gmproto.MediaFormats]MediaType{
gmproto.MediaFormats_CAL_TEXT_XVCALENDAR: MimeToMediaType["text/x-calendar"],
gmproto.MediaFormats_CAL_APPLICATION_VCS: MimeToMediaType["text/x-calendar"],
gmproto.MediaFormats_CAL_APPLICATION_ICS: MimeToMediaType["text/calendar"],
//gmproto.MediaFormats_CAL_APPLICATION_HBSVCS: ???
2023-06-30 09:54:08 +00:00
}
func init() {
for key, mediaType := range MimeToMediaType {
if strings.ContainsRune(key, '/') {
mediaType.Format = key
}
FormatToMediaType[mediaType.Type] = mediaType
2023-06-30 09:54:08 +00:00
}
}
2023-07-17 13:51:31 +00:00
func (c *Client) UploadMedia(data []byte, fileName, mime string) (*gmproto.MediaContent, error) {
mediaType := MimeToMediaType[mime]
if mediaType.Type == 0 {
mediaType = MimeToMediaType[strings.Split(mime, "/")[0]]
}
2023-07-16 11:36:13 +00:00
decryptionKey := crypto.GenerateKey(32)
2023-07-17 14:56:34 +00:00
cryptor, err := crypto.NewAESGCMHelper(decryptionKey)
2023-07-15 16:48:26 +00:00
if err != nil {
return nil, err
2023-06-30 09:54:08 +00:00
}
encryptedBytes, err := cryptor.EncryptData(data)
if err != nil {
return nil, err
2023-06-30 09:54:08 +00:00
}
startUploadImage, err := c.StartUploadMedia(encryptedBytes, mime)
2023-07-15 16:48:26 +00:00
if err != nil {
return nil, err
2023-06-30 09:54:08 +00:00
}
2023-07-15 16:48:26 +00:00
upload, err := c.FinalizeUploadMedia(startUploadImage)
2023-06-30 09:54:08 +00:00
if err != nil {
return nil, err
}
2023-07-17 13:51:31 +00:00
return &gmproto.MediaContent{
Format: mediaType.Type,
2023-07-15 16:48:26 +00:00
MediaID: upload.MediaID,
MediaName: fileName,
Size: int64(len(data)),
DecryptionKey: decryptionKey,
2023-06-30 09:54:08 +00:00
}, nil
}
2023-07-17 13:43:34 +00:00
type StartGoogleUpload struct {
UploadID string
UploadURL string
UploadStatus string
ChunkGranularity int64
ControlURL string
MimeType string
EncryptedMediaBytes []byte
}
type MediaUpload struct {
MediaID string
MediaNumber int64
}
var (
errStartUploadMedia = errors.New("failed to start uploading media")
errFinalizeUploadMedia = errors.New("failed to finalize uploading media")
)
func (c *Client) FinalizeUploadMedia(upload *StartGoogleUpload) (*MediaUpload, error) {
encryptedImageSize := strconv.Itoa(len(upload.EncryptedMediaBytes))
finalizeUploadHeaders := util.NewMediaUploadHeaders(encryptedImageSize, "upload, finalize", "0", upload.MimeType, "")
req, reqErr := http.NewRequest("POST", upload.UploadURL, bytes.NewBuffer(upload.EncryptedMediaBytes))
if reqErr != nil {
return nil, reqErr
}
req.Header = *finalizeUploadHeaders
res, resErr := c.http.Do(req)
if resErr != nil {
panic(resErr)
}
statusCode := res.StatusCode
if statusCode != 200 {
return nil, errFinalizeUploadMedia
}
defer res.Body.Close()
rHeaders := res.Header
googleResponse, err3 := io.ReadAll(base64.NewDecoder(base64.StdEncoding, res.Body))
if err3 != nil {
return nil, err3
}
uploadStatus := rHeaders.Get("x-goog-upload-status")
c.Logger.Debug().Str("upload_status", uploadStatus).Msg("Upload complete")
2023-07-17 13:51:31 +00:00
mediaIDs := &gmproto.UploadMediaResponse{}
2023-07-17 13:43:34 +00:00
err3 = proto.Unmarshal(googleResponse, mediaIDs)
if err3 != nil {
return nil, err3
}
return &MediaUpload{
MediaID: mediaIDs.Media.MediaID,
MediaNumber: mediaIDs.Media.MediaNumber,
}, nil
}
func (c *Client) StartUploadMedia(encryptedImageBytes []byte, mime string) (*StartGoogleUpload, error) {
encryptedImageSize := strconv.Itoa(len(encryptedImageBytes))
startUploadHeaders := util.NewMediaUploadHeaders(encryptedImageSize, "start", "", mime, "resumable")
startUploadPayload, buildPayloadErr := c.buildStartUploadPayload()
if buildPayloadErr != nil {
return nil, buildPayloadErr
}
req, reqErr := http.NewRequest("POST", util.UploadMediaURL, bytes.NewBuffer([]byte(startUploadPayload)))
if reqErr != nil {
return nil, reqErr
}
req.Header = *startUploadHeaders
res, resErr := c.http.Do(req)
if resErr != nil {
panic(resErr)
}
statusCode := res.StatusCode
if statusCode != 200 {
return nil, errStartUploadMedia
}
rHeaders := res.Header
chunkGranularity, convertErr := strconv.Atoi(rHeaders.Get("x-goog-upload-chunk-granularity"))
if convertErr != nil {
return nil, convertErr
}
uploadResponse := &StartGoogleUpload{
UploadID: rHeaders.Get("x-guploader-uploadid"),
UploadURL: rHeaders.Get("x-goog-upload-url"),
UploadStatus: rHeaders.Get("x-goog-upload-status"),
ChunkGranularity: int64(chunkGranularity),
ControlURL: rHeaders.Get("x-goog-upload-control-url"),
MimeType: mime,
EncryptedMediaBytes: encryptedImageBytes,
}
return uploadResponse, nil
}
func (c *Client) buildStartUploadPayload() (string, error) {
2023-07-17 23:57:20 +00:00
protoData := &gmproto.StartMediaUploadRequest{
AttachmentType: 1,
2023-07-17 13:51:31 +00:00
AuthData: &gmproto.AuthMessage{
2023-07-17 13:43:34 +00:00
RequestID: uuid.NewString(),
TachyonAuthToken: c.AuthData.TachyonAuthToken,
ConfigVersion: util.ConfigMessage,
},
Mobile: c.AuthData.Mobile,
}
protoDataBytes, err := proto.Marshal(protoData)
if err != nil {
return "", err
}
protoDataEncoded := base64.StdEncoding.EncodeToString(protoDataBytes)
return protoDataEncoded, nil
}