From 118ba4dcb7dbf85bdb1b54b2ed2b440cd2366f5f Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Sun, 7 May 2017 20:47:04 +1000 Subject: [PATCH] upload image and download it back from wechat server --- download.go | 30 ++++++++++++++++++++++++++++++ upload_test.go | 20 +++++++++++++++++++- wechat_hitxy_access_token | 2 +- 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 download.go diff --git a/download.go b/download.go new file mode 100644 index 0000000..3f7bdf4 --- /dev/null +++ b/download.go @@ -0,0 +1,30 @@ +package main + +import ( + "io" + "io/ioutil" + "log" + "net/http" + "os" + "time" +) + +func saveURL(url string) (tmpFile string, contentType string, err error) { + var myClient = &http.Client{Timeout: 20 * time.Second} + r, err := myClient.Get(url) + if err != nil { + log.Fatal("Fail to get URL:" + url) + log.Fatal(err) + return "", "", err + } + defer r.Body.Close() + file, err := ioutil.TempFile(os.TempDir(), "prefix") + // Use io.Copy to just dump the response body to the file. This supports huge files + _, err = io.Copy(file, r.Body) + if err != nil { + log.Fatal(err) + } + tmpFile = file.Name() + file.Close() + return +} diff --git a/upload_test.go b/upload_test.go index 0f83549..a5e3351 100644 --- a/upload_test.go +++ b/upload_test.go @@ -2,11 +2,29 @@ package main import ( "fmt" + "log" "testing" + + "github.com/stretchr/testify/assert" + "github.com/udhos/equalfile" ) func TestUploadImage(t *testing.T) { SetupConfig() - mediaID := uploadImage("media_for_test/640x480.jpg") + src := "media_for_test/640x480.jpg" + mediaID := uploadImage(src) fmt.Printf("get MediaID: %s ", mediaID) + //200 OK + //{"type":"image","media_id":"3wx8MSvsBYDubAqNi_QZSzNyCrOQ4eoOPslXzpOm6Kzv2Sfh2RngUFlDLfxcSdG2","created_at":1494140549} + //mediaID = "3wx8MSvsBYDubAqNi_QZSzNyCrOQ4eoOPslXzpOm6Kzv2Sfh2RngUFlDLfxcSdG2" + //mediaID = "L2Cyf6F5GvVXpubv75Pnf51f-Z7Ah5fOROqt1qsZTLmrebtOmeQy9Co2xw7JI6ck" + assert.NotEqual(t, mediaID, "", "MediaID should not be Empty") + filename, e := saveMedia2File(mediaID) + log.Println("saved :" + filename) + AssertEqual(t, e, nil, "mediaID should be saved without any error") + //make sure two files are the same + cmp := equalfile.New(nil, equalfile.Options{}) // compare using single mode + equal, err := cmp.CompareFile(src, filename) + AssertEqual(t, err, nil, "file should be equal ") + AssertEqual(t, equal, true, "file should be equal ") } diff --git a/wechat_hitxy_access_token b/wechat_hitxy_access_token index 3111cc1..ea2e0c5 100644 --- a/wechat_hitxy_access_token +++ b/wechat_hitxy_access_token @@ -1 +1 @@ -{"access_token":"-6lyvdFk4jKGE1H0UZBPAzHXz793OXyuXmbjqtdWC0TXFY8KmSUwcL_2dRYYi6k0kp2HwMukFVTKnd1PvfoZrrAjM6dXegRGf1wKQpvobx7C-Zdf-1REcM_BvO9GBJkgQNKeAGAAXU","expires_in":7200,"created_at":"2017-05-07T17:02:29.012072846+10:00"} \ No newline at end of file +{"access_token":"VkqKPaCmYeSLh5p4-0AA7pew_ncB7YY-u5P4S-hc2T8V1KQiRoSsXwjvh4QVb4_eZqEaG_gdrL0e-o9Lz3ajV3HzqftUdzMhi_K7gr0ueguf4MQLFGfahofD7VFoSf98LXVbAHABNL","expires_in":7200,"created_at":"2017-05-07T20:32:46.746644722+10:00"} \ No newline at end of file