initial commit

This commit is contained in:
silva guimaraes 2025-08-23 19:48:04 -03:00
commit dd48aef782
8 changed files with 1010 additions and 0 deletions

157
shared/shared.go Normal file
View file

@ -0,0 +1,157 @@
package shared
import (
"encoding/base64"
"encoding/json"
"fmt"
"image/png"
"net/http"
"os"
"path/filepath"
"gocv.io/x/gocv"
"gorm.io/gorm"
)
type Page struct {
gorm.Model
Name, Path, Url string
DescriptorBlob []byte
UserID uint
Publication Publication `gorm:"foreignKey:UserID"`
Order int
Image gocv.Mat `gorm:"-:all"`
Phash, Avghash gocv.Mat `gorm:"-:all"`
Keypoints []gocv.KeyPoint `gorm:"-:all"`
Descriptors gocv.Mat `gorm:"-:all"`
B64 string `gorm:"-:all"`
// b64, mime string
}
var (
BlankImage = fmt.Errorf("blank image")
)
var (
nomask gocv.Mat = gocv.NewMat()
orb gocv.ORB = gocv.NewORB()
)
type Language string
type Category string
const (
Jp Language = "japanese"
Cn Language = "chinese"
En Language = "english"
None Language = "none"
)
const (
Doujinshi Category = "doujinshi"
)
type Tag struct {
gorm.Model
Name string `gorm:"unique"`
}
type Author struct {
gorm.Model
Name string `gorm:"unique"`
}
type Publication struct {
gorm.Model
Title string
Source string // url original
Host string
Favorites int
Authors []Author `gorm:"many2many:publication_authors"`
Language *Language
Category *Category
Tags []Tag `gorm:"many2many:publication_tags"`
Pages []Page `gorm:"foreignKey:UserID"`
}
func (p Publication) SaveInfoToFile(dir string) {
bytes, err := json.Marshal(p)
if err != nil {
panic(err)
}
file, err := os.Create(filepath.Join(dir, "info.json"))
if err != nil {
panic(err)
}
defer file.Close()
file.Write(bytes)
}
func LoadImageFromBytes(bytes []byte) (Page, error) {
fileMat, err := gocv.IMDecode(bytes, gocv.IMReadColor)
if err != nil {
panic(err)
}
img, err := LoadImage("", fileMat)
if err != nil {
return Page{}, err
}
img.B64 = fmt.Sprintf(
"data:%s;base64,%s",
http.DetectContentType(bytes),
base64.StdEncoding.EncodeToString(bytes),
)
return img, nil
}
func LoadImage(path string, img gocv.Mat) (Page, error) {
// img := gocv.IMRead(path, gocv.IMReadColor)
// if img.Empty() {
// log.Panic("cannot read image", path)
// }
keypoints, descriptors := orb.DetectAndCompute(img, nomask)
if len(keypoints) == 0 {
return Page{}, BlankImage
}
e := Page{
Image: img,
Name: filepath.Base(path),
Path: "/" + path,
// Phash: gocv.NewMat(),
// Avghash: gocv.NewMat(),
Keypoints: keypoints,
Descriptors: descriptors,
DescriptorBlob: descriptors.ToBytes(),
// b64: base64.StdEncoding.EncodeToString(img.ToBytes()),
// mime: http.DetectContentType(img.ToBytes()),
}
// e.hashImage()
return e, nil
}
func (e Page) SaveORBtoDisk(path string) {
img, err := e.Descriptors.ToImage()
if err != nil {
panic(err)
}
cache, err := os.Create(path)
if err != nil {
panic(err)
}
defer cache.Close()
err = png.Encode(cache, img)
}