Skip to content

Commit

Permalink
More stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
petyos committed May 4, 2024
1 parent 967f08a commit 75a8778
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 63 deletions.
6 changes: 5 additions & 1 deletion core/logic_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,15 @@ func (e eventsLogic) processImages(allWebtoolsEvents []model.WebToolsEvent) erro
return err
} */

err := e.applyProcessImages(allWebtoolsEvents)


/*err := e.applyProcessImages(allWebtoolsEvents)
if err != nil {
e.logger.Error("Error on processing images")
return err
}
*/
/* for _, t := range contentImagesFromTheDataBase {
for _, l := range images {
if t.ID != l.ID && t.ImageURL != l.ImageURL {
Expand Down
2 changes: 1 addition & 1 deletion core/model/legacyEvents.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type WebToolsEvent struct {
ID string `xml:"id"`
Name string `xml:"name"`
} `xml:"topic"`
ImageURL string `json:"imageURL"`
ImageURL string //we keep preprocessed image url in this field
}

// WebToolsItem represents web tools blacklist ids
Expand Down
125 changes: 64 additions & 61 deletions driven/image/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ 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 @@ -86,68 +87,70 @@ func (im Adapter) downloadWebtoolImages(item model.WebToolsEvent) (*model.ImageD
}
defer imageResponse.Body.Close()

if imageResponse.StatusCode == http.StatusNotFound {
webtoolImageURL = ""
}

if imageResponse.StatusCode == http.StatusOK {

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

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

// 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)
if err != nil {
fmt.Println("Error fetching image:", err)
return nil, nil
}
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
}
webtoolImage = model.ImageData{ImageData: imageData, Height: height, Width: width,
Quality: 100, Path: "event/tout/tmp", FileName: filename}
//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
response, err := http.Get(webtoolImageURL)
if err != nil {
fmt.Println("Error while downloading the image:", err)
return nil, nil
}
defer response.Body.Close()

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

// 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)
if err != nil {
fmt.Println("Error fetching image:", err)
return nil, nil
}
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
}
webtoolImage = model.ImageData{ImageData: imageData, Height: height, Width: width,
Quality: 100, Path: "event/tout/tmp", FileName: filename}

return &webtoolImage, nil
}

Expand Down

0 comments on commit 75a8778

Please sign in to comment.