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

rework of japi methods #10

Merged
merged 6 commits into from
Oct 3, 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
29 changes: 15 additions & 14 deletions japicore/fidRouteHandling.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package japicore

import (
"bytes"
"fmt"
"net/http"
"strings"

"github.com/JackalLabs/jackalapi/jutils"
"github.com/JackalLabs/jackalgo/handlers/file_io_handler"
"github.com/uptrace/bunrouter"
)

func IpfsHandler(fileIo *file_io_handler.FileIoHandler, queue *FileIoQueue) bunrouter.HandlerFunc {
func (j JApiCore) IpfsHandler() bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
var allBytes []byte

Expand All @@ -31,7 +29,7 @@ func IpfsHandler(fileIo *file_io_handler.FileIoHandler, queue *FileIoQueue) bunr

cid := strings.ReplaceAll(id, "/", "_")

handler, err := fileIo.DownloadFile(fmt.Sprintf("%s/%s", operatingRoot, cid))
handler, err := j.FileIo.DownloadFile(fmt.Sprintf("%s/%s", operatingRoot, cid))
if err != nil {
if !toClone {
warning := "IPFS CID Not Found"
Expand All @@ -44,11 +42,14 @@ func IpfsHandler(fileIo *file_io_handler.FileIoHandler, queue *FileIoQueue) bunr
return err
}

byteReader := bytes.NewReader(byteBuffer.Bytes())
workingBytes := jutils.CloneBytes(byteReader)
allBytes = jutils.CloneBytes(byteReader)
clonedBytes1, clonedBytes2, err := jutils.CloneByteSlice(byteBuffer.Bytes())
if err != nil {
jutils.ProcessHttpError("httpGetFileRequest", err, 404, w)
return err
}
allBytes = clonedBytes2

fid := processUpload(w, fileIo, workingBytes, cid, operatingRoot, queue)
fid := processUpload(w, j.FileIo, clonedBytes1, cid, operatingRoot, j.FileIoQueue)
if len(fid) == 0 {
warning := "Failed to get FID post-upload"
return jutils.ProcessCustomHttpError("IpfsHandler", warning, 500, w)
Expand All @@ -65,7 +66,7 @@ func IpfsHandler(fileIo *file_io_handler.FileIoHandler, queue *FileIoQueue) bunr
}
}

func DownloadByFidHandler(fileIo *file_io_handler.FileIoHandler) bunrouter.HandlerFunc {
func (j JApiCore) DownloadByFidHandler() bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
id := req.Param("id")
if len(id) == 0 {
Expand All @@ -74,7 +75,7 @@ func DownloadByFidHandler(fileIo *file_io_handler.FileIoHandler) bunrouter.Handl
}
fid := strings.ReplaceAll(id, "/", "_")

handler, err := fileIo.DownloadFileFromFid(fid)
handler, err := j.FileIo.DownloadFileFromFid(fid)
if err != nil {
return err
}
Expand All @@ -88,7 +89,7 @@ func DownloadByFidHandler(fileIo *file_io_handler.FileIoHandler) bunrouter.Handl
}
}

func DeleteByFidHandler(fileIo *file_io_handler.FileIoHandler, queue *FileIoQueue) bunrouter.HandlerFunc {
func (j JApiCore) DeleteByFidHandler() bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
id := req.Param("id")
if len(id) == 0 {
Expand All @@ -101,21 +102,21 @@ func DeleteByFidHandler(fileIo *file_io_handler.FileIoHandler, queue *FileIoQueu

// TODO - update after deletion by fid is added to jackalgo

//folder, err := fileIo.DownloadFolder(queue.GetRoot("bulk"))
//folder, err := j.FileIo.DownloadFolder(j.FileIoQueue.GetRoot("bulk"))
//if err != nil {
// jutils.ProcessHttpError("DeleteFile", err, 404, w)
// return err
//}
//
//err = fileIo.DeleteTargets([]string{fid}, folder)
//err = j.FileIo.DeleteTargets([]string{fid}, folder)
//if err != nil {
// jutils.ProcessHttpError("DeleteFile", err, 500, w)
// return err
//}

// message := createJsonResponse("Deletion complete")
message := createJsonResponse("Deletion Not Implemented")
condensedWriteJSON(w, message)
jutils.SimpleWriteJSON(w, message)
return nil
}
}
10 changes: 5 additions & 5 deletions japicore/miscRouteHandling.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@ var (
Module = "Jackal API Core"
)

func Handler() bunrouter.HandlerFunc {
func (j JApiCore) Handler() bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
return nil
}
}

