forked from metafates/mangal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packer.go
232 lines (192 loc) · 5.04 KB
/
packer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"archive/zip"
"fmt"
"github.com/bmaupin/go-epub"
pdfcpu "github.com/pdfcpu/pdfcpu/pkg/api"
"github.com/spf13/afero"
"io"
"math/rand"
"path/filepath"
"strconv"
"strings"
)
// PackToPDF packs chapter to .pdf
func PackToPDF(images []string, destination string) (string, error) {
destination += ".pdf"
err := RemoveIfExists(destination)
if err != nil {
return "", err
}
// Create parent directory since pdfcpu have some troubles when it doesn't exist
if exists, err := Afero.Exists(filepath.Dir(destination)); err != nil {
return "", err
} else if !exists {
if err := Afero.MkdirAll(filepath.Dir(destination), 0777); err != nil {
return "", err
}
}
if err := pdfcpu.ImportImagesFile(images, destination, nil, nil); err != nil {
return "", err
}
return destination, nil
}
// EpubFile global epub file that is used to save multiple chapters in a single file
var EpubFile *epub.Epub
// PackToEpub adds chapter to the epub file
func PackToEpub(images []string, destination string) (string, error) {
coverSet := true
mangaTitle := filepath.Base(filepath.Dir(destination))
chapterTitle := filepath.Base(destination)
destination = filepath.Join(filepath.Dir(destination), mangaTitle+".epub")
// Initialize epub file
if EpubFile == nil {
coverSet = false
EpubFile = epub.NewEpub(mangaTitle)
EpubFile.SetPpd("rtl")
if err := RemoveIfExists(destination); err != nil {
return "", err
}
if err := Afero.MkdirAll(filepath.Dir(destination), 0777); err != nil {
return "", err
}
file, err := Afero.Create(destination)
if err != nil {
return "", err
}
if err = file.Close(); err != nil {
return "", err
}
}
var epubImages = make([]string, len(images))
for i, image := range images {
epubImage, err := EpubFile.AddImage(image, strconv.Itoa(rand.Intn(100000))+filepath.Base(image))
if err != nil {
return "", err
}
if !coverSet {
EpubFile.SetCover(epubImage, "")
coverSet = true
}
epubImages[i] = epubImage
}
imgTags := Map(epubImages, func(pathToImage string) string {
return fmt.Sprintf(`
<p style="display:block;margin:0;">
<img src="%s" style="height:auto;width:auto;"/>
</p>
`, pathToImage)
})
_, err := EpubFile.AddSection(strings.Join(imgTags, "\n"), chapterTitle, "", "")
if err != nil {
return "", err
}
return destination, nil
}
// PackToCBZ packs chapter to .cbz format
func PackToCBZ(images []string, destination string) (string, error) {
zipFile, err := PackToZip(images, destination, false)
if err != nil {
return "", err
}
cbzFile := zipFile + ".cbz"
err = RemoveIfExists(cbzFile)
if err != nil {
return "", err
}
if err := Afero.Rename(zipFile, cbzFile); err != nil {
return "", err
}
return cbzFile, nil
}
// PackToZip packs chapter to .zip format
func PackToZip(images []string, destination string, withExtension bool) (string, error) {
if withExtension {
destination += ".zip"
err := RemoveIfExists(destination)
if err != nil {
return "", err
}
}
if exists, err := Afero.Exists(filepath.Dir(destination)); err != nil {
return "", err
} else if !exists {
if err := Afero.MkdirAll(filepath.Dir(destination), 0777); err != nil {
return "", err
}
}
zipFile, err := Afero.Create(destination)
if err != nil {
return "", err
}
defer func(zipFile afero.File) {
_ = zipFile.Close()
}(zipFile)
zipWriter := zip.NewWriter(zipFile)
defer func(zipWriter *zip.Writer) {
_ = zipWriter.Close()
}(zipWriter)
for i, image := range images {
if err = addFileToZip(zipWriter, image, i); err != nil {
return "", err
}
}
return destination, nil
}
// addFileToZip adds files to zip writer
func addFileToZip(zipWriter *zip.Writer, filename string, index int) error {
file, err := Afero.Open(filename)
if err != nil {
return err
}
defer func(file afero.File) {
_ = file.Close()
}(file)
// Get the file information
info, err := file.Stat()
if err != nil {
return err
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Name = strconv.Itoa(index) + filepath.Ext(header.Name)
// Change to deflate to gain better compression
// see http://golang.org/pkg/archive/zip/#pkg-constants
header.Method = zip.Deflate
writer, err := zipWriter.CreateHeader(header)
if err != nil {
return err
}
_, err = io.Copy(writer, file)
return err
}
// PackToPlain packs chapter in the plain format
func PackToPlain(images []string, destination string) (string, error) {
err := Afero.MkdirAll(destination, 0700)
if err != nil {
return "", err
}
for i, image := range images {
imageContents, err := Afero.ReadFile(image)
if err != nil {
return "", err
}
if err = Afero.WriteFile(
filepath.Join(destination, strconv.Itoa(i)+filepath.Ext(image)),
imageContents,
0700,
); err != nil {
return "", err
}
}
return destination, nil
}
var Packers = map[FormatType]func([]string, string) (string, error){
PDF: PackToPDF,
Plain: PackToPlain,
Zip: func(f []string, d string) (string, error) { return PackToZip(f, d, true) },
CBZ: PackToCBZ,
Epub: PackToEpub,
}