From 2990367e2f77a760d6f6191d0aa0722820d5dd94 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Fri, 9 Feb 2024 20:16:48 +0200 Subject: [PATCH] fix export type --- internal/epub/epub.go | 32 +++++++------- .../epub_image_filters_autocontrast.go | 2 +- internal/epub/tree/epub_tree.go | 44 +++++++++++-------- internal/epub/zip/epub_zip.go | 4 +- internal/sortpath/sortpath.go | 4 +- 5 files changed, 49 insertions(+), 37 deletions(-) diff --git a/internal/epub/epub.go b/internal/epub/epub.go index 95d60e4..ff91ba9 100644 --- a/internal/epub/epub.go +++ b/internal/epub/epub.go @@ -23,7 +23,7 @@ import ( "github.com/gofrs/uuid" ) -type ePub struct { +type EPub struct { *epuboptions.Options UID string Publisher string @@ -40,7 +40,7 @@ type epubPart struct { } // New initialize EPUB -func New(options *epuboptions.Options) *ePub { +func New(options *epuboptions.Options) *EPub { uid := uuid.Must(uuid.NewV4()) tmpl := template.New("parser") tmpl.Funcs(template.FuncMap{ @@ -48,7 +48,7 @@ func New(options *epuboptions.Options) *ePub { "zoom": func(s int, z float32) int { return int(float32(s) * z) }, }) - return &ePub{ + return &EPub{ Options: options, UID: uid.String(), Publisher: "GO Comic Converter", @@ -59,7 +59,7 @@ func New(options *epuboptions.Options) *ePub { } // render templates -func (e *ePub) render(templateString string, data map[string]any) string { +func (e *EPub) render(templateString string, data map[string]any) string { var result strings.Builder tmpl := template.Must(e.templateProcessor.Parse(templateString)) if err := tmpl.Execute(&result, data); err != nil { @@ -69,7 +69,7 @@ func (e *ePub) render(templateString string, data map[string]any) string { } // write image to the zip -func (e *ePub) writeImage(wz *epubzip.EPUBZip, img *epubimage.Image, zipImg *zip.File) error { +func (e *EPub) writeImage(wz *epubzip.EPUBZip, img *epubimage.Image, zipImg *zip.File) error { err := wz.WriteContent( img.EPUBPagePath(), []byte(e.render(epubtemplates.Text, map[string]any{ @@ -87,7 +87,7 @@ func (e *ePub) writeImage(wz *epubzip.EPUBZip, img *epubimage.Image, zipImg *zip } // write blank page -func (e *ePub) writeBlank(wz *epubzip.EPUBZip, img *epubimage.Image) error { +func (e *EPub) writeBlank(wz *epubzip.EPUBZip, img *epubimage.Image) error { return wz.WriteContent( img.EPUBSpacePath(), []byte(e.render(epubtemplates.Blank, map[string]any{ @@ -98,7 +98,7 @@ func (e *ePub) writeBlank(wz *epubzip.EPUBZip, img *epubimage.Image) error { } // write title image -func (e *ePub) writeCoverImage(wz *epubzip.EPUBZip, img *epubimage.Image, part, totalParts int) error { +func (e *EPub) writeCoverImage(wz *epubzip.EPUBZip, img *epubimage.Image, part, totalParts int) error { title := "Cover" text := "" if totalParts > 1 { @@ -141,7 +141,7 @@ func (e *ePub) writeCoverImage(wz *epubzip.EPUBZip, img *epubimage.Image, part, } // write title image -func (e *ePub) writeTitleImage(wz *epubzip.EPUBZip, img *epubimage.Image, title string) error { +func (e *EPub) writeTitleImage(wz *epubzip.EPUBZip, img *epubimage.Image, title string) error { titleAlign := "" if !e.Image.View.PortraitOnly { if e.Image.Manga { @@ -197,7 +197,7 @@ func (e *ePub) writeTitleImage(wz *epubzip.EPUBZip, img *epubimage.Image, title } // extract image and split it into part -func (e *ePub) getParts() (parts []*epubPart, imgStorage *epubzip.StorageImageReader, err error) { +func (e *EPub) getParts() (parts []*epubPart, imgStorage *epubzip.StorageImageReader, err error) { images, err := e.imageProcessor.Load() if err != nil { @@ -268,7 +268,7 @@ func (e *ePub) getParts() (parts []*epubPart, imgStorage *epubzip.StorageImageRe // create a tree from the directories. // // this is used to simulate the toc. -func (e *ePub) getTree(images []*epubimage.Image, skipFiles bool) string { +func (e *EPub) getTree(images []*epubimage.Image, skipFiles bool) string { t := epubtree.New() for _, img := range images { if skipFiles { @@ -278,14 +278,14 @@ func (e *ePub) getTree(images []*epubimage.Image, skipFiles bool) string { } } c := t.Root() - if skipFiles && e.StripFirstDirectoryFromToc && len(c.Children) == 1 { - c = c.Children[0] + if skipFiles && e.StripFirstDirectoryFromToc && c.ChildCount() == 1 { + c = c.FirstChild() } return c.WriteString("") } -func (e *ePub) computeAspectRatio(epubParts []*epubPart) float64 { +func (e *EPub) computeAspectRatio(epubParts []*epubPart) float64 { var ( bestAspectRatio float64 bestAspectRatioCount int @@ -312,7 +312,7 @@ func (e *ePub) computeAspectRatio(epubParts []*epubPart) float64 { return bestAspectRatio } -func (e *ePub) computeViewPort(epubParts []*epubPart) { +func (e *EPub) computeViewPort(epubParts []*epubPart) { if e.Image.View.AspectRatio == -1 { return //keep device size } @@ -331,7 +331,7 @@ func (e *ePub) computeViewPort(epubParts []*epubPart) { } } -func (e *ePub) writePart(path string, currentPart, totalParts int, part *epubPart, imgStorage *epubzip.StorageImageReader) error { +func (e *EPub) writePart(path string, currentPart, totalParts int, part *epubPart, imgStorage *epubzip.StorageImageReader) error { hasTitlePage := e.TitlePage == 1 || (e.TitlePage == 2 && totalParts > 1) wz, err := epubzip.New(path) @@ -410,7 +410,7 @@ func (e *ePub) writePart(path string, currentPart, totalParts int, part *epubPar } // create the zip -func (e *ePub) Write() error { +func (e *EPub) Write() error { epubParts, imgStorage, err := e.getParts() if err != nil { return err diff --git a/internal/epub/imagefilters/epub_image_filters_autocontrast.go b/internal/epub/imagefilters/epub_image_filters_autocontrast.go index 48c9edf..f334a87 100644 --- a/internal/epub/imagefilters/epub_image_filters_autocontrast.go +++ b/internal/epub/imagefilters/epub_image_filters_autocontrast.go @@ -9,7 +9,7 @@ import ( ) // AutoContrast Automatically improve contrast -func AutoContrast() *autocontrast { +func AutoContrast() gift.Filter { return &autocontrast{} } diff --git a/internal/epub/tree/epub_tree.go b/internal/epub/tree/epub_tree.go index a6557b6..6afac0a 100644 --- a/internal/epub/tree/epub_tree.go +++ b/internal/epub/tree/epub_tree.go @@ -23,52 +23,60 @@ import ( "strings" ) -type tree struct { - Nodes map[string]*node +type Tree struct { + nodes map[string]*Node } -type node struct { - Value string - Children []*node +type Node struct { + value string + children []*Node } // New initialize tree with a root node -func New() *tree { - return &tree{map[string]*node{ - ".": {".", []*node{}}, +func New() *Tree { + return &Tree{map[string]*Node{ + ".": {".", []*Node{}}, }} } // Root root node -func (n *tree) Root() *node { - return n.Nodes["."] +func (n *Tree) Root() *Node { + return n.nodes["."] } // Add the filename to the tree -func (n *tree) Add(filename string) { +func (n *Tree) Add(filename string) { cn := n.Root() cp := "" for _, p := range strings.Split(filepath.Clean(filename), string(filepath.Separator)) { cp = filepath.Join(cp, p) - if _, ok := n.Nodes[cp]; !ok { - n.Nodes[cp] = &node{Value: p, Children: []*node{}} - cn.Children = append(cn.Children, n.Nodes[cp]) + if _, ok := n.nodes[cp]; !ok { + n.nodes[cp] = &Node{value: p, children: []*Node{}} + cn.children = append(cn.children, n.nodes[cp]) } - cn = n.Nodes[cp] + cn = n.nodes[cp] } } +func (n *Node) ChildCount() int { + return len(n.children) +} + +func (n *Node) FirstChild() *Node { + return n.children[0] +} + // WriteString string version of the tree -func (n *node) WriteString(indent string) string { +func (n *Node) WriteString(indent string) string { r := strings.Builder{} if indent != "" { r.WriteString(indent) r.WriteString("- ") - r.WriteString(n.Value) + r.WriteString(n.value) r.WriteString("\n") } indent += " " - for _, c := range n.Children { + for _, c := range n.children { r.WriteString(c.WriteString(indent)) } return r.String() diff --git a/internal/epub/zip/epub_zip.go b/internal/epub/zip/epub_zip.go index 207d958..28e0890 100644 --- a/internal/epub/zip/epub_zip.go +++ b/internal/epub/zip/epub_zip.go @@ -38,7 +38,7 @@ func (e *EPUBZip) Close() error { // // This will be valid with epubcheck tools. func (e *EPUBZip) WriteMagic() error { - t := time.Now() + t := time.Now().UTC() fh := &zip.FileHeader{ Name: "mimetype", Method: zip.Store, @@ -49,6 +49,8 @@ func (e *EPUBZip) WriteMagic() error { UncompressedSize64: 20, CRC32: 0x2cab616f, } + fh.CreatorVersion = fh.CreatorVersion&0xff00 | 20 // preserve compatibility byte + fh.ReaderVersion = 20 fh.SetMode(0600) m, err := e.wz.CreateRaw(fh) diff --git a/internal/sortpath/sortpath.go b/internal/sortpath/sortpath.go index fc27038..6570127 100644 --- a/internal/sortpath/sortpath.go +++ b/internal/sortpath/sortpath.go @@ -26,6 +26,8 @@ Example: */ package sortpath +import "sort" + // struct that implement interface for sort.Sort type by struct { filenames []string @@ -40,7 +42,7 @@ func (b by) Swap(i, j int) { } // By use sortpath.By with sort.Sort -func By(filenames []string, mode int) by { +func By(filenames []string, mode int) sort.Interface { var p [][]part for _, filename := range filenames { p = append(p, parse(filename, mode))