github.com/status-im/status-go@v1.1.0/images/identity.go (about) 1 package images 2 3 import ( 4 "encoding/json" 5 "errors" 6 7 "github.com/status-im/status-go/eth-node/crypto" 8 "github.com/status-im/status-go/protocol/protobuf" 9 ) 10 11 type IdentityImage struct { 12 KeyUID string `json:"keyUID"` 13 Name string `json:"name"` 14 Payload []byte `json:"payload"` 15 Width int `json:"width"` 16 Height int `json:"height"` 17 FileSize int `json:"fileSize"` 18 ResizeTarget int `json:"resizeTarget"` 19 Clock uint64 `json:"clock"` 20 LocalURL string `json:"localUrl,omitempty"` 21 } 22 23 func (i IdentityImage) GetType() (ImageType, error) { 24 it := GetType(i.Payload) 25 if it == UNKNOWN { 26 return it, errors.New("unsupported file type") 27 } 28 29 return it, nil 30 } 31 32 func (i IdentityImage) Hash() []byte { 33 return crypto.Keccak256(i.Payload) 34 } 35 36 func (i IdentityImage) GetDataURI() (string, error) { 37 return GetPayloadDataURI(i.Payload) 38 } 39 40 func (i IdentityImage) MarshalJSON() ([]byte, error) { 41 uri, err := i.GetDataURI() 42 if err != nil { 43 return nil, err 44 } 45 46 temp := struct { 47 KeyUID string `json:"keyUid"` 48 Name string `json:"type"` 49 URI string `json:"uri"` 50 Width int `json:"width"` 51 Height int `json:"height"` 52 FileSize int `json:"fileSize"` 53 ResizeTarget int `json:"resizeTarget"` 54 Clock uint64 `json:"clock"` 55 LocalURL string `json:"localUrl,omitempty"` 56 }{ 57 KeyUID: i.KeyUID, 58 Name: i.Name, 59 URI: uri, 60 Width: i.Width, 61 Height: i.Height, 62 FileSize: i.FileSize, 63 ResizeTarget: i.ResizeTarget, 64 Clock: i.Clock, 65 LocalURL: i.LocalURL, 66 } 67 68 return json.Marshal(temp) 69 } 70 71 func (i *IdentityImage) ToProtobuf() *protobuf.MultiAccount_IdentityImage { 72 return &protobuf.MultiAccount_IdentityImage{ 73 KeyUid: i.KeyUID, 74 Name: i.Name, 75 Payload: i.Payload, 76 Width: int64(i.Width), 77 Height: int64(i.Height), 78 Filesize: int64(i.FileSize), 79 ResizeTarget: int64(i.ResizeTarget), 80 Clock: i.Clock, 81 } 82 } 83 84 func (i *IdentityImage) FromProtobuf(ii *protobuf.MultiAccount_IdentityImage) { 85 i.KeyUID = ii.KeyUid 86 i.Name = ii.Name 87 i.Payload = ii.Payload 88 i.Width = int(ii.Width) 89 i.Height = int(ii.Height) 90 i.FileSize = int(ii.Filesize) 91 i.ResizeTarget = int(ii.ResizeTarget) 92 i.Clock = ii.Clock 93 } 94 95 func (i IdentityImage) IsEmpty() bool { 96 return i.KeyUID == "" && i.Name == "" && len(i.Payload) == 0 && i.Width == 0 && i.Height == 0 && i.FileSize == 0 && i.ResizeTarget == 0 && i.Clock == 0 97 }