Skip to content

Commit

Permalink
in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Vitanov committed Apr 30, 2024
1 parent 02d9d6c commit ebd487c
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 30 deletions.
113 changes: 99 additions & 14 deletions core/logic_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ package core
import (
"application/core/model"
"application/driven/storage"
"bytes"
"encoding/xml"
"errors"
"fmt"
"image"
"image/png"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
"strconv"
Expand Down Expand Up @@ -691,11 +694,6 @@ func counstructImage(originatingCalendarID, dataSourceEventID, eventID string, p
currAppConfig,
)

/*imageStorePath := "./images"
if prefixPath != "" {
imageStorePath = prefixPath
}*/

imageResponse, err := http.Get(webtoolImageURL)
if err != nil {
fmt.Println("Error:", err)
Expand All @@ -718,7 +716,7 @@ func counstructImage(originatingCalendarID, dataSourceEventID, eventID string, p
defer response.Body.Close()

// Decode the image
img, format, err := image.Decode(response.Body)
img, _, err := image.Decode(response.Body)
if err != nil {
fmt.Println("Error while decoding the image:", err)
return ""
Expand All @@ -731,7 +729,6 @@ func counstructImage(originatingCalendarID, dataSourceEventID, eventID string, p

// Set the filename and quality for the JPEG file
filename := "image.png"
quality := "100" // Quality doesn't apply for PNG format

// Create a new file to save the image as PNG
file, err := os.Create(filename)
Expand All @@ -748,18 +745,106 @@ func counstructImage(originatingCalendarID, dataSourceEventID, eventID string, p
return ""
}

fmt.Printf("Image downloaded and saved as PNG successfully.\n")
fmt.Printf("Format: %s\n", format)
fmt.Printf("Width: %d\n", width)
fmt.Printf("Height: %d\n", height)
fmt.Printf("Filename: %s\n", filename)
fmt.Printf("Quality: %s\n", quality)
fmt.Printf("url: %s\n", webtoolImageURL)
// 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 ""
}
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 ""
}

// Upload the image with additional data
imageURL, err := uploadImage(imageData, height, width, 100, "event/tout", filename)
if err != nil {
fmt.Println("Error uploading image:", err)
return ""
}

fmt.Println("Image uploaded successfully!")
webtoolImageURL = imageURL
}
return webtoolImageURL
}

// Function to upload image to another API along with additional data
func uploadImage(imageData []byte, height int, width int, quality int, path, fileName string) (string, error) {
// URL to which the request will be sent
targetURL := "https://api-dev.rokwire.illinois.edu/content/bbs/image"

// Send the request and get the response
response, err := sendRequest(targetURL, path, width, height, quality, string(imageData))
if err != nil {
fmt.Println("Error:", err)
return "", err
}

fmt.Println("Response:", response)
return response, nil
}

func sendRequest(targetURL, path string, width, height, quality int, filePath string) (string, error) {
// Create a new buffer to store the multipart form data
var requestBody bytes.Buffer
writer := multipart.NewWriter(&requestBody)

// Add the path, width, height, and quality as form fields
_ = writer.WriteField("path", path)
_ = writer.WriteField("width", strconv.Itoa(width))
_ = writer.WriteField("height", strconv.Itoa(height))
_ = writer.WriteField("quality", strconv.Itoa(quality))

// Add the file as a form file field
fileWriter, err := writer.CreateFormFile("fileName", "image.jpg")
if err != nil {
return "", fmt.Errorf("error creating form file: %w", err)
}

// Copy the file data into the file writer
_, err = io.Copy(fileWriter, bytes.NewReader([]byte(filePath)))
if err != nil {
return "", fmt.Errorf("error copying file data: %w", err)
}

// Close the multipart writer
writer.Close()

// Create the HTTP request
request, err := http.NewRequest("POST", targetURL, &requestBody)
if err != nil {
return "", fmt.Errorf("error creating HTTP request: %w", err)
}

// Set the content type header
request.Header.Set("Content-Type", writer.FormDataContentType())

// Send the request
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
return "", fmt.Errorf("error sending HTTP request: %w", err)
}
defer response.Body.Close()

// Read the response body
responseBody, err := io.ReadAll(response.Body)
if err != nil {
return "", fmt.Errorf("error reading response body: %w", err)
}

// Convert response body to string
responseString := string(responseBody)

return responseString, nil
}

