2022-10-23 17:29:35 +08:00
|
|
|
package problem
|
|
|
|
|
|
|
|
import (
|
2023-07-14 21:47:11 +08:00
|
|
|
"git.0x7f.app/WOJ/woj-server/internal/e"
|
|
|
|
"git.0x7f.app/WOJ/woj-server/internal/model"
|
|
|
|
"git.0x7f.app/WOJ/woj-server/pkg/utils"
|
2022-10-23 17:29:35 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Upload
|
|
|
|
// @Summary get upload url
|
|
|
|
// @Description get upload url
|
2023-12-18 21:21:15 +08:00
|
|
|
// @Tags problem
|
2022-10-23 17:29:35 +08:00
|
|
|
// @Produce json
|
|
|
|
// @Response 200 {object} e.Response "upload url and key"
|
|
|
|
// @Security Authentication
|
|
|
|
// @Router /v1/problem/upload [post]
|
|
|
|
func (h *handler) Upload(c *gin.Context) {
|
|
|
|
claim, exist := c.Get("claim")
|
|
|
|
if !exist {
|
|
|
|
e.Pong(c, e.UserUnauthenticated, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-15 16:19:49 +08:00
|
|
|
role := claim.(*model.Claim).Role
|
2022-10-23 17:29:35 +08:00
|
|
|
if role < model.RoleAdmin {
|
|
|
|
e.Pong(c, e.UserUnauthorized, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
key := utils.RandomString(16)
|
|
|
|
url, status := h.storageService.Upload(key, time.Second*60*60)
|
|
|
|
|
|
|
|
if status != e.Success {
|
|
|
|
e.Pong(c, status, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
e.Pong(c, e.Success, gin.H{
|
|
|
|
"key": key,
|
|
|
|
"url": url,
|
|
|
|
})
|
|
|
|
}
|