download files from a dropbox folder - node.js

I'm using node.js and the dropbox package to download files from a folder. I'm able to authenticate and list the files, but not downloading them. I'd like to be able to download none shared files as well.
client.filesListFolder({ path: directory })
.then(function (response) {
response.entries.forEach(function (entry) {
if (entry[".tag"] === "file") {
client.filesDownload({ url: entry.path_display})
.then(function (data) {
// ...
})
.catch(function (err) {
throw err;
});
}
});
})
.catch(function (err) {
me.ThrowError(null, err.status, err.error);
});

Read this link:
https://blogs.dropbox.com/developers/2015/04/a-preview-of-the-new-dropbox-api-v2/
filesDownload({ url: entry.path_display}) passed a wrong arg.
I think it should be {path: entry.path_display}
I actually just tried it, but I only get the file object, I don't know how to get the file data be downloaded. It is may be because I am using Electron.js
As discussed in their github issue:
https://github.com/dropbox/dropbox-sdk-js/issues/88
Therefore, you could try to use function filesGetTemporaryLink({ path: entry.path_display})to a temp link and then use sharingGetSharedLinkFile({ url: res.link })

Related

Download .txt file stored on server using Hapi js

Folks, I am trying to create .txt file on submitting a form and storing it in /files folder. Then, onclick of Download button I should be able to download that file. I am using inert package from Hapi js to download the file. files folder is in root of the project.
This is my route handler for downloading
handler: async (req: Hapi.Request, h: Hapi.ResponseToolkit) => {
try {
const user = await prisma.user.findUnique({
where: {
id: Number(req.params.id)
}
});
if (user) {
h.file(`${__dirname}/../../files/certificate_2.txt`, {
mode: 'attachment',
filename: 'certificate_2.txt'
});
return h.redirect('/');
}
} catch (err) {
console.log(err);
}
But, I am not able to download the file on client side. Please help me to solve this issue

Drive Api - large file stream in nodejs

Drive Api - large file stream in nodejs
hello , I went to stream large file from google drive to my website but I have issue that...
app.get("/blog.mkv", (req, ress) => {
const p38290token = new google.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
p38290token.setCredentials({ refresh_token: token.acc });
const p38290Id = google.drive({
version: "v3",
auth: p38290token,
});
try {
p38290Id.files.get({
fileId: "1OU3BXc4FmyRD0rCW9S4XFfVxIl48vy3v",
alt: "media",
// arraybuffer , stream , blob
}, { responseType: "stream" },
(err, res) => {
if (err) {
console.log(err.message)
if (err.message === "invalid_grant") {
// fatchToken(exportFile)
}
} else {
res.data
.on("end", () => {
console.log("Done");
})
.on("error", err => {
console.log("Error", err);
})
.pipe(ress);
}
}
)
} catch (error) {
}
})
when user come to /blog.mkv video is start stream but user can't skip it (can't go forward or backwards ) , what should I do ?
Check this repo for streaming and downloading files from Google Drive.
Google-drive-video-streaming-nodejs
This is a small script in nodejs that allow you to watch a video stored in your Google Drive directly in your video player.
Install
You need only to install all the dependencies by typing this command:
npm install
Usage
Just type this command to startup the script.
node ./app.js
Now that the server is started you can start watching your video or download it.
Streaming
Paste this link into your player to start streaming the video.
http://127.0.0.1:9001/
Download
To download it, type this URL in a new browser tab.
http://127.0.0.1:9001/download
if you want you can specify the parameter p, that indicates as a percentage what portion of the video will be skipped. For example, to start downloading the video from starting from the halfway point you should use this link:
http://127.0.0.1:9001/download?p=50
You can even use the parameter c that indicates from what chunk the download must be started. To stop the downloading process use this URL:
http://127.0.0.1:9001/download_stop

Google drive API downloading file nodejs

