import (
"bytes"
- //"encoding/base64"
"encoding/json"
"fmt"
"io"
"strconv"
image "bpa-restapi-agent/internal/app"
+ minioc "bpa-restapi-agent/internal/storage"
"github.com/gorilla/mux"
)
// We will set this variable with a mock interface for testing
client image.ImageManager
dirPath string
+ minioI minioc.MinIOInfo
+ storeName string // as minio client bucketname
}
// CreateHandler handles creation of the image entry in the database
}
}
-
// getHandler handles GET operations on a particular name
// Returns an Image
func (h imageHandler) getHandler(w http.ResponseWriter, r *http.Request) {
return
}
+ h.minioI.DeleteImage(h.storeName, imageName)
+
w.WriteHeader(http.StatusNoContent)
}
e := "Upload already completed"
w.WriteHeader(http.StatusUnprocessableEntity)
w.Write([]byte(e))
+ log.Println("Upload already completed")
return
}
off, err := strconv.Atoi(r.Header.Get("Upload-Offset"))
}
log.Printf("Upload offset %d\n", off)
if *file.ImageOffset != off {
- e := fmt.Sprintf("Expected Offset %d got offset %d", *file.ImageOffset, off)
+ e := fmt.Sprintf("Expected Offset %d, actual offset %d", *file.ImageOffset, off)
w.WriteHeader(http.StatusConflict)
w.Write([]byte(e))
+ log.Printf("Expected Offset:%d doesn't match got offset:%d\n", *file.ImageOffset, off)
return
}
w.WriteHeader(http.StatusInternalServerError)
return
}
- f, err := os.OpenFile(fp, os.O_APPEND|os.O_WRONLY, 0644)
+ f, err := os.OpenFile(fp, os.O_WRONLY, 0644)
if err != nil {
log.Printf("unable to open file %s\n", err)
w.WriteHeader(http.StatusInternalServerError)
w.WriteHeader(http.StatusInternalServerError)
return
}
+
+ log.Printf("Start to Patch image, bucket: %s, image: %s, dirpath: %s, offset: %d, n: %d\n",
+ h.storeName, imageName, fp, *file.ImageOffset, n)
+ uploadbytes, err := h.minioI.PatchImage(h.storeName, imageName, fp, int64(*file.ImageOffset), int64(n))
+ if err != nil || uploadbytes == 0 {
+ log.Printf("MinIO upload with offset %d failed: %s", *file.ImageOffset, err)
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+
log.Println("number of bytes written ", n)
no := *file.ImageOffset + n
file.ImageOffset = &no