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

Refactor/add attachment #95

Merged
merged 2 commits into from
Aug 19, 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
6 changes: 5 additions & 1 deletion cmd/commands/datasetIngestor.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,11 @@ For Windows you need instead to specify -user username:password on the command l
datasetId := datasetIngestor.IngestDataset(client, APIServer, metaDataMap, fullFileArray, user)
// add attachment optionally
if addAttachment != "" {
datasetIngestor.AddAttachment(client, APIServer, datasetId, metaDataMap, user["accessToken"], addAttachment, addCaption)
err := datasetIngestor.AddAttachment(client, APIServer, datasetId, metaDataMap, user["accessToken"], addAttachment, addCaption)
if err != nil {
log.Println("Couldn't add attachment:", err)
}
log.Printf("Attachment file %v added to dataset %v\n", addAttachment, datasetId)
}
if copyFlag {
err := datasetIngestor.SyncDataToFileserver(datasetId, user, RSYNCServer, sourceFolder, absFileListing)
Expand Down
74 changes: 39 additions & 35 deletions datasetIngestor/addAttachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"bytes"
"encoding/base64"
"encoding/json"
"log"
"fmt"
"net/http"
"os"
"strings"
Expand All @@ -17,70 +17,74 @@ func ReadAndEncodeImage(attachmentFile string) (string, error) {
return "", err
}
defer imgFile.Close()

// create a new buffer base on file size
fInfo, _ := imgFile.Stat()
fInfo, err := imgFile.Stat()
if err != nil {
return "", err
}
var size int64 = fInfo.Size()
buf := make([]byte, size)

// read file content into buffer
fReader := bufio.NewReader(imgFile)
fReader.Read(buf)

_, err = fReader.Read(buf)
if err != nil {
return "", err
}

// convert the buffer bytes to base64 string
imgBase64Str := base64.StdEncoding.EncodeToString(buf)
return imgBase64Str, nil
}

