mirror of
https://github.com/Luzifer/tex-api.git
synced 2024-11-09 08:40:02 +00:00
Knut Ahlers
4937284b0e
this breaks the previous approach of writing the zip file to the file system and letting the script unpack the files itself Signed-off-by: Knut Ahlers <knut@ahlers.me>
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"io/fs"
|
|
"net/http"
|
|
"net/url"
|
|
"path"
|
|
"time"
|
|
|
|
"github.com/Luzifer/go_helpers/v2/str"
|
|
"github.com/gofrs/uuid"
|
|
"github.com/pkg/errors"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/spf13/afero"
|
|
)
|
|
|
|
const zipHeaderLength = 4
|
|
|
|
func hasZipHeader(data []byte) bool {
|
|
if len(data) < zipHeaderLength {
|
|
// ZIP header must be 4 chars
|
|
return false
|
|
}
|
|
|
|
return str.StringInSlice(string(data[:4]), []string{
|
|
// https://en.wikipedia.org/wiki/ZIP_(file_format)
|
|
"PK\x03\x04",
|
|
"PK\x05\x06",
|
|
"PK\x07\x08",
|
|
})
|
|
}
|
|
|
|
func pathFromUUID(uid uuid.UUID, filename string) string {
|
|
return path.Join(cfg.StorageDir, uid.String(), filename)
|
|
}
|
|
|
|
func serverErrorf(res http.ResponseWriter, err error, tpl string, args ...interface{}) {
|
|
logrus.WithError(err).Errorf(tpl, args...)
|
|
http.Error(res, "An error occurred. See details in log.", http.StatusInternalServerError)
|
|
}
|
|
|
|
func syncFilesToOverlay(base, overlay afero.Fs) error {
|
|
cow := afero.NewCopyOnWriteFs(base, overlay)
|
|
return errors.Wrap(
|
|
afero.Walk(cow, "", func(path string, info fs.FileInfo, err error) error {
|
|
if info.IsDir() {
|
|
return nil
|
|
}
|
|
|
|
return errors.Wrapf(
|
|
cow.Chtimes(path, time.Now(), time.Now()),
|
|
"triggering copy for %s by changing time", path,
|
|
)
|
|
}),
|
|
"copying files to overlay",
|
|
)
|
|
}
|
|
|
|
func urlMust(u *url.URL, err error) *url.URL {
|
|
if err != nil {
|
|
logrus.WithError(err).Fatal("Unable to retrieve URL from router")
|
|
}
|
|
return u
|
|
}
|