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-85]Webtools fixes #91

Merged
merged 36 commits into from
May 5, 2024
Merged
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
f04c4d0
in progress
Apr 25, 2024
02d9d6c
in progress
Apr 29, 2024
ebd487c
in progress
Apr 30, 2024
abb293d
fix lint issues
Apr 30, 2024
7ed283e
fix duration
May 1, 2024
d0d5d91
resolve comment
May 1, 2024
970c636
set the imageAdapter
May 1, 2024
2ab40a5
add new model and new mongo collection for the images
May 1, 2024
bfb704b
FindAllImages from the database
May 1, 2024
2c12949
download image from webtools API in progress
May 1, 2024
66ab100
upload image
May 1, 2024
0d6f519
in progress
May 1, 2024
996020e
in progress
May 1, 2024
ac82d8f
fix the url
May 1, 2024
a386d80
fix lint issues
May 1, 2024
b7eaf79
fix lint issues
May 1, 2024
475e489
fix comment
May 1, 2024
ba01c3c
Remove image png
petyos May 2, 2024
01e99fb
Use prefixes
petyos May 2, 2024
c86139d
Align a bit
petyos May 2, 2024
01d01b3
Fix priv key?
petyos May 2, 2024
7a69889
Align code
petyos May 2, 2024
65518f0
Define process images function
petyos May 2, 2024
0125a9c
Remove useless loop and fix log
petyos May 2, 2024
6eef7d2
Handle when error occured
petyos May 2, 2024
9c0a151
Use the library when calling bbs API!
petyos May 3, 2024
2250262
Return the url
petyos May 3, 2024
e608ce1
In progress
petyos May 3, 2024
967f08a
Why do you call this API two times??
petyos May 3, 2024
75a8778
More stuff
petyos May 4, 2024
9271505
Get the events for images processing
petyos May 5, 2024
9fa7bcf
Get the events which are not processed
petyos May 5, 2024
28e8ea2
Apply process images
petyos May 5, 2024
c9a4a3e
Work with the processed image data
petyos May 5, 2024
fb0a520
Pix path
petyos May 5, 2024
52a992c
Fix timer
petyos May 5, 2024
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
Prev Previous commit
Next Next commit
Handle when error occured
petyos committed May 2, 2024
commit 6eef7d293c11f9c61033f5473a7748480b80d8de
40 changes: 20 additions & 20 deletions core/logic_events.go
Original file line number Diff line number Diff line change
@@ -326,39 +326,39 @@ func (e eventsLogic) processWebToolsEvents() {
}

