Implemented GetImages and StoreImages in API

This commit is contained in:
Samuel Hawksby-Robinson 2020-10-27 15:14:00 +00:00 committed by Andrea Maria Piana
parent a7358fcd3f
commit 2960fc49d4
5 changed files with 62 additions and 14 deletions

View File

@ -5,6 +5,7 @@ package api
import ( import (
"context" "context"
"database/sql" "database/sql"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/status-im/status-go/images" "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 // GetProfileImages returns an array of base64 encoded images related to the user's profile
func (b *GethStatusBackend) GetProfileImages() (string, error) { func (b *GethStatusBackend) GetProfileImages() (string, error) {
// TODO idb := images.NewDatabase(b.appDB)
return "", nil 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. // 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 // 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 // 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) { 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
} }

View File

@ -12,7 +12,7 @@ type Database struct {
} }
type IdentityImage struct { type IdentityImage struct {
Type string Type string //TODO change this to Name, also in the db migration
Payload []byte Payload []byte
Width int Width int
Height int Height int
@ -64,7 +64,7 @@ func (d *Database) GetIdentityImage(it string) (*IdentityImage, error) {
return &ii, nil 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{}) tx, err := d.db.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil { if err != nil {
return return

View File

@ -86,7 +86,7 @@ func setupTestDB(t *testing.T) (Database, func()) {
} }
func seedTestDB(t *testing.T, db Database) { func seedTestDB(t *testing.T, db Database) {
iis := []IdentityImage{ iis := []*IdentityImage{
{ {
Type: "thumbnail", Type: "thumbnail",
Payload: testJpegBytes, Payload: testJpegBytes,

View File

@ -5,7 +5,7 @@ import (
"image" "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) img, err := Decode(filepath)
if err != nil { if err != nil {
return nil, err return nil, err
@ -20,7 +20,7 @@ func GenerateProfileImages(filepath string, aX, aY, bX, bY int) ([][]byte, error
return nil, err return nil, err
} }
imgs := make([][]byte, len(ResizeDimensions)) iis := make([]*IdentityImage, len(ResizeDimensions))
for _, s := range ResizeDimensions { for _, s := range ResizeDimensions {
rImg := Resize(s, cImg) rImg := Resize(s, cImg)
@ -29,8 +29,18 @@ func GenerateProfileImages(filepath string, aX, aY, bX, bY int) ([][]byte, error
if err != nil { if err != nil {
return nil, err 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
} }

View File

@ -13,23 +13,42 @@ const (
const ( const (
MaxJpegQuality = 80 MaxJpegQuality = 80
MinJpegQuality = 50 MinJpegQuality = 50
smallDim = ResizeDimension(80)
largeDim = ResizeDimension(240)
smallDimName = "thumbnail"
largeDimName = "large"
) )
var ( 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 // 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 // Figures are based on the following sample data https://github.com/status-im/status-react/issues/11047#issuecomment-694970473
DimensionSizeLimit = map[ResizeDimension]DimensionLimits{ DimensionSizeLimit = map[ResizeDimension]DimensionLimits{
80: { smallDim: {
Ideal: 2560, // Base on the largest sample image at quality 60% (2,554 bytes ∴ 1024 * 2.5) 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) 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) 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) 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 { type DimensionLimits struct {