mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-24 15:52:38 +02:00
move epub image data and epub zip
This commit is contained in:
parent
1ad8f9ddd0
commit
2444dfee3b
@ -10,6 +10,7 @@ import (
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/epub/epubzip"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/epub/templates"
|
||||
"github.com/gofrs/uuid"
|
||||
)
|
||||
@ -95,7 +96,7 @@ func (e *ePub) render(templateString string, data any) string {
|
||||
return stripBlank.ReplaceAllString(result.String(), "\n")
|
||||
}
|
||||
|
||||
func (e *ePub) writeImage(wz *epubZip, img *Image) error {
|
||||
func (e *ePub) writeImage(wz *epubzip.EpubZip, img *Image) error {
|
||||
err := wz.WriteFile(
|
||||
fmt.Sprintf("OEBPS/%s", img.TextPath()),
|
||||
e.render(templates.Text, map[string]any{
|
||||
@ -113,7 +114,7 @@ func (e *ePub) writeImage(wz *epubZip, img *Image) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (e *ePub) writeBlank(wz *epubZip, img *Image) error {
|
||||
func (e *ePub) writeBlank(wz *epubzip.EpubZip, img *Image) error {
|
||||
return wz.WriteFile(
|
||||
fmt.Sprintf("OEBPS/Text/%d_sp.xhtml", img.Id),
|
||||
e.render(templates.Blank, map[string]any{
|
||||
@ -230,7 +231,7 @@ func (e *ePub) Write() error {
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("%s%s%s", e.Output[0:len(e.Output)-len(ext)], suffix, ext)
|
||||
wz, err := newEpubZip(path)
|
||||
wz, err := epubzip.New(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/epub/imagedata"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/epub/sortpath"
|
||||
"github.com/disintegration/gift"
|
||||
"github.com/golang/freetype"
|
||||
@ -34,7 +35,7 @@ type Image struct {
|
||||
Id int
|
||||
Part int
|
||||
Raw image.Image
|
||||
Data *ImageData
|
||||
Data *imagedata.ImageData
|
||||
Width int
|
||||
Height int
|
||||
IsCover bool
|
||||
@ -238,7 +239,7 @@ func (e *ePub) LoadImages() ([]*Image, error) {
|
||||
Id: img.Id,
|
||||
Part: 0,
|
||||
Raw: raw,
|
||||
Data: newImageData(img.Id, 0, dst, e.ImageOptions.Quality),
|
||||
Data: imagedata.New(img.Id, 0, dst, e.ImageOptions.Quality),
|
||||
Width: dst.Bounds().Dx(),
|
||||
Height: dst.Bounds().Dy(),
|
||||
IsCover: img.Id == 0,
|
||||
@ -266,7 +267,7 @@ func (e *ePub) LoadImages() ([]*Image, error) {
|
||||
imageOutput <- &Image{
|
||||
Id: img.Id,
|
||||
Part: part,
|
||||
Data: newImageData(img.Id, part, dst, e.ImageOptions.Quality),
|
||||
Data: imagedata.New(img.Id, part, dst, e.ImageOptions.Quality),
|
||||
Width: dst.Bounds().Dx(),
|
||||
Height: dst.Bounds().Dy(),
|
||||
IsCover: false,
|
||||
@ -519,7 +520,7 @@ func loadPdf(input string) (int, chan *imageTask, error) {
|
||||
return nbPages, output, nil
|
||||
}
|
||||
|
||||
func (e *ePub) createTitleImageDate(title string, img *Image, currentPart, totalPart int) *ImageData {
|
||||
func (e *ePub) createTitleImageDate(title string, img *Image, currentPart, totalPart int) *imagedata.ImageData {
|
||||
// Create a blur version of the cover
|
||||
g := gift.New(gift.GaussianBlur(8))
|
||||
dst := image.NewGray(g.Bounds(img.Raw.Bounds()))
|
||||
@ -576,5 +577,5 @@ func (e *ePub) createTitleImageDate(title string, img *Image, currentPart, total
|
||||
}
|
||||
c.DrawString(title, freetype.Pt(textLeft, img.Height/2+textHeight/4))
|
||||
|
||||
return newData("OEBPS/Images/title.jpg", dst, e.Quality)
|
||||
return imagedata.NewRaw("OEBPS/Images/title.jpg", dst, e.Quality)
|
||||
}
|
||||
|
@ -1,34 +1,36 @@
|
||||
package epub
|
||||
package epubzip
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/epub/imagedata"
|
||||
)
|
||||
|
||||
type epubZip struct {
|
||||
type EpubZip struct {
|
||||
w *os.File
|
||||
wz *zip.Writer
|
||||
}
|
||||
|
||||
func newEpubZip(path string) (*epubZip, error) {
|
||||
func New(path string) (*EpubZip, error) {
|
||||
w, err := os.Create(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
wz := zip.NewWriter(w)
|
||||
return &epubZip{w, wz}, nil
|
||||
return &EpubZip{w, wz}, nil
|
||||
}
|
||||
|
||||
func (e *epubZip) Close() error {
|
||||
func (e *EpubZip) Close() error {
|
||||
if err := e.wz.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return e.w.Close()
|
||||
}
|
||||
|
||||
func (e *epubZip) WriteMagic() error {
|
||||
func (e *EpubZip) WriteMagic() error {
|
||||
t := time.Now()
|
||||
fh := &zip.FileHeader{
|
||||
Name: "mimetype",
|
||||
@ -50,7 +52,7 @@ func (e *epubZip) WriteMagic() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (e *epubZip) WriteImage(image *ImageData) error {
|
||||
func (e *EpubZip) WriteImage(image *imagedata.ImageData) error {
|
||||
m, err := e.wz.CreateRaw(image.Header)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -59,7 +61,7 @@ func (e *epubZip) WriteImage(image *ImageData) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (e *epubZip) WriteFile(file string, data any) error {
|
||||
func (e *EpubZip) WriteFile(file string, data any) error {
|
||||
var content []byte
|
||||
switch b := data.(type) {
|
||||
case string:
|
@ -1,4 +1,4 @@
|
||||
package epub
|
||||
package imagedata
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
@ -20,12 +20,12 @@ func (img *ImageData) CompressedSize() uint64 {
|
||||
return img.Header.CompressedSize64 + 30 + uint64(len(img.Header.Name))
|
||||
}
|
||||
|
||||
func newImageData(id int, part int, img image.Image, quality int) *ImageData {
|
||||
func New(id int, part int, img image.Image, quality int) *ImageData {
|
||||
name := fmt.Sprintf("OEBPS/Images/%d_p%d.jpg", id, part)
|
||||
return newData(name, img, quality)
|
||||
return NewRaw(name, img, quality)
|
||||
}
|
||||
|
||||
func newData(name string, img image.Image, quality int) *ImageData {
|
||||
func NewRaw(name string, img image.Image, quality int) *ImageData {
|
||||
data := bytes.NewBuffer([]byte{})
|
||||
if err := jpeg.Encode(data, img, &jpeg.Options{Quality: quality}); err != nil {
|
||||
panic(err)
|
Loading…
x
Reference in New Issue
Block a user