mindee 1.0.7 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -40,9 +40,9 @@ class APIFinancialDocument extends APIObject {
40
40
  } = _ref;
41
41
  var inputFile = new Input({
42
42
  file: input,
43
- inputType,
44
- filename,
45
- cutPdf
43
+ inputType: inputType,
44
+ filename: filename,
45
+ allowCutPdf: cutPdf
46
46
  });
47
47
  _this.apiToken = inputFile.fileExtension === "pdf" ? _this.invoiceToken : _this.receiptToken;
48
48
  yield inputFile.init();
@@ -43,9 +43,9 @@ class APIInvoice extends APIObject {
43
43
 
44
44
  var inputFile = new Input({
45
45
  file: input,
46
- inputType,
47
- filename,
48
- cutPdf
46
+ inputType: inputType,
47
+ filename: filename,
48
+ allowCutPdf: cutPdf
49
49
  });
50
50
  yield inputFile.init();
51
51
  var url = "v".concat(version, "/predict");
@@ -43,9 +43,9 @@ class APIReceipt extends APIObject {
43
43
 
44
44
  var inputFile = new Input({
45
45
  file: input,
46
- inputType,
47
- filename,
48
- cutPdf
46
+ inputType: inputType,
47
+ filename: filename,
48
+ allowCutPdf: cutPdf
49
49
  });
50
50
  yield inputFile.init();
51
51
  var url = "v".concat(version, "/predict");
@@ -55,9 +55,13 @@ var request = function request(url, method, headers, input) {
55
55
  responseBody += chunk;
56
56
  });
57
57
  res.on("end", function () {
58
- resolve(_objectSpread(_objectSpread({}, res), {}, {
59
- data: JSON.parse(responseBody)
60
- }));
58
+ try {
59
+ resolve(_objectSpread(_objectSpread({}, res), {}, {
60
+ data: JSON.parse(responseBody)
61
+ }));
62
+ } catch (error) {
63
+ console.log(responseBody);
64
+ }
61
65
  });
62
66
  });
