Added images/database functionality

This commit is contained in:
Samuel Hawksby-Robinson 2020-10-26 17:45:51 +00:00 committed by Andrea Maria Piana
parent 1b59d088d8
commit 7beebaaf92
3 changed files with 98 additions and 4 deletions

85
images/database.go Normal file
View File

@ -0,0 +1,85 @@
package images
import (
"database/sql"
"encoding/base64"
"encoding/json"
)
type Database struct {
db *sql.DB
}
type IdentityImage struct {
Type string `json:"type"`
Payload []byte
Width int `json:"width"`
Height int `json:"height"`
FileSize int `json:"file_size"`
ResizeTarget int `json:"resize_target"`
}
func NewDatabase(db *sql.DB) Database {
return Database{db: db}
}
func (d *Database) GetIdentityImages() ([]*IdentityImage, error) {
rows, err := d.db.Query(`SELECT type, image_payload, width, height, file_size, resize_target FROM identity_images`)
if err != nil {
return nil, err
}
defer rows.Close()
var iis []*IdentityImage
for rows.Next() {
ii := &IdentityImage{}
err = rows.Scan(&ii.Type, &ii.Payload, &ii.Width, &ii.Height, &ii.FileSize, &ii.ResizeTarget)
if err != nil {
return nil, err
}
iis = append(iis, ii)
}
return iis, nil
}
func (i IdentityImage) GetDataURI() (string, error) {
mt, err := GetMimeType(i.Payload)
if err != nil {
return "", err
}
b64 := base64.StdEncoding.EncodeToString(i.Payload)
return "data:image/" + mt + ";base64," + b64 + "\\", nil
}
func (d *Database) StoreIdentityImages() {
// TODO
}
func (i IdentityImage) MarshalJSON() ([]byte, error) {
uri, err := i.GetDataURI()
if err != nil {
return nil, err
}
temp := struct {
Type string `json:"type"`
URI string `json:"uri"`
Width int `json:"width"`
Height int `json:"height"`
FileSize int `json:"file_size"`
ResizeTarget int `json:"resize_target"`
}{
Type: i.Type,
URI: uri,
Width: i.Width,
Height: i.Height,
FileSize: i.FileSize,
ResizeTarget: i.ResizeTarget,
}
return json.Marshal(temp)
}

13
images/database_test.go Normal file
View File

@ -0,0 +1,13 @@
package images
import "testing"
func TestIdentityImage_GetDataURI(t *testing.T) {
// TODO
}
func TestIdentityImage_MarshalJSON(t *testing.T) {
// TODO
}

View File

@ -2,7 +2,6 @@ package images
import (
"errors"
"github.com/davecgh/go-spew/spew"
"image"
"image/gif"
"image/jpeg"
@ -32,9 +31,6 @@ func prepareFileForDecode(file *os.File) ([]byte, error) {
// Read the first 14 bytes, used for performing image type checks before parsing the image data
fb := make([]byte, 14)
_, err := file.Read(fb)
spew.Dump(fb)
if err != nil {
return nil, err
}