✨ Publish
This commit is contained in:
@ -13,8 +13,14 @@ func InitAdministration() *fiber.App {
|
||||
ServerHeader: fmt.Sprintf("RoadSign Administration v%s", roadsign.AppVersion),
|
||||
DisableStartupMessage: true,
|
||||
EnableIPValidation: true,
|
||||
EnablePrintRoutes: viper.GetBool("debug.print_routes"),
|
||||
TrustedProxies: viper.GetStringSlice("security.administration_trusted_proxies"),
|
||||
})
|
||||
|
||||
webhooks := app.Group("/webhooks").Name("WebHooks")
|
||||
{
|
||||
webhooks.Put("/publish/:site/:upstream", doPublish)
|
||||
}
|
||||
|
||||
return app
|
||||
}
|
||||
|
63
pkg/administration/publish.go
Normal file
63
pkg/administration/publish.go
Normal file
@ -0,0 +1,63 @@
|
||||
package administration
|
||||
|
||||
import (
|
||||
"code.smartsheep.studio/goatworks/roadsign/pkg/fs"
|
||||
"code.smartsheep.studio/goatworks/roadsign/pkg/sign"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func doPublish(ctx *fiber.Ctx) error {
|
||||
var upstream *sign.UpstreamConfig
|
||||
for _, item := range sign.App.Sites {
|
||||
if item.ID == ctx.Params("site") {
|
||||
for _, stream := range item.Upstreams {
|
||||
if stream.ID == ctx.Params("upstream") {
|
||||
upstream = &stream
|
||||
break
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if upstream == nil {
|
||||
return fiber.ErrNotFound
|
||||
} else if upstream.GetType() != sign.UpstreamTypeFile {
|
||||
return fiber.ErrUnprocessableEntity
|
||||
}
|
||||
|
||||
workdir, _ := upstream.GetRawURI()
|
||||
|
||||
if ctx.Query("overwrite", "yes") == "yes" {
|
||||
files, _ := filepath.Glob(filepath.Join(workdir, "*"))
|
||||
for _, file := range files {
|
||||
_ = os.Remove(file)
|
||||
}
|
||||
}
|
||||
|
||||
if form, err := ctx.MultipartForm(); err == nil {
|
||||
files := form.File["attachments"]
|
||||
for _, file := range files {
|
||||
mimetype := file.Header["Content-Type"][0]
|
||||
switch mimetype {
|
||||
case "application/zip":
|
||||
dst := filepath.Join(os.TempDir(), uuid.NewString()+".zip")
|
||||
if err := ctx.SaveFile(file, dst); err != nil {
|
||||
return err
|
||||
} else {
|
||||
_ = fs.Unzip(dst, workdir)
|
||||
}
|
||||
default:
|
||||
dst := filepath.Join(workdir, file.Filename)
|
||||
if err := ctx.SaveFile(file, dst); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ctx.SendStatus(fiber.StatusOK)
|
||||
}
|
Reference in New Issue
Block a user