func (e eventsLogic) processImages(allWebtoolsEvents []model.WebToolsEvent) error {
contentImagesFromTheDataBase, err := e.app.storage.FindImageItems()
/*contentImagesFromTheDataBase, err := e.app.storage.FindImageItems()
if err != nil {
e.logger.Error("Error on finding image items")
return err
}
} */

images, err := e.app.imageAdapter.ProcessImages(allWebtoolsEvents)
_, err := e.app.imageAdapter.ProcessImages(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 {
err = e.app.storage.InsertImageItems(l)
/* for _, t := range contentImagesFromTheDataBase {
for _, l := range images {
if t.ID != l.ID && t.ImageURL != l.ImageURL {
err = e.app.storage.InsertImageItems(l)

}
}
}
}

// Create a map to store ImageURLs with corresponding IDs
imageURLMap := make(map[string]string)
for _, ciu := range images {
imageURLMap[ciu.ID] = ciu.ImageURL
}

for i := range allWebtoolsEvents {
if allWebtoolsEvents[i].LargeImageUploaded == "false" {
allWebtoolsEvents[i].ImageURL = ""
} else if imageURL, ok := imageURLMap[allWebtoolsEvents[i].EventID]; ok {
allWebtoolsEvents[i].ImageURL = imageURL
// Create a map to store ImageURLs with corresponding IDs
imageURLMap := make(map[string]string)
for _, ciu := range images {
imageURLMap[ciu.ID] = ciu.ImageURL
}
}

for i := range allWebtoolsEvents {
if allWebtoolsEvents[i].LargeImageUploaded == "false" {
allWebtoolsEvents[i].ImageURL = ""
} else if imageURL, ok := imageURLMap[allWebtoolsEvents[i].EventID]; ok {
allWebtoolsEvents[i].ImageURL = imageURL
}
} */

//TODO
return nil
61 changes: 47 additions & 14 deletions driven/image/adapter.go
Original file line number Diff line number Diff line change
@@ -22,39 +22,67 @@ import (
"image/png"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
"strconv"

"github.com/rokwire/core-auth-library-go/v3/authservice"
"github.com/rokwire/logging-library-go/v2/errors"
"github.com/rokwire/logging-library-go/v2/logs"
)

// Adapter implements the Image interface
type Adapter struct {
baseURL string
accountManager *authservice.ServiceAccountManager

logger logs.Logger
}

// ProcessImages downloads from webtools and uploads in content
func (im Adapter) ProcessImages(item []model.WebToolsEvent) ([]model.ContentImagesURL, error) {
var contentImageURL []model.ContentImagesURL
itemsWithImages := 0
for _, w := range item {
if w.LargeImageUploaded != "false" {
webtoolsImage, err := im.downloadWebtoolImages(w)
if err != nil {
return nil, err
}
uploadImageFromContent, _ := im.uploadImageFromContent(webtoolsImage.ImageData,
webtoolsImage.Height, webtoolsImage.Width, webtoolsImage.Quality,
webtoolsImage.Path, webtoolsImage.FileName)
contentImage := model.ContentImagesURL{ID: w.EventID, ImageURL: uploadImageFromContent}
contentImageURL = append(contentImageURL, contentImage)
if w.LargeImageUploaded != "true" {
continue
}

err := im.processImage(w)
if err != nil {
return nil, err
}

itemsWithImages++
}
return contentImageURL, nil

im.logger.Infof("events with images: %d", itemsWithImages)

return nil, errors.New("not implemented")
}

func (im Adapter) processImage(item model.WebToolsEvent) error {
//downlaod
webtoolsImage, err := im.downloadWebtoolImages(item)
if err != nil {
return err
}

//upload
uploadImageFromContent, err := im.uploadImageFromContent(webtoolsImage.ImageData,
webtoolsImage.Height, webtoolsImage.Width, webtoolsImage.Quality,
webtoolsImage.Path, webtoolsImage.FileName)
if err != nil {
return err
}

//contentImage := model.ContentImagesURL{ID: item.EventID, ImageURL: uploadImageFromContent}
//contentImageURL = append(contentImageURL, contentImage)
fmt.Println(uploadImageFromContent)
return nil
}

func (im Adapter) downloadWebtoolImages(item model.WebToolsEvent) (*model.ImageData, error) {
var webtoolImage model.ImageData
currentAppConfig := "https://calendars.illinois.edu/eventImage"
@@ -197,6 +225,11 @@ func sendRequest(targetURL, path string, width, height, quality int, filePath st
}
defer response.Body.Close()

if response.StatusCode != 200 {
log.Printf("error with response code - %d", response.StatusCode)
return "", fmt.Errorf("error with response code != 200")
}

// Read the response body
responseBody, err := io.ReadAll(response.Body)
if err != nil {
@@ -210,6 +243,6 @@ func sendRequest(targetURL, path string, width, height, quality int, filePath st
}

// NewImageAdapter creates a new image adapter instance
func NewImageAdapter(imageHost string, accountManager *authservice.ServiceAccountManager) *Adapter {
return &Adapter{baseURL: imageHost, accountManager: accountManager}
func NewImageAdapter(imageHost string, accountManager *authservice.ServiceAccountManager, logger logs.Logger) *Adapter {
return &Adapter{baseURL: imageHost, accountManager: accountManager, logger: logger}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -125,7 +125,7 @@ func main() {

// image adapter
imageBaseURL := envLoader.GetAndLogEnvVar(envPrefix+"CONTENT_BB_BASE_URL", true, true)
imageAdapter := image.NewImageAdapter(imageBaseURL, serviceAccountManager)
imageAdapter := image.NewImageAdapter(imageBaseURL, serviceAccountManager, *logger)
if err != nil {
logger.Fatalf("Error initializing sports adapter: %v", err)
}