Need to convert html to google doc native format using Java - google-drive-api

I've read through the API documentation at https://developers.google.com/drive/v2/reference/ however I cannot find the answer to my question. And attempts to google a solution have failed.
I have a series of previously uploaded small HTML files sitting in Google Drive. What I want to do is write a short application to convert each of these to native Google Document format (mime type "application/vnd.google-apps.document").
I want to do this using Java code and not using GAS code.
The approach I used was to query drive for the File object corresponding to the item I want to convert. Then I pull that file's content as a string. Then I create a new file of mime type "application/vnd.google-apps.document" and upload it with the HTML content. Not surprisingly it didn't work.
So then I tried a different approach: Upload the content as "text/html" but set the "convert" flag to "true". Well I didn't see any direct API to set the convert flag to true. So I tried:
File oBody = new File() ;
oBody.setTitle ( sTitle ) ;
oBody.setDescription ( sDescription ) ;
oBody.setMimeType ( sMimeType ) ;
oBody.set("convert", bConvert);
This did not fail. But it did not create a Google Document either. It just created a text file identical to the original file.
How do I upload a document containing "text/html" content and get Google Drive to convert it automatically to a Google Document?

The convert flag has to be set in the files.insert request and not the File resource.
Using the snippet in the files.insert documentation as reference, this is what you should do:
...
File file = service.files().insert(body, mediaContent).setConvert(true).execute();
...

Related

Write Full HTML Elements in JSON and onto screen [duplicate]

So what I'm trying to do is get text from a file in the same directory as my html file using JavaScript. I want to store an array inside a text file and change it whenever i want instead of constantly having to go into the code, save it, check if it works etc.
I've tried looking around but couldn't find any clear information, most of what I found is using .readAsBinaryString, etc..
I'm mostly seeing things like this but i can't seem find anything which is actually getting information from a textfile without making the person find the text file directory.
function storearray(newval){
var file = "file location;"
var txt = file.txt;
var array = txt.split("|");
txt = txt + newval + " | ";
return array;
}
To read a file from the user's disk you need to use FileReader and the user must explicitly select the file using a file input. (See JavaScript read file without using input).
To read a a file from the website you need to use Ajax (with fetch, XMLHttpRequest or a library that wraps around them like Axios). (See Using fetch from MDN).
If (as it seems here) you want to read data from the website but the website exists only on the user's disk then you still need to use Ajax but will usually run into security restrictions. Some browsers allow you to disable the security protection, but the general solution is to install a web server and load both HTML and the data file using HTTP.
Alternatively, you can store your data in JavaScript (you are generating an array from your text file, you can so that manually or have a build-time script do it) and just load it with a <script> element.

Convert html to google docs or docx using drive api v3

I'm trying to convert a html file (or preformatted html String) to Google Docs using drive api v3 and android studio, using these lines:
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("report.html")
.setMimeType("text/html")
.build();
(I extract the code from android-demos-master examples )
If I try put another mimetype like "application/vnd.google-apps.document", my app crash. I want to upload the file and convert to Gdocs editor or Docx. I need convert before or after upload the file. Can someone guide me?
Using the python libraries, I found I had to specify two mimetypes:
Use 'application/vnd.google-apps.document' when creating the metadata for the Drive file. This is the type of file you want created - a Google Document.
Use 'text/html' for the object representing uploaded data, as that is the type of the content. In python, this were objects of type io.MediaUpload (file upload) or io.MediaIoBaseUpload (in-memory content).
I imagine it's something similar in Java.

change Google Drive mimeType on update file

I am trying to make a "Save as" to google drive functionality for my web app. The user is shown the Google Picker and chooses a file to save over. At this point the application is updating the content of the file successfully, but it doesn't change the mimeType of the file being overwritten.
The api reference specifies the following about the mimeType metadata:
"The MIME type of the file. This is only mutable on update when uploading new content [this is the case]. This field can be left blank, and the mimetype will be determined from the uploaded content's MIME type."
I am leaving it blank. In fact, I am doing a simple upload to PUT https://www.googleapis.com/upload/drive/v2/files/{fileId}. And the http request has the Content-Type header set to the mimeType of the file being uploaded. In this case (if this helps) is "application/vnd.ms-project". I also tried adding the uploadType=media parameter to the url, with no success.
I also tried making a multipart update using the Api playground. Sent the metadata content empty {} and the error message was "Unsupported content with type: application/vnd.ms-project". Funny it is unsuported because I did manage to create a new file with the same mimeType, with no problem.
What to do? Any sugestions?
You can't replace an existing native Docs file with a regular blob. The only way to update a docs file with a blob is switching on the Docs conversion which is not possible for a Microsoft Project file.

Uploading zero size files to google drive

I am using gdata python client for uploading/downloading files. I am not able to figure out how to upload zero sized files. Here is what I have.
#To update
file = gdata.data.MediaSource()
r = client.GetResourceById(resid)
client.UpdateResource(r, media=file, new_revision=True)
#To create a new file
file = gdata.data.MediaSource()
doc = gdata.docs.data.Resource(type='file', title="Title")
r = client.CreateResource(doc, create_uri=uri, media=file)
I tried with removing media=None and also not including in the options. How can I make it work for zero sized files?
You can use the snippet for the files.insert method of the Drive API and not specify a media_body to create an empty file:
https://developers.google.com/drive/v2/reference/files/insert

Copy document contents in google drive api

I need to copy the content of a document into another, both stored in google drive, using Google Drive Api for Java. I'm able to upload or download documents but I don't know how directly transfer the content of a document into another one. I thought of something like that:
public void copyContent(String sourceId, String destinationId) {
File sourceFile = service.files().get(sourceId).execute();
AbstractInputStreamContent content = null; //sourceFile.getContent()??? :-(
File destinationFile = service.files().get(destinationId).execute();
service.files().update(destinationId, destinationFile, content).execute();
}
There's a way to get the file content as AbstractInputStreamContent? Or maybe it exists another approach to copy contents? I need a solution without exporting / uploading documents because I noticed some conversion problem. Thanks!
You can use the files.copy method of the Drive API to create a copy of an existing document:
https://developers.google.com/drive/v2/reference/files/copy