|
- package main
-
- import (
- "biukop.com/sfm/loan"
- log "github.com/sirupsen/logrus"
- "net/http"
- "os"
- )
-
- func apiV1UploadAsPDF(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
- strId := r.URL.Path[len(apiV1Prefix+"upload-as-pdf/"):] //remove prefix
- if strId == "default" {
- http.ServeFile(w, r, config.UploadsDir.PdfDefault)
- return
- }
- ul, e := getRequestedUpload(strId, w, r, ss)
- if e != nil {
- return
- }
-
- //get file type
- fileType, e := ul.GetFileType()
- if e != nil {
- apiV1Client403Error(w, r, ss)
- return
- }
-
- // if its ready pdf, no need to convert
- if fileType == "pdf" {
- f, e := os.Open(ul.filePath())
- if e == nil {
- defer f.Close()
- fi, e := f.Stat()
- if e == nil {
- if forceHttpDownload(r) {
- w.Header().Set("Content-Disposition", "attachment; filename="+ul.Upload.FileName)
- }
- http.ServeContent(w, r, ul.Upload.FileName, fi.ModTime(), f)
- return
- }
- }
- // if we reach here, some err has happened
- log.Error("failed to serve pdf file ", ul, e)
- apiV1Server500Error(w, r)
- return
- }
-
- // see if a converted pdf exist, if not convert it and then send
- if !fileExists(ul.pdfPath()) {
- e = ul.convertUploadsToPDF()
- if e != nil {
- // serve a default no preview is available
- http.ServeFile(w, r, config.UploadsDir.PdfDefault)
- log.Error("error creating preview image", ul, e)
- return
- }
- }
- if fileExists(ul.pdfPath()) {
- http.ServeFile(w, r, ul.pdfPath())
- return
- } else {
- apiV1Client404Error(w, r, ss)
- }
- }
-
- func apiV1UploadCreatePDF(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
- //time.Sleep(1* time.Second)
-
- strId := r.URL.Path[len(apiV1Prefix+"upload-as-pdf/"):] //remove prefix
- if strId == "" {
- apiV1Client404Error(w, r, ss)
- return
- }
- ul, e := getRequestedUpload(strId, w, r, ss)
- if e != nil {
- return
- }
-
- //get file type
- fileType, e := ul.GetFileType()
- if e != nil {
- apiV1Client403Error(w, r, ss)
- return
- }
-
- // if its ready pdf, no need to convert
- if fileType == "pdf" {
- apiV1SendJson(true, w, r, ss)
- return
- }
-
- // see if a converted pdf exist, if not convert it and then send
- if !fileExists(ul.pdfPath()) {
- e = ul.convertUploadsToPDF()
- if e != nil {
- log.Error("error creating pdf", ul, e)
- _, e = copyFile(config.UploadsDir.PdfDefault, ul.pdfPath())
- if e != nil {
- log.Error("failed copy default pdf", e)
- }
- }
- }
- if fileExists(ul.pdfPath()) {
- apiV1SendJson(true, w, r, ss)
- return
- } else {
- apiV1SendJson(false, w, r, ss)
- }
- }
|