🐛 Bug fixes
All checks were successful
release-nightly / build-docker (push) Successful in 1m11s

This commit is contained in:
2023-12-10 19:19:40 +08:00
parent f490a8e309
commit 996827968d
8 changed files with 51 additions and 199 deletions

View File

@ -1,14 +1,15 @@
package administration
import (
"context"
"os"
"path/filepath"
"code.smartsheep.studio/goatworks/roadsign/pkg/filesystem"
"code.smartsheep.studio/goatworks/roadsign/pkg/sign"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/samber/lo"
"github.com/saracen/fastzip"
)
func doPublish(c *fiber.Ctx) error {
@ -64,7 +65,12 @@ func doPublish(c *fiber.Ctx) error {
if err := c.SaveFile(file, dst); err != nil {
return err
} else {
_ = filesystem.Unzip(dst, workdir)
if ex, err := fastzip.NewExtractor(dst, workdir); err != nil {
return err
} else if err = ex.Extract(context.Background()); err != nil {
defer ex.Close()
return err
}
}
default:
dst := filepath.Join(workdir, file.Filename)

View File

@ -30,13 +30,19 @@ func doSyncSite(c *fiber.Ctx) error {
defer file.Close()
}
pushed := false
sign.App.Sites = lo.Map(sign.App.Sites, func(item *sign.SiteConfig, idx int) *sign.SiteConfig {
if item.ID == id {
pushed = true
return &req
} else {
return item
}
})
if !pushed {
sign.App.Sites = append(sign.App.Sites, &req)
}
return c.SendStatus(fiber.StatusOK)
}

View File

@ -12,9 +12,9 @@ import (
"code.smartsheep.studio/goatworks/roadsign/pkg/sign"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/mholt/archiver/v4"
"github.com/rs/zerolog/log"
"github.com/samber/lo"
"github.com/saracen/fastzip"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"
)
@ -41,13 +41,7 @@ var DeployCommands = []*cli.Command{
if ctx.Args().Len() < 3 || !strings.HasSuffix(ctx.Args().Get(3), ".zip") {
log.Info().Msg("Preparing file to upload, please stand by...")
filelist, err := archiver.FilesFromDisk(nil, map[string]string{
lo.Ternary(ctx.Args().Len() > 3, ctx.Args().Get(4), "."): "",
})
if err != nil {
return fmt.Errorf("failed to prepare file: %q", err)
}
dest := lo.Ternary(ctx.Args().Len() > 3, ctx.Args().Get(3), workdir)
filename = filepath.Join(workdir, fmt.Sprintf("rds-deploy-cache-%s.zip", uuid.NewString()))
out, err := os.Create(filename)
if err != nil {
@ -55,8 +49,22 @@ var DeployCommands = []*cli.Command{
}
defer out.Close()
if err := (archiver.Zip{}).Archive(context.Background(), out, filelist); err != nil {
return fmt.Errorf("failed to prepare file: %q", err)
arc, err := fastzip.NewArchiver(out, dest)
if err != nil {
return err
}
defer arc.Close()
filelist := make(map[string]os.FileInfo)
if err := filepath.Walk(dest, func(pathname string, info os.FileInfo, err error) error {
filelist[pathname] = info
return nil
}); err != nil {
return err
}
if err := arc.Archive(context.Background(), filelist); err != nil {
return err
}
} else if ctx.Args().Len() > 3 {
cleanup = false

View File

@ -1,66 +0,0 @@
package filesystem
import (
"archive/zip"
"io"
"os"
"path/filepath"
)
func Unzip(src, dest string) error {
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer func() {
if err := r.Close(); err != nil {
panic(err)
}
}()
_ = os.MkdirAll(dest, 0755)
extract := func(f *zip.File) error {
rc, err := f.Open()
if err != nil {
return err
}
defer func() {
if err := rc.Close(); err != nil {
panic(err)
}
}()
path := filepath.Join(dest, f.Name)
if f.FileInfo().IsDir() {
_ = os.MkdirAll(path, f.Mode())
} else {
_ = os.MkdirAll(filepath.Dir(path), f.Mode())
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
_, err = io.Copy(f, rc)
if err != nil {
return err
}
}
return nil
}
for _, f := range r.File {
err := extract(f)
if err != nil {
return err
}
}
return nil
}