feat: implement /s5/blob/:cid

This commit is contained in:
Derrick Hammer 2024-01-17 21:23:33 -05:00
parent dfd03673c9
commit 0c88e80a66
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 33 additions and 0 deletions

View File

@ -41,6 +41,9 @@ func getRoutes(h *s5.HttpHandler, portal interfaces.Portal) map[string]jape.Hand
"POST /s5/upload": s5.AuthMiddleware(h.SmallFileUpload, portal),
"POST /s5/upload/directory": s5.AuthMiddleware(h.DirectoryUpload, portal),
// Download API
"GET /s5/blob/:cid": s5.AuthMiddleware(h.DownloadBlob, portal),
// Pins API
"POST /s5/pin/:cid": s5.AuthMiddleware(h.AccountPin, portal),
"DELETE /s5/delete/:cid": s5.AuthMiddleware(h.AccountPinDelete, portal),

View File

@ -1107,6 +1107,36 @@ func (h *HttpHandler) getNode() s5interfaces.Node {
return protoInstance.Node()
}
func (h *HttpHandler) DownloadBlob(jc jape.Context) {
var cid string
if jc.DecodeParam("cid", &cid) != nil {
return
}
cid = strings.Split(cid, ".")[0]
cidDecoded, err := encoding.CIDFromString(cid)
if jc.Check("error decoding cid", err) != nil {
return
}
dlUriProvider := s5storage.NewStorageLocationProvider(h.getNode(), &cidDecoded.Hash, types.StorageLocationTypeFull, types.StorageLocationTypeFile, types.StorageLocationTypeBridge)
err = dlUriProvider.Start()
if jc.Check("error starting search", err) != nil {
return
}
next, err := dlUriProvider.Next()
if jc.Check("error fetching blob", err) != nil {
return
}
http.Redirect(jc.ResponseWriter, jc.Request, next.Location().BytesURL(), http.StatusFound)
}
func setAuthCookie(jwt string, jc jape.Context) {
authCookie := http.Cookie{
Name: "s5-auth-token",