From f8f50d16c04481f526764a3e9ffd6f75819f9f05 Mon Sep 17 00:00:00 2001 From: sp Date: Sun, 25 Apr 2021 03:47:46 +1000 Subject: [PATCH] uploads seems working. --- UploadsOnDisk.go | 9 +++++++-- apiV1UploadAnalysis.go | 4 ++++ pay-in-decode.go | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/UploadsOnDisk.go b/UploadsOnDisk.go index 02f4363..93c6c37 100644 --- a/UploadsOnDisk.go +++ b/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()) diff --git a/apiV1UploadAnalysis.go b/apiV1UploadAnalysis.go index 37db776..d4ce607 100644 --- a/apiV1UploadAnalysis.go +++ b/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) diff --git a/pay-in-decode.go b/pay-in-decode.go index 752e054..3f1e7c8 100644 --- a/pay-in-decode.go +++ b/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)