sp 4 лет назад
Родитель
Сommit
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 Просмотреть файл

@@ -136,14 +136,15 @@ func (m *uploadsOnDisk) convertExcelTo(format string) (e error) {
}
defer os.RemoveAll(dir)

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

// 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

// Create the command with our context
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.
out, e := cmd.Output()
libreOfficeMutex.Unlock()
@@ -204,6 +205,7 @@ func (m *uploadsOnDisk) convertExcelTo(format string) (e error) {

// first page to thumbnail
// 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) {

if fileExists(m.jpgPath()) {
@@ -219,6 +221,7 @@ func (m *uploadsOnDisk) convertPDFToJpg() (e error) {
}
defer os.RemoveAll(dir)

convertPDFMutex.Lock()
// convert -density 3000 abc.pdf path/tmp/result.png
// could be path/tmp/result-0, result-1, result-2, ... png
target := dir + string(os.PathSeparator) + "result.jpg" //.jpg suffix is important
@@ -226,6 +229,8 @@ func (m *uploadsOnDisk) convertPDFToJpg() (e error) {
strCmd := cmd.String()
log.Debug("command is ", strCmd)
out, e := cmd.Output()
convertPDFMutex.Unlock()

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

+ 4
- 0
apiV1UploadAnalysis.go Просмотреть файл

@@ -5,9 +5,11 @@ import (
log "github.com/sirupsen/logrus"
"net/http"
"strconv"
"sync"
"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) {
strId := r.URL.Path[len(apiV1Prefix+"upload-analysis/"):] //remove prefix
Id, e := strconv.Atoi(strId)
@@ -25,8 +27,10 @@ func apiV1UploadAnalysis(w http.ResponseWriter, r *http.Request, ss *loan.Sessio
return
}

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

+ 1
- 1
pay-in-decode.go Просмотреть файл

@@ -66,7 +66,7 @@ func (m *AiDecodeIncome) decodePdf() (e error) {
cmd := exec.Command("pdftotext", "-layout", m.ul.filePath(), "-")
out, e := cmd.Output()
if e != nil {
log.Fatal(e)
log.Error("cannot convert pdf to text ", e)
}

raw := string(out)

Загрузка…
Отмена
Сохранить