From 95eb3497e7cc40b45318f329ee1b4279f80e5f70 Mon Sep 17 00:00:00 2001
From: celogeek <65178+celogeek@users.noreply.github.com>
Date: Tue, 27 Dec 2022 15:32:54 +0100
Subject: [PATCH] add limit option

---
 internal/epub/core.go |  6 ++++++
 main.go               | 22 +++++++++++++++-------
 2 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/internal/epub/core.go b/internal/epub/core.go
index 5d6ab87..b0a6ee7 100644
--- a/internal/epub/core.go
+++ b/internal/epub/core.go
@@ -45,6 +45,7 @@ type EPub struct {
 	ViewHeight int
 	Quality    int
 	Crop       bool
+	LimitMb    int
 
 	Images          []*Images
 	FirstImageTitle string
@@ -99,6 +100,11 @@ func (e *EPub) SetCrop(c bool) *EPub {
 	return e
 }
 
+func (e *EPub) SetLimitMb(l int) *EPub {
+	e.LimitMb = l
+	return e
+}
+
 func (e *EPub) WriteString(wz *zip.Writer, file string, content string) error {
 	return e.WriteBuffer(wz, file, strings.NewReader(content))
 }
diff --git a/main.go b/main.go
index b369aaa..ad0073f 100644
--- a/main.go
+++ b/main.go
@@ -26,6 +26,7 @@ type Option struct {
 	Title   string
 	Quality int
 	NoCrop  bool
+	LimitMb int
 }
 
 func (o *Option) String() string {
@@ -36,15 +37,20 @@ func (o *Option) String() string {
 		width = profile.Width
 		height = profile.Height
 	}
+	limitmb := "nolimit"
+	if o.LimitMb > 0 {
+		limitmb = fmt.Sprintf("%d Mb", o.LimitMb)
+	}
 
 	return fmt.Sprintf(`Options:
-	Input  : %s
-	Output : %s
-	Profile: %s - %s - %dx%d
-	Author : %s
-	Title  : %s
-	Quality: %d
-	Crop   : %v
+    Input  : %s
+    Output : %s
+    Profile: %s - %s - %dx%d
+    Author : %s
+    Title  : %s
+    Quality: %d
+    Crop   : %v
+    LimitMb: %s
 `,
 		o.Input,
 		o.Output,
@@ -56,6 +62,7 @@ func (o *Option) String() string {
 		o.Title,
 		o.Quality,
 		!o.NoCrop,
+		limitmb,
 	)
 }
 
@@ -73,6 +80,7 @@ func main() {
 	flag.StringVar(&opt.Title, "title", "", "Title of the epub")
 	flag.IntVar(&opt.Quality, "quality", 85, "Quality of the image: Default 75")
 	flag.BoolVar(&opt.NoCrop, "nocrop", false, "Disable cropping: Default false")
+	flag.IntVar(&opt.LimitMb, "limitmb", 0, "Limit size of the ePub: Default nolimit")
 	flag.Parse()
 
 	if opt.Input == "" || opt.Output == "" {