Skip to content

Commit 0db52da

Browse files
authored
Merge pull request #10 from apprentice3d/develop
fixed the model derivative result target decoding;
2 parents 8db5245 + e48888c commit 0db52da

File tree

3 files changed

+115
-23
lines changed

3 files changed

+115
-23
lines changed

md/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func translate(path string, params TranslationParams, token string) (result Tran
142142

143143
decoder := json.NewDecoder(response.Body)
144144

145-
err = decoder.Decode(&response)
145+
err = decoder.Decode(&result)
146146

147147
return
148148
}

recap/call.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import (
44
"bytes"
55
"encoding/json"
66
"errors"
7-
"io"
87
"io/ioutil"
98
"log"
109
"mime/multipart"
1110
"net/http"
1211
"net/url"
1312
"strconv"
1413
"strings"
14+
"time"
15+
"math/rand"
1516
)
1617

1718
func createPhotoScene(path string, name string, formats []string, sceneType string, token string) (scene PhotoScene, err error) {
@@ -74,10 +75,16 @@ func addFileToSceneUsingLink(path string, photoSceneID string, link string, toke
7475

7576
task := http.Client{}
7677

77-
params := `photosceneid=` + photoSceneID + `&type=image`
78-
params += `&file[0]=` + link
78+
//params := `photosceneid=` + photoSceneID + `&type=image`
79+
//params += `&file[0]=` + link
80+
//
81+
//body := strings.NewReader(params)
7982

80-
body := strings.NewReader(params)
83+
body := &bytes.Buffer{}
84+
writer := multipart.NewWriter(body)
85+
writer.WriteField("photosceneid", photoSceneID)
86+
writer.WriteField("type", "image")
87+
writer.WriteField("file[0", link)
8188

8289
req, err := http.NewRequest("POST",
8390
path+"/file",
@@ -88,7 +95,7 @@ func addFileToSceneUsingLink(path string, photoSceneID string, link string, toke
8895
return
8996
}
9097

91-
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
98+
req.Header.Set("Content-Type", writer.FormDataContentType())
9299
req.Header.Set("Authorization", "Bearer "+token)
93100
response, err := task.Do(req)
94101
if err != nil {
@@ -122,22 +129,18 @@ func addFileToSceneUsingLink(path string, photoSceneID string, link string, toke
122129

123130
func addFileToSceneUsingFileData(path string, photoSceneID string, data []byte, token string) (result FileUploadingReply, err error) {
124131

132+
rand.Seed(time.Now().UnixNano())
133+
125134
body := &bytes.Buffer{}
126135
writer := multipart.NewWriter(body)
127-
formFile, err := writer.CreateFormFile("file[0]", "datafile")
136+
writer.WriteField("photosceneid", photoSceneID)
137+
writer.WriteField("type", "image")
138+
formFile, err := writer.CreateFormFile("file[0]", "data" + strconv.Itoa(rand.Int()))
128139
if err != nil {
129140
log.Println(err.Error())
130141
return
131142
}
132-
133-
dataContent := bytes.NewReader(data)
134-
if _, err = io.Copy(formFile, dataContent); err != nil {
135-
log.Println(err.Error())
136-
return
137-
}
138-
139-
writer.WriteField("photosceneid", photoSceneID)
140-
writer.WriteField("type", "image")
143+
formFile.Write(data)
141144
writer.Close()
142145

143146
task := http.Client{}

recap/recap_test.go

Lines changed: 96 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,46 @@ func TestReCapAPIWorkflowUsingRemoteLinks(t *testing.T) {
8888
}
8989
})
9090

91+
92+
t.Run("Check the result file size for normal size", func(t *testing.T) {
93+
response, err := recapAPI.GetSceneResults(scene.ID, testingFormat)
94+
if err != nil {
95+
t.Error(err.Error())
96+
}
97+
if len(response.PhotoScene.SceneLink) == 0 {
98+
t.Error("The received link is empty")
99+
}
100+
101+
filename := "temp.zip"
102+
103+
resp, err := http.Get(response.PhotoScene.SceneLink)
104+
105+
if err != nil {
106+
return
107+
}
108+
defer resp.Body.Close()
109+
result, err := os.Create(filename)
110+
if err != nil {
111+
return
112+
}
113+
defer result.Close()
114+
115+
tempFile, err := os.Stat(filename)
116+
117+
if tempFile.Size() <= 22 {
118+
t.Error("The scene was processed, but the result file is abnormally small: ", tempFile.Size())
119+
}
120+
121+
err = os.Remove(filename)
122+
if err != nil {
123+
t.Error(err.Error())
124+
}
125+
126+
return
127+
128+
})
129+
130+
91131
t.Run("Delete the scene", func(t *testing.T) {
92132
_, err := recapAPI.DeleteScene(scene.ID)
93133
if err != nil {
@@ -190,12 +230,50 @@ func TestReCapAPIWorkflowUsingLocalFiles(t *testing.T) {
190230
}
191231
})
192232

193-
//t.Run("Delete the scene", func(t *testing.T) {
194-
// _, err := recapAPI.DeleteScene(scene.ID)
195-
// if err != nil {
196-
// t.Fatal(err.Error())
197-
// }
198-
//})
233+
t.Run("Check the result file size for normal size", func(t *testing.T) {
234+
response, err := recapAPI.GetSceneResults(scene.ID, testingFormat)
235+
if err != nil {
236+
t.Error(err.Error())
237+
}
238+
if len(response.PhotoScene.SceneLink) == 0 {
239+
t.Error("The received link is empty")
240+
}
241+
242+
filename := "temp.zip"
243+
244+
resp, err := http.Get(response.PhotoScene.SceneLink)
245+
246+
if err != nil {
247+
return
248+
}
249+
defer resp.Body.Close()
250+
result, err := os.Create(filename)
251+
if err != nil {
252+
return
253+
}
254+
defer result.Close()
255+
256+
tempFile, err := os.Stat(filename)
257+
258+
if tempFile.Size() <= 22 {
259+
t.Error("The scene was processed, but the result file is abnormally small: ", tempFile.Size())
260+
}
261+
262+
err = os.Remove(filename)
263+
if err != nil {
264+
t.Error(err.Error())
265+
}
266+
267+
return
268+
269+
})
270+
271+
t.Run("Delete the scene", func(t *testing.T) {
272+
_, err := recapAPI.DeleteScene(scene.ID)
273+
if err != nil {
274+
t.Fatal(err.Error())
275+
}
276+
})
199277

200278
}
201279

@@ -205,13 +283,24 @@ func TestCreatePhotoScene(t *testing.T) {
205283
clientID := os.Getenv("FORGE_CLIENT_ID")
206284
clientSecret := os.Getenv("FORGE_CLIENT_SECRET")
207285
recapAPI := recap.NewAPIWithCredentials(clientID, clientSecret)
286+
var sceneID string
208287

209288
t.Run("Create a scene", func(t *testing.T) {
210-
_, err := recapAPI.CreatePhotoScene("testare", nil, "object")
289+
response, err := recapAPI.CreatePhotoScene("testare", nil, "object")
211290

212291
if err != nil {
213292
t.Fatalf("Failed to create a photoscene: %s\n", err.Error())
214293
}
294+
295+
sceneID = response.ID
296+
})
297+
298+
t.Run("Delete the test scene", func(t *testing.T) {
299+
_, err := recapAPI.DeleteScene(sceneID)
300+
301+
if err != nil {
302+
t.Fatalf("Failed to delete the photoscene: %s\n", err.Error())
303+
}
215304
})
216305

217306
t.Run("Check fail on create a scene with empty name", func(t *testing.T) {

0 commit comments

Comments
 (0)