Преглед на файлове

access crm attachment through our server as a proxy.

master
Patrick Peng Sun преди 8 години
родител
ревизия
e4f4799429
променени са 4 файла, в които са добавени 36 реда и са изтрити 4 реда
  1. +3
    -2
      crmAttachment_test.go
  2. +8
    -0
      fileinfo.go
  3. +1
    -0
      main.go
  4. +24
    -2
      server.go

+ 3
- 2
crmAttachment_test.go Целия файл

@@ -24,6 +24,7 @@ func TestDecodeJsonResponse(t *testing.T) {

func TestCRMDownloadAttachment(t *testing.T) {
SetupConfig()
crmDownloadAttachment("591ef9c61d6ca779c")
crmDownloadAttachmentAs("591ef9c61d6ca779c", "/tmp/wechat_hitxy_测试")
id := "59054884ef1b0c04f"
//crmDownloadAttachment(id)
crmDownloadAttachmentAs(id, "/tmp/wechat_hitxy_测试")
}

+ 8
- 0
fileinfo.go Целия файл

@@ -103,3 +103,11 @@ func getFileInfo4CRM(path string) (info crmFileInfo, err error) {
info.Size, err = getFileSize(path)
return
}

func isFileExist(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
// path/to/whatever does not exist
return false
}
return true
}

+ 1
- 0
main.go Целия файл

@@ -35,5 +35,6 @@ func main() {
//http.HandleFunc("/", webrootHandler)
http.HandleFunc("/api", apiV1Main)
http.HandleFunc("/upload", uploadHandler)
http.HandleFunc("/crmfiles/", crmAttachmentHandler)
http.ListenAndServe(":65500", nil)
}

+ 24
- 2
server.go Целия файл

@@ -188,6 +188,28 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
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")
}

Loading…
Отказ
Запис