Embedded File is still being accessed by createReadStream asynchronously even after deletion in officegen for Word Document
Opened this issue · 0 comments
Environment
node -v
: 18.16.0npm -v
: 9.5.1- Operating system: Windows 10
- Microsoft Office version: 2016
- 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)
}
})