| func TestCRMDownloadAttachment(t *testing.T) { | func TestCRMDownloadAttachment(t *testing.T) { | ||||
| SetupConfig() | SetupConfig() | ||||
| crmDownloadAttachment("591ef9c61d6ca779c") | |||||
| crmDownloadAttachmentAs("591ef9c61d6ca779c", "/tmp/wechat_hitxy_测试") | |||||
| id := "59054884ef1b0c04f" | |||||
| //crmDownloadAttachment(id) | |||||
| crmDownloadAttachmentAs(id, "/tmp/wechat_hitxy_测试") | |||||
| } | } |
| info.Size, err = getFileSize(path) | info.Size, err = getFileSize(path) | ||||
| return | return | ||||
| } | } | ||||
| func isFileExist(path string) bool { | |||||
| if _, err := os.Stat(path); os.IsNotExist(err) { | |||||
| // path/to/whatever does not exist | |||||
| return false | |||||
| } | |||||
| return true | |||||
| } |
| //http.HandleFunc("/", webrootHandler) | //http.HandleFunc("/", webrootHandler) | ||||
| http.HandleFunc("/api", apiV1Main) | http.HandleFunc("/api", apiV1Main) | ||||
| http.HandleFunc("/upload", uploadHandler) | http.HandleFunc("/upload", uploadHandler) | ||||
| http.HandleFunc("/crmfiles/", crmAttachmentHandler) | |||||
| http.ListenAndServe(":65500", nil) | http.ListenAndServe(":65500", nil) | ||||
| } | } |
| io.Copy(f, file) | io.Copy(f, file) | ||||
| } | } | ||||
| func crmAttachmentHandler(w http.Response, r *http.Request) { | |||||
| http.ServeFile(w, r) | |||||
| func crmAttachmentHandler(w http.ResponseWriter, r *http.Request) { | |||||
| fileID := getCrmFileID(r.URL.Path) | |||||
| saveAs := GlobalPath.CRMAttachment + "crm_attach_" + fileID //add prefix for easy deleting | |||||
| if fileID == "" { | |||||
| log.Println("invalid fileID") | |||||
| response404Handler(w) | |||||
| return | |||||
| } | |||||
| log.Printf("download %s => %s", fileID, saveAs) | |||||
| crmDownloadAttachmentAs(fileID, saveAs) | |||||
| if !isFileExist(saveAs) { | |||||
| response404Handler(w) | |||||
| return | |||||
| } | |||||
| http.ServeFile(w, r, saveAs) | |||||
| } | |||||
| func getCrmFileID(urlPath string) string { | |||||
| return strings.TrimPrefix(urlPath, "/crmfiles/") | |||||
| } | |||||
| func response404Handler(w http.ResponseWriter) { | |||||
| w.WriteHeader(http.StatusNotFound) | |||||
| fmt.Fprintf(w, "not found") | |||||
| } | } |