Feat: finish object storage
This commit is contained in:
@@ -3,8 +3,11 @@ package api
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.konchin.com/go2025/backend/middlewares"
|
||||
"gitea.konchin.com/go2025/backend/models"
|
||||
"gitea.konchin.com/go2025/backend/types"
|
||||
"gitea.konchin.com/go2025/backend/utils"
|
||||
"github.com/uptrace/bunrouter"
|
||||
)
|
||||
@@ -21,6 +24,16 @@ import (
|
||||
func (self *Handlers) PostImage(
|
||||
w http.ResponseWriter, req bunrouter.Request,
|
||||
) error {
|
||||
ctx := req.Context()
|
||||
|
||||
claim, ok := ctx.Value(types.AccessToken("")).(models.AccessTokenClaim)
|
||||
if !ok {
|
||||
return middlewares.HTTPError{
|
||||
StatusCode: http.StatusUnauthorized,
|
||||
Message: "missing access token",
|
||||
}
|
||||
}
|
||||
|
||||
typeHeader := strings.Split(req.Header.Get("Content-Type"), "/")
|
||||
if len(typeHeader) != 2 || typeHeader[0] != "image" {
|
||||
return middlewares.HTTPError{
|
||||
@@ -29,6 +42,28 @@ func (self *Handlers) PostImage(
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
image := models.Image{
|
||||
Extension: typeHeader[1],
|
||||
Uploader: claim.UserId,
|
||||
UploadTS: time.Now(),
|
||||
}
|
||||
err := self.db.InsertImage(ctx, &image)
|
||||
if err != nil {
|
||||
return middlewares.HTTPError{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Message: "failed to insert image to db",
|
||||
OriginError: err,
|
||||
}
|
||||
}
|
||||
|
||||
err = self.s3.PutImage(ctx,
|
||||
image.Filename(), req.Body, req.ContentLength)
|
||||
if err != nil {
|
||||
return middlewares.HTTPError{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Message: "failed to put image to s3",
|
||||
OriginError: err,
|
||||
}
|
||||
}
|
||||
return utils.Success(w)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user