static-hoster/src/api/api.go

37 lines
1,023 B
Go
Raw Normal View History

2023-01-24 14:50:38 +00:00
package api
import (
2023-01-26 17:52:03 +00:00
"fmt"
2023-01-24 14:50:38 +00:00
"net/http"
"os"
2023-01-25 08:12:44 +00:00
"github.com/c4milo/unpackit"
2023-01-24 14:50:38 +00:00
"github.com/gin-gonic/gin"
2023-01-26 17:52:03 +00:00
"nikurasu.gay/static-hoster/envloader"
2023-01-24 14:50:38 +00:00
)
2023-01-26 17:52:03 +00:00
func PostUpdate(env *envloader.Environment) gin.HandlerFunc {
return func(ctx *gin.Context) {
fmt.Println(env.ApiKey)
siteUpdate, err := ctx.FormFile("STATIC_PAGE")
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"message": "File not recieved",
})
return
}
if err := ctx.SaveUploadedFile(siteUpdate, fmt.Sprintf("%s%s", env.RootDir, siteUpdate.Filename)); err != nil {
ctx.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"message": "Unable to save file",
})
return
}
file, err := os.Open(fmt.Sprintf("%s%s", env.RootDir, siteUpdate.Filename))
os.RemoveAll(env.StaticDir)
os.Mkdir(env.StaticDir, os.ModePerm)
unpackit.Unpack(file, env.StaticDir)
os.RemoveAll(fmt.Sprintf("%s%s", env.RootDir, siteUpdate.Filename))
ctx.JSON(http.StatusOK, gin.H{"data": siteUpdate.Filename, "err": err})
2023-01-24 14:50:38 +00:00
}
}