49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package models
|
|
|
|
import (
|
|
"bytes"
|
|
"database/sql/driver"
|
|
"encoding/binary"
|
|
|
|
"github.com/Kagami/go-face"
|
|
)
|
|
|
|
type FaceGroup struct {
|
|
Model
|
|
Label *string
|
|
ImageFaces []ImageFace `gorm:"constraint:OnDelete:CASCADE;"`
|
|
}
|
|
|
|
type ImageFace struct {
|
|
Model
|
|
FaceGroupID int `gorm:"not null;index"`
|
|
MediaID int `gorm:"not null;index"`
|
|
Media Media `gorm:"constraint:OnDelete:CASCADE;"`
|
|
Descriptor FaceDescriptor `gorm:"not null"`
|
|
}
|
|
|
|
type FaceDescriptor face.Descriptor
|
|
|
|
// GormDataType datatype used in database
|
|
func (fd FaceDescriptor) GormDataType() string {
|
|
return "BLOB"
|
|
}
|
|
|
|
// Scan tells GORM how to convert database data to Go format
|
|
func (fd *FaceDescriptor) Scan(value interface{}) error {
|
|
byteValue := value.([]byte)
|
|
reader := bytes.NewReader(byteValue)
|
|
binary.Read(reader, binary.LittleEndian, fd)
|
|
return nil
|
|
}
|
|
|
|
// Value tells GORM how to save into the database
|
|
func (fd FaceDescriptor) Value() (driver.Value, error) {
|
|
buf := new(bytes.Buffer)
|
|
if err := binary.Write(buf, binary.LittleEndian, fd); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
}
|