mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-24 15:52:38 +02:00
write buffer
This commit is contained in:
parent
c6fec88582
commit
0ee44ed40b
@ -3,6 +3,7 @@ package epub
|
|||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -22,7 +23,7 @@ import (
|
|||||||
|
|
||||||
type ImageDetails struct {
|
type ImageDetails struct {
|
||||||
*Images
|
*Images
|
||||||
Data string
|
Data io.Reader
|
||||||
Width int
|
Width int
|
||||||
Height int
|
Height int
|
||||||
}
|
}
|
||||||
@ -98,7 +99,11 @@ func (e *EPub) SetCrop(c bool) *EPub {
|
|||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *EPub) WriteFile(wz *zip.Writer, file, content string) error {
|
func (e *EPub) WriteString(wz *zip.Writer, file string, content string) error {
|
||||||
|
return e.WriteBuffer(wz, file, strings.NewReader(content))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *EPub) WriteBuffer(wz *zip.Writer, file string, content io.Reader) error {
|
||||||
m, err := wz.CreateHeader(&zip.FileHeader{
|
m, err := wz.CreateHeader(&zip.FileHeader{
|
||||||
Name: file,
|
Name: file,
|
||||||
Modified: time.Now(),
|
Modified: time.Now(),
|
||||||
@ -106,7 +111,7 @@ func (e *EPub) WriteFile(wz *zip.Writer, file, content string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = m.Write([]byte(content))
|
_, err = io.Copy(m, content)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,7 +225,7 @@ func (e *EPub) Write() error {
|
|||||||
wz := zip.NewWriter(w)
|
wz := zip.NewWriter(w)
|
||||||
defer wz.Close()
|
defer wz.Close()
|
||||||
for _, content := range zipContent {
|
for _, content := range zipContent {
|
||||||
if err := e.WriteFile(wz, content[0], content[1]); err != nil {
|
if err := e.WriteString(wz, content[0], content[1]); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -230,10 +235,10 @@ func (e *EPub) Write() error {
|
|||||||
for img := range e.ProcessingImages() {
|
for img := range e.ProcessingImages() {
|
||||||
text := fmt.Sprintf("OEBPS/Text/%s.xhtml", img.Title)
|
text := fmt.Sprintf("OEBPS/Text/%s.xhtml", img.Title)
|
||||||
image := fmt.Sprintf("OEBPS/Images/%s.jpg", img.Title)
|
image := fmt.Sprintf("OEBPS/Images/%s.jpg", img.Title)
|
||||||
if err := e.WriteFile(wz, text, e.Render(TEMPLATE_TEXT, img)); err != nil {
|
if err := e.WriteString(wz, text, e.Render(TEMPLATE_TEXT, img)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := e.WriteFile(wz, image, img.Data); err != nil {
|
if err := e.WriteBuffer(wz, image, img.Data); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
bar.Add(1)
|
bar.Add(1)
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"golang.org/x/image/draw"
|
"golang.org/x/image/draw"
|
||||||
@ -116,13 +117,13 @@ func Resize(img *image.Gray, w, h int) *image.Gray {
|
|||||||
return newImg
|
return newImg
|
||||||
}
|
}
|
||||||
|
|
||||||
func Get(img *image.Gray, quality int) string {
|
func Get(img *image.Gray, quality int) io.Reader {
|
||||||
b := bytes.NewBuffer([]byte{})
|
b := bytes.NewBuffer([]byte{})
|
||||||
err := jpeg.Encode(b, img, &jpeg.Options{Quality: quality})
|
err := jpeg.Encode(b, img, &jpeg.Options{Quality: quality})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
return string(b.Bytes())
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func Save(img *image.Gray, output string, quality int) {
|
func Save(img *image.Gray, output string, quality int) {
|
||||||
@ -142,7 +143,7 @@ func Save(img *image.Gray, output string, quality int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Convert(path string, crop bool, w, h int, quality int) (string, int, int) {
|
func Convert(path string, crop bool, w, h int, quality int) (io.Reader, int, int) {
|
||||||
img := Load(path)
|
img := Load(path)
|
||||||
if crop {
|
if crop {
|
||||||
img = CropMarging(img)
|
img = CropMarging(img)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user