Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add car file upload support #17

Merged
merged 2 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions api/status_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ func ConfigureStatusCheckRouter(e *echo.Group, node *core.LightNode) {
var content core.Content
node.DB.Raw("select * from contents as c where requesting_api_key = ? and id = ?", authParts[1], c.Param("id")).Scan(&content)
content.RequestingApiKey = ""

if content.ID == 0 {
return c.JSON(404, map[string]interface{}{
"message": "Content not found. Please check if you have the proper API key or if the content id is valid",
})
}

return c.JSON(200, map[string]interface{}{
"content": content,
})
Expand Down
51 changes: 51 additions & 0 deletions api/upload.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package api

import (
"context"
"fmt"
"github.com/application-research/edge-ur/jobs"
"github.com/ipld/go-car"
"github.com/spf13/viper"
"strings"
"time"
Expand Down Expand Up @@ -63,9 +65,58 @@ func ConfigurePinningRouter(e *echo.Group, node *core.LightNode) {
var DeltaUploadApi = viper.Get("DELTA_NODE_API").(string)
content := e.Group("/content")
content.POST("/add", handlePinAddToNode(node, DeltaUploadApi))
content.POST("/add-car", handlePinAddCarToNode(node, DeltaUploadApi))

}

func handlePinAddCarToNode(node *core.LightNode, DeltaUploadApi string) func(c echo.Context) error {
return func(c echo.Context) error {
authorizationString := c.Request().Header.Get("Authorization")
authParts := strings.Split(authorizationString, " ")

file, err := c.FormFile("data")
src, err := file.Open()
srcR := src

if err != nil {
c.JSON(500, UploadResponse{
Status: "error",
Message: "Error pinning the car file:" + err.Error(),
})
}

carHeader, err := car.LoadCar(context.Background(), node.Node.Blockstore, src)
if err != nil {
c.JSON(500, UploadResponse{
Status: "error",
Message: "Error loading car file: " + err.Error(),
})
}

rootCid := carHeader.Roots[0].String()

// insert a new record
newContent := core.Content{
Name: file.Filename,
Size: file.Size,
Cid: rootCid,
DeltaNodeUrl: DeltaUploadApi,
RequestingApiKey: authParts[1],
Status: "pinned",
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}

node.DB.Create(&newContent)

job := jobs.CreateNewDispatcher()
job.AddJob(jobs.NewUploadToEstuaryProcessor(node, newContent, srcR))
job.Start(1)

return nil
}
}

func handlePinAddToNode(node *core.LightNode, DeltaUploadApi string) func(c echo.Context) error {
return func(c echo.Context) error {
authorizationString := c.Request().Header.Get("Authorization")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/ipfs/go-path v0.3.0
github.com/ipfs/go-unixfs v0.4.1
github.com/ipfs/go-unixfsnode v1.4.0
github.com/ipld/go-car v0.5.0
github.com/ipld/go-codec-dagpb v1.3.2
github.com/ipld/go-ipld-prime v0.17.0
github.com/labstack/echo/v4 v4.9.1
Expand Down Expand Up @@ -127,7 +128,6 @@ require (
github.com/ipfs/go-peertaskqueue v0.7.1 // indirect
github.com/ipfs/go-verifcid v0.0.1 // indirect
github.com/ipfs/interface-go-ipfs-core v0.7.0 // indirect
github.com/ipld/go-car v0.5.0 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect
github.com/jbenet/goprocess v0.1.4 // indirect
Expand Down