From 8677c78652769dabbf66b6805534ec3d27b748a4 Mon Sep 17 00:00:00 2001 From: Anton Kopti Date: Tue, 14 May 2024 11:08:43 -0400 Subject: [PATCH] Refactor ModelVersion To use S3Controller --- handlers/modelversion.go | 39 +++++++++++++++++++++++++-------------- main.go | 2 +- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/handlers/modelversion.go b/handlers/modelversion.go index e20e9c7..a6b7ce6 100644 --- a/handlers/modelversion.go +++ b/handlers/modelversion.go @@ -2,6 +2,7 @@ package handlers import ( "bufio" + "bytes" "fmt" "net/http" "path/filepath" @@ -11,7 +12,6 @@ import ( "github.com/Dewberry/mcat-ras/tools" "github.com/Dewberry/s3api/blobstore" - "github.com/USACE/filestore" // warning: replaces standard errors "github.com/labstack/echo/v4" ) @@ -25,19 +25,25 @@ import ( // @Success 200 {string} string "4.0" // @Failure 500 {object} SimpleResponse // @Router /modelversion [get] -func ModelVersion(fs *filestore.FileStore) echo.HandlerFunc { +func ModelVersion(bh *blobstore.BlobHandler) echo.HandlerFunc { return func(c echo.Context) error { definitionFile := c.QueryParam("definition_file") + bucket := c.QueryParam("bucket") if definitionFile == "" { return c.JSON(http.StatusBadRequest, "Missing query parameter: `definition_file`") } + s3Ctrl, err := bh.GetController(bucket) + if err != nil { + errMsg := fmt.Errorf("error getting S3 controller: %s", err.Error()) + return c.JSON(http.StatusInternalServerError, errMsg.Error()) + } - if !isAModel(fs, definitionFile) { + if !isAModel(s3Ctrl, bucket, definitionFile) { return c.JSON(http.StatusBadRequest, definitionFile+" is not a valid RAS prj file.") } - version, err := getVersions(definitionFile, *fs) + version, err := getVersions(s3Ctrl, bucket, definitionFile) if err != nil { return c.JSON(http.StatusInternalServerError, err.Error()) } @@ -45,11 +51,11 @@ func ModelVersion(fs *filestore.FileStore) echo.HandlerFunc { return c.JSON(http.StatusOK, version) } } -func modFiles(s3ctrl *blobstore.S3Controller, bucket, definitionFile string) ([]string, error) { +func modFiles(s3Ctrl *blobstore.S3Controller, bucket, definitionFile string) ([]string, error) { mFiles := make([]string, 0) prefix := filepath.Dir(definitionFile) + "/" - result, err := s3ctrl.GetList(bucket, prefix, false) + result, err := s3Ctrl.GetList(bucket, prefix, false) if err != nil { return mFiles, err } @@ -75,17 +81,18 @@ func modFiles(s3ctrl *blobstore.S3Controller, bucket, definitionFile string) ([] return mFiles, nil } -func pullVersion(fp string, fs filestore.FileStore) (string, error) { - f, err := fs.GetObject(fp) +func pullVersion(s3Ctrl *blobstore.S3Controller, bucket, fp string) (string, error) { + key := strings.TrimPrefix(fp, "/") + content, err := s3Ctrl.FetchObjectContent(bucket, key) if err != nil { return "", err } - defer f.Close() - sc := bufio.NewScanner(f) + // Create a new reader from the byte slice + reader := bytes.NewReader(content) + sc := bufio.NewScanner(reader) var line string for sc.Scan() { - line = sc.Text() match, err := regexp.MatchString("Program Version=", line) @@ -98,13 +105,17 @@ func pullVersion(fp string, fs filestore.FileStore) (string, error) { } } + if err := sc.Err(); err != nil { + return "", err + } + return "", fmt.Errorf("unable to find program version in file %s", fp) } -func getVersions(definitionFile string, fs filestore.FileStore) (string, error) { +func getVersions(s3Ctrl *blobstore.S3Controller, bucket, definitionFile string) (string, error) { var version string - mFiles, err := modFiles(definitionFile, fs) + mFiles, err := modFiles(s3Ctrl, bucket, definitionFile) if err != nil { return version, err } @@ -116,7 +127,7 @@ func getVersions(definitionFile string, fs filestore.FileStore) (string, error) if tools.RasRE.Plan.MatchString(ext) || tools.RasRE.Geom.MatchString(ext) || tools.RasRE.AllFlow.MatchString(ext) { - ver, err := pullVersion(fp, fs) + ver, err := pullVersion(s3Ctrl, bucket, fp) if err != nil { fmt.Println(err) } else { diff --git a/main.go b/main.go index 3b98afa..ca70c06 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,7 @@ func main() { // and then apply receiver functions to the struct to answer desired question e.GET("/isamodel", handlers.IsAModel(appConfig.Bh)) e.GET("/modeltype", handlers.ModelType(appConfig.Bh)) - e.GET("/modelversion", handlers.ModelVersion(appConfig.FileStore)) + e.GET("/modelversion", handlers.ModelVersion(appConfig.Bh)) e.GET("/index", handlers.Index(appConfig.FileStore)) e.GET("/isgeospatial", handlers.IsGeospatial(appConfig.FileStore)) e.GET("/geospatialdata", handlers.GeospatialData(appConfig))