2023-02-09 12:16:36 +01:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ZipBundle struct {
|
2024-05-02 09:43:22 +02:00
|
|
|
reader *zip.Reader
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ZipBundle) File(filename string) (io.ReadCloser, os.FileInfo, error) {
|
|
|
|
ctx := logger.With(
|
|
|
|
context.Background(),
|
|
|
|
logger.F("filename", filename),
|
|
|
|
)
|
|
|
|
|
|
|
|
logger.Debug(ctx, "opening file")
|
|
|
|
|
2024-05-02 09:43:22 +02:00
|
|
|
f, err := b.reader.Open(filename)
|
2023-02-09 12:16:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stat, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return f, stat, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ZipBundle) Dir(dirname string) ([]os.FileInfo, error) {
|
|
|
|
files := make([]os.FileInfo, 0)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-05-02 09:43:22 +02:00
|
|
|
for _, f := range b.reader.File {
|
2023-02-09 12:16:36 +01:00
|
|
|
if !strings.HasPrefix(f.Name, dirname) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
relPath, err := filepath.Rel(dirname, f.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get relative path")
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug(
|
|
|
|
ctx, "checking file prefix",
|
|
|
|
logger.F("dirname", dirname),
|
|
|
|
logger.F("filename", f.Name),
|
|
|
|
logger.F("relpath", relPath),
|
|
|
|
)
|
|
|
|
|
|
|
|
if relPath == filepath.Base(f.Name) {
|
|
|
|
files = append(files, f.FileInfo())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
2024-05-02 09:43:22 +02:00
|
|
|
func NewZipBundleFromReader(reader io.ReaderAt, size int64) (*ZipBundle, error) {
|
|
|
|
zipReader, err := zip.NewReader(reader, size)
|
2023-02-09 12:16:36 +01:00
|
|
|
if err != nil {
|
2024-05-02 09:43:22 +02:00
|
|
|
return nil, errors.WithStack(err)
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|
|
|
|
|
2024-05-02 09:43:22 +02:00
|
|
|
return &ZipBundle{
|
|
|
|
reader: zipReader,
|
|
|
|
}, nil
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|
|
|
|
|
2024-05-02 09:43:22 +02:00
|
|
|
func NewZipBundleFromPath(filename string) (*ZipBundle, error) {
|
|
|
|
file, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stat, err := file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewZipBundleFromReader(file, stat.Size())
|
|
|
|
}
|
|
|
|
|
|
|
|
func Must(bundle Bundle, err error) Bundle {
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.WithStack(err))
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|
2024-05-02 09:43:22 +02:00
|
|
|
|
|
|
|
return bundle
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|