Ziv-Barber / officegen

Standalone Office Open XML files (Microsoft Office 2007 and later) generator for Word (docx), PowerPoint (pptx) and Excell (xlsx) in javascript. The output is a stream.
MIT License
2.64k stars 472 forks source link

Embedded File is still being accessed by createReadStream asynchronously even after deletion in officegen for Word Document #408

Open DilipSingh06 opened 1 year ago

DilipSingh06 commented 1 year ago

Environment

  1. node -v: 18.16.0
  2. npm -v: 9.5.1
  3. Operating system: Windows 10
  4. Microsoft Office version: 2016
  5. Problem with Powerpoint, Excel or Word document: Word Document

Steps to Reproduce

Try to embed a excel file called sample.xlsx into word document. On adding a new resource I am passing "removed_after_used": true. This removes the temporary file once it is embedded in the document.

Once it is deleted in the code, still the createReadStream is trying to look for the file as the contents copying is still not finished. This is resulting into the below error

ENOENT: no such file or directory, open 'D:\sample1.xlsx

This stops the officegen to generate a docx file [fill]

Expected Behavior

The file should be deleted only after the createReadStream has finished reading the file. It should be done on the finished event of createReadStream or the reading should be done synchronously using readFileSync

Actual Behavior

The file is still being accessed by createReadStream even after it has been deleted by officegen resulting in an error.


case 'file': resStream = fs.readFileSync( privateData.resources[cur_index].data || privateData.resources[cur_index].name ) break

This is deleting the file after use but the above createReadStream is still accessing it. privateData.resources.forEach(function (resource) { if (resource.removed_after_used) { var filename = resource.data || resource.name

        if (officegenGlobals.settings.verbose) {
          console.log('[officegen] Removing resource: ', filename)
        }

        fs.unlinkSync(filename)
      }
    })