Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

node.js - Can we somehow rename the file that is being downloaded using puppeteer?

I am downloading a file through puppeteer into my directory. I need to upload this file to an s3 bucket so I need to pick up the file name. But the problem is, this file name has a time stamp that changes every time so I can't keep a hard coded name. So is there a way around this to get a constant name every time (even if the old file is replaced), or how to rename the file being downloaded?

I thought of using node's fs.rename() function but that would again require the current file name.

I want a constant file name to hard code and then upload into the s3 bucket.

  await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: './xml'}); // This sets the directory

  await page.keyboard.press('Tab');

  await page.keyboard.press('Enter'); // This downloads an XML file.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have two options:

  1. Monitor the requests/responses to log the name of the file and rename it via Node.js
  2. Use the Chrome DevTools Protocol to modify the response header

Option 1: Monitor the requests / response

This is the most straight-forward way to do it. Monitor all responses and in case you notice the response that is being downloaded, use the name to rename it locally via fs.rename.

Code Sample

const path = require('path');

// ...
page.on('response', response => {
    const url = response.request().url();
    const contentType = response.headers()['content-type'];
    if (/* URL and/or contentType matches pattern */) {
        const fileName = path.basename(response.request().url());
        // handle and rename file name (after making sure it's downloaded)
    }
});

The code listens to all responses and wait for a specific pattern (e.g. contentType === 'application/pdf'). Then it takes the file name from the request. Depending on your use case, you might want to check the Content-Disposition header in addition. After that, you have to wait until the file is downloaded (e.g. file is present and file size does not change) and then you can rename it.


Option 2: Use the Chrome DevTools Protocol to modify the response header

I'm 99% sure, that this is possible. You need to intercept the response which is currently not supported by puppeteer itself. But as the Chrome DevTools Protocol is supporting this functionality, you can use it using the low-level protocol.

The idea is to intercept the response and change the Content-Disposition header to your desired file name.

Here is the idea:

  1. Use chrome-remote-interface or a CDP Session to activate Network.requestIntercepted
  2. Listen for Network.requestIntercepted events
  3. Send Network.getResponseBodyForInterception to receive the body of the response
  4. Modify the body and add (or change) the Content-Disposition header to include your filename
  5. Call Network.continueInterceptedRequest with your modified response

Your file should then be save with your modified file name. Check out this comment on github for a code sample. As I already explained it is a rather sophisticated approach as long as puppeteer does not support modifying responses.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...