Im trying to get the contents of a file using the google drive API v3 in node.js.
I read in this documentation I get a stream back from drive.files.get({fileId, alt: 'media'})but that isn't the case. I get a promise back.
https://developers.google.com/drive/api/v3/manage-downloads
Can someone tell me how I can get a stream from that method?
I believe your goal and situation as follows.
You want to retrieve the steam type from the method of drive.files.get.
You want to achieve this using googleapis with Node.js.
You have already done the authorization process for using Drive API.
For this, how about this answer? In this case, please use responseType. Ref
Pattern 1:
In this pattern, the file is downloaded as the stream type and it is saved as a file.
Sample script:
var dest = fs.createWriteStream("###"); // Please set the filename of the saved file.
drive.files.get(
{fileId: id, alt: "media"},
{responseType: "stream"},
(err, {data}) => {
if (err) {
console.log(err);
return;
}
data
.on("end", () => console.log("Done."))
.on("error", (err) => {
console.log(err);
return process.exit();
})
.pipe(dest);
}
);
Pattern 2:
In this pattern, the file is downloaded as the stream type and it is put to the buffer.
Sample script:
drive.files.get(
{fileId: id, alt: "media",},
{responseType: "stream"},
(err, { data }) => {
if (err) {
console.log(err);
return;
}
let buf = [];
data.on("data", (e) => buf.push(e));
data.on("end", () => {
const buffer = Buffer.concat(buf);
console.log(buffer);
});
}
);
Reference:
Google APIs Node.js Client

Google Drive File Download api in node js cannot working?

I am using google drive file download api for download the file from google drive.
I am using the following code,
var fileId = '1RDrrFGV2fM7jvnxGFileId';
var dest = fs.createWriteStream('./sample.xlsx');
drive.files.get({fileId: fileId, alt: 'media'}, {responseType: 'stream'},
function(err, res){
res.data
.on('end', () => {
console.log('Done');
})
.on('error', err => {
console.log('Error', err);
})
.pipe(dest);
}
);
Downloaded file was empty, How to get the file data.
drive.files.get will return only metadata of the file. Since you're trying to download file and retrieve the content you should use
drive.files.export({fileId: fileId, mimeType: <MIME type of your file>})
You can refer the client documentation https://apis-nodejs.firebaseapp.com/drive/index.html
I got a another solution for download the google drive file. use the following code snippet
var downloadfile="https://docs.google.com/uc?id=" + fileId
return downloadfile;
Browse the downloadfile url to download the file.

Node - sequential download and zip extraction

I am creating an Express server which would allow the client to receive downloaded files from different sources.
One of the file that is downloaded is a zip file, which needs to be extracted before it is send to the client. The API which is exposed to the node client has a Promise.all() to ensure that files are not send back until everything is downloaded and extracted.
// Client would call this as /download API. Once the download is completed (along with zip extraction), these files would be send to client
function download() {
return Promise.all([downloadInvoice(), downloadImages()]);
})
}).then(function (result) {
// this is happening even before the zip is extracted
console.log('content downloaded and zip extracted, send everything to client');
})
})
}
The downloadImages function calls a REST service (POST) to get images, which are then written to filesystem using pipe. Once the zip is saved in the file system, I am using extract module to extract the zip to a folder.
var req = require('request');
var extract = require('extract-zip')
function downloadImages() {
return new Promise(function(resolve, reject) {
var imageUrl = 'http://localhost:8080/restserver/downloadImages';
var postData = {
username: 'abc',
password: 'xyz'
}
var options = {
method: 'post',
body: postData,
json: true,
url: imageUrl
}
req(options, function (err, res, body) {
if (err) {
console.log('Error posting json '+err)
return;
}
console.log('Status code '+res.statusCode);
}).pipe(fs.createWriteStream('C:/temp/Images.zip'))
.on('finish',function() { // 'finish' fired as download completed. Now extract
console.log('Finished downloading Images, now extract them');
extract('C:/temp/Images.zip',
{dir: 'C:/temp/Images'},
function (err) {
if(err) {
console.log('Error extracting Images zip '+err);
}
resolve("Promised resolved for Images");
})
}).on('error', function(error) {
reject('Error extracting images', error);
});
}
}
Challenge that I am facing is that, even before zip extracts, Promise.all resolves. So, an empty folder is send to the client.
I would like things to happen in this order -
1. Promise.all in download() is used to instantiate the download
2. DownloadImages() downloads the zip of images and saves on the node server (using pipe)
3. DownloadImages() extracts the zip
4. Promise.all in download() is completed and extracted images are send to the client.
Step 3 is happening after step 4. I am not able to make out what am I doing incorrectly to chain these operations in the right sequence.

Resources