Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/internal/archive/iso9660/utils.go
1562 views
1
package iso9660
2
3
import (
4
"os"
5
stdpath "path"
6
"strings"
7
8
"github.com/alist-org/alist/v3/internal/errs"
9
"github.com/alist-org/alist/v3/internal/model"
10
"github.com/alist-org/alist/v3/internal/stream"
11
"github.com/alist-org/alist/v3/pkg/utils"
12
"github.com/kdomanski/iso9660"
13
)
14
15
func getImage(ss *stream.SeekableStream) (*iso9660.Image, error) {
16
reader, err := stream.NewReadAtSeeker(ss, 0)
17
if err != nil {
18
return nil, err
19
}
20
return iso9660.OpenImage(reader)
21
}
22
23
func getObj(img *iso9660.Image, path string) (*iso9660.File, error) {
24
obj, err := img.RootDir()
25
if err != nil {
26
return nil, err
27
}
28
if path == "/" {
29
return obj, nil
30
}
31
paths := strings.Split(strings.TrimPrefix(path, "/"), "/")
32
for _, p := range paths {
33
if !obj.IsDir() {
34
return nil, errs.ObjectNotFound
35
}
36
children, err := obj.GetChildren()
37
if err != nil {
38
return nil, err
39
}
40
exist := false
41
for _, child := range children {
42
if child.Name() == p {
43
obj = child
44
exist = true
45
break
46
}
47
}
48
if !exist {
49
return nil, errs.ObjectNotFound
50
}
51
}
52
return obj, nil
53
}
54
55
func toModelObj(file *iso9660.File) model.Obj {
56
return &model.Object{
57
Name: file.Name(),
58
Size: file.Size(),
59
Modified: file.ModTime(),
60
IsFolder: file.IsDir(),
61
}
62
}
63
64
func decompress(f *iso9660.File, path string, up model.UpdateProgress) error {
65
file, err := os.OpenFile(stdpath.Join(path, f.Name()), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
66
if err != nil {
67
return err
68
}
69
defer file.Close()
70
_, err = utils.CopyWithBuffer(file, &stream.ReaderUpdatingProgress{
71
Reader: &stream.SimpleReaderWithSize{
72
Reader: f.Reader(),
73
Size: f.Size(),
74
},
75
UpdateProgress: up,
76
})
77
return err
78
}
79
80
func decompressAll(children []*iso9660.File, path string) error {
81
for _, child := range children {
82
if child.IsDir() {
83
nextChildren, err := child.GetChildren()
84
if err != nil {
85
return err
86
}
87
nextPath := stdpath.Join(path, child.Name())
88
if err = os.MkdirAll(nextPath, 0700); err != nil {
89
return err
90
}
91
if err = decompressAll(nextChildren, nextPath); err != nil {
92
return err
93
}
94
} else {
95
if err := decompress(child, path, func(_ float64) {}); err != nil {
96
return err
97
}
98
}
99
}
100
return nil
101
}
102
103