Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

113 rindas
2.6KB

  1. package main
  2. import (
  3. "biukop.com/sfm/loan"
  4. "errors"
  5. log "github.com/sirupsen/logrus"
  6. "net/http"
  7. "os"
  8. "os/exec"
  9. "strings"
  10. )
  11. type FunderType string
  12. const (
  13. Funder_AAA FunderType = "AAA Financial"
  14. Funder_Pepper = "Pepper"
  15. Funder_Resimac = "Resimac"
  16. Funder_Unknown = "cannot detect funder type"
  17. )
  18. type AiDecodeIncome struct {
  19. Input struct {
  20. Uploads loan.Uploads
  21. FileName string //a local file on disk
  22. InMime string //may not be correct, just some suggestion only.
  23. }
  24. Mime string //mime actually detected.
  25. PayIn []loan.PayIn
  26. Funder FunderType
  27. AAA PayInAAAData
  28. }
  29. func decodePayInMain(filename string, format string) (ai AiDecodeIncome, e error) {
  30. ai.Input.FileName = filename
  31. ai.Input.InMime = format
  32. ai.PayIn = make([]loan.PayIn, 0, 10)
  33. ai.Mime, e = GetFileContentType(filename)
  34. if e != nil {
  35. return
  36. }
  37. switch ai.Mime {
  38. case "application/pdf":
  39. ai.decodePayInPdf(filename, format)
  40. }
  41. return ai, e
  42. }
  43. // tested, not accurate with xls, xlsx, it becomes zip and octstream sometime.
  44. func GetFileContentType(filename string) (contentType string, e error) {
  45. contentType = ""
  46. input, e := os.OpenFile(filename, os.O_RDONLY, 0755)
  47. // Only the first 512 bytes are used to sniff the content type.
  48. buffer := make([]byte, 512)
  49. _, e = input.Read(buffer)
  50. if e != nil {
  51. return
  52. }
  53. // Use the net/http package's handy DectectContentType function. Always returns a valid
  54. // content-type by returning "application/octet-stream" if no others seemed to match.
  55. contentType = http.DetectContentType(buffer)
  56. return
  57. }
  58. func (m *AiDecodeIncome) decodePayInPdf(filename string, format string) (ret []loan.PayIn, e error) {
  59. cmd := exec.Command("pdftotext", "-layout", filename, "-")
  60. //log.Println(cmd.String())
  61. out, e := cmd.Output()
  62. if e != nil {
  63. log.Fatal(e)
  64. }
  65. raw := string(out)
  66. switch m.detectFunder(raw) {
  67. case Funder_AAA:
  68. e = m.AAA.decodeAAAPdf(raw)
  69. log.Println("AAA final result", m.AAA)
  70. break
  71. case Funder_Unknown:
  72. e = errors.New(Funder_Unknown)
  73. break // not able to detect Funder
  74. }
  75. return
  76. }
  77. func (m *AiDecodeIncome) detectFunder(raw string) FunderType {
  78. if m.isAAA(raw) {
  79. return Funder_AAA
  80. }
  81. return Funder_Unknown
  82. }
  83. func (m *AiDecodeIncome) isAAA(raw string) bool {
  84. keyword := "AAA Financial Trail Report"
  85. lines := strings.Split(raw, "\n")
  86. return m.checkFunderKeyword(keyword, lines, 0, 3)
  87. }
  88. func (m *AiDecodeIncome) checkFunderKeyword(keyword string, lines []string, start int, end int) bool {
  89. for idx, line := range lines {
  90. // first 10 lines has Key word
  91. if strings.Contains(line, keyword) && idx >= start && idx <= 10 {
  92. return true
  93. }
  94. }
  95. return false
  96. }