working on image archive
This commit is contained in:
parent
44c615a120
commit
7c34878c19
|
@ -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: "*"
|
||||
|
|
3
go.mod
3
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
|
||||
)
|
||||
|
|
2
go.sum
2
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=
|
||||
|
|
47
handlers.go
47
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)
|
||||
}
|
||||
|
|
|
@ -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://')
|
||||
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
404 page not found
|
Loading…
Reference in New Issue