48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package problem
|
|
|
|
import (
|
|
"git.0x7f.app/WOJ/woj-server/internal/e"
|
|
"git.0x7f.app/WOJ/woj-server/internal/model"
|
|
"git.0x7f.app/WOJ/woj-server/pkg/utils"
|
|
"github.com/gin-gonic/gin"
|
|
"time"
|
|
)
|
|
|
|
type uploadResponse struct {
|
|
Key string `json:"key"`
|
|
URL string `json:"url"`
|
|
}
|
|
|
|
// Upload
|
|
// @Summary [admin] get upload url
|
|
// @Description Retrieve a pre-signed upload URL from the object storage
|
|
// @Tags problem,admin
|
|
// @Produce json
|
|
// @Response 200 {object} e.Response[uploadResponse] "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[any](c, e.UserUnauthenticated, nil)
|
|
return
|
|
}
|
|
|
|
// only admin can upload
|
|
role := claim.(*model.Claim).Role
|
|
if role < model.RoleAdmin {
|
|
e.Pong[any](c, e.UserUnauthorized, nil)
|
|
return
|
|
}
|
|
|
|
// generate random key
|
|
key := utils.RandomString(16)
|
|
url, status := h.storageService.Upload(key, time.Second*60*60)
|
|
if status != e.Success {
|
|
e.Pong[any](c, status, nil)
|
|
return
|
|
}
|
|
|
|
e.Pong(c, e.Success, uploadResponse{Key: key, URL: url})
|
|
}
|