From 7c34878c19a60d174818080637f31e1fa7aa6544 Mon Sep 17 00:00:00 2001 From: Steve White Date: Wed, 23 Oct 2024 17:56:10 -0500 Subject: [PATCH] working on image archive --- config/config.yaml | 4 +-- go.mod | 3 ++- go.sum | 2 ++ handlers.go | 47 ++++++++++++++++++----------------- scripts/ImageArchiveTest.bash | 6 ++--- scripts/images.tar.gz | 1 - 6 files changed, 33 insertions(+), 30 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index 5de9a2d..7bd80ea 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -1,8 +1,8 @@ database_path: "data/boxes.db" test_database_path: "data/test_database.db" jwt_secret: "super_secret_key" -image_storage_dir: "images" +image_storage_dir: "/Users/stwhite/CODE/boxes/images" listening_port: 8080 log_file: "boxes.log" -static_files_dir: "/app/build/" +static_files_dir: "/Users/stwhite/CODE/boxes/build" allowed_origins: "*" diff --git a/go.mod b/go.mod index 61bdf94..471a4b9 100644 --- a/go.mod +++ b/go.mod @@ -7,11 +7,12 @@ require ( github.com/gorilla/mux v1.8.1 github.com/jinzhu/gorm v1.9.16 github.com/rs/cors v1.11.1 + golang.org/x/crypto v0.28.0 gopkg.in/yaml.v2 v2.4.0 ) require ( github.com/jinzhu/inflection v1.0.0 // indirect github.com/mattn/go-sqlite3 v1.14.0 // indirect - golang.org/x/crypto v0.28.0 // indirect + github.com/pkg/errors v0.9.1 // indirect ) diff --git a/go.sum b/go.sum index e93689c..b63ac78 100644 --- a/go.sum +++ b/go.sum @@ -22,6 +22,8 @@ github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4= github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA= github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA= github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/handlers.go b/handlers.go index 3241503..c0dda94 100644 --- a/handlers.go +++ b/handlers.go @@ -15,6 +15,8 @@ import ( "strings" "time" + "github.com/pkg/errors" + "github.com/dgrijalva/jwt-go" "github.com/gorilla/mux" "golang.org/x/crypto/bcrypt" @@ -418,8 +420,10 @@ func AuthMiddleware(next http.Handler) http.Handler { } func GetImageArchiveHandler(w http.ResponseWriter, r *http.Request) { + + fmt.Println("Getting image archive") // Create a pipe to write the archive to - pr, pw := io.Pipe() + _, pw := io.Pipe() // Create an MD5 hash writer hash := md5.New() @@ -439,57 +443,54 @@ func GetImageArchiveHandler(w http.ResponseWriter, r *http.Request) { err := filepath.Walk(config.ImageStorageDir, func(path string, info os.FileInfo, err error) error { if err != nil { - return err + return errors.Wrap(err, fmt.Sprintf("Failed to access path: %s", path)) } header, err := tar.FileInfoHeader(info, info.Name()) if err != nil { - return err + return errors.Wrap(err, fmt.Sprintf("Failed to create tar header for: %s", path)) } relPath, err := filepath.Rel(config.ImageStorageDir, path) if err != nil { - return err + return errors.Wrap(err, fmt.Sprintf("Failed to get relative path for: %s", path)) } header.Name = relPath if err := tarWriter.WriteHeader(header); err != nil { - return err + return errors.Wrap(err, fmt.Sprintf("Failed to write tar header for: %s", path)) } if !info.IsDir() { file, err := os.Open(path) if err != nil { - return err + return errors.Wrap(err, fmt.Sprintf("Failed to open file: %s", path)) } defer file.Close() _, err = io.Copy(tarWriter, file) if err != nil { - return err + return errors.Wrap(err, fmt.Sprintf("Failed to copy file contents for: %s", path)) } } return nil }) + // Set headers + w.Header().Set("Content-Type", "application/gzip") + w.Header().Set("Content-Disposition", "attachment; filename=images.tar.gz") + + // Copy the archive to the response writer if err != nil { - pw.CloseWithError(err) + fmt.Println("Error in GetImageArchiveHandler:", err) + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return } + + // Calculate and set the MD5 sum header + md5sum := hex.EncodeToString(hash.Sum(nil)) + w.Header().Set("X-Archive-MD5", md5sum) + }() - - // Set headers - w.Header().Set("Content-Type", "application/gzip") - w.Header().Set("Content-Disposition", "attachment; filename=images.tar.gz") - - // Copy the archive to the response writer - _, err := io.Copy(w, pr) - if err != nil { - http.Error(w, fmt.Sprintf("Error sending archive: %v", err), http.StatusInternalServerError) - return - } - - // Calculate and set the MD5 sum header - md5sum := hex.EncodeToString(hash.Sum(nil)) - w.Header().Set("X-Archive-MD5", md5sum) } diff --git a/scripts/ImageArchiveTest.bash b/scripts/ImageArchiveTest.bash index f4be8d7..bb5fc32 100644 --- a/scripts/ImageArchiveTest.bash +++ b/scripts/ImageArchiveTest.bash @@ -2,7 +2,7 @@ # Set variables SERVER_URL="http://localhost:8080" # Replace with your server URL -ENDPOINT="/admin/imagearchive" +ENDPOINT="/api/v1/admin/imagearchive" OUTPUT_FILE="images.tar.gz" EXTRACT_DIR="extracted_images" USERNAME=boxuser @@ -29,7 +29,7 @@ fi echo "Downloading archive..." HTTP_RESPONSE=$(curl -s -w "HTTPSTATUS:%{http_code}" -o "$OUTPUT_FILE" \ -H "Authorization: Bearer $AUTH_TOKEN" \ - "$SERVER_URL/api/v1/login") + "$SERVER_URL/api/v1/admin/imagearchive" ) HTTP_BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g') HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://') @@ -70,4 +70,4 @@ echo "Cleaning up..." rm "$OUTPUT_FILE" rm -r "$EXTRACT_DIR" -echo "Test completed successfully." \ No newline at end of file +echo "Test completed successfully." diff --git a/scripts/images.tar.gz b/scripts/images.tar.gz index 834a5f3..e69de29 100644 --- a/scripts/images.tar.gz +++ b/scripts/images.tar.gz @@ -1 +0,0 @@ -404 page not found