-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.go
More file actions
48 lines (38 loc) · 1.03 KB
/
Copy pathfunction.go
File metadata and controls
48 lines (38 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package function
import (
"context"
"fmt"
"os"
"strings"
"source.cloud.google.com/my-finance-6f9c3/bitbucket_pqadev_vm-cf/event"
"source.cloud.google.com/my-finance-6f9c3/bitbucket_pqadev_vm-cf/internal"
)
var projectId = os.Getenv("PROJECT_ID")
func Image2Expense(ctx context.Context, e event.GCS) error {
if !strings.Contains(e.ContentType, "image") {
return fmt.Errorf("%s is not an image", e.SelfLink)
}
parts := strings.Split(e.Name, "/")
if len(parts) <= 1 {
return fmt.Errorf("Invalid gs path. Please upload the image below the user's folder.")
}
gs := fmt.Sprintf("gs://%s/%s", e.Bucket, e.Name)
des, err := internal.Img2Text(ctx, gs)
if err != nil {
return err
}
fmt.Print(des)
if len(des) == 0 {
return fmt.Errorf("Could not detect text from the receipt image")
}
expense, err := internal.Text2ExpenseInfo(ctx, des)
if err != nil {
return err
}
id, err := internal.SaveExpense(ctx, projectId, parts[0], expense, des)
if err != nil {
return err
}
fmt.Printf("Document added: %s", id)
return nil
}