mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-05-24 07:42:37 +02:00
stream rar to image converter
This commit is contained in:
parent
6721df759d
commit
dad70fc3fa
@ -210,42 +210,24 @@ func loadCbz(input string) (int, chan *imageTask, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func loadCbr(input string) (int, chan *imageTask, error) {
|
func loadCbr(input string) (int, chan *imageTask, error) {
|
||||||
rr, err := os.Open(input)
|
// listing and indexing
|
||||||
|
rl, err := rardecode.OpenReader(input, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, nil, err
|
return 0, nil, err
|
||||||
}
|
}
|
||||||
defer rr.Close()
|
names := make([]string, 0)
|
||||||
rs, err := rr.Stat()
|
|
||||||
if err != nil {
|
|
||||||
return 0, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
bar := progressbar.DefaultBytes(rs.Size(), "Uncompressing rar")
|
|
||||||
defer bar.Close()
|
|
||||||
|
|
||||||
r, err := rardecode.NewReader(io.TeeReader(rr, bar), "")
|
|
||||||
if err != nil {
|
|
||||||
return 0, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
type imageContent struct {
|
|
||||||
Name string
|
|
||||||
Data io.ReadCloser
|
|
||||||
}
|
|
||||||
|
|
||||||
images := make([]*imageContent, 0)
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
f, err := r.Next()
|
f, err := rl.Next()
|
||||||
|
|
||||||
|
if err != nil && err != io.EOF {
|
||||||
|
rl.Close()
|
||||||
|
return 0, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
if f == nil {
|
if f == nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return 0, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if f.IsDir {
|
if f.IsDir {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -254,32 +236,50 @@ func loadCbr(input string) (int, chan *imageTask, error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
b := bytes.NewBuffer([]byte{})
|
names = append(names, f.Name)
|
||||||
io.Copy(b, r)
|
|
||||||
|
|
||||||
images = append(images, &imageContent{
|
|
||||||
Name: f.Name,
|
|
||||||
Data: readFakeCloser{b},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
rl.Close()
|
||||||
|
|
||||||
if len(images) == 0 {
|
if len(names) == 0 {
|
||||||
return 0, nil, fmt.Errorf("no images found")
|
return 0, nil, fmt.Errorf("no images found")
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.SliceStable(images, func(i, j int) bool {
|
sort.Strings(names)
|
||||||
return strings.Compare(images[i].Name, images[j].Name) < 0
|
|
||||||
})
|
|
||||||
|
|
||||||
|
indexedNames := make(map[string]int)
|
||||||
|
for i, name := range names {
|
||||||
|
indexedNames[name] = i
|
||||||
|
}
|
||||||
|
|
||||||
|
// send file to the queue
|
||||||
output := make(chan *imageTask)
|
output := make(chan *imageTask)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(output)
|
defer close(output)
|
||||||
for i, img := range images {
|
r, err := rardecode.OpenReader(input, "")
|
||||||
output <- &imageTask{
|
if err != nil {
|
||||||
Id: i,
|
panic(err)
|
||||||
Reader: img.Data,
|
}
|
||||||
|
defer r.Close()
|
||||||
|
|
||||||
|
for {
|
||||||
|
f, err := r.Next()
|
||||||
|
if err != nil && err != io.EOF {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if f == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if idx, ok := indexedNames[f.Name]; ok {
|
||||||
|
b := bytes.NewBuffer([]byte{})
|
||||||
|
io.Copy(b, r)
|
||||||
|
|
||||||
|
output <- &imageTask{
|
||||||
|
Id: idx,
|
||||||
|
Reader: readFakeCloser{b},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
return len(images), output, nil
|
|
||||||
|
return len(names), output, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user