package main import ( "biukop.com/sfm/loan" log "github.com/sirupsen/logrus" "net/http" "os" "strings" ) func apiV1UploadAsImage(w http.ResponseWriter, r *http.Request, ss *loan.Session) { // time.Sleep(5* time.Second) strId := r.URL.Path[len(apiV1Prefix+"upload-as-image/"):] //remove prefix if strId == "default" { http.ServeFile(w, r, config.UploadsDir.JpgDefault) return } ul, e := getRequestedUpload(strId, w, r, ss) if e != nil { return } // if this is image itself, serve it directly if strings.Contains(strings.ToLower(ul.Upload.Format), "image") { f, e := os.Open(ul.filePath()) if e == nil { defer f.Close() fi, e := f.Stat() if e == nil { //w.Header().Set("Content-Disposition", "attachment; filename="+ul.Upload.FileName) http.ServeContent(w, r, ul.filePath(), fi.ModTime(), f) return } } // if we reach here, some err has happened log.Error("failed to serve image file ", ul, e) apiV1Server500Error(w, r) return } // see if a converted image exist, if not convert it and then send if !fileExists(ul.jpgPath()) { e = ul.convertUploadsToJpg() if e != nil { // serve a default no preview is available http.ServeFile(w, r, config.UploadsDir.JpgDefault) log.Error("error creating preview image", ul, e) return } } if fileExists(ul.jpgPath()) { http.ServeFile(w, r, ul.jpgPath()) return } else { apiV1Client404Error(w, r, ss) } } func apiV1UploadCreateImage(w http.ResponseWriter, r *http.Request, ss *loan.Session) { //time.Sleep(1 * time.Second) strId := r.URL.Path[len(apiV1Prefix+"upload-as-image/"):] //remove prefix if strId == "" { apiV1Client404Error(w, r, ss) return } ul, e := getRequestedUpload(strId, w, r, ss) if e != nil { return } // if this is image itself, serve it directly if strings.Contains(strings.ToLower(ul.Upload.Format), "image") { apiV1SendJson(true, w, r, ss) return } // see if a converted image exist, if not convert it and then send if !fileExists(ul.jpgPath()) { e = ul.convertUploadsToJpg() if e != nil { log.Error("error creating preview image", ul, e) } } if fileExists(ul.jpgPath()) { apiV1SendJson(true, w, r, ss) return } else { apiV1SendJson(false, w, r, ss) } }