RoadSign/pkg/administration/publish.go

80 lines
1.8 KiB
Go
Raw Normal View History

2023-11-18 06:30:35 +00:00
package administration
import (
2023-12-10 03:30:31 +00:00
"os"
"path/filepath"
"code.smartsheep.studio/goatworks/roadsign/pkg/filesystem"
2023-11-18 06:30:35 +00:00
"code.smartsheep.studio/goatworks/roadsign/pkg/sign"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
2023-12-10 06:52:00 +00:00
"github.com/samber/lo"
2023-11-18 06:30:35 +00:00
)
2023-12-10 06:52:00 +00:00
func doPublish(c *fiber.Ctx) error {
2023-12-10 10:26:04 +00:00
var workdir string
2023-12-10 06:52:00 +00:00
var site *sign.SiteConfig
2023-12-10 10:26:04 +00:00
var upstream *sign.UpstreamConfig
var process *sign.ProcessConfig
2023-11-18 06:30:35 +00:00
for _, item := range sign.App.Sites {
2023-12-10 06:52:00 +00:00
if item.ID == c.Params("site") {
site = item
2023-11-18 06:30:35 +00:00
for _, stream := range item.Upstreams {
2023-12-10 10:26:04 +00:00
if stream.ID == c.Params("slug") {
2023-12-10 03:30:31 +00:00
upstream = stream
2023-12-10 10:26:04 +00:00
workdir, _ = stream.GetRawURI()
break
}
}
for _, proc := range item.Processes {
if proc.ID == c.Params("slug") {
process = proc
workdir = proc.Workdir
2023-11-18 06:30:35 +00:00
break
}
}
break
}
}
2023-12-10 10:26:04 +00:00
if upstream == nil && process == nil {
2023-11-18 06:30:35 +00:00
return fiber.ErrNotFound
2023-12-10 10:26:04 +00:00
} else if upstream != nil && upstream.GetType() != sign.UpstreamTypeFile {
2023-11-18 06:30:35 +00:00
return fiber.ErrUnprocessableEntity
}
2023-12-10 06:52:00 +00:00
for _, process := range site.Processes {
process.StopProcess()
}
if c.Query("overwrite", "yes") == "yes" {
2023-11-18 06:30:35 +00:00
files, _ := filepath.Glob(filepath.Join(workdir, "*"))
for _, file := range files {
_ = os.Remove(file)
}
}
2023-12-10 06:52:00 +00:00
if form, err := c.MultipartForm(); err == nil {
2023-11-18 06:30:35 +00:00
files := form.File["attachments"]
for _, file := range files {
2023-12-10 06:52:00 +00:00
mimetype := lo.Ternary(len(c.Query("mimetype")) > 0, c.Query("mimetype"), file.Header["Content-Type"][0])
2023-11-18 06:30:35 +00:00
switch mimetype {
case "application/zip":
dst := filepath.Join(os.TempDir(), uuid.NewString()+".zip")
2023-12-10 06:52:00 +00:00
if err := c.SaveFile(file, dst); err != nil {
2023-11-18 06:30:35 +00:00
return err
} else {
2023-12-10 03:30:31 +00:00
_ = filesystem.Unzip(dst, workdir)
2023-11-18 06:30:35 +00:00
}
default:
dst := filepath.Join(workdir, file.Filename)
2023-12-10 06:52:00 +00:00
if err := c.SaveFile(file, dst); err != nil {
2023-11-18 06:30:35 +00:00
return err
}
}
}
}
2023-12-10 06:52:00 +00:00
return c.SendStatus(fiber.StatusOK)
2023-11-18 06:30:35 +00:00
}