Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

54 lines
1.1KB

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  6. )
  7. func crmcache(w http.ResponseWriter, r *http.Request) {
  8. //debugDumpHTTPRequest(r)
  9. if !checkSignatureByToken(r, IntraAPIConfig.CRMSecrete) {
  10. w.WriteHeader(http.StatusUnauthorized)
  11. fmt.Fprintf(w, "Expired or invalid link")
  12. return
  13. }
  14. v, err := url.ParseQuery(r.URL.RawQuery)
  15. if err != nil {
  16. w.WriteHeader(http.StatusNotFound)
  17. fmt.Fprintf(w, "parse query not valid")
  18. return
  19. }
  20. attachmentid, ok := v["a"]
  21. if !ok {
  22. w.WriteHeader(http.StatusNotFound)
  23. fmt.Fprintf(w, "reference number is not found")
  24. return
  25. }
  26. id := attachmentid[0]
  27. path := crmcacheFileName(id)
  28. if isFileExist(path) {
  29. http.ServeFile(w, r, path)
  30. return
  31. }
  32. err = crmDownloadAttachmentAs(id, path)
  33. if err != nil {
  34. w.WriteHeader(http.StatusNotFound)
  35. fmt.Fprintf(w, "reference number is not right")
  36. return
  37. }
  38. if isFileExist(path) {
  39. http.ServeFile(w, r, path)
  40. return
  41. }
  42. w.WriteHeader(http.StatusInternalServerError)
  43. fmt.Fprintf(w, "cannot build reference cache")
  44. }
  45. func crmcacheFileName(id string) string {
  46. return CRMConfig.AttachmentCache + id
  47. }