From a65814671ae61d1c86de3fd4259060061e5ecbeb Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Sat, 20 May 2017 00:41:25 +1000 Subject: [PATCH] download attachment and save as a new name --- crmAttachment.go | 13 ++++++++++--- crmAttachment_test.go | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/crmAttachment.go b/crmAttachment.go index 5393bfc..50638a2 100644 --- a/crmAttachment.go +++ b/crmAttachment.go @@ -6,6 +6,7 @@ import ( "fmt" "io/ioutil" "log" + "os" ) type crmFileInfo struct { @@ -131,16 +132,22 @@ func crmCreateAttachment(path string) (result attachmentInfo, err error) { } func crmDownloadAttachmentAs(fileID, saveAs string) (err error) { - return nil + + f, err := crmDownloadAttachment(fileID) + if err != nil { + return + } + err = os.Rename(f, saveAs) + return } -func crmDownloadAttachment(fileID string) (err error) { +func crmDownloadAttachment(fileID string) (filename string, err error) { u := crmURL4DownloadAttachmet(fileID) headers := map[string]string{} headers["Authorization"] = crmAuthHeader() f, _, err := saveURLwithHTTPHeader(u, headers) log.Println(f) - return nil + return f, err } func crmURL4DownloadAttachmet(fileID string) string { diff --git a/crmAttachment_test.go b/crmAttachment_test.go index cae38ca..a1ec049 100644 --- a/crmAttachment_test.go +++ b/crmAttachment_test.go @@ -25,4 +25,5 @@ func TestDecodeJsonResponse(t *testing.T) { func TestCRMDownloadAttachment(t *testing.T) { SetupConfig() crmDownloadAttachment("591ef9c61d6ca779c") + crmDownloadAttachmentAs("591ef9c61d6ca779c", "/tmp/wechat_hitxy_测试") }