Receiving "error: no such file or directory, open" when passing a remote file to libreoffice-convert library in a Node.js app - node.js

I'm currently building a Node.js application that will eventually be used to convert certain file formats into other formats. Most of the work is being done by the libreoffice-convert library.
I am able to do file conversions without any issues when passing a local file path to the library but it doesn't seem to be working when I grab the contents of a remote file via request() and pass the received body to libreoffice-convert.
This is the relevant code I have right now:
request(fileUrl, {encoding: 'binary'}, function(error, response, body) {
const ext = '.html';
libre.convert(body, ext, undefined, (err, done) => {
if (err) {
console.log(`Error converting file: ${err}`);
res.sendStatus(500);
} else {
console.log(done);
}
});
});
I can see that when I run this, libreoffice starts the conversion but eventually, I'm getting this error:
Error: ENOENT: no such file or directory, open '/var/folders/j9/z_z85kh5501dbslrg53mpjsw0000gn/T/libreofficeConvert_-6529-x08o2o3peLMh/source..html
The example libreoffice-convert code gets the local file using fs.readFileSync() but given that I want to get my contents from a remote file, I'm passing the body received in the request() call.
To be sure that body has the correct contents, I compared the result I receive from fs.readFileSync() to the result I receive from request() when calling for the same exact file locally and remotely. There didn't seem to be any differences at all.
Am I missing something or it's a matter that the libreoffice-convert library or libreoffice itself doesn't support this?

libreoffice-convert is dependent on some linux package, i.e. libreoffice-writer. apt install libreoffice-writer will solve your problem.

Related

How to use docx npm package in production to save a file

I am generating a doc file using docx-template npm package in node js. and the file is getting successfully saved in my backend/controller folder on my local machine. Now i want to do prod deployment on Heroku but i dont know what path has to be set to save the file in production.
I have used 'fs' module to read and write file. Shown below.
fs.writeFileSync(
path.resolve(__dirname, `${contractName} ${element.frequency}.docx`),
buffer
);
You probably have to use the "send(Buffer)" feature of express.
See the following page :
https://expressjs.com/en/api.html#res.send
const contentTypes = {
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
};
res.set('Content-Type', contentTypes.docx);
res.send(buffer);
This is because the "fs" module is used to write the data on the disk, but if it is in production, most likely what you want is to let the user "download" a file, and that is done using res.send() which takes as an argument a Buffer to achieve this feature.

Can node.js request php file as its endpoint on the same file system, not an http request?

I am new to node.js and am building a chat appication that is running inside an existing PHP application. I am using expressjs and also postman-request module. I can easily hit absolute urls but when I try to request a file that lives on my own file system, it fails. I have watch tutorials and read the docs and it seems like the only examples ever shown are how to hit external urls.. I can't imagine that it is not possible to hit files that reside on your own file system.
Here is code below: (in my main index.js server file)
const request = require('postman-request');
const url = 'utils/config.php'; // this file merely echos out a json encoded string.
request(url, function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response
console.log('body:', body);
});
Here is the error message:
error: Error: Invalid URI "utils/config.php"
This is the file structure:
-node_modules
-public
-src
-utils
-config.php
index.js (start for node.js - inside src folder)
Any help would be appreciated.
Requesting refers to requesting a file from a webserver. Here, you're trying to request a file path, while the computer thinks is a non-existent URL. The PHP file is just text. It doesn't mean anything to the computer, and the PHP interpreter is what runs the PHP code. You aren't running a PHP server in this case, and you're requesting a file path, not a URL.
You have to start a PHP server first, using the php command. Make sure to pass in the URL (for example, localhost:8080/config.php), and not the file path.
If you wanted to do it entirely from the Node script, you could start a server, request the URL, and then stop the server. It would be possible to use the spawn function from the built-in child_process module (refer here). You could also use exec, but this is probably unnecessarily harder.

Opening an excel file with opn while another excel file is open, will execute code immediately without waiting

I'm using a library called opn. This makes it easier to spawn a child process to open a file in the main process of electron. I could use shell but I need to be able to detect when the file is closed which shell API doesn't provide.
Whenever I open an excel file and there are no other excel files opened, it will work fine and it will wait until I close the file before it resolve the promise. However, if there are other excel files open then it won't wait for the file to be closed and it will resolve the promise immediately.
I did some digging in the source code of the library and the command used to open the file is this for windows: start "" /wait name_of_file where name_of_file is a place holder for the file. The actual command the library executes is different but this one is enough for my use case. I ran into the same problem even when doing it manually without using the library. Maybe I should post this under super user instead of stackoverflow.
Here is the code that opens the file, wait for the file then executes callback.
opn(file, { app: 'EXCEL', wait: true }).then(() => {
const stream = fs.createReadStream(file);
papa.parse(stream, {
delimiter: ',',
complete: (results) => {
mainWindow.webContents.send('uploadNewIndexFileRen', key,
results.data);
},
error: (err) => {
throw err;
}
});
}).catch((error) => { throw error; });
UPDATE:
Here is a quick nodejs repo. All you need to do is clone/fork to check the problem or make a pull request then do npm install to download needed packages. To reproduce the exact problem that I have you might need to be in the following software versions:
Window 10
npm 5.5.1
node v8.9.1
excel 2013
opn 5.2.0 as seen in package.json
This is probably due to the way Excel handles files. I'm guessing Excel handles files with a single process so subsequent opening of files simply signals to the existing process that another files needs opening before closing.
You may be able to force Excel to open a new instance for each file by following these instructions.
Apparently using the /x command line switch will also start a new process:
excel.exe /x

Node https get file from url and unzip

I am using node https/http module to download file. The code looks like this:
https.get(url)
.on('response', (response) => {
response.pipe(fs.createWriteStream(dataDir + filename))
}
In the next step I use unzip module to unzip the file.
fs.createReadStream(dataDir + filename)
.pipe(unzip.Extract({path: dataDir}))
.on('close', () => { something }
.on('error', () => { catch error}
For most cases, Everything works well. However, sometimes unzip module returns this error:
Error: invalid signature: 0x6d74683c
The only issue I can notice is the file name. I grab filename from url. It is the last parameter of a url. However, in some cases, the exact filename is different from the one shown in url. For instance:
https://github.com/request/request/archive/master.zip
The name shown in url is master.zip but when I download it the name is request-master.zip.
Is this the actual problem? If so, how can I solve it?
The filename is most likely a symptom rather than the root cause. The unzip module is unmaintained and has many open issues. It is likely to blame and should not be used, regardless.
Try out decompress or yauzl instead. Also, since you are attempting to extract the archive in a stream, you may want to read about how that is not truly possible.

Node.js: Check if file is an symbolic link when iterating over directory with 'fs'

Supervisor is a package for Node.js that monitors files in your app directory for modifications and reloads the app when a modification occurs.
This script interprets symbolic links as regular files and logs out a warning. I would like to fork Supervisor so that either this can be fixed entirely or that a more descriptive warning is produced.
How can I use the File System module of Node.js to determine if a given file is really an symbolic link?
You can use fs.lstat and then call statis.isSymbolicLink() on the fs.Stats object that's passed into your lstat callback.
fs.lstat('myfilename', function(err, stats) {
console.log(stats.isSymbolicLink());
});
Seems like you can use isSymbolicLink()
const files = fs.readdirSync(dir, {encoding: 'utf8', withFileTypes: true});
files.forEach((file) => {
if (file.isSymbolicLink()) {
console.log('found symlink!');
}
}

Resources