diff --git a/crmcache.go b/crmcache.go new file mode 100644 index 0000000..c3c212a --- /dev/null +++ b/crmcache.go @@ -0,0 +1,48 @@ +package main + +import ( + "fmt" + "net/http" + "net/url" +) + +func crmcache(w http.ResponseWriter, r *http.Request) { + // if !checkSignature(r) { + // w.WriteHeader(http.StatusUnauthorized) + // fmt.Fprintf(w, "Expired or invalid link") + // } + + v, err := url.ParseQuery(r.URL.RawQuery) + if err != nil { + w.WriteHeader(http.StatusNotFound) + fmt.Fprintf(w, "parse query not valid") + return + } + attachmentid, ok := v["a"] + if !ok { + w.WriteHeader(http.StatusNotFound) + fmt.Fprintf(w, "reference number is not found") + return + } + id := attachmentid[0] + path := crmcacheFileName(id) + + err = crmDownloadAttachmentAs(id, path) + + if err != nil { + w.WriteHeader(http.StatusNotFound) + fmt.Fprintf(w, "reference number is not right") + } + + if isFileExist(path) { + http.ServeFile(w, r, path) + return + } + + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, "cannot build reference cache") +} + +func crmcacheFileName(id string) string { + return CRMConfig.AttachmentCache + id +} diff --git a/main.go b/main.go index cf83eae..20c6304 100644 --- a/main.go +++ b/main.go @@ -64,6 +64,7 @@ func setupHTTPHandler() { http.HandleFunc("/iapi/getAccessToken", supplyAccessToken) http.HandleFunc("/iapi/createWechatQr", iapiCreateWechatQrCode) http.HandleFunc("/crmpixel.png", crmpixel) //tracking pixel. + http.HandleFunc("/crmcache", crmcache) http.ListenAndServe(":65500", nil) }