utilitas 1995.2.3 → 1995.2.5

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.
package/lib/manifest.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  const manifest = {
2
2
  "name": "utilitas",
3
3
  "description": "Just another common utility for JavaScript.",
4
- "version": "1995.2.3",
4
+ "version": "1995.2.5",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",
package/lib/storage.mjs CHANGED
@@ -16,7 +16,8 @@ const _NEED = ['mime-types', '@google-cloud/storage'];
16
16
  const errorMessage = 'Invalid file.';
17
17
  const defaultMetadata = { cacheControl: 'public, max-age=31536000' };
18
18
  const getGcUrlByBucket = bucket => `https://storage.cloud.google.com/${bucket}`;
19
- const getGs = id => `gs://${id}`.replace(/[\/][^\/]*$/, '');
19
+ const getGsById = id => `gs://${id}`.replace(/[\/][^\/]*$/, '');
20
+ const getIdByGs = gs => gs.replace(/^gs:\/\/[^/]*\/(.*)$/, '$1');
20
21
  const mapFilename = name => join(name.substr(0, 2), name.substr(2, 2));
21
22
  const [_zip, _unzip] = [__zip, __unzip].map(promisify);
22
23
  const log = content => _log(content, import.meta.url);
@@ -349,7 +350,7 @@ const uploadToCloud = async (data, options) => {
349
350
  });
350
351
  const result = options?.raw ? raw : raw[0].metadata;
351
352
  input !== FILE && await tryRm(file.path);
352
- !options?.raw && result && (result.gs = getGs(result?.id));
353
+ !options?.raw && result && (result.gs = getGsById(result?.id));
353
354
  return result;
354
355
  };
355
356
 
@@ -428,6 +429,7 @@ export {
428
429
  getConfig,
429
430
  getConfigFilename,
430
431
  getGcUrlByBucket,
432
+ getIdByGs,
431
433
  getTempPath,
432
434
  handleError,
433
435
  init,
package/lib/vision.mjs CHANGED
@@ -1,7 +1,8 @@
1
1
  import { getApiKeyCredentials } from './encryption.mjs';
2
+ import get from './shot.mjs';
2
3
 
3
4
  import {
4
- convert, deleteOnCloud, downloadFromCloud, uploadToCloud,
5
+ convert, deleteOnCloud, downloadFromCloud, getIdByGs, uploadToCloud,
5
6
  } from './storage.mjs';
6
7
 
7
8
  import {
@@ -12,6 +13,7 @@ const _NEED = ['@google-cloud/vision', 'tesseract.js'];
12
13
  const [BASE64, BUFFER, FILE, DEFAULT_LANG] = ['BASE64', 'BUFFER', 'FILE', 'eng'];
13
14
  const ceil = num => num.toFixed(4);
14
15
  const errorMessage = 'Invalid image data.';
16
+ const getTextFromBatch = bt => bt.responses.map(p => p.fullTextAnnotation.text);
15
17
  const log = content => _log(content, import.meta.url);
16
18
 
17
19
  let client;
@@ -125,16 +127,14 @@ const read = async (image, options) => {
125
127
  const content = await convert(image, {
126
128
  input: options?.input, expected: BASE64, errorMessage,
127
129
  });
128
- const [response] = await client.batchAnnotateFiles({
130
+ let result = await client.batchAnnotateFiles({
129
131
  requests: [{
130
132
  inputConfig: { mimeType: 'application/pdf', content },
131
133
  features: [{ type: 'DOCUMENT_TEXT_DETECTION' }],
132
134
  pages: [1, 2, 3, 4, 5], // max 5 pages
133
135
  }],
134
136
  });
135
- let pages = response.responses[0].responses;
136
- options?.raw || (pages = pages.map(x => x.fullTextAnnotation.text));
137
- return pages;
137
+ return options?.raw ? result : getTextFromBatch(result[0].responses[0]);
138
138
  };
139
139
 
140
140
  const readAll = async (image, options) => {
@@ -145,7 +145,8 @@ const readAll = async (image, options) => {
145
145
  });
146
146
  const uri = result.upload?.gs;
147
147
  const destination = `${uri}_result/`;
148
- result.clear = await deleteOnCloud(destination);
148
+ const resultId = getIdByGs(destination);
149
+ result.clear = await deleteOnCloud(resultId);
149
150
  result.submit = await client.asyncBatchAnnotateFiles({
150
151
  requests: [{
151
152
  inputConfig: { mimeType: 'application/pdf', gcsSource: { uri } },
@@ -154,20 +155,13 @@ const readAll = async (image, options) => {
154
155
  }],
155
156
  });
156
157
  result.response = await result.submit[0].promise();
157
- result.result = await downloadFromCloud(
158
- result.response[0].responses[0].outputConfig.gcsDestination.uri.replace(
159
- /^gs:\/\/[^/]*\/(.*)$/, '$1'
160
- ),
161
- { expected: 'JSON' }
162
- );
163
- if (options?.raw) { return result; }
164
- const pages = [];
165
- for (let file in result.result) {
166
- result.result[file].responses.map(x => pages.push(
167
- x.fullTextAnnotation.text
168
- ));
169
- }
170
- return pages;
158
+ result.result = await downloadFromCloud(resultId, { expected: 'JSON' });
159
+ options?.keep || (result.cleanup = await Promise.all(
160
+ [getIdByGs(uri), resultId].map(deleteOnCloud)
161
+ ));
162
+ return options?.raw ? result : Object.keys(result.result).map(
163
+ f => getTextFromBatch(result.result[f])
164
+ ).flat();
171
165
  };
172
166
 
173
167
  export {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "utilitas",
3
3
  "description": "Just another common utility for JavaScript.",
4
- "version": "1995.2.3",
4
+ "version": "1995.2.5",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",