diff --git a/japicore/pathRouteHandling.go b/japicore/pathRouteHandling.go index 410c4e8..fe66dc6 100644 --- a/japicore/pathRouteHandling.go +++ b/japicore/pathRouteHandling.go @@ -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 { @@ -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 { @@ -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) } diff --git a/main.go b/main.go index 08b2dfe..c000841 100644 --- a/main.go +++ b/main.go @@ -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")