func CreateMetadataMap(datasetId string, caption string, metaDataDataset map[string]interface{}, imgBase64Str string) (map[string]interface{}, error) {
func CreateAttachmentMap(datasetId string, caption string, datasetMetadata map[string]interface{}, imgBase64Str string) (map[string]interface{}, error) {
// assemble json structure
var metaDataMap map[string]interface{}
metaDataMap = make(map[string]interface{})
metaDataMap["thumbnail"] = "data:image/jpeg;base64," + imgBase64Str
metaDataMap["caption"] = caption
metaDataMap["datasetId"] = datasetId
if ownerGroup, ok := metaDataDataset["ownerGroup"]; ok {
metaDataMap["ownerGroup"], _ = ownerGroup.(string)
metadata := make(map[string]interface{})
metadata["thumbnail"] = "data:image/jpeg;base64," + imgBase64Str
metadata["caption"] = caption
metadata["datasetId"] = datasetId
// if we're able, extract some informations from the dataset metadata
if ownerGroup, ok := datasetMetadata["ownerGroup"]; ok {
metadata["ownerGroup"], _ = ownerGroup.(string)
}
if accessGroups, ok := metaDataDataset["accessGroups"]; ok {
metaDataMap["accessGroups"], ok = accessGroups.([]string)
if !ok {
metaDataMap["accessGroups"], _ = accessGroups.([]interface{})
if accessGroups, ok := datasetMetadata["accessGroups"]; ok {
if metadata["accessGroups"], ok = accessGroups.([]string); !ok {
metadata["accessGroups"] = accessGroups // fallback (might fail at JSON conversion later)
}
}
return metaDataMap, nil
return metadata, nil
}

func AddAttachment(client *http.Client, APIServer string, datasetId string, metaDataDataset map[string]interface{}, accessToken string, attachmentFile string, caption string) {
func AddAttachment(client *http.Client, APIServer string, datasetId string, datasetMetadata map[string]interface{}, accessToken string, attachmentFile string, caption string) error {
imgBase64Str, err := ReadAndEncodeImage(attachmentFile)
if err != nil {
log.Fatalf("Can not open attachment file %v \n", attachmentFile)
return err
}
metaDataMap, err := CreateMetadataMap(datasetId, caption, metaDataDataset, imgBase64Str)

attachmentMap, err := CreateAttachmentMap(datasetId, caption, datasetMetadata, imgBase64Str)
if err != nil {
log.Fatal("Connect serialize meta data map:", metaDataMap)
return err
}
bm, err := json.Marshal(metaDataMap)

attachmentJson, err := json.Marshal(attachmentMap)
if err != nil {
log.Fatal("Connect serialize meta data map:", metaDataMap)
return err
}
myurl := APIServer + "/Datasets/" + strings.Replace(datasetId, "/", "%2F", 1) + "/attachments?access_token=" + accessToken
req, err := http.NewRequest("POST", myurl, bytes.NewBuffer(bm))

req, err := http.NewRequest("POST", myurl, bytes.NewBuffer(attachmentJson))
if err != nil {
log.Fatal(err)
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
return err
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
log.Printf("Attachment file %v added to dataset %v\n", attachmentFile, datasetId)
} else {
log.Fatalf("Attachment file %v could not be added to dataset %v", attachmentFile, datasetId)
if resp.StatusCode != 200 {
return fmt.Errorf("attachment file %v could not be added to dataset %v - status code: %d", attachmentFile, datasetId, resp.StatusCode)
}
return nil
}
49 changes: 26 additions & 23 deletions datasetIngestor/addAttachment_test.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,47 @@
package datasetIngestor

import (
"encoding/base64"
"net/http"
"net/http/httptest"
"os"
"testing"
"encoding/base64"
)

//Check whether the function is called without a panic
// Check whether the function is called without a panic
func TestAddAttachment(t *testing.T) {
// Create a mock server
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer mockServer.Close()

client := &http.Client{}
APIServer := mockServer.URL
datasetId := "testDatasetId"
metaDataDataset := make(map[string]interface{})
accessToken := "testAccessToken"
caption := "testCaption"

// Create a temporary file
tempFile, err := os.CreateTemp("", "testAttachmentFile")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tempFile.Name()) // clean up

attachmentFile := tempFile.Name()

defer func() {
if r := recover(); r != nil {
t.Errorf("The code panicked with %v", r)
}
}()

AddAttachment(client, APIServer, datasetId, metaDataDataset, accessToken, attachmentFile, caption)

err = AddAttachment(client, APIServer, datasetId, metaDataDataset, accessToken, attachmentFile, caption)
if err != nil {
t.Errorf("The function returned an error: \"%v\"", err)
}
}

func TestReadAndEncodeImage(t *testing.T) {
Expand All @@ -48,9 +51,9 @@ func TestReadAndEncodeImage(t *testing.T) {
t.Fatal(err)
}
defer os.Remove(tempFile.Name()) // clean up

imageFile := tempFile.Name()

encodedStr, err := ReadAndEncodeImage(imageFile)
if err != nil {
t.Errorf("ReadAndEncodeImage returned an error: %v", err)
Expand All @@ -59,52 +62,52 @@ func TestReadAndEncodeImage(t *testing.T) {
// Check if the output is a valid base64 string
_, err = base64.StdEncoding.DecodeString(encodedStr)
if err != nil {
t.Errorf("ReadAndEncodeImage returned an invalid base64 string: %v", err)
t.Errorf("ReadAndEncodeImage returned an invalid base64 string: %v", err)
}
}

func TestCreateMetadataMap(t *testing.T) {
func TestCreateAttachmentMap(t *testing.T) {
datasetId := "testDatasetId"
caption := "testCaption"
metaDataDataset := make(map[string]interface{})
attachmentMap := make(map[string]interface{})

// Create a temporary file
tempFile, err := os.CreateTemp("", "testImageFile")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tempFile.Name()) // clean up

imageFile := tempFile.Name()

imgBase64Str, err := ReadAndEncodeImage(imageFile)
if err != nil {
t.Fatal(err)
}
metaDataMap, err := CreateMetadataMap(datasetId, caption, metaDataDataset, imgBase64Str)

metaDataMap, err := CreateAttachmentMap(datasetId, caption, attachmentMap, imgBase64Str)
if err != nil {
t.Errorf("CreateMetadataMap returned an error: %v", err)
}

// Check if the map contains the correct keys and values
if metaDataMap["thumbnail"] != "data:image/jpeg;base64,"+imgBase64Str {
t.Errorf("Incorrect thumbnail: got %v, want %v", metaDataMap["thumbnail"], "data:image/jpeg;base64,"+imgBase64Str)
}

if metaDataMap["caption"] != caption {
t.Errorf("Incorrect caption: got %v, want %v", metaDataMap["caption"], caption)
}

if metaDataMap["datasetId"] != datasetId {
t.Errorf("Incorrect datasetId: got %v, want %v", metaDataMap["datasetId"], datasetId)
}

// Check if the map does not contain the keys "ownerGroup" and "accessGroups"
if _, ok := metaDataMap["ownerGroup"]; ok {
t.Errorf("Map contains unexpected key: ownerGroup")
}

if _, ok := metaDataMap["accessGroups"]; ok {
t.Errorf("Map contains unexpected key: accessGroups")
}
Expand Down