Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pulling pdfs #90

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions credentials.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"web": {
"client_id": "399568367003-jtp5dilvk061f13btbnaeff2eho321uj.apps.googleusercontent.com",
"project_id": "nodevpn-402816",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "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"
}
}
19 changes: 19 additions & 0 deletions download_view.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
<form action="/download-file" method="post">
<input placeholder="enter or paste url" name="url" type="text" required/> <br/>
<input placeholder="enter file name" name="name" type="text" required/> <br/>
<input placeholder="enter protocol" name="protocol" type="text" required/> <br/>
<button type="submit">Submit</button>
<button type="reset">Reset</button>
</form>
</body>

</html>
6 changes: 6 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!DOCTYPE html>
<html>
<head>
<script src="index.js"></script>
</head>
</html>
92 changes: 86 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,87 @@
const express = require('express')
const app = express()
app.all('/', (req, res) => {
console.log("Just got a request!")
res.send('Yo!')
const express = require('express');
const http = require('http');
const https = require('https');
const fs = require('fs');
const path = require('path');
const ejs = require('ejs');
const formidable = require('formidable');

var app = express();
var cors = require('cors');
port = 8888;
const API = "AIzaSyAudZ2GG8YYJBDJjVLojkPUMy9FsbTg-rA";

app.get("/",(req,res) => {
res.send("Welcome to node server");
});

app.get("/download",(req,res) => {
ejs.renderFile('download_view.ejs', {}, {}, (err, template) => {
if(err) throw err;
else res.send(template);
});
})
app.listen(process.env.PORT || 3000)

app.get("/view", (req,res) => {
res.render('view',{loadPage: false,url:undefined});
})

app.post("/view-page",(req,res) => {
const form = new formidable.IncomingForm();
let body = {};
form.parse(req,(err,fields,files) => {
if(err) {
res.send("Some error occured");
throw err;
}
body = fields;
let url = String(body.url[0]);
if(!url.startsWith("http")) {
url = `https://drive.google.com/u/0/uc?id=${url}&export=download`
}
res.render('view',{loadPage: true, url})
});
})

app.post("/download-file", (req,res) => {
const form = new formidable.IncomingForm();
let body = {};
form.parse(req,(err,fields,files) => {
if(err) {
res.send("Some error occured");
throw err;
}
body = fields;
let url = String(body.url[0]);
if(url.includes("drive.google.com")) {
let spl = url.split("/");
let id = spl[spl.length - 2];
url = `https://www.googleapis.com/drive/v3/files/${id}/?alt=media&key=${API}`;
}
fileName = body.name[0];
protocol = body.protocol[0];
let proto = https;
if(protocol === 'http') proto = http;
let file = fs.createWriteStream(fileName);
proto.get(url, response => {
response.pipe(file);
file.on('finish',() => {
file.close();
console.log('Download completed');
filePath = path.join(__dirname,fileName);
res.download(filePath);
});
});
});
});

app.listen(port,() => {
console.log(`Server is running on ${port}`);
});
app.use(express.urlencoded({extended:true}));
app.set('view engine','ejs');
// app.use(cors({
// origin:'http://localhost:8888',
// credentials:true, //access-control-allow-credentials:true
// optionSuccessStatus:200
// }));
Loading