You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.2KB

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