A fast and simple blog backend.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
mediocre-blog/src/post/asset/loader_archive.go

57 lines
919 B

package asset
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"strings"
)
type archiveLoader struct {
loader Loader
}
func (l *archiveLoader) Load(path string, into io.Writer, opts LoadOpts) error {
id, subPath, ok := strings.Cut(strings.TrimPrefix(path, "/"), "/")
if !ok {
return l.loader.Load(path, into, opts)
}
var isGzipped bool
switch {
case strings.HasSuffix(id, ".tar.gz"),
strings.HasSuffix(id, ".tgz"):
isGzipped = true
case strings.HasSuffix(id, ".tar"):
// ok
default:
// unsupported
return l.loader.Load(path, into, opts)
}
buf := new(bytes.Buffer)
if err := l.loader.Load(id, buf, opts); err != nil {
return fmt.Errorf("loading archive into buffer: %w", err)
}
var (
from io.Reader = buf
err error
)
if isGzipped {
if from, err = gzip.NewReader(from); err != nil {
return fmt.Errorf("decompressing archive asset with id %q: %w", id, err)
}
}
}