feat: implement /s5/metadata/:cid

This commit is contained in:
Derrick Hammer 2024-01-17 22:16:04 -05:00
parent 510a57162c
commit f957ef5d78
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 40 additions and 1 deletions

View File

@ -42,7 +42,8 @@ func getRoutes(h *s5.HttpHandler, portal interfaces.Portal) map[string]jape.Hand
"POST /s5/upload/directory": s5.AuthMiddleware(h.DirectoryUpload, portal), "POST /s5/upload/directory": s5.AuthMiddleware(h.DirectoryUpload, portal),
// Download API // Download API
"GET /s5/blob/:cid": s5.AuthMiddleware(h.DownloadBlob, portal), "GET /s5/blob/:cid": s5.AuthMiddleware(h.DownloadBlob, portal),
"GET /s5/metadata/:cid": s5.AuthMiddleware(h.DownloadMetadata, portal),
// Pins API // Pins API
"POST /s5/pin/:cid": s5.AuthMiddleware(h.AccountPin, portal), "POST /s5/pin/:cid": s5.AuthMiddleware(h.AccountPin, portal),

View File

@ -1238,6 +1238,44 @@ func (h *HttpHandler) DebugStorageLocations(jc jape.Context) {
}) })
} }
func (h *HttpHandler) DownloadMetadata(jc jape.Context) {
var cid string
if jc.DecodeParam("cid", &cid) != nil {
return
}
cidDecoded, err := encoding.CIDFromString(cid)
if jc.Check("error decoding cid", err) != nil {
return
}
switch cidDecoded.Type {
case types.CIDTypeRaw:
_ = jc.Error(errors.New("Raw CIDs do not have metadata"), http.StatusBadRequest)
return
case types.CIDTypeResolver:
_ = jc.Error(errors.New("Resolver CIDs not yet supported"), http.StatusBadRequest)
return
}
meta, err := h.getNode().GetMetadataByCID(cidDecoded)
if jc.Check("error getting metadata", err) != nil {
return
}
if cidDecoded.Type != types.CIDTypeBridge {
jc.ResponseWriter.Header().Set("Cache-Control", "public, max-age=31536000")
} else {
jc.ResponseWriter.Header().Set("Cache-Control", "public, max-age=60")
}
jc.Encode(&meta)
}
func setAuthCookie(jwt string, jc jape.Context) { func setAuthCookie(jwt string, jc jape.Context) {
authCookie := http.Cookie{ authCookie := http.Cookie{
Name: "s5-auth-token", Name: "s5-auth-token",