63
67
  req.on("error", err => {
package/lib/inputs.js CHANGED
@@ -16,21 +16,20 @@ var {
16
16
  PDFDocument
17
17
  } = require("pdf-lib");
18
18
 
19
- var magic = require("stream-mmmagic");
20
-
21
19
  var concat = require("concat-stream");
22
20
 
23
21
  var {
24
22
  Base64Encode
25
23
  } = require("base64-stream");
26
24
 
27
- var ReadableStreamClone = require("readable-stream-clone");
25
+ var fileType = require("file-type");
26
+
27
+ var ArrayBufferEncode = require("base64-arraybuffer");
28
28
 
29
29
  class Input {
30
30
  /**
31
31
  * @param {(String | Buffer)} file - the file that will be read. Either path or base64 string, or a steam
32
32
  * @param {String} inputType - the type of input used in file ("base64", "path", "dummy").
33
- * NB: in case of base64 file, only jpeg file is supported
34
33
  * NB: dummy is only used for tests purposes
35
34
  * @param {String} filename - File name of the input
36
35
  * @param {Boolean} cut_pdf: Automatically reconstruct pdf with more than 4 pages
@@ -71,53 +70,60 @@ class Input {
71
70
  var _this = this;
72
71
 
73
72
  return _asyncToGenerator(function* () {
74
- if (_this.inputType === "base64") _this.initBase64();else if (_this.inputType === "path") yield _this.initFile();else if (_this.inputType === "stream") yield _this.initStream();else _this.initDummy();
73
+ if (_this.inputType === "base64") yield _this.initBase64();else if (_this.inputType === "path") yield _this.initFile();else if (_this.inputType === "stream") yield _this.initStream();else _this.initDummy();
75
74
  })();
76
75
  }
77
76
 
78
77
  initBase64() {
79
- this.fileObject = this.file;
80
- this.filepath = undefined;
81
- this.fileExtension = undefined;
78
+ var _this2 = this;
79
+
80
+ return _asyncToGenerator(function* () {
81
+ _this2.fileObject = _this2.file;
82
+ _this2.filepath = undefined;
83
+ _this2.fileExtension = undefined;
84
+
85
+ if (_this2.allowCutPdf == true) {
86
+ var typeOfFile = yield fileType.fromBuffer(Buffer.from(_this2.fileObject, "base64"));
87
+
88
+ if (typeOfFile === undefined) {
89
+ console.error("Cannot detect mime type of: ".concat(_this2.fileObject));
90
+ } else if (typeOfFile.mime === "application/pdf") {
91
+ yield _this2.cutPdf();
92
+ }
93
+ }
94
+ })();
82
95
  }
83
96
 
84
97
  initFile() {
85
- var _this2 = this;
98
+ var _this3 = this;
86
99
 
87
100
  return _asyncToGenerator(function* () {
88
- _this2.fileObject = yield fs.readFile(_this2.file);
89
- _this2.filepath = _this2.file;
90
- _this2.filename = _this2.filename || path.basename(_this2.file); // Check if file type is valid
101
+ _this3.fileObject = yield fs.readFile(_this3.file);
102
+ _this3.filepath = _this3.file;
103
+ _this3.filename = _this3.filename || path.basename(_this3.file); // Check if file type is valid
91
104
 
92
- var filetype = _this2.filename.split(".").pop();
105
+ var filetype = _this3.filename.split(".").pop();
93
106
 
94
- if (!(filetype in _this2.MIMETYPES)) {
95
- errorHandler.throw(new Error("File type is not allowed. It must be ".concat(Object.keys(_this2.MIMETYPES).toString())));
107
+ if (!(filetype in _this3.MIMETYPES)) {
108
+ errorHandler.throw(new Error("File type is not allowed. It must be ".concat(Object.keys(_this3.MIMETYPES).toString())));
96
109
  }
97
110
 
98
- _this2.fileExtension = _this2.MIMETYPES[filetype];
111
+ _this3.fileExtension = _this3.MIMETYPES[filetype];
99
112
 
100
- if (_this2.fileExtension === "application/pdf" && _this2.allowCutPdf == true) {
101
- yield _this2.cutPdf();
113
+ if (_this3.fileExtension === "application/pdf" && _this3.allowCutPdf == true) {
114
+ yield _this3.cutPdf();
102
115
  }
103
116
  })();
104
117
  }
105
118
 
106
119
  initStream() {
107
- var _this3 = this;
120
+ var _this4 = this;
108
121
 
109
122
  return _asyncToGenerator(function* () {
110
- _this3.fileObject = _this3.file;
111
- _this3.filename = _this3.filename || "stream";
112
- _this3.filepath = undefined; //Copy the ReadableStream
113
-
114
- var stream = new ReadableStreamClone(_this3.fileObject);
115
- _this3.fileObject = new ReadableStreamClone(_this3.fileObject);
116
- var [mime, output] = yield magic.promise(stream);
117
-
118
- if (mime.type === "application/pdf" && _this3.allowCutPdf == true) {
119
- yield _this3.cutPdf();
120
- }
123
+ var file = yield _this4.streamToBase64(_this4.file);
124
+ _this4.file = file;
125
+ _this4.inputType = "base64";
126
+ yield _this4.initBase64();
121
127
  })();
122
128
  }
123
129
 
@@ -137,7 +143,7 @@ class Input {
137
143
 
138
144
  streamToBase64(stream) {
139
145
  return _asyncToGenerator(function* () {
140
- return yield new Promise((resolve, reject) => {
146
+ return new Promise((resolve, reject) => {
141
147
  var base64 = new Base64Encode();
142
148
 
143
149
  var cbConcat = base64 => {
@@ -154,26 +160,26 @@ class Input {
154
160
 
155
161
 
156
162
  cutPdf() {
157
- var _this4 = this;
163
+ var _this5 = this;
158
164
 
159
165
  return _asyncToGenerator(function* () {
160
166
  // convert document to PDFDocument & cut CUT_PDF_SIZE - 1 first pages and last page
161
- var pdfDocument;
162
-
163
- if (_this4.filename == "stream") {
164
- pdfDocument = yield PDFDocument.load(yield _this4.streamToBase64(_this4.fileObject));
165
- } else {
166
- pdfDocument = yield PDFDocument.load(_this4.fileObject);
167
- }
168
-
167
+ var pdfDocument = yield PDFDocument.load(_this5.fileObject, {
168
+ ignoreEncryption: true
169
+ });
169
170
  var splitedPdfDocument = yield PDFDocument.create();
170
171
  var pdfLength = pdfDocument.getPageCount();
171
- if (pdfLength <= _this4.CUT_PDF_SIZE) return;
172
- var pagesNumbers = [...Array(_this4.CUT_PDF_SIZE - 1).keys(), pdfLength - 1];
172
+ if (pdfLength <= _this5.CUT_PDF_SIZE) return;
173
+ var pagesNumbers = [...Array(_this5.CUT_PDF_SIZE - 1).keys(), pdfLength - 1];
173
174
  var pages = yield splitedPdfDocument.copyPages(pdfDocument, pagesNumbers);
174
175
  pages.forEach(page => splitedPdfDocument.addPage(page));
175
176
  var data = yield splitedPdfDocument.save();
176
- _this4.fileObject = Buffer.from(data);
177
+
178
+ if (_this5.inputType === "path") {
179
+ _this5.fileObject = Buffer.from(data);
180
+ } else if (_this5.inputType === "base64") {
181
+ _this5.fileObject = ArrayBufferEncode.encode(data);
182
+ }
177
183
  })();
178
184
  }
179
185
 
@@ -24,7 +24,12 @@ class APIFinancialDocument extends APIObject {
24
24
  cutPdf = true,
25
25
  includeWords = false,
26
26
  }) {
27
- const inputFile = new Input({ file: input, inputType, filename, cutPdf });
27
+ const inputFile = new Input({
28
+ file: input,
29
+ inputType: inputType,
30
+ filename: filename,
31
+ allowCutPdf: cutPdf,
32
+ });
28
33
  this.apiToken =
29
34
  inputFile.fileExtension === "pdf" ? this.invoiceToken : this.receiptToken;
30
35
  await inputFile.init();
@@ -24,7 +24,12 @@ class APIInvoice extends APIObject {
24
24
  includeWords = false,
25
25
  }) {
26
26
  super.parse();
27
- const inputFile = new Input({ file: input, inputType, filename, cutPdf });
27
+ const inputFile = new Input({
28
+ file: input,
29
+ inputType: inputType,
30
+ filename: filename,
31
+ allowCutPdf: cutPdf,
32
+ });
28
33
  await inputFile.init();
29
34
  const url = `v${version}/predict`;
30
35
  return await super._request(url, inputFile, includeWords);
@@ -24,7 +24,12 @@ class APIReceipt extends APIObject {
24
24
  includeWords = false,
25
25
  }) {
26
26
  super.parse();
27
- const inputFile = new Input({ file: input, inputType, filename, cutPdf });
27
+ const inputFile = new Input({
28
+ file: input,
29
+ inputType: inputType,
30
+ filename: filename,
31
+ allowCutPdf: cutPdf,
32
+ });
28
33
  await inputFile.init();
29
34
  const url = `v${version}/predict`;
30
35
  return await super._request(url, inputFile, includeWords);
@@ -38,10 +38,14 @@ const request = (url, method, headers, input, includeWords = false) => {
38
38
  });
39
39
 
40
40
  res.on("end", function () {
41
- resolve({
42
- ...res,
43
- data: JSON.parse(responseBody),
44
- });
41
+ try {
42
+ resolve({
43
+ ...res,
44
+ data: JSON.parse(responseBody),
45
+ });
46
+ } catch (error) {
47
+ console.log(responseBody);
48
+ }
45
49
  });
46
50
  });
47
51
 
package/mindee/index.js CHANGED
@@ -3,6 +3,7 @@ const logger = require("./logger");
3
3
  const APIReceipt = require("./api/receipt");
4
4
  const APIInvoice = require("./api/invoice");
5
5
  const APIFinancialDocument = require("./api/financialDocument");
6
+
6
7
  class Client {
7
8
  /**
8
9
  * @param {string} receiptToken - Receipt Expense Token from Mindee dashboard
package/mindee/inputs.js CHANGED
@@ -2,10 +2,10 @@ const fs = require("fs").promises;
2
2
  const errorHandler = require("./errors/handler");
3
3
  const path = require("path");
4
4
  const { PDFDocument } = require("pdf-lib");
5
- const magic = require("stream-mmmagic");
6
5
  const concat = require("concat-stream");
7
6
  const { Base64Encode } = require("base64-stream");
8
- const ReadableStreamClone = require("readable-stream-clone");
7
+ const fileType = require("file-type");
8
+ const ArrayBufferEncode = require("base64-arraybuffer");
9
9
 
10
10
  class Input {
11
11
  MIMETYPES = {
@@ -21,7 +21,6 @@ class Input {
21
21
  /**
22
22
  * @param {(String | Buffer)} file - the file that will be read. Either path or base64 string, or a steam
23
23
  * @param {String} inputType - the type of input used in file ("base64", "path", "dummy").
24
- * NB: in case of base64 file, only jpeg file is supported
25
24
  * NB: dummy is only used for tests purposes
26
25
  * @param {String} filename - File name of the input
27
26
  * @param {Boolean} cut_pdf: Automatically reconstruct pdf with more than 4 pages
@@ -44,16 +43,28 @@ class Input {
44
43
  }
45
44
 
46
45
  async init() {
47
- if (this.inputType === "base64") this.initBase64();
46
+ if (this.inputType === "base64") await this.initBase64();
48
47
  else if (this.inputType === "path") await this.initFile();
49
48
  else if (this.inputType === "stream") await this.initStream();
50
49
  else this.initDummy();
51
50
  }
52
51
 
53
- initBase64() {
52
+ async initBase64() {
54
53
  this.fileObject = this.file;
55
54
  this.filepath = undefined;
56
55
  this.fileExtension = undefined;
56
+
57
+ if (this.allowCutPdf == true) {
58
+ const typeOfFile = await fileType.fromBuffer(
59
+ Buffer.from(this.fileObject, "base64")
60
+ );
61
+
62
+ if (typeOfFile === undefined) {
63
+ console.error(`Cannot detect mime type of: ${this.fileObject}`);
64
+ } else if (typeOfFile.mime === "application/pdf") {
65
+ await this.cutPdf();
66
+ }
67
+ }
57
68
  }
58
69
 
59
70
  async initFile() {
@@ -80,19 +91,11 @@ class Input {
80
91
  }
81
92
 
82
93
  async initStream() {
83
- this.fileObject = this.file;
84
- this.filename = this.filename || "stream";
85
- this.filepath = undefined;
86
-
87
- //Copy the ReadableStream
88
- const stream = new ReadableStreamClone(this.fileObject);
89
- this.fileObject = new ReadableStreamClone(this.fileObject);
94
+ const file = await this.streamToBase64(this.file);
90
95
 
91
- const [mime, output] = await magic.promise(stream);
92
-
93
- if (mime.type === "application/pdf" && this.allowCutPdf == true) {
94
- await this.cutPdf();
95
- }
96
+ this.file = file;
97
+ this.inputType = "base64";
98
+ await this.initBase64();
96
99
  }
97
100
 
98
101
  initDummy() {
@@ -109,7 +112,7 @@ class Input {
109
112
  * @returns Base64 encoded String
110
113
  */
111
114
  async streamToBase64(stream) {
112
- return await new Promise((resolve, reject) => {
115
+ return new Promise((resolve, reject) => {
113
116
  const base64 = new Base64Encode();
114
117
 
115
118
  const cbConcat = (base64) => {
@@ -128,16 +131,9 @@ class Input {
128
131
  /** Cut PDF if pages > 5 */
129
132
  async cutPdf() {
130
133
  // convert document to PDFDocument & cut CUT_PDF_SIZE - 1 first pages and last page
131
- let pdfDocument;
132
-
133
- if (this.filename == "stream") {
134
- pdfDocument = await PDFDocument.load(
135
- await this.streamToBase64(this.fileObject)
136
- );
137
- } else {
138
- pdfDocument = await PDFDocument.load(this.fileObject);
139
- }
140
-
134
+ let pdfDocument = await PDFDocument.load(this.fileObject, {
135
+ ignoreEncryption: true,
136
+ });
141
137
  const splitedPdfDocument = await PDFDocument.create();
142
138
  const pdfLength = pdfDocument.getPageCount();
143
139
  if (pdfLength <= this.CUT_PDF_SIZE) return;
@@ -148,7 +144,11 @@ class Input {
148
144
  const pages = await splitedPdfDocument.copyPages(pdfDocument, pagesNumbers);
149
145
  pages.forEach((page) => splitedPdfDocument.addPage(page));
150
146
  const data = await splitedPdfDocument.save();
151
- this.fileObject = Buffer.from(data);
147
+ if (this.inputType === "path") {
148
+ this.fileObject = Buffer.from(data);
149
+ } else if (this.inputType === "base64") {
150
+ this.fileObject = ArrayBufferEncode.encode(data);
151
+ }
152
152
  }
153
153
  }
154
154
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mindee",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "Mindee API SDK for Node.js",
5
5
  "main": "mindee/index.js",
6
6
  "license": "GPL-3.0",
@@ -34,19 +34,18 @@
34
34
  "prettier": "^2.2.0"
35
35
  },
36
36
  "dependencies": {
37
+ "base64-arraybuffer": "^1.0.1",
37
38
  "base64-stream": "^1.0.0",
38
39
  "concat-stream": "^2.0.0",
40
+ "file-type": "^16.5.3",
39
41
  "form-data": "^3.0.0",
40
- "pdf-lib": "^1.13.0",
41
- "readable-stream-clone": "^0.0.7",
42
- "stream-mmmagic": "^2.3.0"
42
+ "pdf-lib": "^1.13.0"
43
43
  },
44
44
  "keywords": [
45
45
  "javascript",
46
46
  "mindee",
47
47
  "api",
48
48
  "SDK",
49
- "nodejs",
50
- "OCR"
49
+ "nodejs"
51
50
  ]
52
51
  }