// newAppEventsLogic creates new appShared
func newAppEventsLogic(app *Application, eventsBBAdapter EventsBBAdapter, logger logs.Logger) eventsLogic {
timerDone := make(chan bool)
Expand Down
4 changes: 2 additions & 2 deletions driver/web/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (a Adapter) wrapFunc(handler handlerFunc, authorization tokenauth.Handler)
}

// NewWebAdapter creates new WebAdapter instance
func NewWebAdapter(baseURL string, port string, serviceID string, apiKey string, app *core.Application, serviceRegManager *authservice.ServiceRegManager, logger *logs.Logger) Adapter {
func NewWebAdapter(baseURL string, port string, serviceID string, apiKey string, app *core.Application, serviceRegManager *authservice.ServiceRegManager, serviceAccountManager *authservice.ServiceAccountManager, logger *logs.Logger) Adapter {
yamlDoc, err := loadDocsYAML(baseURL)
if err != nil {
logger.Fatalf("error parsing docs yaml - %s", err.Error())
Expand All @@ -224,7 +224,7 @@ func NewWebAdapter(baseURL string, port string, serviceID string, apiKey string,
defaultAPIsHandler := NewDefaultAPIsHandler(app)
clientAPIsHandler := NewClientAPIsHandler(app)
adminAPIsHandler := NewAdminAPIsHandler(app)
bbsAPIsHandler := NewBBsAPIsHandler(app)
bbsAPIsHandler := NewBBsAPIsHandler(app, serviceAccountManager)
tpsAPIsHandler := NewTPSAPIsHandler(app)
apiKeyHandler := NewAPIKeyHandler(app)
return Adapter{baseURL: baseURL, port: port, serviceID: serviceID, cachedYamlDoc: yamlDoc, auth: auth, defaultAPIsHandler: defaultAPIsHandler,
Expand Down
3 changes: 2 additions & 1 deletion driver/web/apis_bbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"time"

"github.com/gorilla/mux"
"github.com/rokwire/core-auth-library-go/v3/authservice"
"github.com/rokwire/core-auth-library-go/v3/tokenauth"
"github.com/rokwire/logging-library-go/v2/logs"
"github.com/rokwire/logging-library-go/v2/logutils"
Expand Down Expand Up @@ -350,7 +351,7 @@ func (h BBsAPIsHandler) checkAppointmentParams(reqParms *utils.Filter, req *http
}

// NewBBsAPIsHandler creates new Building Block API handler instance
func NewBBsAPIsHandler(app *core.Application) BBsAPIsHandler {
func NewBBsAPIsHandler(app *core.Application, serviceAccountManager *authservice.ServiceAccountManager) BBsAPIsHandler {
return BBsAPIsHandler{app: app}
}

Expand Down
23 changes: 13 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ go 1.21

require (
github.com/PuerkitoBio/goquery v1.8.1
github.com/google/uuid v1.3.1
github.com/google/uuid v1.6.0
github.com/gorilla/mux v1.8.0
github.com/oapi-codegen/runtime v1.0.0
github.com/rokwire/core-auth-library-go/v3 v3.0.1
github.com/rokwire/logging-library-go/v2 v2.2.0
github.com/rokwire/core-auth-library-go/v3 v3.2.0
github.com/rokwire/logging-library-go/v2 v2.3.0
github.com/swaggo/http-swagger v1.3.4
github.com/swaggo/swag v1.16.1
go.mongodb.org/mongo-driver v1.14.0
golang.org/x/sync v0.3.0
golang.org/x/sync v0.6.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/casbin/casbin/v2 v2.71.1 // indirect
github.com/casbin/casbin/v2 v2.82.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/spec v0.20.9 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
Expand All @@ -31,28 +31,31 @@ require (
github.com/google/go-cmp v0.5.4 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/swaggo/files v1.0.1 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.11.0 // indirect
gopkg.in/go-playground/validator.v9 v9.31.0 // indirect
)

require (
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/aws/aws-sdk-go v1.44.296 // indirect
github.com/aws/aws-sdk-go v1.50.29 // indirect
github.com/casbin/govaluate v1.1.1 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/rokwire/core-auth-library-go v1.0.9 // indirect
github.com/rokwire/core-auth-library-go/v2 v2.2.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit ebd487c

Please sign in to comment.