/
zip.go
190 lines (157 loc) · 3.99 KB
/
zip.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package file
import (
"archive/zip"
"bytes"
"errors"
"fmt"
"io"
"io/fs"
"path/filepath"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models"
"github.com/xWTF/chardet"
"golang.org/x/net/html/charset"
"golang.org/x/text/transform"
)
var (
errNotReaderAt = errors.New("not a ReaderAt")
errZipFSOpenZip = errors.New("cannot open zip file inside zip file")
)
// ZipFS is a file system backed by a zip file.
type zipFS struct {
*zip.Reader
zipFileCloser io.Closer
zipInfo fs.FileInfo
zipPath string
}
func newZipFS(fs models.FS, path string, info fs.FileInfo) (*zipFS, error) {
reader, err := fs.Open(path)
if err != nil {
return nil, err
}
asReaderAt, _ := reader.(io.ReaderAt)
if asReaderAt == nil {
reader.Close()
return nil, errNotReaderAt
}
zipReader, err := zip.NewReader(asReaderAt, info.Size())
if err != nil {
reader.Close()
return nil, err
}
// Concat all Name and Comment for better detection result
var buffer bytes.Buffer
for _, f := range zipReader.File {
buffer.WriteString(f.Name)
buffer.WriteString(f.Comment)
}
buffer.WriteString(zipReader.Comment)
// Detect encoding
d, err := chardet.NewTextDetector().DetectBest(buffer.Bytes())
if err != nil {
// If we can't detect the encoding, just assume it's UTF8
logger.Warnf("Unable to detect decoding for %s: %w", path, err)
}
// If the charset is not UTF8, decode'em
if d != nil && d.Charset != "UTF-8" {
logger.Debugf("Detected non-utf8 zip charset %s (%s): %s", d.Charset, d.Language, path)
e, _ := charset.Lookup(d.Charset)
if e == nil {
// if we can't find the encoding, just assume it's UTF8
logger.Warnf("Failed to lookup charset %s, language %s", d.Charset, d.Language)
} else {
decoder := e.NewDecoder()
for _, f := range zipReader.File {
newName, _, err := transform.String(decoder, f.Name)
if err != nil {
reader.Close()
logger.Warnf("Failed to decode %v: %v", []byte(f.Name), err)
} else {
f.Name = newName
}
// Comments are not decoded cuz stash doesn't use that
}
}
}
return &zipFS{
Reader: zipReader,
zipFileCloser: reader,
zipInfo: info,
zipPath: path,
}, nil
}
func (f *zipFS) rel(name string) (string, error) {
if f.zipPath == name {
return ".", nil
}
relName, err := filepath.Rel(f.zipPath, name)
if err != nil {
return "", fmt.Errorf("internal error getting relative path: %w", err)
}
// convert relName to use slash, since zip files do so regardless
// of os
relName = filepath.ToSlash(relName)
return relName, nil
}
func (f *zipFS) Stat(name string) (fs.FileInfo, error) {
reader, err := f.Open(name)
if err != nil {
return nil, err
}
defer reader.Close()
return reader.Stat()
}
func (f *zipFS) Lstat(name string) (fs.FileInfo, error) {
return f.Stat(name)
}
func (f *zipFS) OpenZip(name string) (models.ZipFS, error) {
return nil, errZipFSOpenZip
}
func (f *zipFS) IsPathCaseSensitive(path string) (bool, error) {
return true, nil
}
type zipReadDirFile struct {
fs.File
}
func (f *zipReadDirFile) ReadDir(n int) ([]fs.DirEntry, error) {
asReadDirFile, _ := f.File.(fs.ReadDirFile)
if asReadDirFile == nil {
return nil, fmt.Errorf("internal error: not a ReadDirFile")
}
return asReadDirFile.ReadDir(n)
}
func (f *zipFS) Open(name string) (fs.ReadDirFile, error) {
relName, err := f.rel(name)
if err != nil {
return nil, err
}
r, err := f.Reader.Open(relName)
if err != nil {
return nil, err
}
return &zipReadDirFile{
File: r,
}, nil
}
func (f *zipFS) Close() error {
return f.zipFileCloser.Close()
}
// openOnly returns a ReadCloser where calling Close will close the zip fs as well.
func (f *zipFS) OpenOnly(name string) (io.ReadCloser, error) {
r, err := f.Open(name)
if err != nil {
return nil, err
}
return &wrappedReadCloser{
ReadCloser: r,
outer: f,
}, nil
}
type wrappedReadCloser struct {
io.ReadCloser
outer io.Closer
}
func (f *wrappedReadCloser) Close() error {
_ = f.ReadCloser.Close()
return f.outer.Close()
}