func RouteNotFoundHandler() bunrouter.HandlerFunc {
func (j JApiCore) RouteNotFoundHandler() bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
warning := fmt.Sprintf("%s is not an availble route", req.URL.Path)
return jutils.ProcessCustomHttpError("MethodNotAllowedHandler", warning, 404, w)
}
}

func MethodNotAllowedHandler() bunrouter.HandlerFunc {
func (j JApiCore) MethodNotAllowedHandler() bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
warning := fmt.Sprintf("%s method not availble for \"%s\"", req.URL.Path, req.Method)
return jutils.ProcessCustomHttpError("MethodNotAllowedHandler", warning, 405, w)
}
}

func VersionHandler() bunrouter.HandlerFunc {
func (j JApiCore) VersionHandler() bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
message := createJsonResponse("")
condensedWriteJSON(w, message)
jutils.SimpleWriteJSON(w, message)
return nil
}
}
85 changes: 55 additions & 30 deletions japicore/pathRouteHandling.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ import (
"sync"

"github.com/JackalLabs/jackalapi/jutils"
"github.com/JackalLabs/jackalgo/handlers/file_io_handler"
"github.com/uptrace/bunrouter"
)

func downloadByPathCore(fileIo *file_io_handler.FileIoHandler, operatingRoot string) bunrouter.HandlerFunc {
func (j JApiCore) downloadByPathCore(operatingRoot string, reportFunc func(num int64)) bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
location := req.Param("location")
if len(location) == 0 {
warning := "Failed to get Location"
return jutils.ProcessCustomHttpError("DownloadByPathHandler", warning, 404, w)
return jutils.ProcessCustomHttpError("BasicDownloadByPathHandler", warning, 404, w)
}

uniquePath := readUniquePath(req)
Expand All @@ -29,21 +28,24 @@ func downloadByPathCore(fileIo *file_io_handler.FileIoHandler, operatingRoot str
}
operatingRoot += "/" + location

handler, err := fileIo.DownloadFile(operatingRoot)
handler, err := j.FileIo.DownloadFile(operatingRoot)
if err != nil {
return err
}

size := handler.GetFile().Details.Size
reportFunc(size)

fileBytes := handler.GetFile().Buffer().Bytes()
_, err = w.Write(fileBytes)
if err != nil {
jutils.ProcessError("WWriteError for DownloadByPathHandler", err)
jutils.ProcessError("WWriteError for BasicDownloadByPathHandler", err)
}
return nil
}
}

