您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

110 行
2.4KB

  1. package main
  2. import (
  3. "biukop.com/sfm/loan"
  4. log "github.com/sirupsen/logrus"
  5. "net/http"
  6. "os"
  7. )
  8. func apiV1UploadAsPDF(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  9. strId := r.URL.Path[len(apiV1Prefix+"upload-as-pdf/"):] //remove prefix
  10. if strId == "default" {
  11. http.ServeFile(w, r, config.UploadsDir.PdfDefault)
  12. return
  13. }
  14. ul, e := getRequestedUpload(strId, w, r, ss)
  15. if e != nil {
  16. return
  17. }
  18. //get file type
  19. fileType, e := ul.GetFileType()
  20. if e != nil {
  21. apiV1Client403Error(w, r, ss)
  22. return
  23. }
  24. // if its ready pdf, no need to convert
  25. if fileType == "pdf" {
  26. f, e := os.Open(ul.filePath())
  27. if e == nil {
  28. defer f.Close()
  29. fi, e := f.Stat()
  30. if e == nil {
  31. if forceHttpDownload(r) {
  32. w.Header().Set("Content-Disposition", "attachment; filename="+ul.Upload.FileName)
  33. }
  34. http.ServeContent(w, r, ul.Upload.FileName, fi.ModTime(), f)
  35. return
  36. }
  37. }
  38. // if we reach here, some err has happened
  39. log.Error("failed to serve pdf file ", ul, e)
  40. apiV1Server500Error(w, r)
  41. return
  42. }
  43. // see if a converted pdf exist, if not convert it and then send
  44. if !fileExists(ul.pdfPath()) {
  45. e = ul.convertUploadsToPDF()
  46. if e != nil {
  47. // serve a default no preview is available
  48. http.ServeFile(w, r, config.UploadsDir.PdfDefault)
  49. log.Error("error creating preview image", ul, e)
  50. return
  51. }
  52. }
  53. if fileExists(ul.pdfPath()) {
  54. http.ServeFile(w, r, ul.pdfPath())
  55. return
  56. } else {
  57. apiV1Client404Error(w, r, ss)
  58. }
  59. }
  60. func apiV1UploadCreatePDF(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  61. //time.Sleep(1* time.Second)
  62. strId := r.URL.Path[len(apiV1Prefix+"upload-as-pdf/"):] //remove prefix
  63. if strId == "" {
  64. apiV1Client404Error(w, r, ss)
  65. return
  66. }
  67. ul, e := getRequestedUpload(strId, w, r, ss)
  68. if e != nil {
  69. return
  70. }
  71. //get file type
  72. fileType, e := ul.GetFileType()
  73. if e != nil {
  74. apiV1Client403Error(w, r, ss)
  75. return
  76. }
  77. // if its ready pdf, no need to convert
  78. if fileType == "pdf" {
  79. apiV1SendJson(true, w, r, ss)
  80. return
  81. }
  82. // see if a converted pdf exist, if not convert it and then send
  83. if !fileExists(ul.pdfPath()) {
  84. e = ul.convertUploadsToPDF()
  85. if e != nil {
  86. log.Error("error creating pdf", ul, e)
  87. _, e = copyFile(config.UploadsDir.PdfDefault, ul.pdfPath())
  88. if e != nil {
  89. log.Error("failed copy default pdf", e)
  90. }
  91. }
  92. }
  93. if fileExists(ul.pdfPath()) {
  94. apiV1SendJson(true, w, r, ss)
  95. return
  96. } else {
  97. apiV1SendJson(false, w, r, ss)
  98. }
  99. }