move epubimage outside epub

This commit is contained in:
Celogeek 2024-01-04 19:26:41 +01:00
parent db58244946
commit cd34e4b962
Signed by: celogeek
SSH Key Fingerprint: SHA256:DEDfxIK2nUWXbslbRkww3zsauDjhWHlTXar+ak4lDJ4
5 changed files with 35 additions and 35 deletions

View File

@ -15,8 +15,8 @@ import (
"text/template" "text/template"
"time" "time"
epubimage "github.com/celogeek/go-comic-converter/v2/pkg/epub/image"
epubtemplates "github.com/celogeek/go-comic-converter/v2/pkg/epub/templates" epubtemplates "github.com/celogeek/go-comic-converter/v2/pkg/epub/templates"
"github.com/celogeek/go-comic-converter/v2/pkg/epubimage"
"github.com/celogeek/go-comic-converter/v2/pkg/epubimageprocessor" "github.com/celogeek/go-comic-converter/v2/pkg/epubimageprocessor"
"github.com/celogeek/go-comic-converter/v2/pkg/epuboptions" "github.com/celogeek/go-comic-converter/v2/pkg/epuboptions"
"github.com/celogeek/go-comic-converter/v2/pkg/epubprogress" "github.com/celogeek/go-comic-converter/v2/pkg/epubprogress"
@ -36,8 +36,8 @@ type ePub struct {
} }
type epubPart struct { type epubPart struct {
Cover *epubimage.Image Cover *epubimage.EPUBImage
Images []*epubimage.Image Images []*epubimage.EPUBImage
Reader *zip.ReadCloser Reader *zip.ReadCloser
} }
@ -71,7 +71,7 @@ func (e *ePub) render(templateString string, data map[string]any) string {
} }
// write image to the zip // 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.EPUBImage, zipImg *zip.File) error {
err := wz.WriteContent( err := wz.WriteContent(
img.EPUBPagePath(), img.EPUBPagePath(),
[]byte(e.render(epubtemplates.Text, map[string]any{ []byte(e.render(epubtemplates.Text, map[string]any{
@ -89,7 +89,7 @@ func (e *ePub) writeImage(wz *epubzip.EPUBZip, img *epubimage.Image, zipImg *zip
} }
// write blank page // write blank page
func (e *ePub) writeBlank(wz *epubzip.EPUBZip, img *epubimage.Image) error { func (e *ePub) writeBlank(wz *epubzip.EPUBZip, img *epubimage.EPUBImage) error {
return wz.WriteContent( return wz.WriteContent(
img.EPUBSpacePath(), img.EPUBSpacePath(),
[]byte(e.render(epubtemplates.Blank, map[string]any{ []byte(e.render(epubtemplates.Blank, map[string]any{
@ -100,7 +100,7 @@ func (e *ePub) writeBlank(wz *epubzip.EPUBZip, img *epubimage.Image) error {
} }
// write title image // 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.EPUBImage, part, totalParts int) error {
title := "Cover" title := "Cover"
text := "" text := ""
if totalParts > 1 { if totalParts > 1 {
@ -143,7 +143,7 @@ func (e *ePub) writeCoverImage(wz *epubzip.EPUBZip, img *epubimage.Image, part,
} }
// write title image // 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.EPUBImage, title string) error {
titleAlign := "" titleAlign := ""
if !e.Image.View.PortraitOnly { if !e.Image.View.PortraitOnly {
if e.Image.Manga { if e.Image.Manga {
@ -240,7 +240,7 @@ func (e *ePub) getParts() (parts []*epubPart, imgStorage *epubzip.EPUBZipImageRe
baseSize := uint64(128*1024) + imgStorage.Size(cover.EPUBImgPath())*2 baseSize := uint64(128*1024) + imgStorage.Size(cover.EPUBImgPath())*2
currentSize := baseSize currentSize := baseSize
currentImages := make([]*epubimage.Image, 0) currentImages := make([]*epubimage.EPUBImage, 0)
part := 1 part := 1
for _, img := range images { for _, img := range images {
@ -252,7 +252,7 @@ func (e *ePub) getParts() (parts []*epubPart, imgStorage *epubzip.EPUBZipImageRe
}) })
part += 1 part += 1
currentSize = baseSize currentSize = baseSize
currentImages = make([]*epubimage.Image, 0) currentImages = make([]*epubimage.EPUBImage, 0)
} }
currentSize += imgSize currentSize += imgSize
currentImages = append(currentImages, img) currentImages = append(currentImages, img)
@ -270,7 +270,7 @@ func (e *ePub) getParts() (parts []*epubPart, imgStorage *epubzip.EPUBZipImageRe
// create a tree from the directories. // create a tree from the directories.
// //
// this is used to simulate the toc. // this is used to simulate the toc.
func (e *ePub) getTree(images []*epubimage.Image, skip_files bool) string { func (e *ePub) getTree(images []*epubimage.EPUBImage, skip_files bool) string {
t := epubtree.New() t := epubtree.New()
for _, img := range images { for _, img := range images {
if skip_files { if skip_files {
@ -350,7 +350,7 @@ func (e *ePub) Write() error {
fmt.Fprintf(os.Stderr, "TOC:\n - %s\n%s\n", e.Title, e.getTree(p.Images, true)) fmt.Fprintf(os.Stderr, "TOC:\n - %s\n%s\n", e.Title, e.getTree(p.Images, true))
if e.DryVerbose { if e.DryVerbose {
if e.Image.HasCover { if e.Image.HasCover {
fmt.Fprintf(os.Stderr, "Cover:\n%s\n", e.getTree([]*epubimage.Image{p.Cover}, false)) fmt.Fprintf(os.Stderr, "Cover:\n%s\n", e.getTree([]*epubimage.EPUBImage{p.Cover}, false))
} }
fmt.Fprintf(os.Stderr, "Files:\n%s\n", e.getTree(p.Images, false)) fmt.Fprintf(os.Stderr, "Files:\n%s\n", e.getTree(p.Images, false))
} }

View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"github.com/beevik/etree" "github.com/beevik/etree"
epubimage "github.com/celogeek/go-comic-converter/v2/pkg/epub/image" "github.com/celogeek/go-comic-converter/v2/pkg/epubimage"
"github.com/celogeek/go-comic-converter/v2/pkg/epuboptions" "github.com/celogeek/go-comic-converter/v2/pkg/epuboptions"
) )
@ -16,8 +16,8 @@ type ContentOptions struct {
Publisher string Publisher string
UpdatedAt string UpdatedAt string
ImageOptions *epuboptions.Image ImageOptions *epuboptions.Image
Cover *epubimage.Image Cover *epubimage.EPUBImage
Images []*epubimage.Image Images []*epubimage.EPUBImage
Current int Current int
Total int Total int
} }
@ -140,7 +140,7 @@ func getMeta(o *ContentOptions) []tag {
func getManifest(o *ContentOptions) []tag { func getManifest(o *ContentOptions) []tag {
var imageTags, pageTags, spaceTags []tag var imageTags, pageTags, spaceTags []tag
addTag := func(img *epubimage.Image, withSpace bool) { addTag := func(img *epubimage.EPUBImage, withSpace bool) {
imageTags = append(imageTags, imageTags = append(imageTags,
tag{"item", tagAttrs{"id": img.ImgKey(), "href": img.ImgPath(), "media-type": fmt.Sprintf("image/%s", o.ImageOptions.Format)}, ""}, tag{"item", tagAttrs{"id": img.ImgKey(), "href": img.ImgPath(), "media-type": fmt.Sprintf("image/%s", o.ImageOptions.Format)}, ""},
) )

View File

@ -5,11 +5,11 @@ import (
"strings" "strings"
"github.com/beevik/etree" "github.com/beevik/etree"
epubimage "github.com/celogeek/go-comic-converter/v2/pkg/epub/image" "github.com/celogeek/go-comic-converter/v2/pkg/epubimage"
) )
// create toc // create toc
func Toc(title string, hasTitle bool, stripFirstDirectoryFromToc bool, images []*epubimage.Image) string { func Toc(title string, hasTitle bool, stripFirstDirectoryFromToc bool, images []*epubimage.EPUBImage) string {
doc := etree.NewDocument() doc := etree.NewDocument()
doc.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`) doc.CreateProcInst("xml", `version="1.0" encoding="UTF-8"`)
doc.CreateDirective("DOCTYPE html") doc.CreateDirective("DOCTYPE html")

View File

@ -9,7 +9,7 @@ import (
"strings" "strings"
) )
type Image struct { type EPUBImage struct {
Id int Id int
Part int Part int
Raw image.Image Raw image.Image
@ -27,47 +27,47 @@ type Image struct {
} }
// key name of the blank plage after the image // key name of the blank plage after the image
func (i *Image) SpaceKey() string { func (i *EPUBImage) SpaceKey() string {
return fmt.Sprintf("space_%d", i.Id) return fmt.Sprintf("space_%d", i.Id)
} }
// path of the blank page // path of the blank page
func (i *Image) SpacePath() string { func (i *EPUBImage) SpacePath() string {
return fmt.Sprintf("Text/%s.xhtml", i.SpaceKey()) return fmt.Sprintf("Text/%s.xhtml", i.SpaceKey())
} }
// path of the blank page into the EPUB // path of the blank page into the EPUB
func (i *Image) EPUBSpacePath() string { func (i *EPUBImage) EPUBSpacePath() string {
return fmt.Sprintf("OEBPS/%s", i.SpacePath()) return fmt.Sprintf("OEBPS/%s", i.SpacePath())
} }
// key for page // key for page
func (i *Image) PageKey() string { func (i *EPUBImage) PageKey() string {
return fmt.Sprintf("page_%d_p%d", i.Id, i.Part) return fmt.Sprintf("page_%d_p%d", i.Id, i.Part)
} }
// page path linked to the image // page path linked to the image
func (i *Image) PagePath() string { func (i *EPUBImage) PagePath() string {
return fmt.Sprintf("Text/%s.xhtml", i.PageKey()) return fmt.Sprintf("Text/%s.xhtml", i.PageKey())
} }
// page path into the EPUB // page path into the EPUB
func (i *Image) EPUBPagePath() string { func (i *EPUBImage) EPUBPagePath() string {
return fmt.Sprintf("OEBPS/%s", i.PagePath()) return fmt.Sprintf("OEBPS/%s", i.PagePath())
} }
// key for image // key for image
func (i *Image) ImgKey() string { func (i *EPUBImage) ImgKey() string {
return fmt.Sprintf("img_%d_p%d", i.Id, i.Part) return fmt.Sprintf("img_%d_p%d", i.Id, i.Part)
} }
// image path // image path
func (i *Image) ImgPath() string { func (i *EPUBImage) ImgPath() string {
return fmt.Sprintf("Images/%s.%s", i.ImgKey(), i.Format) return fmt.Sprintf("Images/%s.%s", i.ImgKey(), i.Format)
} }
// image path into the EPUB // image path into the EPUB
func (i *Image) EPUBImgPath() string { func (i *EPUBImage) EPUBImgPath() string {
return fmt.Sprintf("OEBPS/%s", i.ImgPath()) return fmt.Sprintf("OEBPS/%s", i.ImgPath())
} }
@ -75,7 +75,7 @@ func (i *Image) EPUBImgPath() string {
// //
// center by default. // center by default.
// align to left or right if it's part of the splitted double page. // align to left or right if it's part of the splitted double page.
func (i *Image) ImgStyle(viewWidth, viewHeight int, align string) string { func (i *EPUBImage) ImgStyle(viewWidth, viewHeight int, align string) string {
relWidth, relHeight := i.RelSize(viewWidth, viewHeight) relWidth, relHeight := i.RelSize(viewWidth, viewHeight)
marginW, marginH := float64(viewWidth-relWidth)/2, float64(viewHeight-relHeight)/2 marginW, marginH := float64(viewWidth-relWidth)/2, float64(viewHeight-relHeight)/2
@ -100,7 +100,7 @@ func (i *Image) ImgStyle(viewWidth, viewHeight int, align string) string {
return strings.Join(style, "; ") return strings.Join(style, "; ")
} }
func (i *Image) RelSize(viewWidth, viewHeight int) (relWidth, relHeight int) { func (i *EPUBImage) RelSize(viewWidth, viewHeight int) (relWidth, relHeight int) {
w, h := viewWidth, viewHeight w, h := viewWidth, viewHeight
srcw, srch := i.Width, i.Height srcw, srch := i.Width, i.Height

View File

@ -10,8 +10,8 @@ import (
"os" "os"
"sync" "sync"
epubimage "github.com/celogeek/go-comic-converter/v2/pkg/epub/image"
epubimagefilters "github.com/celogeek/go-comic-converter/v2/pkg/epub/imagefilters" epubimagefilters "github.com/celogeek/go-comic-converter/v2/pkg/epub/imagefilters"
"github.com/celogeek/go-comic-converter/v2/pkg/epubimage"
"github.com/celogeek/go-comic-converter/v2/pkg/epuboptions" "github.com/celogeek/go-comic-converter/v2/pkg/epuboptions"
"github.com/celogeek/go-comic-converter/v2/pkg/epubprogress" "github.com/celogeek/go-comic-converter/v2/pkg/epubprogress"
"github.com/celogeek/go-comic-converter/v2/pkg/epubzip" "github.com/celogeek/go-comic-converter/v2/pkg/epubzip"
@ -27,8 +27,8 @@ func New(o *epuboptions.EPUBOptions) *EPUBImageProcessor {
} }
// extract and convert images // extract and convert images
func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) { func (e *EPUBImageProcessor) Load() (images []*epubimage.EPUBImage, err error) {
images = make([]*epubimage.Image, 0) images = make([]*epubimage.EPUBImage, 0)
imageCount, imageInput, err := e.load() imageCount, imageInput, err := e.load()
if err != nil { if err != nil {
return nil, err return nil, err
@ -37,7 +37,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
// dry run, skip convertion // dry run, skip convertion
if e.Dry { if e.Dry {
for img := range imageInput { for img := range imageInput {
images = append(images, &epubimage.Image{ images = append(images, &epubimage.EPUBImage{
Id: img.Id, Id: img.Id,
Path: img.Path, Path: img.Path,
Name: img.Name, Name: img.Name,
@ -48,7 +48,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
return images, nil return images, nil
} }
imageOutput := make(chan *epubimage.Image) imageOutput := make(chan *epubimage.EPUBImage)
// processing // processing
bar := epubprogress.New(epubprogress.Options{ bar := epubprogress.New(epubprogress.Options{
@ -85,7 +85,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
raw = dst raw = dst
} }
img := &epubimage.Image{ img := &epubimage.EPUBImage{
Id: input.Id, Id: input.Id,
Part: part, Part: part,
Raw: raw, Raw: raw,