status-go/images/identity.go

98 lines
2.4 KiB
Go
Raw Permalink Normal View History

2020-10-26 17:45:51 +00:00
package images
import (
"encoding/json"
"errors"
2020-12-10 10:12:51 +00:00
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/protocol/protobuf"
2020-10-26 17:45:51 +00:00
)
type IdentityImage struct {
2022-08-19 13:38:34 +00:00
KeyUID string `json:"keyUID"`
Name string `json:"name"`
Payload []byte `json:"payload"`
Width int `json:"width"`
Height int `json:"height"`
2022-08-19 13:38:34 +00:00
FileSize int `json:"fileSize"`
ResizeTarget int `json:"resizeTarget"`
Clock uint64 `json:"clock"`
LocalURL string `json:"localUrl,omitempty"`
2020-10-26 17:45:51 +00:00
}
func (i IdentityImage) GetType() (ImageType, error) {
it := GetType(i.Payload)
if it == UNKNOWN {
return it, errors.New("unsupported file type")
}
return it, nil
}
2020-12-10 10:12:51 +00:00
func (i IdentityImage) Hash() []byte {
return crypto.Keccak256(i.Payload)
}
2020-10-26 17:45:51 +00:00
func (i IdentityImage) GetDataURI() (string, error) {
return GetPayloadDataURI(i.Payload)
2020-10-26 17:45:51 +00:00
}
func (i IdentityImage) MarshalJSON() ([]byte, error) {
uri, err := i.GetDataURI()
if err != nil {
return nil, err
}
temp := struct {
KeyUID string `json:"keyUid"`
Name string `json:"type"`
2020-10-26 17:45:51 +00:00
URI string `json:"uri"`
Width int `json:"width"`
Height int `json:"height"`
FileSize int `json:"fileSize"`
ResizeTarget int `json:"resizeTarget"`
2022-03-24 09:35:56 +00:00
Clock uint64 `json:"clock"`
LocalURL string `json:"localUrl,omitempty"`
2020-10-26 17:45:51 +00:00
}{
KeyUID: i.KeyUID,
Name: i.Name,
2020-10-26 17:45:51 +00:00
URI: uri,
Width: i.Width,
Height: i.Height,
FileSize: i.FileSize,
ResizeTarget: i.ResizeTarget,
2022-03-24 09:35:56 +00:00
Clock: i.Clock,
LocalURL: i.LocalURL,
2020-10-26 17:45:51 +00:00
}
return json.Marshal(temp)
}
func (i *IdentityImage) ToProtobuf() *protobuf.MultiAccount_IdentityImage {
return &protobuf.MultiAccount_IdentityImage{
KeyUid: i.KeyUID,
Name: i.Name,
Payload: i.Payload,
Width: int64(i.Width),
Height: int64(i.Height),
Filesize: int64(i.FileSize),
ResizeTarget: int64(i.ResizeTarget),
Clock: i.Clock,
}
}
func (i *IdentityImage) FromProtobuf(ii *protobuf.MultiAccount_IdentityImage) {
i.KeyUID = ii.KeyUid
i.Name = ii.Name
i.Payload = ii.Payload
i.Width = int(ii.Width)
i.Height = int(ii.Height)
i.FileSize = int(ii.Filesize)
i.ResizeTarget = int(ii.ResizeTarget)
i.Clock = ii.Clock
}
func (i IdentityImage) IsEmpty() bool {
2022-08-19 13:38:34 +00:00
return i.KeyUID == "" && i.Name == "" && len(i.Payload) == 0 && i.Width == 0 && i.Height == 0 && i.FileSize == 0 && i.ResizeTarget == 0 && i.Clock == 0
}