Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ID-92]Webtools images fixes #93

Merged
merged 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Webtools images fixes [#92](https://github.com/rokwire/gateway-building-block/issues/92)

[2.8.0] - 2024-05-05
### Changed
Expand Down
75 changes: 22 additions & 53 deletions driven/image/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ import (
"image"
"image/png"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
"strconv"

"github.com/rokwire/core-auth-library-go/v3/authservice"
Expand Down Expand Up @@ -69,8 +67,6 @@ func (im Adapter) ProcessImage(item model.WebToolsEvent) (*model.ContentImagesUR

// Why do you call this API two times??
func (im Adapter) downloadWebtoolImages(item model.WebToolsEvent) (*model.ImageData, error) {
var webtoolImage model.ImageData

currentAppConfig := "https://calendars.illinois.edu/eventImage"
currAppConfig := "large.png"
webtoolImageURL := fmt.Sprintf("%s/%s/%s/%s",
Expand All @@ -80,76 +76,49 @@ func (im Adapter) downloadWebtoolImages(item model.WebToolsEvent) (*model.ImageD
currAppConfig,
)

imageResponse, err := http.Get(webtoolImageURL)
if err != nil {
fmt.Println("Error:", err)
return nil, nil
}
defer imageResponse.Body.Close()

//if not 200 then we do not care
if imageResponse.StatusCode != http.StatusOK {
im.logger.Infof("response code %d for %s", imageResponse.StatusCode, item.EventID)
return nil, nil
}

//it is response code 200

// Make a GET request to the image URL
// Make a GET request to download the image
response, err := http.Get(webtoolImageURL)
if err != nil {
fmt.Println("Error while downloading the image:", err)
return nil, nil
return nil, err
}
defer response.Body.Close()

// Check if the response status code is OK
if response.StatusCode != http.StatusOK {
im.logger.Infof("response code %d for %s", response.StatusCode, item.EventID)
return nil, nil //do not return error when cannot get/find an image
}

// Decode the image
img, _, err := image.Decode(response.Body)
if err != nil {
fmt.Println("Error while decoding the image:", err)
return nil, nil
return nil, err
}

// Get the image dimensions
bounds := img.Bounds()
width := bounds.Dx()
height := bounds.Dy()

// Set the filename and quality for the JPEG file
filename := "image.png"

// Create a new file to save the image as PNG
file, err := os.Create(filename)
if err != nil {
fmt.Println("Error creating file:", err)
return nil, nil
}
defer file.Close()

// Encode the image as PNG and save it to the file
err = png.Encode(file, img)
if err != nil {
fmt.Println("Error while saving the image as PNG:", err)
return nil, nil
}

// Download the image and fetch additional data
// Fetch the image from the URL
resp, err := http.Get(webtoolImageURL)
// Encode the image as PNG
var buf bytes.Buffer
err = png.Encode(&buf, img)
if err != nil {
fmt.Println("Error fetching image:", err)
return nil, nil
fmt.Println("Error while encoding the image as PNG:", err)
return nil, err
}
defer resp.Body.Close()

// Read the image data into a byte slice
imageData, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading image data:", err)
return nil, nil
// Fetch additional data and return
webtoolImage := model.ImageData{
ImageData: buf.Bytes(),
Height: height,
Width: width,
Quality: 100,
Path: "event/tout",
FileName: "image.png",
}
webtoolImage = model.ImageData{ImageData: imageData, Height: height, Width: width,
Quality: 100, Path: "event/tout", FileName: filename}

return &webtoolImage, nil
}
Expand Down
Loading