From 75a8778e8b0ae1eb0138b7c1aefe04601b17b7ca Mon Sep 17 00:00:00 2001 From: Petyo Stoyanov Date: Sat, 4 May 2024 22:05:29 +0300 Subject: [PATCH] More stuff --- core/logic_events.go | 6 +- core/model/legacyEvents.go | 2 +- driven/image/adapter.go | 125 +++++++++++++++++++------------------ 3 files changed, 70 insertions(+), 63 deletions(-) diff --git a/core/logic_events.go b/core/logic_events.go index b0d7afce..049476c5 100644 --- a/core/logic_events.go +++ b/core/logic_events.go @@ -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 { diff --git a/core/model/legacyEvents.go b/core/model/legacyEvents.go index 5de6aed2..44d4cd32 100644 --- a/core/model/legacyEvents.go +++ b/core/model/legacyEvents.go @@ -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 diff --git a/driven/image/adapter.go b/driven/image/adapter.go index ff1ae612..c05a49d6 100644 --- a/driven/image/adapter.go +++ b/driven/image/adapter.go @@ -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", @@ -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 }