func deleteByPathCore(fileIo *file_io_handler.FileIoHandler, operatingRoot string) bunrouter.HandlerFunc {
func (j JApiCore) deleteByPathCore(operatingRoot string, delFunc func(num int64)) bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
filename := req.Param("filename")
if len(filename) == 0 {
Expand All @@ -54,7 +56,7 @@ func deleteByPathCore(fileIo *file_io_handler.FileIoHandler, operatingRoot strin
location := req.Param("location")
if len(location) == 0 {
warning := "Failed to get Location"
return jutils.ProcessCustomHttpError("DownloadByPathHandler", warning, 404, w)
return jutils.ProcessCustomHttpError("BasicDownloadByPathHandler", warning, 404, w)
}

cleanFilename := strings.ReplaceAll(filename, "/", "_")
Expand All @@ -66,25 +68,28 @@ func deleteByPathCore(fileIo *file_io_handler.FileIoHandler, operatingRoot strin
}
operatingRoot += "/" + location

folder, err := fileIo.DownloadFolder(operatingRoot)
folder, err := j.FileIo.DownloadFolder(operatingRoot)
if err != nil {
jutils.ProcessHttpError("DeleteFile", err, 404, w)
return err
}

err = fileIo.DeleteTargets([]string{cleanFilename}, folder)
deletionSize := folder.GetChildFiles()[cleanFilename].Size
delFunc(deletionSize)

err = j.FileIo.DeleteTargets([]string{cleanFilename}, folder)
if err != nil {
jutils.ProcessHttpError("DeleteFile", err, 500, w)
return err
}

message := createJsonResponse("Deletion complete")
condensedWriteJSON(w, message)
jutils.SimpleWriteJSON(w, message)
return nil
}
}

func ImportHandler(fileIo *file_io_handler.FileIoHandler, queue *ScrapeQueue) bunrouter.HandlerFunc {
func (j JApiCore) ImportHandler() bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
operatingRoot := jutils.LoadEnvVarOrFallback("JAPI_BULK_ROOT", "s/JAPI/Bulk")

Expand All @@ -107,28 +112,18 @@ func ImportHandler(fileIo *file_io_handler.FileIoHandler, queue *ScrapeQueue) bu

for _, target := range data.Targets {
wg.Add(1)
queue.Push(fileIo, w, &wg, operatingRoot, target, source)
j.ScrapeQueue.Push(j.FileIo, w, &wg, operatingRoot, target, source)
}

wg.Wait()

message := createJsonResponse("Import complete")
condensedWriteJSON(w, message)
jutils.SimpleWriteJSON(w, message)
return nil
}
}

func DownloadFromBulkByPathHandler(fileIo *file_io_handler.FileIoHandler) bunrouter.HandlerFunc {
operatingRoot := jutils.LoadEnvVarOrFallback("JAPI_BULK_ROOT", "s/JAPI/Bulk")
return downloadByPathCore(fileIo, operatingRoot)
}

func DownloadByPathHandler(fileIo *file_io_handler.FileIoHandler) bunrouter.HandlerFunc {
operatingRoot := jutils.LoadEnvVarOrFallback("JAPI_OP_ROOT", "s/JAPI")
return downloadByPathCore(fileIo, operatingRoot)
}

func UploadByPathHandler(fileIo *file_io_handler.FileIoHandler, queue *FileIoQueue) bunrouter.HandlerFunc {
func (j JApiCore) UploadByPathHandler() bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
operatingRoot := jutils.LoadEnvVarOrFallback("JAPI_OP_ROOT", "s/JAPI")
var byteBuffer bytes.Buffer
Expand Down Expand Up @@ -176,7 +171,7 @@ func UploadByPathHandler(fileIo *file_io_handler.FileIoHandler, queue *FileIoQue
return err
}

fid := processUpload(w, fileIo, byteBuffer.Bytes(), head.Filename, operatingRoot, queue)
fid := processUpload(w, j.FileIo, byteBuffer.Bytes(), head.Filename, operatingRoot, j.FileIoQueue)
if len(fid) == 0 {
warning := "Failed to get FID"
return jutils.ProcessCustomHttpError("processUpload", warning, 500, w)
Expand All @@ -192,17 +187,47 @@ func UploadByPathHandler(fileIo *file_io_handler.FileIoHandler, queue *FileIoQue
}

message := createJsonResponse("Upload complete")
condensedWriteJSON(w, message)
jutils.SimpleWriteJSON(w, message)
return nil
}
}

