gmessages/libgm/image_builder.go

149 lines
5.9 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-15 16:48:26 +00:00
"go.mau.fi/mautrix-gmessages/libgm/binary"
2023-06-30 09:54:08 +00:00
"go.mau.fi/mautrix-gmessages/libgm/crypto"
"go.mau.fi/mautrix-gmessages/libgm/util"
)
2023-07-15 16:48:26 +00:00
type MediaType struct {
2023-06-30 09:54:08 +00:00
Extension string
Format string
Type int64
}
2023-07-15 16:48:26 +00:00
var MediaTypes = map[string]MediaType{
"image/jpeg": {Extension: "jpeg", Format: "image/jpeg", Type: 1},
"image/jpg": {Extension: "jpg", Format: "image/jpg", Type: 2},
"image/png": {Extension: "png", Format: "image/png", Type: 3},
"image/gif": {Extension: "gif", Format: "image/gif", Type: 4},
"image/wbmp": {Extension: "wbmp", Format: "image/wbmp", Type: 5},
"image/bmp": {Extension: "bmp", Format: "image/bmp", Type: 6},
"image/x-ms-bmp": {Extension: "bmp", Format: "image/x-ms-bmp", Type: 6},
"image": {Type: 7},
"video/mp4": {Extension: "mp4", Format: "video/mp4", Type: 8},
"video/3gpp2": {Extension: "3gpp2", Format: "video/3gpp2", Type: 9},
"video/3gpp": {Extension: "3gpp", Format: "video/3gpp", Type: 10},
"video/webm": {Extension: "webm", Format: "video/webm", Type: 11},
"video/x-matroska": {Extension: "mkv", Format: "video/x-matroska", Type: 12},
"video": {Type: 13},
"audio/aac": {Extension: "aac", Format: "audio/aac", Type: 14},
"audio/amr": {Extension: "amr", Format: "audio/amr", Type: 15},
"audio/mp3": {Extension: "mp3", Format: "audio/mp3", Type: 16},
"audio/mpeg": {Extension: "mpeg", Format: "audio/mpeg", Type: 17},
"audio/mpg": {Extension: "mpg", Format: "audio/mpg", Type: 18},
"audio/mp4": {Extension: "mp4", Format: "audio/mp4", Type: 19},
"audio/mp4-latm": {Extension: "latm", Format: "audio/mp4-latm", Type: 20},
"audio/3gpp": {Extension: "3gpp", Format: "audio/3gpp", Type: 21},
"audio/ogg": {Extension: "ogg", Format: "audio/ogg", Type: 22},
"auidio": {Type: 23},
"text/vcard": {Extension: "vcard", Format: "text/vcard", Type: 24},
"text/x-vcard": {Extension: "vcard", Format: "text/x-vcard", Type: 24},
2023-06-30 09:54:08 +00:00
"application/pdf": {Extension: "pdf", Format: "application/pdf", Type: 25},
2023-07-15 16:48:26 +00:00
"text/plain": {Extension: "txt", Format: "text/plain", Type: 26},
"text/html": {Extension: "html", Format: "text/html", Type: 27},
2023-06-30 09:54:08 +00:00
"application/msword": {Extension: "doc", Format: "application/msword", Type: 28},
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": {Extension: "docx", Format: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", Type: 29},
"application/vnd.openxmlformats-officedocument.presentationml.presentation": {Extension: "pptx", Format: "application/vnd.openxmlformats-officedocument.presentationml.presentation", Type: 30},
"application/vnd.ms-powerpoint": {Extension: "ppt", Format: "application/vnd.ms-powerpoint", Type: 31},
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": {Extension: "xlsx", Format: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", Type: 32},
"application/vnd.ms-excel": {Extension: "xls", Format: "application/vnd.ms-excel", Type: 33},
"application/vnd.android.package-archive": {Extension: "apk", Format: "application/vnd.android.package-archive", Type: 34},
"application/zip": {Extension: "zip", Format: "application/zip", Type: 35},
"application/java-archive": {Extension: "jar", Format: "application/java-archive", Type: 36},
"text/x-vCalendar": {Extension: "vcs", Format: "text/x-vCalendar", Type: 38},
"text/x-vcalendar": {Extension: "ics", Format: "text/x-vcalendar", Type: 39},
"text/calendar": {Extension: "ics", Format: "text/calendar", Type: 40},
"application/vcs": {Extension: "vcs", Format: "application/vcs", Type: 41},
"application/ics": {Extension: "ics", Format: "application/ics", Type: 42},
"application/hbs-vcs": {Extension: "vcs", Format: "application/hbs-vcs", Type: 43},
}
type Image struct {
imageCryptor *crypto.ImageCryptor
imageName string
2023-06-30 13:26:46 +00:00
imageID string
2023-07-15 16:48:26 +00:00
imageType MediaType
2023-06-30 09:54:08 +00:00
imageBytes []byte
imageSize int64
}
func (i *Image) GetEncryptedBytes() ([]byte, error) {
encryptedBytes, encryptErr := i.imageCryptor.EncryptData(i.imageBytes)
if encryptErr != nil {
return nil, encryptErr
}
return encryptedBytes, nil
}
func (i *Image) GetImageCryptor() *crypto.ImageCryptor {
return i.imageCryptor
}
func (i *Image) GetImageName() string {
return i.imageName
}
func (i *Image) GetImageBytes() []byte {
return i.imageBytes
}
func (i *Image) GetImageSize() int64 {
return i.imageSize
}
2023-07-15 16:48:26 +00:00
func (i *Image) GetImageType() MediaType {
2023-06-30 09:54:08 +00:00
return i.imageType
}
2023-06-30 13:26:46 +00:00
func (i *Image) GetImageID() string {
return i.imageID
2023-06-30 09:54:08 +00:00
}
// This is the equivalent of dragging an image into the window on messages web
//
// Keep in mind that adding an image to a MessageBuilder will also upload the image to googles server
2023-07-15 16:48:26 +00:00
func (c *Client) UploadMedia(data []byte, mime string) (*binary.MediaContent, error) {
mediaType := MediaTypes[mime]
mediaID := util.GenerateImageID()
fileName := util.RandStr(8) + "." + mediaType.Extension
decryptionKey, err := crypto.GenerateKey(32)
if err != nil {
return nil, err
2023-06-30 09:54:08 +00:00
}
2023-07-15 16:48:26 +00:00
cryptor, err := crypto.NewImageCryptor(decryptionKey)
if err != nil {
return nil, err
2023-06-30 09:54:08 +00:00
}
2023-07-15 16:48:26 +00:00
image := &Image{
imageCryptor: cryptor,
imageID: mediaID,
imageBytes: data,
imageType: mediaType,
imageSize: int64(len(data)),
imageName: fileName,
2023-06-30 09:54:08 +00:00
}
2023-07-15 16:48:26 +00:00
startUploadImage, err := c.StartUploadMedia(image)
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-15 16:48:26 +00:00
return &binary.MediaContent{
Format: binary.MediaFormats(image.GetImageType().Type),
MediaID: upload.MediaID,
MediaName: image.GetImageName(),
Size: image.GetImageSize(),
DecryptionKey: image.GetImageCryptor().GetKey(),
2023-06-30 09:54:08 +00:00
}, nil
}