Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion internal/index/bleve_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,11 @@ func (b *BleveIndexer) File(slug string) (*IndexedFile, error) {
FileName: filepath.Base(doc.ID),
ContentType: "application/pdf",
}
if ext == ".epub" {
switch ext {
case ".epub":
result.ContentType = "application/epub+zip"
case ".cbz":
result.ContentType = "application/vnd.comicbook+zip"
}
return result, nil
}
Expand Down
17 changes: 14 additions & 3 deletions internal/index/related.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,30 @@ func distanceToDate(referenceDate float64, match *search.DocumentMatch) float64
}

// SameAuthors returns an array of metadata of documents by the same authors which
// does not belong to the same collection
// does not belong to the same collection. Returns no results if the document has
// no authors or only empty author slugs.
func (b *BleveIndexer) SameAuthors(slugID string, quantity int) ([]Document, error) {
doc, err := b.Document(slugID)
if err != nil {
return []Document{}, err
}

if len(doc.Authors) == 0 {
return []Document{}, err
hasAuthor := false
for _, slug := range doc.AuthorsSlugs {
if slug != "" {
hasAuthor = true
break
}
}
if !hasAuthor {
return []Document{}, nil
}

authorsCompoundQuery := bleve.NewDisjunctionQuery()
for _, slug := range doc.AuthorsSlugs {
if slug == "" {
continue
}
qu := bleve.NewTermQuery(slug)
qu.SetField("AuthorsSlugs")
authorsCompoundQuery.AddQuery(qu)
Expand Down
112 changes: 112 additions & 0 deletions internal/index/search_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package index_test

import (
"archive/zip"
"encoding/base64"
"encoding/xml"
"fmt"
"html/template"
"os"
"path/filepath"
"reflect"
"strconv"
"testing"
Expand Down Expand Up @@ -1628,3 +1633,110 @@ func testIndexAndSearchCases() []testCase {
},
}
}

// cbzTestMinimalPNG is a 1×1 transparent PNG (valid image for CBZ tests).
var cbzTestMinimalPNG, _ = base64.StdEncoding.DecodeString(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
)

func TestCbzReader_Metadata_Illustrators(t *testing.T) {
t.Parallel()
dir := t.TempDir()
cbPath := filepath.Join(dir, "issue.cbz")

comicXML := metadata.ComicInfo{
Title: "Test Issue",
Writer: "Alice Writer",
Penciller: "Bob Penciller",
Inker: "Carol Inker",
Colorist: "Dan Colorist",
Letterer: "Eve Letterer",
Editor: "Frank Editor",
}
comicXML.CoverArtist = "Gia Cover"
comicXML.Illustrator = "Helen Illustrator"

data, err := xml.Marshal(comicXML)
if err != nil {
t.Fatal(err)
}
if err := writeTestCBZ(cbPath, string(data)); err != nil {
t.Fatal(err)
}

meta, err := metadata.CbzReader{}.Metadata(cbPath)
if err != nil {
t.Fatal(err)
}

wantAuthors := []string{"Alice Writer", "Frank Editor"}
if !reflect.DeepEqual(meta.Authors, wantAuthors) {
t.Errorf("Authors = %#v; want %#v", meta.Authors, wantAuthors)
}

wantIll := []string{
"Bob Penciller",
"Carol Inker",
"Dan Colorist",
"Eve Letterer",
"Gia Cover",
"Helen Illustrator",
}
if !reflect.DeepEqual(meta.Illustrators, wantIll) {
t.Errorf("Illustrators = %#v; want %#v", meta.Illustrators, wantIll)
}
}

func TestCbzReader_Metadata_IllustratorsDedupe(t *testing.T) {
t.Parallel()
dir := t.TempDir()
cbPath := filepath.Join(dir, "dedupe.cbz")

comicXML := metadata.ComicInfo{
Writer: "Same Person",
Penciller: "Same Person",
Inker: "Same Person",
}
data, err := xml.Marshal(comicXML)
if err != nil {
t.Fatal(err)
}
if err := writeTestCBZ(cbPath, string(data)); err != nil {
t.Fatal(err)
}

meta, err := metadata.CbzReader{}.Metadata(cbPath)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(meta.Authors, []string{"Same Person"}) {
t.Errorf("Authors = %#v", meta.Authors)
}
if !reflect.DeepEqual(meta.Illustrators, []string{"Same Person"}) {
t.Errorf("Illustrators = %#v; want one deduped art credit", meta.Illustrators)
}
}

func writeTestCBZ(path, comicInfoXML string) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
zw := zip.NewWriter(f)
wci, err := zw.Create("ComicInfo.xml")
if err != nil {
return err
}
if _, err := wci.Write([]byte(xml.Header + comicInfoXML)); err != nil {
return err
}
wimg, err := zw.Create("001.png")
if err != nil {
return err
}
if _, err := wimg.Write(cbzTestMinimalPNG); err != nil {
return err
}
return zw.Close()
}
Loading
Loading