Skip to content

Commit

Permalink
update of path-based handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
karnthis committed Sep 26, 2023
1 parent 83b1a12 commit e690430
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 49 deletions.
135 changes: 89 additions & 46 deletions japicore/pathRouteHandling.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,87 @@ import (
"github.com/uptrace/bunrouter"
)

func downloadByPathCore(fileIo *file_io_handler.FileIoHandler, operatingRoot string) 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)
}

uniquePath := readUniquePath(req)
if len(uniquePath) > 0 {
operatingRoot += "/" + uniquePath
}
operatingRoot += "/" + location

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

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

func deleteByPathCore(fileIo *file_io_handler.FileIoHandler, operatingRoot string) bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
filename := req.Param("filename")
if len(filename) == 0 {
warning := "Failed to get FileName"
return jutils.ProcessCustomHttpError("processUpload", warning, 400, w)
}

location := req.Param("location")
if len(location) == 0 {
warning := "Failed to get Location"
return jutils.ProcessCustomHttpError("DownloadByPathHandler", warning, 404, w)
}

cleanFilename := strings.ReplaceAll(filename, "/", "_")
fmt.Println(cleanFilename)

uniquePath := readUniquePath(req)
if len(uniquePath) > 0 {
operatingRoot += "/" + uniquePath
}
operatingRoot += "/" + location

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

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

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

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

uniquePath := readUniquePath(req)
if len(uniquePath) > 0 {
operatingRoot += "/" + uniquePath
}

var data fileScrape
source := req.Header.Get("J-Source-Path")
operatingRoot += "/" + source

err := json.NewDecoder(req.Body).Decode(&data)
if err != nil {
Expand All @@ -43,27 +118,14 @@ func ImportHandler(fileIo *file_io_handler.FileIoHandler, queue *ScrapeQueue) bu
}
}

func DownloadByPathHandler(fileIo *file_io_handler.FileIoHandler) bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
id := req.Param("id")
if len(id) == 0 {
warning := "Failed to get FileName"
return jutils.ProcessCustomHttpError("processUpload", warning, 404, w)
}
fid := strings.ReplaceAll(id, "/", "_")

handler, err := fileIo.DownloadFileFromFid(fid)
if err != nil {
return err
}
func DownloadFromBulkByPathHandler(fileIo *file_io_handler.FileIoHandler) bunrouter.HandlerFunc {
operatingRoot := jutils.LoadEnvVarOrFallback("JAPI_BULK_ROOT", "s/JAPI/Bulk")
return downloadByPathCore(fileIo, operatingRoot)
}

fileBytes := handler.GetFile().Buffer().Bytes()
_, err = w.Write(fileBytes)
if err != nil {
jutils.ProcessError("WWriteError for DownloadByFidHandler", err)
}
return nil
}
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 {
Expand Down Expand Up @@ -135,31 +197,12 @@ func UploadByPathHandler(fileIo *file_io_handler.FileIoHandler, queue *FileIoQue
}
}

func DeleteByPathHandler(fileIo *file_io_handler.FileIoHandler, queue *FileIoQueue) bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
id := req.Param("id")
if len(id) == 0 {
warning := "Failed to get FileName"
return jutils.ProcessCustomHttpError("processUpload", warning, 400, w)
}

fid := strings.ReplaceAll(id, "/", "_")
fmt.Println(fid)

folder, err := fileIo.DownloadFolder(queue.GetRoot("bulk"))
if err != nil {
jutils.ProcessHttpError("DeleteFile", err, 404, w)
return err
}

err = fileIo.DeleteTargets([]string{fid}, folder)
if err != nil {
jutils.ProcessHttpError("DeleteFile", err, 500, w)
return err
}
func DeleteFromBulkByPathHandler(fileIo *file_io_handler.FileIoHandler) bunrouter.HandlerFunc {
operatingRoot := jutils.LoadEnvVarOrFallback("JAPI_BULK_ROOT", "s/JAPI/Bulk")
return deleteByPathCore(fileIo, operatingRoot)
}

message := createJsonResponse("Deletion complete")
condensedWriteJSON(w, message)
return nil
}
func DeleteByPathHandler(fileIo *file_io_handler.FileIoHandler) bunrouter.HandlerFunc {
operatingRoot := jutils.LoadEnvVarOrFallback("JAPI_OP_ROOT", "s/JAPI")
return deleteByPathCore(fileIo, operatingRoot)
}
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ func main() {
})

group.WithGroup("/p", func(group *bunrouter.Group) {
group.GET("/download/:id", japicore.DownloadByPathHandler(fileIo))
group.GET("/d/:id", japicore.DownloadByPathHandler(fileIo))
group.GET("/downloadfrombulk/*location", japicore.DownloadFromBulkByPathHandler(fileIo))
group.GET("/download/*location", japicore.DownloadByPathHandler(fileIo))
group.GET("/d/*location", japicore.DownloadByPathHandler(fileIo))

group.POST("/import", japicore.ImportHandler(fileIo, scrapeQueue))
group.POST("/upload", japicore.UploadByPathHandler(fileIo, fileIoQueue))
group.POST("/u", japicore.UploadByPathHandler(fileIo, fileIoQueue))
group.DELETE("/del/:id", japicore.DeleteByPathHandler(fileIo, fileIoQueue))
group.DELETE("/delfrombulk/:filename/*location", japicore.DeleteFromBulkByPathHandler(fileIo))
group.DELETE("/del/:filename/*location", japicore.DeleteByPathHandler(fileIo))
})

port := jutils.LoadEnvVarOrFallback("JAPI_PORT", "3535")
Expand Down

0 comments on commit e690430

Please sign in to comment.