Will webViewLink generated for a file in appdata folder work? - google-drive-api

I have stored a pdf file to appDataFolder which is application specific, using the get call I am trying to get the file url using 'webViewLink, webContentLink'. But opening the links does not show anything but using alt=media I was able to get the buffer and write it to a file.
const test = await drive.files.get({
fileId: fileId,
fields: 'webViewLink, webContentLink',
})

Related

How to download a file generated "on the fly" with Puppeteer?

I want to download and rename a PDF file that is generated "on the fly" by an Angular app.
With this code, the PDF file is downloaded but I want to rename it before writing to disk.
//Download PDF
await secondTab.waitForSelector('img[title="Format PDF"]', { visible: true, timeout : 10000 })
await secondTab.waitForTimeout(10000);
var current_element = await secondTab.$('img[title="Format PDF"]');
var parent_node = await current_element.getProperty('parentNode');
await parent_node.click();
console.log("Downloading PDF");
I wasn't able to find a solution. All solutions talk about files downloaded from URL.
I tried to trace events on the page (cf. PageEventObjet), but no one catch this "on the fly" generated PDF file.
My question : is there a way to catch that stream, name it and save it to disk ?
Thank you very much.
Note : I dont want to play with the File system (i.e. find my downloaded PDF, rename it, move it, etc.)

how to select directory to save the output?

How select folder save our output file using html css and javascript.
we can select the input file using input type = file, how to select the directory where we wanna save the output file.
This isn't possible in a typical web browser context, but since you tagged this electron, you can use the dialog API from Electron remote:
const { dialog } = require('electron').remote;
const directory = dialog.showOpenDialogSync({
properties: ['openDirectory'],
});

Finding a .txt file on Google Drive by name and getting its contents

I want to find text files by their name and then get their contents and make them into a var.
I have tried to find the file by its name, but it doesn't seem to work. I'm clueless as to how to find the file contents though.
My code to find the file:
function testThing() {
var findquestions = DriveApp.getFilesByName('tempquestions.txt')
Logger.log(findquestions)
}
I want it to log what it found, but the output is nothing but: "FileIterator". I don't know what that means.
As you can see in the documentation, .getFilesByName return fileiterator. What's a file iterator? The documentation states
An iterator that allows scripts to iterate over a potentially large collection of files
There may be large amount of files with the same name. This iterator provides access to all those files.
What methods provide access to file from fileIterator? This method does.
How to get contents of such file? Get blob from file and getDataAsString from blob
Logger.log(DriveApp
.getFilesByName('tempquestions.txt') //fileIterator
.next() //file(first file with that name)
.getBlob() //blob
.getDataAsString() //string
)

Overwrite pdf while retaining URL in Google App Script

I'm fairly new so forgive my question.
I am trying to overwrite a pdf file with a script, but would like to retain the same URL so that users can click the URL and get a dynamically changing pdf (rosters being filled).
setContent(content) only allows me to write a string to a file, not replace the pdf.
You can use the Advanced Drive API to overwrite the content of a file without changing the URL.
var file = DriveApp.getFileById("FILEID");
Drive.Files.update({
title: file.getName(), mimeType: file.getMimeType()
}, file.getId(), blob);

Difference in size of HTML file directly downloaded from Google Drive and via Drive API

I uploaded a word document on Google Drive. I am downloading the file in HTML format in following two ways.
First method: I open the file in Google Docs and then downloaded the file: File -> Download as -> Web page (.html, zipped). I unzip the tar and then I get the HTML file. Its size is 62 kB.
Second method: I used Drive API v3 to create (or upload) the word document to Google Drive. Then I exported the file to HTML format. Its size is 173 kB.
My question is why there is difference of almost three times in size of HTML file? What should be done to get the same file size (62 kB) when downloading (or exporting) using Drive API?
This is the Drive API code I am using to create and export the file.
Drive service = getDriveService();
File fileMetadata = new File();
fileMetadata.setName("Test Document");
fileMetadata.setMimeType("application/vnd.google-apps.document");
FileContent fileContent = new FileContent("application/vnd.openxmlformats-officedocument.wordprocessingml.document", new java.io.File("/home/test/test.doc"));
File createResponse = service.files().create(fileMetadata, fileContent).execute();
java.io.File parentDir = new java.io.File("/home/test/");
if (!parentDir.exists()) {
throw new IOException("Parent directory does not exists.");
}
OutputStream out = new FileOutputStream(new java.io.File(parentDir, "Test Document"));
service.files().export(createResponse.getId(), "text/html").executeAndDownloadTo(out);
Kindly help me with this issue.
Thanks.