forked from metafates/mangal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
comicinfo.go
82 lines (67 loc) · 1.67 KB
/
comicinfo.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
package update
import (
"encoding/xml"
"fmt"
"github.com/belphemur/mangal/filesystem"
"github.com/belphemur/mangal/source"
"github.com/belphemur/mangal/util"
"os"
"path/filepath"
"strings"
)
func getAnyChapterComicInfo(mangaPath string) (*source.ComicInfo, error) {
// recursively search for .cbz files
// find the first one and get the name from it
var cbzFiles []string
err := filepath.Walk(mangaPath, func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".cbz") {
cbzFiles = append(cbzFiles, path)
}
return nil
})
if err != nil {
return nil, err
}
if len(cbzFiles) == 0 {
return nil, fmt.Errorf("no .cbz files found")
}
comicInfo, err := getComicInfoXML(cbzFiles[0])
if err != nil {
return nil, err
}
return comicInfo, nil
}
func getComicInfoXML(chapter string) (*source.ComicInfo, error) {
if !strings.HasSuffix(chapter, ".cbz") {
return nil, fmt.Errorf("chapter must be a .cbz file")
}
// open chapter as ReaderAt
file, err := filesystem.Api().Open(chapter)
if err != nil {
return nil, err
}
filesystem.SetMemMapFs()
defer filesystem.SetOsFs()
// extract ComicInfo.xml
stat, err := file.Stat()
if err != nil {
return nil, err
}
// No need to delete the file as it will be deleted when the memmap filesystem is reset
err = util.Unzip(file, stat.Size(), "T")
if err != nil {
return nil, err
}
// read ComicInfo.xml
contents, err := filesystem.Api().ReadFile(filepath.Join("T", "ComicInfo.xml"))
if err != nil {
return nil, err
}
// parse ComicInfo.xml
var comicInfo source.ComicInfo
err = xml.Unmarshal(contents, &comicInfo)
if err != nil {
return nil, err
}
return &comicInfo, nil
}