diff --git a/pkg/backend/posts/controllers.go b/pkg/backend/posts/controllers.go index a35ab28d..1f49cdaf 100644 --- a/pkg/backend/posts/controllers.go +++ b/pkg/backend/posts/controllers.go @@ -179,7 +179,7 @@ func addNewPost(w http.ResponseWriter, r *http.Request) { content := key + "." + extension - // image pkg magic + // decode image from []byte stream img, format, err := decodeImage(post.Data) if err != nil { resp.Message = "backend error: cannot decode given byte stream" @@ -190,10 +190,10 @@ func addNewPost(w http.ResponseWriter, r *http.Request) { return } - // upload to local storage - //if err := os.WriteFile("/opt/pix/"+content, post.Data, 0600); err != nil { - if err := os.WriteFile("/opt/pix/"+content, post.Data, 0600); err != nil { - resp.Message = "backend error: couldn't save a figure to a file: " + err.Error() + // fix the image orientation for decoded image + img, err = fixOrientation(img, post.Data) + if err != nil { + resp.Message = "backend error: cannot fix image's oriantation: " + err.Error() resp.Code = http.StatusInternalServerError l.Println(resp.Message, resp.Code) @@ -201,10 +201,32 @@ func addNewPost(w http.ResponseWriter, r *http.Request) { return } - // fix the image orientation for thumbnail - img, err = fixOrientation(img, post.Data) + // re-encode the image + newBytes, err := encodeImage(img, format) if err != nil { - resp.Message = "backend error: cannot fix image's oriantation: " + err.Error() + resp.Message = "backend error: cannot re-encode the novel image" + resp.Code = http.StatusBadRequest + + l.Println(resp.Message, resp.Code) + resp.Write(w) + return + } + + // remove EXIF metadata + _, newBytes, err = removeExif(newBytes, format) + if err != nil { + resp.Message = "backend error: cannot remove EXIF metadata" + resp.Code = http.StatusBadRequest + + l.Println(resp.Message, resp.Code) + resp.Write(w) + return + } + + // upload to local storage + //if err := os.WriteFile("/opt/pix/"+content, post.Data, 0600); err != nil { + if err := os.WriteFile("/opt/pix/"+content, newBytes, 0600); err != nil { + resp.Message = "backend error: couldn't save a figure to a file: " + err.Error() resp.Code = http.StatusInternalServerError l.Println(resp.Message, resp.Code) diff --git a/pkg/backend/posts/image.go b/pkg/backend/posts/image.go index 6aca8e7d..27a1e679 100644 --- a/pkg/backend/posts/image.go +++ b/pkg/backend/posts/image.go @@ -14,6 +14,33 @@ import ( _ "image/png" ) +func removeExif(imgBytes []byte, format string) (image.Image, []byte, error) { + // Decode the image + img, _, err := image.Decode(bytes.NewReader(imgBytes)) + if err != nil { + return nil, nil, err + } + + // Create a buffer to hold the new image data (without EXIF metadata) + var buf bytes.Buffer + + // Encode the image without EXIF metadata + switch format { + case "jpeg": + err = jpeg.Encode(&buf, img, nil) + case "png": + err = png.Encode(&buf, img) + default: + return nil, nil, err + } + + if err != nil { + return nil, nil, err + } + + return img, buf.Bytes(), nil +} + // ResizeImage resizes an image to a target width and height while maintaining aspect ratio /*func resizeImage(img image.Image, targetWidth, targetHeight int) image.Image { thumb := image.NewRGBA(image.Rect(0, 0, targetWidth, targetHeight)) @@ -121,7 +148,7 @@ func fixOrientation(img image.Image, imgBytes []byte) (image.Image, error) { // Find the Orientation tag for _, entry := range entries { if entry.TagName == "Orientation" { - fmt.Printf("orientation: entry.Value: %v\n", entry.Value) + //fmt.Printf("orientation: entry.Value: %v\n", entry.Value) orientationRaw := entry.Value.([]uint16) // Orientation should be a uint16 value orientation := orientationRaw[0]