Skip to content

Commit

Permalink
fix: don't import empty blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
sentriz committed Sep 20, 2021
1 parent 466a9df commit 163fd46
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions backend/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ func (db *DB) SearchMedias(options SearchMediasOptions) ([]*Media, error) {
}

func (db *DB) CreateBlocks(blocks []*Block) error {
if len(blocks) == 0 {
return nil
}

q := db.
Insert("blocks").
Columns("media_id", "index", "min_x", "min_y", "max_x", "max_y", "body")
Expand Down
5 changes: 5 additions & 0 deletions backend/importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ func (i *Importer) insertBlocks(id db.MediaID, image image.Image) error {

blocks := make([]*db.Block, 0, len(rawBlocks))
for idx, rawBlock := range rawBlocks {
if strings.TrimSpace(rawBlock.Word) == "" {
continue
}

rect := imagery.ScaleDownRect(rawBlock.Box)
blocks = append(blocks, &db.Block{
MediaID: id,
Expand All @@ -274,6 +278,7 @@ func (i *Importer) insertBlocks(id db.MediaID, image image.Image) error {
Body: rawBlock.Word,
})
}

if err := i.db.CreateBlocks(blocks); err != nil {
return fmt.Errorf("inserting blocks: %w", err)
}
Expand Down

0 comments on commit 163fd46

Please sign in to comment.