Skip to content

Commit

Permalink
Merge pull request #33 from TheCacophonyProject/use-file
Browse files Browse the repository at this point in the history
change upload thermal to take whole map
  • Loading branch information
CameronRP authored Jul 24, 2022
2 parents ff8d1a3 + 4560352 commit a9ba68d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
18 changes: 8 additions & 10 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,8 @@ func shaHash(r io.Reader) (string, error) {
return hashString, nil
}

// UploadThermalRaw uploads the file to Cacophony API as a multipartmessage
// with data of type thermalRaw specified
func (api *CacophonyAPI) UploadThermalRaw(r io.Reader, metadata map[string]interface{}) (int, error) {
// UploadVideo uploads the file to Cacophony API as a multipartmessage
func (api *CacophonyAPI) UploadVideo(r io.Reader, data map[string]interface{}) (int, error) {
buf := new(bytes.Buffer)
w := multipart.NewWriter(buf)
//This will write to fileBytes as it reads r to get the sha hash
Expand All @@ -302,15 +301,14 @@ func (api *CacophonyAPI) UploadThermalRaw(r io.Reader, metadata map[string]inter
if err != nil {
return 0, err
}

data := map[string]interface{}{
"type": "thermalRaw",
"fileHash": hash,
if data == nil {
data = make(map[string]interface{})
}

if metadata != nil {
data["metadata"] = metadata
if _, ok := data["type"]; !ok {
data["type"] = "thermalRaw"
}
data["fileHash"] = hash

// JSON encoded "data" parameter.
dataBuf, err := json.Marshal(data)

Expand Down
18 changes: 9 additions & 9 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ func TestNewTokenHttpRequest(t *testing.T) {
assert.NoError(t, err)
}

func TestUploadThermalRawHttpRequest(t *testing.T) {
ts := GetUploadThermalRawServer(t)
func TestUploadVideoHttpRequest(t *testing.T) {
ts := GetUploadVideoServer(t)
defer ts.Close()

api := getAPI(ts.URL, "", true)
reader := strings.NewReader(rawThermalData)
id, err := api.UploadThermalRaw(reader, nil)
id, err := api.UploadVideo(reader, nil)
assert.NoError(t, err)
assert.NotEmpty(t, id)
}
Expand Down Expand Up @@ -182,9 +182,9 @@ func getMimeParts(r *http.Request) (map[string]interface{}, string) {
return data, fileData
}

//GetUploadThermalRawServer checks that the message is multipart and contains the required multipartmime file:file and Value:data
//GetUploadVideoServer checks that the message is multipart and contains the required multipartmime file:file and Value:data
//and Authorization header
func GetUploadThermalRawServer(t *testing.T) *httptest.Server {
func GetUploadVideoServer(t *testing.T) *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
assert.NotEmpty(t, r.Header.Get("Authorization"))
Expand Down Expand Up @@ -218,7 +218,7 @@ func randomRegister() (*CacophonyAPI, error) {
return Register(randString(20), randString(20), defaultGroup, apiURL, int(rand.Int31()))
}

func TestAPIUploadThermalRaw(t *testing.T) {
func TestAPIUploadVideo(t *testing.T) {
defer newFs(t, "")()
api, err := randomRegister()
require.NoError(t, err)
Expand All @@ -227,7 +227,7 @@ func TestAPIUploadThermalRaw(t *testing.T) {
assert.NoError(t, err)
defer reader.Close()

id, err := api.UploadThermalRaw(reader, nil)
id, err := api.UploadVideo(reader, nil)
assert.NoError(t, err)
assert.NotEmpty(t, id)
}
Expand Down Expand Up @@ -304,7 +304,7 @@ func TestRegisterAndNew(t *testing.T) {
assert.NoError(t, err)
defer reader.Close()

id, err := api2.UploadThermalRaw(reader, nil)
id, err := api2.UploadVideo(reader, nil)
assert.NoError(t, err, "check that api can upload recordings")
assert.NotEmpty(t, id, "check that recording id is not 0")

Expand Down Expand Up @@ -426,7 +426,7 @@ func TestDeviceReregister(t *testing.T) {
assert.NoError(t, err)
defer reader.Close()

id, err := api2.UploadThermalRaw(reader, nil)
id, err := api2.UploadVideo(reader, nil)
assert.NoError(t, err)
assert.NotEmpty(t, id)
}
Expand Down

0 comments on commit a9ba68d

Please sign in to comment.