Compare commits
No commits in common. "971d7d41e0ba089d91e8ee940666adbbf9b410f0" and "5169ccc1714a92ddb8ddd9f9250eef409ff342cf" have entirely different histories.
971d7d41e0
...
5169ccc171
6
arch.go
6
arch.go
@ -48,8 +48,7 @@ func Compress(archPath, contentDir string) error {
|
|||||||
)
|
)
|
||||||
|
|
||||||
log.Printf("compressing %s...", archPath)
|
log.Printf("compressing %s...", archPath)
|
||||||
if output, err := toolCmd.CombinedOutput(); err != nil {
|
if err := toolCmd.Run(); err != nil {
|
||||||
fmt.Println(string(output))
|
|
||||||
return errors.Join(fmt.Errorf("compression of %s failed", archPath), err)
|
return errors.Join(fmt.Errorf("compression of %s failed", archPath), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,8 +91,7 @@ func Decompress(archPath string, allocator *TempDirAllocator) (string, error) {
|
|||||||
toolCmd.Dir = contentDir
|
toolCmd.Dir = contentDir
|
||||||
|
|
||||||
log.Printf("decompressing %s...", archPath)
|
log.Printf("decompressing %s...", archPath)
|
||||||
if output, err := toolCmd.CombinedOutput(); err != nil {
|
if err := toolCmd.Run(); err != nil {
|
||||||
fmt.Println(string(output))
|
|
||||||
return "", errors.Join(fmt.Errorf("decompression of %s failed", archPath), err)
|
return "", errors.Join(fmt.Errorf("decompression of %s failed", archPath), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
0
mex/.gitignore → cmd/.gitignore
vendored
0
mex/.gitignore → cmd/.gitignore
vendored
154
media.go
154
media.go
@ -2,39 +2,19 @@ package mex
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha256"
|
|
||||||
_ "embed"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed regexp.txt
|
|
||||||
var volumeExpStrs string
|
|
||||||
|
|
||||||
func parseVolumeIndex(path string) *int {
|
|
||||||
for _, expStr := range strings.Split(volumeExpStrs, "\n") {
|
|
||||||
exp := regexp.MustCompile(expStr)
|
|
||||||
if matches := exp.FindStringSubmatch(filepath.Base(path)); len(matches) >= 2 {
|
|
||||||
if index, err := strconv.ParseInt(matches[1], 10, 32); err == nil {
|
|
||||||
indexInt := int(index)
|
|
||||||
return &indexInt
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func isImagePath(path string) bool {
|
func isImagePath(path string) bool {
|
||||||
switch strings.ToLower(filepath.Ext(path)) {
|
switch strings.ToLower(filepath.Ext(path)) {
|
||||||
case ".jpg", ".jpeg", ".png", ".webp", ".bmp", ".gif":
|
case ".jpg", ".jpeg", ".png", ".webp", ".bmp", ".gif":
|
||||||
@ -112,13 +92,23 @@ type Volume struct {
|
|||||||
Book *Book
|
Book *Book
|
||||||
Pages []*Page
|
Pages []*Page
|
||||||
Index int
|
Index int
|
||||||
|
}
|
||||||
|
|
||||||
avgSize int
|
func (self *Volume) AveragePageSize() int {
|
||||||
hash []byte
|
if len(self.Pages) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
var totalSize int
|
||||||
|
for _, page := range self.Pages {
|
||||||
|
totalSize += int(page.Node.Info.Size())
|
||||||
|
}
|
||||||
|
|
||||||
|
return totalSize / len(self.Pages)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Volume) export(path string, config ExportConfig, allocator *TempDirAllocator) error {
|
func (self *Volume) export(path string, config ExportConfig, allocator *TempDirAllocator) error {
|
||||||
name, err := buildTemplatedName(config.VolumeTemplate, stripExt(self.Node.Name), self.Index, self.Book.VolumeCount)
|
name, err := buildTemplatedName(config.VolumeTemplate, stripExt(self.Node.Name), self.Index, self.Book.MaxVolume)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -155,28 +145,24 @@ func (self *Volume) export(path string, config ExportConfig, allocator *TempDirA
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Volume) compare(other *Volume) int {
|
func (self *Volume) supercedes(other *Volume) bool {
|
||||||
if len(self.Pages) > len(other.Pages) {
|
if len(self.Pages) > len(other.Pages) {
|
||||||
return 1
|
log.Printf("picking %s over %s because it has more pages", self.Node.Name, other.Node.Name)
|
||||||
} else if len(self.Pages) < len(other.Pages) {
|
return true
|
||||||
return -1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.avgSize > other.avgSize {
|
if self.AveragePageSize() > other.AveragePageSize() {
|
||||||
return 1
|
log.Printf("picking %s over %s because it has larger pages", self.Node.Name, other.Node.Name)
|
||||||
} else if self.avgSize < other.avgSize {
|
return true
|
||||||
return -1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return bytes.Compare(self.hash, other.hash)
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
type Book struct {
|
type Book struct {
|
||||||
Node *Node
|
Node *Node
|
||||||
Volumes map[int]*Volume
|
Volumes map[int]*Volume
|
||||||
VolumeCount int
|
MaxVolume int
|
||||||
|
|
||||||
orphans []*Volume
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Book) Export(path string, config ExportConfig, allocator *TempDirAllocator) error {
|
func (self *Book) Export(path string, config ExportConfig, allocator *TempDirAllocator) error {
|
||||||
@ -244,41 +230,16 @@ func (self *Book) Export(path string, config ExportConfig, allocator *TempDirAll
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Book) addVolume(newVolume *Volume) {
|
func (self *Book) addVolume(volume *Volume) {
|
||||||
insert := func(v *Volume) {
|
currVolume, _ := self.Volumes[volume.Index]
|
||||||
self.Volumes[v.Index] = v
|
if currVolume == nil || volume.supercedes(currVolume) {
|
||||||
if v.Index >= self.VolumeCount {
|
self.Volumes[volume.Index] = volume
|
||||||
self.VolumeCount = v.Index + 1
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
currVolume, _ := self.Volumes[newVolume.Index]
|
func (self *Book) parseVolumes(node *Node) {
|
||||||
if currVolume == nil {
|
|
||||||
insert(newVolume)
|
|
||||||
} else {
|
|
||||||
switch currVolume.compare(newVolume) {
|
|
||||||
case 1:
|
|
||||||
self.addOrphan(newVolume)
|
|
||||||
case -1:
|
|
||||||
self.addOrphan(currVolume)
|
|
||||||
insert(newVolume)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *Book) addOrphan(newVolume *Volume) {
|
|
||||||
for _, volume := range self.orphans {
|
|
||||||
if volume.compare(newVolume) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.orphans = append(self.orphans, newVolume)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *Book) parseVolumes(node *Node) error {
|
|
||||||
if !node.Info.IsDir() {
|
if !node.Info.IsDir() {
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
volume := &Volume{
|
volume := &Volume{
|
||||||
@ -289,55 +250,29 @@ func (self *Book) parseVolumes(node *Node) error {
|
|||||||
var pageIndex int
|
var pageIndex int
|
||||||
for _, child := range node.Children {
|
for _, child := range node.Children {
|
||||||
if child.Info.IsDir() {
|
if child.Info.IsDir() {
|
||||||
if err := self.parseVolumes(child); err != nil {
|
self.parseVolumes(child)
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else if isImagePath(child.Name) {
|
} else if isImagePath(child.Name) {
|
||||||
volume.Pages = append(volume.Pages, &Page{child, volume, pageIndex})
|
volume.Pages = append(volume.Pages, &Page{child, volume, pageIndex})
|
||||||
pageIndex++
|
pageIndex++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(volume.Pages) == 0 {
|
if len(volume.Pages) > 0 {
|
||||||
return nil
|
exp := regexp.MustCompile(`(\d+)\D*$`)
|
||||||
}
|
if matches := exp.FindStringSubmatch(node.Name); len(matches) >= 2 {
|
||||||
|
index, err := strconv.ParseInt(matches[1], 10, 32)
|
||||||
sort.Slice(volume.Pages, func(i, j int) bool {
|
|
||||||
return strings.Compare(volume.Pages[i].Node.Name, volume.Pages[j].Node.Name) < 0
|
|
||||||
})
|
|
||||||
|
|
||||||
var (
|
|
||||||
hasher = sha256.New()
|
|
||||||
totalSize = 0
|
|
||||||
)
|
|
||||||
|
|
||||||
for _, page := range volume.Pages {
|
|
||||||
fp, err := os.Open(page.Node.Path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
size, err := io.Copy(hasher, fp)
|
volume.Index = int(index)
|
||||||
fp.Close()
|
if volume.Index > self.MaxVolume {
|
||||||
|
self.MaxVolume = volume.Index
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
totalSize += int(size)
|
|
||||||
}
|
|
||||||
|
|
||||||
volume.avgSize = totalSize / len(volume.Pages)
|
|
||||||
volume.hash = hasher.Sum(nil)
|
|
||||||
|
|
||||||
if index := parseVolumeIndex(node.Name); index != nil {
|
|
||||||
volume.Index = *index
|
|
||||||
self.addVolume(volume)
|
self.addVolume(volume)
|
||||||
} else {
|
|
||||||
self.addOrphan(volume)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseBook(node *Node) (*Book, error) {
|
func ParseBook(node *Node) (*Book, error) {
|
||||||
@ -348,19 +283,6 @@ func ParseBook(node *Node) (*Book, error) {
|
|||||||
|
|
||||||
book.parseVolumes(node)
|
book.parseVolumes(node)
|
||||||
|
|
||||||
if len(book.orphans) > 0 {
|
|
||||||
sort.Slice(book.orphans, func(i, j int) bool {
|
|
||||||
return strings.Compare(book.orphans[i].Node.Name, book.orphans[j].Node.Name) < 0
|
|
||||||
})
|
|
||||||
|
|
||||||
for _, volume := range book.orphans {
|
|
||||||
volume.Index = book.VolumeCount
|
|
||||||
book.addVolume(volume)
|
|
||||||
}
|
|
||||||
|
|
||||||
book.orphans = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(book.Volumes) == 0 {
|
if len(book.Volumes) == 0 {
|
||||||
return nil, errors.New("no volumes found")
|
return nil, errors.New("no volumes found")
|
||||||
}
|
}
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
第\s*(\d+)\s*巻
|
|
||||||
(?i)vo?l?u?m?e?[.\s]*(\d+)
|
|
||||||
(?i)cha?p?t?e?r?[.\s]*(\d+)
|
|
||||||
(\d+)\D*$
|
|
Loading…
Reference in New Issue
Block a user