func DeleteFromBulkByPathHandler(fileIo *file_io_handler.FileIoHandler) bunrouter.HandlerFunc {
func (j JApiCore) BasicDownloadFromBulkByPathHandler() bunrouter.HandlerFunc {
operatingRoot := jutils.LoadEnvVarOrFallback("JAPI_BULK_ROOT", "s/JAPI/Bulk")
return j.downloadByPathCore(operatingRoot, func(num int64) {})
}

func (j JApiCore) BasicDownloadByPathHandler() bunrouter.HandlerFunc {
operatingRoot := jutils.LoadEnvVarOrFallback("JAPI_OP_ROOT", "s/JAPI")
return j.downloadByPathCore(operatingRoot, func(num int64) {})
}

func (j JApiCore) BasicDeleteFromBulkByPathHandler() bunrouter.HandlerFunc {
operatingRoot := jutils.LoadEnvVarOrFallback("JAPI_BULK_ROOT", "s/JAPI/Bulk")
return j.deleteByPathCore(operatingRoot, func(num int64) {})
}

func (j JApiCore) BasicDeleteByPathHandler() bunrouter.HandlerFunc {
operatingRoot := jutils.LoadEnvVarOrFallback("JAPI_OP_ROOT", "s/JAPI")
return j.deleteByPathCore(operatingRoot, func(num int64) {})
}

func (j JApiCore) AdvancedDownloadFromBulkByPathHandler(reportFunc func(num int64)) bunrouter.HandlerFunc {
operatingRoot := jutils.LoadEnvVarOrFallback("JAPI_BULK_ROOT", "s/JAPI/Bulk")
return j.downloadByPathCore(operatingRoot, reportFunc)
}

func (j JApiCore) AdvancedDownloadByPathHandler(reportFunc func(num int64)) bunrouter.HandlerFunc {
operatingRoot := jutils.LoadEnvVarOrFallback("JAPI_OP_ROOT", "s/JAPI")
return j.downloadByPathCore(operatingRoot, reportFunc)
}

func (j JApiCore) AdvancedDeleteFromBulkByPathHandler(delFunc func(num int64)) bunrouter.HandlerFunc {
operatingRoot := jutils.LoadEnvVarOrFallback("JAPI_BULK_ROOT", "s/JAPI/Bulk")
return deleteByPathCore(fileIo, operatingRoot)
return j.deleteByPathCore(operatingRoot, delFunc)
}

func DeleteByPathHandler(fileIo *file_io_handler.FileIoHandler) bunrouter.HandlerFunc {
func (j JApiCore) AdvancedDeleteByPathHandler(delFunc func(num int64)) bunrouter.HandlerFunc {
operatingRoot := jutils.LoadEnvVarOrFallback("JAPI_OP_ROOT", "s/JAPI")
return deleteByPathCore(fileIo, operatingRoot)
return j.deleteByPathCore(operatingRoot, delFunc)
}
24 changes: 24 additions & 0 deletions japicore/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package japicore
import (
"net/http"

"github.com/JackalLabs/jackalgo/handlers/wallet_handler"

"github.com/uptrace/bunrouter"

"github.com/JackalLabs/jackalgo/handlers/file_io_handler"
Expand Down Expand Up @@ -31,3 +33,25 @@ func createJsonResponse(message string) jsonResponse {
Version: Version,
}
}

type JApiCore struct {
FileIo *file_io_handler.FileIoHandler
FileIoQueue *FileIoQueue
ScrapeQueue *ScrapeQueue
Wallet *wallet_handler.WalletHandler
}

func InitJApiCore() *JApiCore {
wallet, fileIo := InitWalletSession()
fileIoQueue := NewFileIoQueue()
scrapeQueue := NewScrapeQueue(fileIoQueue)

core := JApiCore{
FileIo: fileIo,
FileIoQueue: fileIoQueue,
ScrapeQueue: scrapeQueue,
Wallet: wallet,
}

return &core
}
18 changes: 0 additions & 18 deletions japicore/utils.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package japicore

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"sync"

Expand Down Expand Up @@ -43,21 +40,6 @@ func processUpload(w http.ResponseWriter, fileIo *file_io_handler.FileIoHandler,
return m.Fid()
}

func condensedWriteJSON(w http.ResponseWriter, respVal interface{}) {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(respVal); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
fmt.Printf("json.NewEncoder.Encode: %v", err)
return
}
w.Header().Set("Content-Type", "application/json")
written, err := w.Write(buf.Bytes())
if err != nil {
fmt.Printf("Written bytes: %d\n", written)
fmt.Println(err)
}
}

func readUniquePath(req bunrouter.Request) string {
uniquePath, ok := req.Context().Value(jutils.ReqUniquePath{}).(string)
if !ok {
Expand Down
Loading
Loading