RoadSign/pkg/administration/publish.go

72 lines
1.6 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-11-18 06:30:35 +00:00
var upstream *sign.UpstreamConfig
2023-12-10 06:52:00 +00:00
var site *sign.SiteConfig
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 06:52:00 +00:00
if stream.ID == c.Params("upstream") {
2023-12-10 03:30:31 +00:00
upstream = stream
2023-11-18 06:30:35 +00:00
break
}
}
break
}
}
if upstream == nil {
return fiber.ErrNotFound
} else if upstream.GetType() != sign.UpstreamTypeFile {
return fiber.ErrUnprocessableEntity
}
2023-12-10 06:52:00 +00:00
for _, process := range site.Processes {
process.StopProcess()
}
2023-11-18 06:30:35 +00:00
workdir, _ := upstream.GetRawURI()
2023-12-10 06:52:00 +00:00
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
}