Skip to content

Commit

Permalink
remove EXIF header from the uploaded pic
Browse files Browse the repository at this point in the history
  • Loading branch information
krustowski committed Sep 7, 2024
1 parent 990a2e7 commit 525351f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
38 changes: 30 additions & 8 deletions pkg/backend/posts/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -190,21 +190,43 @@ 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)
resp.Write(w)
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)
Expand Down
29 changes: 28 additions & 1 deletion pkg/backend/posts/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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]

Expand Down

0 comments on commit 525351f

Please sign in to comment.