feat: implement /s5/delete/:cid

This commit is contained in:
Derrick Hammer 2024-01-17 13:04:32 -05:00
parent 1a5aaa3927
commit 66dabf5150
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 39 additions and 1 deletions

View File

@ -28,7 +28,7 @@ func (s S5API) Initialize(portal interfaces.Portal, protocol interfaces.Protocol
func getRoutes(h *s5.HttpHandler, portal interfaces.Portal) map[string]jape.Handler {
return map[string]jape.Handler{
"POST /s5/upload": s5.AuthMiddleware(h.SmallFileUpload, portal),
// Account API
"GET /s5/account/register": h.AccountRegisterChallenge,
"POST /s5/account/register": h.AccountRegister,
"GET /s5/account/login": h.AccountLoginChallenge,
@ -36,5 +36,11 @@ func getRoutes(h *s5.HttpHandler, portal interfaces.Portal) map[string]jape.Hand
"GET /s5/account": s5.AuthMiddleware(h.AccountInfo, portal),
"GET /s5/account/stats": s5.AuthMiddleware(h.AccountStats, portal),
"GET /s5/account/pins.bin": s5.AuthMiddleware(h.AccountPins, portal),
// Upload API
"POST /s5/upload": s5.AuthMiddleware(h.SmallFileUpload, portal),
// Pins API
"DELETE /s5/delete/:cid": s5.AuthMiddleware(h.AccountPinDelete, portal),
}
}

View File

@ -34,6 +34,7 @@ const (
errAccountRegister = "Error registering account"
errAccountLogin = "Error logging in account"
errFailedToGetPins = "Failed to get pins"
errFailedToDelPin = "Failed to delete pin"
)
var (
@ -50,6 +51,7 @@ var (
errPubkeyNotExist = errors.New("Pubkey does not exist")
errAccountLoginErr = errors.New(errAccountLogin)
errFailedToGetPinsErr = errors.New(errFailedToGetPins)
errFailedToDelPinErr = errors.New(errFailedToDelPin)
)
type HttpHandler struct {
@ -614,6 +616,36 @@ func (h *HttpHandler) AccountPins(jc jape.Context) {
_, _ = jc.ResponseWriter.Write(result)
}
func (h *HttpHandler) AccountPinDelete(jc jape.Context) {
var cid string
if jc.DecodeParam("cid", &cid) != nil {
return
}
errored := func(err error) {
_ = jc.Error(errFailedToDelPinErr, http.StatusInternalServerError)
h.portal.Logger().Error(errFailedToDelPin, zap.Error(err))
}
decodedCid, err := encoding.CIDFromString(cid)
if err != nil {
errored(err)
return
}
hash := hex.EncodeToString(decodedCid.Hash.HashBytes())
err = h.portal.Accounts().DeletePinByHash(hash, uint(jc.Request.Context().Value(AuthUserIDKey).(uint64)))
if err != nil {
errored(err)
}
jc.ResponseWriter.WriteHeader(http.StatusNoContent)
}
func setAuthCookie(jwt string, jc jape.Context) {
authCookie := http.Cookie{
Name: "s5-auth-token",