diff --git a/api/geth_backend.go b/api/geth_backend.go index e233e4753..af926ea9d 100644 --- a/api/geth_backend.go +++ b/api/geth_backend.go @@ -5,6 +5,7 @@ package api import ( "context" "database/sql" + "encoding/json" "errors" "fmt" "github.com/status-im/status-go/images" @@ -1272,8 +1273,15 @@ func (b *GethStatusBackend) SignHash(hexEncodedHash string) (string, error) { // GetProfileImages returns an array of base64 encoded images related to the user's profile func (b *GethStatusBackend) GetProfileImages() (string, error) { - // TODO - return "", nil + idb := images.NewDatabase(b.appDB) + iis, err := idb.GetIdentityImages() + if err != nil { + return "", err + } + + js, err := json.Marshal(iis) + + return string(js), err } // SaveProfileImage takes the filepath of an image, crops it as per the rect coords and finally resizes the image. @@ -1281,7 +1289,18 @@ func (b *GethStatusBackend) GetProfileImages() (string, error) { // aX and aY represent the pixel coordinates of the upper left corner of the image's cropping area // bX and bY represent the pixel coordinates of the lower right corner of the image's cropping area func (b *GethStatusBackend) SaveProfileImage(filepath string, aX, aY, bX, bY int) (string, error) { - imgs, err := images.GenerateProfileImages(filepath, aX, aY, bX, bY) + iis, err := images.GenerateIdentityImages(filepath, aX, aY, bX, bY) + if err != nil { + return "", err + } - return nil, err + idb := images.NewDatabase(b.appDB) + err = idb.StoreIdentityImages(iis) + if err != nil { + return "", err + } + + js, err := json.Marshal(iis) + + return string(js), err } diff --git a/images/database.go b/images/database.go index 656f53684..1191f57be 100644 --- a/images/database.go +++ b/images/database.go @@ -12,7 +12,7 @@ type Database struct { } type IdentityImage struct { - Type string + Type string //TODO change this to Name, also in the db migration Payload []byte Width int Height int @@ -64,7 +64,7 @@ func (d *Database) GetIdentityImage(it string) (*IdentityImage, error) { return &ii, nil } -func (d *Database) StoreIdentityImages(iis []IdentityImage) (err error) { +func (d *Database) StoreIdentityImages(iis []*IdentityImage) (err error) { tx, err := d.db.BeginTx(context.Background(), &sql.TxOptions{}) if err != nil { return diff --git a/images/database_test.go b/images/database_test.go index 35760e625..e8ee0ddd1 100644 --- a/images/database_test.go +++ b/images/database_test.go @@ -86,7 +86,7 @@ func setupTestDB(t *testing.T) (Database, func()) { } func seedTestDB(t *testing.T, db Database) { - iis := []IdentityImage{ + iis := []*IdentityImage{ { Type: "thumbnail", Payload: testJpegBytes, diff --git a/images/main.go b/images/main.go index e807d1ae0..493b9769f 100644 --- a/images/main.go +++ b/images/main.go @@ -5,7 +5,7 @@ import ( "image" ) -func GenerateProfileImages(filepath string, aX, aY, bX, bY int) ([][]byte, error) { +func GenerateIdentityImages(filepath string, aX, aY, bX, bY int) ([]*IdentityImage, error) { img, err := Decode(filepath) if err != nil { return nil, err @@ -20,7 +20,7 @@ func GenerateProfileImages(filepath string, aX, aY, bX, bY int) ([][]byte, error return nil, err } - imgs := make([][]byte, len(ResizeDimensions)) + iis := make([]*IdentityImage, len(ResizeDimensions)) for _, s := range ResizeDimensions { rImg := Resize(s, cImg) @@ -29,8 +29,18 @@ func GenerateProfileImages(filepath string, aX, aY, bX, bY int) ([][]byte, error if err != nil { return nil, err } - imgs = append(imgs, bb.Bytes()) + + ii := &IdentityImage{ + Type: ResizeDimensionToName[s], + Payload: bb.Bytes(), + Width: rImg.Bounds().Dx(), + Height: rImg.Bounds().Dy(), + FileSize: bb.Len(), + ResizeTarget: int(s), + } + + iis = append(iis, ii) } - return imgs, nil + return iis, nil } diff --git a/images/meta.go b/images/meta.go index 01b747190..b08f0dfef 100644 --- a/images/meta.go +++ b/images/meta.go @@ -13,23 +13,42 @@ const ( const ( MaxJpegQuality = 80 MinJpegQuality = 50 + + smallDim = ResizeDimension(80) + largeDim = ResizeDimension(240) + + smallDimName = "thumbnail" + largeDimName = "large" ) var ( - ResizeDimensions = []ResizeDimension{80, 240} + // ResizeDimensions list of all available image resize sizes + ResizeDimensions = []ResizeDimension{smallDim, largeDim} // DimensionSizeLimit the size limits imposed on each resize dimension // Figures are based on the following sample data https://github.com/status-im/status-react/issues/11047#issuecomment-694970473 DimensionSizeLimit = map[ResizeDimension]DimensionLimits{ - 80: { + smallDim: { Ideal: 2560, // Base on the largest sample image at quality 60% (2,554 bytes ∴ 1024 * 2.5) Max: 5632, // Base on the largest sample image at quality 80% + 50% margin (3,683 bytes * 1.5 ≈ 5500 ∴ 1024 * 5.5) }, - 240: { + largeDim: { Ideal: 16384, // Base on the largest sample image at quality 60% (16,143 bytes ∴ 1024 * 16) Max: 38400, // Base on the largest sample image at quality 80% + 50% margin (24,290 bytes * 1.5 ≈ 37500 ∴ 1024 * 37.5) }, } + + // ResizeDimensionToName maps a ResizeDimension to its assigned string name + ResizeDimensionToName = map[ResizeDimension]string{ + smallDim: smallDimName, + largeDim: largeDimName, + } + + // NameToResizeDimension maps a string name to its assigned ResizeDimension + NameToResizeDimension = map[string]ResizeDimension{ + smallDimName: smallDim, + largeDimName: largeDim, + } ) type DimensionLimits struct {