officeparser 4.0.2 → 4.0.3

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.
Files changed (2) hide show
  1. package/officeParser.js +53 -51
  2. package/package.json +1 -1
package/officeParser.js CHANGED
@@ -447,57 +447,57 @@ function parsePdf(filepath, callback, config) {
447
447
  * @returns {void}
448
448
  */
449
449
  function parseOffice(file, callback, config = {}) {
450
+ // Make a clone of the config.
450
451
  const internalConfig = { ...config };
451
- // Check if decompress location in the config is present.
452
- // If it is valid, we set the final decompression location in the config.
453
- // If it is not valid, we reject the promise with appropriate error message.
454
- if (!internalConfig.tempFilesLocation)
455
- internalConfig.tempFilesLocation = DEFAULTDECOMPRESSSUBLOCATION;
456
- else {
457
- const tempFilesLocation = `${internalConfig.tempFilesLocation}${internalConfig.tempFilesLocation.endsWith('/') ? '' : '/'}${DEFAULTDECOMPRESSSUBLOCATION}`;
458
- if (!fs.existsSync(internalConfig.tempFilesLocation))
459
- {
460
- callback(undefined, ERRORMSG.locationNotFound(internalConfig.tempFilesLocation));
461
- return;
462
- }
463
- internalConfig.tempFilesLocation = tempFilesLocation;
464
- }
465
-
466
452
  // Prepare file for processing
467
453
  const filePreparedPromise = new Promise((res, rej) => {
468
- // create temp file subdirectory if it does not exist
469
- fs.mkdirSync(`${internalConfig.tempFilesLocation}/tempfiles`, { recursive: true });
470
-
471
- // Check if buffer
472
- if (Buffer.isBuffer(file)) {
473
- // Guess file type from buffer
474
- fileType.fromBuffer(file)
475
- .then(data =>
476
- {
477
- // temp file name
478
- const newfilepath = `${internalConfig.tempFilesLocation}/tempfiles/${new Date().getTime().toString()}.${data.ext.toLowerCase()}`;
479
- // write new file
480
- fs.writeFileSync(newfilepath, file);
481
- // resolve promise
482
- res(newfilepath);
483
- })
484
- .catch(() => rej(ERRORMSG.improperBuffers));
454
+ // Check if decompress location in the config is present.
455
+ // If it is valid, we set the final decompression location in the config.
456
+ // If it is not valid, we reject the promise with appropriate error message.
457
+ if (!internalConfig.tempFilesLocation)
458
+ internalConfig.tempFilesLocation = DEFAULTDECOMPRESSSUBLOCATION;
459
+ else {
460
+ if (!fs.existsSync(internalConfig.tempFilesLocation))
461
+ {
462
+ rej(ERRORMSG.locationNotFound(internalConfig.tempFilesLocation));
485
463
  return;
486
464
  }
465
+ internalConfig.tempFilesLocation = `${internalConfig.tempFilesLocation}${internalConfig.tempFilesLocation.endsWith('/') ? '' : '/'}${DEFAULTDECOMPRESSSUBLOCATION}`;
466
+ }
467
+
468
+ // create temp file subdirectory if it does not exist
469
+ fs.mkdirSync(`${internalConfig.tempFilesLocation}/tempfiles`, { recursive: true });
470
+
471
+ // Check if buffer
472
+ if (Buffer.isBuffer(file)) {
473
+ // Guess file type from buffer
474
+ fileType.fromBuffer(file)
475
+ .then(data =>
476
+ {
477
+ // temp file name
478
+ const newfilepath = `${internalConfig.tempFilesLocation}/tempfiles/${new Date().getTime().toString()}.${data.ext.toLowerCase()}`;
479
+ // write new file
480
+ fs.writeFileSync(newfilepath, file);
481
+ // resolve promise
482
+ res(newfilepath);
483
+ })
484
+ .catch(() => rej(ERRORMSG.improperBuffers));
485
+ return;
486
+ }
487
487
 
488
- // Not buffers but real file path.
488
+ // Not buffers but real file path.
489
489
 
490
- // Check if file exists
491
- if (!fs.existsSync(file))
492
- throw ERRORMSG.fileDoesNotExist(file);
490
+ // Check if file exists
491
+ if (!fs.existsSync(file))
492
+ throw ERRORMSG.fileDoesNotExist(file);
493
493
 
494
- // temp file name
495
- const newfilepath = `${internalConfig.tempFilesLocation}/tempfiles/${new Date().getTime().toString()}.${file.split(".").pop().toLowerCase()}`;
496
- // Copy the file into a temp location with the temp name
497
- fs.copyFileSync(file, newfilepath)
498
- // resolve promise
499
- res(newfilepath);
500
- });
494
+ // temp file name
495
+ const newfilepath = `${internalConfig.tempFilesLocation}/tempfiles/${new Date().getTime().toString()}.${file.split(".").pop().toLowerCase()}`;
496
+ // Copy the file into a temp location with the temp name
497
+ fs.copyFileSync(file, newfilepath)
498
+ // resolve promise
499
+ res(newfilepath);
500
+ });
501
501
 
502
502
  // Process filePreparedPromise resolution.
503
503
  filePreparedPromise
@@ -531,20 +531,22 @@ function parseOffice(file, callback, config = {}) {
531
531
 
532
532
  /** Internal callback function that calls the user's callback function passed in argument and removes the temp files if required */
533
533
  function internalCallback(data, err) {
534
+ // Check if we need to preserve unzipped content files or delete them.
535
+ if (!internalConfig.preserveTempFiles)
536
+ // Delete decompress sublocation.
537
+ rimraf(internalConfig.tempFilesLocation, rimrafErr => consoleError(rimrafErr, internalConfig.outputErrorToConsole));
538
+
539
+ // Check if there is an error. Throw if there is an error.
534
540
  if (err)
535
- consoleError(err, internalConfig.outputErrorToConsole)
541
+ throw err;
542
+
536
543
  // Call the original callback
537
- callback(data, err);
538
- // Check if we need to preserve unzipped content files or delete them.
539
- if (internalConfig.preserveTempFiles)
540
- return;
541
- // Delete decompress sublocation.
542
- rimraf(internalConfig.tempFilesLocation, rimrafErr => consoleError(rimrafErr, internalConfig.outputErrorToConsole));
544
+ callback(data, undefined);
543
545
  }
544
546
  })
545
547
  .catch(error => {
546
548
  consoleError(error, internalConfig.outputErrorToConsole);
547
- callback(undefined, error);
549
+ callback(undefined, ERRORHEADER + error);
548
550
  });
549
551
  }
550
552
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "officeparser",
3
- "version": "4.0.2",
3
+ "version": "4.0.3",
4
4
  "description": "A Node.js library to parse text out of any office file. Currently supports docx, pptx, xlsx, odt, odp, ods, pdf files.",
5
5
  "main": "officeParser.js",
6
6
  "files": [