瀏覽代碼

uploads seems working.

master
sp 4 年之前
父節點
當前提交
f8f50d16c0
共有 3 個檔案被更改,包括 12 行新增3 行删除
  1. +7
    -2
      UploadsOnDisk.go
  2. +4
    -0
      apiV1UploadAnalysis.go
  3. +1
    -1
      pay-in-decode.go

+ 7
- 2
UploadsOnDisk.go 查看文件

} }
defer os.RemoveAll(dir) defer os.RemoveAll(dir)


libreOfficeMutex.Lock() //ensure only one libreoffice is running

// Create a new context and add a timeout to it // Create a new context and add a timeout to it
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() // The cancel should be deferred so resources are cleaned up defer cancel() // The cancel should be deferred so resources are cleaned up


// Create the command with our context // Create the command with our context
cmd := exec.CommandContext(ctx, "libreoffice", "--convert-to", format, "--outdir", dir, m.filePath()) cmd := exec.CommandContext(ctx, "libreoffice", "--convert-to", format, "--outdir", dir, m.filePath())


libreOfficeMutex.Lock() //ensure only one libreoffice is running
// This time we can simply use Output() to get the result. // This time we can simply use Output() to get the result.
out, e := cmd.Output() out, e := cmd.Output()
libreOfficeMutex.Unlock() libreOfficeMutex.Unlock()


// first page to thumbnail // first page to thumbnail
// all page to single jpg // all page to single jpg
var convertPDFMutex sync.Mutex // make sure we only have one convert running at a time
func (m *uploadsOnDisk) convertPDFToJpg() (e error) { func (m *uploadsOnDisk) convertPDFToJpg() (e error) {


if fileExists(m.jpgPath()) { if fileExists(m.jpgPath()) {
} }
defer os.RemoveAll(dir) defer os.RemoveAll(dir)


convertPDFMutex.Lock()
// convert -density 3000 abc.pdf path/tmp/result.png // convert -density 3000 abc.pdf path/tmp/result.png
// could be path/tmp/result-0, result-1, result-2, ... png // could be path/tmp/result-0, result-1, result-2, ... png
target := dir + string(os.PathSeparator) + "result.jpg" //.jpg suffix is important target := dir + string(os.PathSeparator) + "result.jpg" //.jpg suffix is important
strCmd := cmd.String() strCmd := cmd.String()
log.Debug("command is ", strCmd) log.Debug("command is ", strCmd)
out, e := cmd.Output() out, e := cmd.Output()
convertPDFMutex.Unlock()

if e != nil { if e != nil {
log.Error("cannot create png file for PDF", m.Upload, e) log.Error("cannot create png file for PDF", m.Upload, e)
_, e = copyFile(config.UploadsDir.JpgDefault, m.jpgPath()) _, e = copyFile(config.UploadsDir.JpgDefault, m.jpgPath())

+ 4
- 0
apiV1UploadAnalysis.go 查看文件

log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"net/http" "net/http"
"strconv" "strconv"
"sync"
"time" "time"
) )


var analysisMutex sync.Mutex // make sure only one analysis run at a time.
func apiV1UploadAnalysis(w http.ResponseWriter, r *http.Request, ss *loan.Session) { func apiV1UploadAnalysis(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
strId := r.URL.Path[len(apiV1Prefix+"upload-analysis/"):] //remove prefix strId := r.URL.Path[len(apiV1Prefix+"upload-analysis/"):] //remove prefix
Id, e := strconv.Atoi(strId) Id, e := strconv.Atoi(strId)
return return
} }


analysisMutex.Lock()
ai := AiDecodeIncome{} ai := AiDecodeIncome{}
e = ai.decodeUploadToPayIn(ul) e = ai.decodeUploadToPayIn(ul)
analysisMutex.Unlock()
if e != nil { if e != nil {
log.Error("Invalid uploads Id cannot conver to integer", Id, e) log.Error("Invalid uploads Id cannot conver to integer", Id, e)
apiV1Server500Error(w, r) apiV1Server500Error(w, r)

+ 1
- 1
pay-in-decode.go 查看文件

cmd := exec.Command("pdftotext", "-layout", m.ul.filePath(), "-") cmd := exec.Command("pdftotext", "-layout", m.ul.filePath(), "-")
out, e := cmd.Output() out, e := cmd.Output()
if e != nil { if e != nil {
log.Fatal(e)
log.Error("cannot convert pdf to text ", e)
} }


raw := string(out) raw := string(out)

Loading…
取消
儲存