Friday, 24 January 2020

Create HTML Files in Google Drive and View in Browser

HTML files can be created in Google Drive using these Google Drive apps:

  • Text Editor for Google Drive
  • HTML Editor for Google Drive
When getting link for the HTML file from Google Drive by clicking 'Get Shareable Link', the URL is:

https://drive.google.com/open?id=XXX

The above link is to View UI of Google Drive. Change it to get the direct link to the file:

https://drive.google.com/uc?id=XXX

Images work okay! but for HTML files, Google purposely deny users from viewing in browser by forcing a download, Google servers set a response header:

Content-Disposition: attachment;

Now, how to view the HTML content in browser anyway? Nginx proxy_pass doesn't work, Google blocks it. Nginx rewrite & return doesn't work as there are multiple HTTP 302 redirects and can't remove the above header.

Solution
Create a web server just to load the HTML files and send to browser!

Source Code:
//Libs
import express from "express";
import request from "request";

//Make a lock
function new_lock(){
  var Unlock,Lock=new Promise((Resolve,Reject)=>{ Unlock=Resolve; });
  return [Lock,Unlock];
}

//PROGRAMME ENTRY POINT=========================================================
var Server = express();

Server.get("/gd/*",async (Req,Res)=>{
  var Url     = Req.url;
  var Id      = Url.replace("/gd/","");
  var Gd_Url  = `https://drive.google.com/uc?id=${Id}`;

  //make request to Google Drive
  var Content       = "no-contents";
  var [Lock,Unlock] = new_lock();

  request(Gd_Url,(Err,Gdres,Body)=>{
    Content = Body;
    Unlock();
  });

  await Lock;
  Res.send(Content);
});

Server.listen(8080);
//EOF

No comments:

Post a Comment