Ver código fonte

download attachment worked. (with eye witnessed test)

master
Patrick Peng Sun 8 anos atrás
pai
commit
a77b7199e3
3 arquivos alterados com 74 adições e 25 exclusões
  1. +14
    -1
      crmAttachment.go
  2. +5
    -0
      crmAttachment_test.go
  3. +55
    -24
      download.go

+ 14
- 1
crmAttachment.go Ver arquivo

@@ -130,6 +130,19 @@ func crmCreateAttachment(path string) (result attachmentInfo, err error) {
return
}

func crmDownloadAttachment(fileID, saveAs string) (err error) {
func crmDownloadAttachmentAs(fileID, saveAs string) (err error) {
return nil
}

func crmDownloadAttachment(fileID string) (err error) {
u := crmURL4DownloadAttachmet(fileID)
headers := map[string]string{}
headers["Authorization"] = crmAuthHeader()
f, _, err := saveURLwithHTTPHeader(u, headers)
log.Println(f)
return nil
}

func crmURL4DownloadAttachmet(fileID string) string {
return CRMConfig.BaseURL + "?entryPoint=download&id=" + fileID
}

+ 5
- 0
crmAttachment_test.go Ver arquivo

@@ -21,3 +21,8 @@ func TestDecodeJsonResponse(t *testing.T) {
err := json.Unmarshal([]byte(msg), &info)
AssertEqual(t, err, nil, "json decode shold be correct")
}

func TestCRMDownloadAttachment(t *testing.T) {
SetupConfig()
crmDownloadAttachment("591ef9c61d6ca779c")
}

+ 55
- 24
download.go Ver arquivo

@@ -14,12 +14,57 @@ import (
)

func saveURL(url string) (tmpFile string, contentType string, err error) {
log.Println("saveURL: " + url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return
}
return saveHTTPRequest(req)
}

//{"video_url":"http://153.37.232.145/vweixinp.tc.qq.com/1007_6475d21fb443453b8adeac7e59cc0e1c.f10.mp4?vkey=E79C6B0DA06E7A434D15E24774F91412386D1956768C400AC93ECA5320ACAAAF050E1E1BA5C22DFD81B5EE3FBA97E928E0FC2DC597CF611B3E6641BC1AEE2892736FFE29E993F200AA4A0811FEB4E234C48516131207DDE4&sha=0&save=1"}
type videoURL struct {
VideoURL string `json:"video_url"`
}

//readVideoURL try to read a file into json structure
//containing video_url
//
func readVideoURL(path string) (u string) {
body, err := ioutil.ReadFile(path)
if err == nil {
var v = videoURL{}
err = json.Unmarshal([]byte(body), &v)
if err == nil {
_, err = url.ParseRequestURI(v.VideoURL)
if err == nil {
return v.VideoURL
}
}
}
return ""
}

func buildHTTPReqWithHeader(httpMethod, targetURL string, headers map[string]string) (req *http.Request, err error) {
req, err = http.NewRequest(httpMethod, targetURL, nil)
if err != nil {
return
}

if headers != nil {
for k, v := range headers {
req.Header.Set(k, v)
}
}
return
}

func saveHTTPRequest(req *http.Request) (tmpFile string, contentType string, err error) {
var myClient = &http.Client{Timeout: 20 * time.Second}
r, err := myClient.Get(url)

r, err := myClient.Do(req)
if err != nil {
log.Fatal("Fail to get URL:" + url)
log.Fatal(err)
log.Printf("Fail to get URL: %s", req.RequestURI)
log.Println(err)
return "", "", err
}
defer r.Body.Close()
@@ -51,25 +96,11 @@ func saveURL(url string) (tmpFile string, contentType string, err error) {
return
}

//{"video_url":"http://153.37.232.145/vweixinp.tc.qq.com/1007_6475d21fb443453b8adeac7e59cc0e1c.f10.mp4?vkey=E79C6B0DA06E7A434D15E24774F91412386D1956768C400AC93ECA5320ACAAAF050E1E1BA5C22DFD81B5EE3FBA97E928E0FC2DC597CF611B3E6641BC1AEE2892736FFE29E993F200AA4A0811FEB4E234C48516131207DDE4&sha=0&save=1"}
type videoURL struct {
VideoURL string `json:"video_url"`
}

//readVideoURL try to read a file into json structure
//containing video_url
//
func readVideoURL(path string) (u string) {
body, err := ioutil.ReadFile(path)
if err == nil {
var v = videoURL{}
err = json.Unmarshal([]byte(body), &v)
if err == nil {
_, err = url.ParseRequestURI(v.VideoURL)
if err == nil {
return v.VideoURL
}
}
func saveURLwithHTTPHeader(url string, headers map[string]string) (tmpFile string, contentType string, err error) {
log.Println("saveURL: " + url)
req, err := buildHTTPReqWithHeader("GET", url, headers)
if err != nil {
return
}
return ""
return saveHTTPRequest(req)
}

Carregando…
Cancelar
Salvar