Compare commits

..

No commits in common. "971d7d41e0ba089d91e8ee940666adbbf9b410f0" and "5169ccc1714a92ddb8ddd9f9250eef409ff342cf" have entirely different histories.

6 changed files with 45 additions and 129 deletions

View File

@ -48,8 +48,7 @@ func Compress(archPath, contentDir string) error {
)
log.Printf("compressing %s...", archPath)
if output, err := toolCmd.CombinedOutput(); err != nil {
fmt.Println(string(output))
if err := toolCmd.Run(); err != nil {
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
log.Printf("decompressing %s...", archPath)
if output, err := toolCmd.CombinedOutput(); err != nil {
fmt.Println(string(output))
if err := toolCmd.Run(); err != nil {
return "", errors.Join(fmt.Errorf("decompression of %s failed", archPath), err)
}

View File

164
media.go
View File

@ -2,39 +2,19 @@ package mex
import (
"bytes"
"crypto/sha256"
_ "embed"
"errors"
"fmt"
"html/template"
"io"
"log"
"math"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"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 {
switch strings.ToLower(filepath.Ext(path)) {
case ".jpg", ".jpeg", ".png", ".webp", ".bmp", ".gif":
@ -112,13 +92,23 @@ type Volume struct {
Book *Book
Pages []*Page
Index int
}
avgSize int
hash []byte
func (self *Volume) AveragePageSize() int {
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 {
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 {
return err
}
@ -155,28 +145,24 @@ func (self *Volume) export(path string, config ExportConfig, allocator *TempDirA
return nil
}
func (self *Volume) compare(other *Volume) int {
func (self *Volume) supercedes(other *Volume) bool {
if len(self.Pages) > len(other.Pages) {
return 1
} else 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)
return true
}
if self.avgSize > other.avgSize {
return 1
} else if self.avgSize < other.avgSize {
return -1
if self.AveragePageSize() > other.AveragePageSize() {
log.Printf("picking %s over %s because it has larger pages", self.Node.Name, other.Node.Name)
return true
}
return bytes.Compare(self.hash, other.hash)
return false
}
type Book struct {
Node *Node
Volumes map[int]*Volume
VolumeCount int
orphans []*Volume
Node *Node
Volumes map[int]*Volume
MaxVolume int
}
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
}
func (self *Book) addVolume(newVolume *Volume) {
insert := func(v *Volume) {
self.Volumes[v.Index] = v
if v.Index >= self.VolumeCount {
self.VolumeCount = v.Index + 1
}
}
currVolume, _ := self.Volumes[newVolume.Index]
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) addVolume(volume *Volume) {
currVolume, _ := self.Volumes[volume.Index]
if currVolume == nil || volume.supercedes(currVolume) {
self.Volumes[volume.Index] = volume
}
}
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 {
func (self *Book) parseVolumes(node *Node) {
if !node.Info.IsDir() {
return nil
return
}
volume := &Volume{
@ -289,55 +250,29 @@ func (self *Book) parseVolumes(node *Node) error {
var pageIndex int
for _, child := range node.Children {
if child.Info.IsDir() {
if err := self.parseVolumes(child); err != nil {
return err
}
self.parseVolumes(child)
} else if isImagePath(child.Name) {
volume.Pages = append(volume.Pages, &Page{child, volume, pageIndex})
pageIndex++
}
}
if len(volume.Pages) == 0 {
return nil
}
if len(volume.Pages) > 0 {
exp := regexp.MustCompile(`(\d+)\D*$`)
if matches := exp.FindStringSubmatch(node.Name); len(matches) >= 2 {
index, err := strconv.ParseInt(matches[1], 10, 32)
if err != nil {
panic(err)
}
sort.Slice(volume.Pages, func(i, j int) bool {
return strings.Compare(volume.Pages[i].Node.Name, volume.Pages[j].Node.Name) < 0
})
volume.Index = int(index)
if volume.Index > self.MaxVolume {
self.MaxVolume = volume.Index
}
var (
hasher = sha256.New()
totalSize = 0
)
for _, page := range volume.Pages {
fp, err := os.Open(page.Node.Path)
if err != nil {
return err
self.addVolume(volume)
}
size, err := io.Copy(hasher, fp)
fp.Close()
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)
} else {
self.addOrphan(volume)
}
return nil
}
func ParseBook(node *Node) (*Book, error) {
@ -348,19 +283,6 @@ func ParseBook(node *Node) (*Book, error) {
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 {
return nil, errors.New("no volumes found")
}

View File

@ -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*$