worker
worker
// Version: 2.3.7
// Author: Parveen Bhadoo
// Website: https://gdi.js.org
// add multiple serviceaccounts as {}, {}, {}, random account will be selected by
each time app is opened.
const player_config = {
"player": "videojs", // videojs || plyr || dplayer || jwplayer
"videojs_version": "8.3.0", // Change videojs version in future when needed.
"plyr_io_version": "3.7.8",
"jwplayer_version": "8.16.2"
}
// DON'T TOUCH BELOW THIS UNLESS YOU KNOW WHAT YOU'RE DOING
var gds = [];
const drive_list = authConfig.roots.map(it => it.id)
let app_js_file
if (environment === 'production') {
app_js_file = uiConfig.jsdelivr_cdn_src + '@' + uiConfig.version +
'/src/app.min.js'
} else if (environment === 'development') {
app_js_file = '/app.js'
} else if (environment === 'local') {
app_js_file = 'http://127.0.0.1:5500/src/app.js'
}
</div>
<div class="${uiConfig.file_count_alert_class} text-center" role="alert"
id="count">Total <span id="n_drives" class="number text-center"></span>
drives</div>
</div>
</div>
<div class="modal fade" id="SearchModel" data-bs-backdrop="static" data-bs-
keyboard="false" tabindex="-1" aria-labelledby="SearchModelLabel" aria-
hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="SearchModelLabel"></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-
label="Close">
<span aria-hidden="true"></span>
</button>
</div>
<div class="modal-body" id="modal-body-space">
</div>
<div class="modal-footer" id="modal-body-space-buttons">
</div>
</div>
</div>
</div>
<br>
<footer class="footer mt-auto py-3 text-muted ${uiConfig.footer_style_class}"
style="${uiConfig.fixed_footer ?'position: fixed; ': ''}left: 0; bottom: 0; width:
100%; color: white; z-index: 9999;${uiConfig.hide_footer ? ' display:none;': '
display:block;'}"> <div class="container" style="width: auto; padding: 0 10px;"> <p
class="float-end"> <a href="#">Back to top</a> </p> ${uiConfig.credit ?
'<p>Redesigned with <svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-
heart-fill" fill="red" xmlns="http://www.w3.org/2000/svg"> <path fill-
rule="evenodd" d="M8 1.314C12.438-3.248 23.534 4.735 8 15-7.534 4.736 3.562-3.248 8
1.314z" /> </svg> by <a href="https://www.npmjs.com/package/@googledrive/index"
target="_blank">TheFirstSpeedster</a>, based on Open Source Softwares.</p>' : ''}
<p>© ${uiConfig.copyright_year} - <a href=" ${uiConfig.company_link}"
target="_blank"> ${uiConfig.company_name}</a>, All Rights Reserved.</p> </div>
</footer>
</div>
</body>
<script
src="${uiConfig.jsdelivr_cdn_src}@${uiConfig.version}/assets/homepage.min.js"></
script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-p34f1UUtsS3wqzfto5wAAmdvj+osOnFyQFpp4Ua3gs/ZVWx6oOypYoCJhGGScy+8"
crossorigin="anonymous"></script>
</html>`
fetch('/login', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: formData.toString()
})
.then(res => res.json())
.then(data => {
if (!data.ok) {
document.getElementById("error-message").style.display = "block";
document.getElementById("error-message").innerHTML = "Invalid
Credentials";
} else {
window.location.reload();
}
});
});
const queryparams = new URLSearchParams(window.location.search);
if (queryparams.get('error')) {
document.getElementById("error-message").style.display = "block";
document.getElementById("error-message").innerHTML =
queryparams.get('error');
}
});
</script>
</head>
<body>
<div class="container-fluid">
<div class="row no-gutter">
<div class="col-md-6 d-none d-md-flex bg-image"></div>
<div class="col-md-6 bg-light">
<div class="login d-flex align-items-center py-5">
<div class="container">
<div class="row">
<div class="col-lg-10 col-xl-7 mx-auto">
<img src="${uiConfig.login_image}" class="img-fluid" style="width:
150px; display: block; margin-left: auto; margin-right: auto; margin-bottom:
20px;">
<div id="error-message" class="alert alert-danger"></div>
<form onsubmit="return false;" method="post">
<p id="error" style="color:red;"></p>
<div class="form-group mb-3">
<input id="email" type="text" placeholder="Username" autofocus=""
class="form-control rounded-pill border-0 shadow-sm px-4" required>
</div>
<div class="form-group mb-3">
<input id="password" type="password" placeholder="Password"
class="form-control rounded-pill border-0 shadow-sm px-4 text-primary" required>
</div>
<button id="btn-login" type="submit" class="btn btn-primary btn-
block text-uppercase mb-2 rounded-pill shadow-sm">Login</button>
${authConfig.enable_signup ? `<a href="/signup" class="btn btn-
outline-danger btn-block text-uppercase mb-2 rounded-pill shadow-sm">Signup</a>` :
''}
</form>
<hr class="solid">
${authConfig.enable_social_login ? `<div id="allsociallogins"
style="display:block;">
<a href="https://accounts.google.com/o/oauth2/v2/auth?
client_id=`+authConfig.google_client_id_for_login+`&redirect_uri=`+authConfig.redir
ect_domain+`/google_callback&response_type=code&scope=email
%20profile&response_mode=query" id="btn-google" class="btn btn-block btn-social
btn-google"><span class="fa fa-google"></span> Sign in with Google</a>
</div>` : ''}
</div>
</div>
</div>
</div>
<center>
<p>
© <script>document.write(new Date().getFullYear())</script> $
{uiConfig.company_name}
</p>
</center>
</div>
</div>
</div>
</body>
</html>`
fetch('/signup_api', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: formData.toString()
})
.then(res => res.json())
.then(data => {
if (!data.ok) {
document.getElementById("error-message").style.display = "block";
document.getElementById("error-message").innerHTML = "Failed to Create
Account, Error: " + data.error;
} else {
document.getElementById("error-message").style.display = "block";
document.getElementById("error-message").innerHTML = "Account Created,
Please Login";
}
});
});
const queryparams = new URLSearchParams(window.location.search);
if (queryparams.get('error')) {
document.getElementById("error-message").style.display = "block";
document.getElementById("error-message").innerHTML =
queryparams.get('error');
}
});
</script>
</head>
<body>
<div class="container-fluid">
<div class="row no-gutter">
<div class="col-md-6 d-none d-md-flex bg-image"></div>
<div class="col-md-6 bg-light">
<div class="login d-flex align-items-center py-5">
<div class="container">
<div class="row">
<div class="col-lg-10 col-xl-7 mx-auto">
<h3 class="logo text-center mb-3">${authConfig.siteName}</h3>
<div id="error-message" class="alert alert-danger"></div>
<form onsubmit="return false;" method="post">
<p id="error" style="color:red;"></p>
<div class="form-group mb-3">
<input id="email" type="text" placeholder="Username" autofocus=""
class="form-control rounded-pill border-0 shadow-sm px-4" required>
</div>
<div class="form-group mb-3">
<input id="password" type="password" placeholder="Password"
class="form-control rounded-pill border-0 shadow-sm px-4 text-primary" required>
</div>
<button id="btn-login" type="submit" class="btn btn-primary btn-
block text-uppercase mb-2 rounded-pill shadow-sm">SIGNUP</button>
<a href="/login" class="btn btn-outline-danger btn-block text-
uppercase mb-2 rounded-pill shadow-sm">LOGIN</a>
</form>
<hr class="solid">
${authConfig.enable_social_login ? `<div id="allsociallogins"
style="display:block;">
<a href="https://accounts.google.com/o/oauth2/v2/auth?
client_id=`+authConfig.google_client_id_for_login+`&redirect_uri=`+authConfig.redir
ect_domain+`/google_callback&response_type=code&scope=email
%20profile&response_mode=query" id="btn-google" class="btn btn-block btn-social
btn-google"><span class="fa fa-google"></span> Sign in with Google</a>
</div>` : ''}
</div>
</div>
</div>
</div>
<center>
<p>
© <script>document.write(new Date().getFullYear())</script> $
{uiConfig.company_name}
</p>
</center>
</div>
</div>
</div>
</body>
</html>`
<script>
document.getElementById("status").innerHTML =
"The requested URL <code>" + window.location.pathname + "</code> was not found on
this server. <ins>That’s all we know.</ins>";
</script>`
</style>
</head>
<body>
<div class="container">
<div class="content">
<div class="message">Access Denied</div>
</div>
</div>
</body>
</html>`
const directlink = `
<html>
<head>
<title>Direct Link - Access Denied</title>
<link href='https://fonts.googleapis.com/css?family=Lato:100' rel='stylesheet'
type='text/css'>
<style>
body{
margin:0;
padding:0;
width:100%;
height:100%;
color:#b0bec5;
display:table;
font-weight:100;
font-family:Lato
}
.container{
text-align:center;
display:table-cell;
vertical-align:middle
}
.content{
text-align:center;
display:inline-block
}
.message{
font-size:80px;
margin-bottom:40px
}
a{
text-decoration:none;
color:#3498db
}
</style>
</head>
<body>
<div class="container">
<div class="content">
<div class="message">Access Denied</div>
<center><a href=""><button id="goto">Click Here to Proceed!</button></a></center>
</div>
</div>
</body>
</html>
`
const SearchFunction = {
formatSearchKeyword: function(keyword) {
let nothing = "";
let space = " ";
if (!keyword) return nothing;
return keyword.replace(/(!=)|['"=<>/\\:]/g, nothing)
.replace(/[,,|(){}]/g, space)
.trim()
}
};
return hmacHex;
}
function login() {
return new Response(login_html, {
status: 401,
headers: {
'Content-Type': 'text/html; charset=utf-8'
}
});
}
// start handlerequest
async function handleRequest(request, event) {
const region = request.headers.get('cf-ipcountry');
const asn_servers = request.cf.asn;
const referer = request.headers.get("Referer");
var user_ip = request.headers.get("CF-Connecting-IP");
let url = new URL(request.url);
let path = url.pathname;
let hostname = url.hostname;
if (path == '/app.js') {
const js = await fetch('https://gitlab.com/GoogleDriveIndex/Google-Drive-
Index/-/raw/dev/src/app.js', {
method: 'GET',
})
const data = await js.text()
return new Response(data, {
status: 200,
headers: {
'Content-Type': 'application/javascript; charset=utf-8',
'Access-Control-Allow-Origin': '*', // Required for CORS support to work
'Access-Control-Allow-Credentials': true, // Required for cookies,
authorization headers with HTTPS
}
});
}
if (path == '/logout') {
let response = new Response("", {});
response.headers.set('Set-Cookie', `session=; HttpOnly; Secure;
SameSite=Lax;`);
response.headers.set("Refresh", "1; url=/?error=Logged Out");
return response;
}
if (path == '/findpath') {
const params = url.searchParams;
const id = params.get('id');
const view = params.get('view') || 'false';
return Response.redirect(url.protocol + hostname + '/0:findpath?id=' + id +
'&view=' + view, 307);
}
if (authConfig.enable_login) {
const login_database = authConfig.login_database.toLowerCase();
//console.log("Login Enabled")
if (path == '/download.aspx' && !authConfig.disable_anonymous_download) {
console.log("Anonymous Download")
} else if (path == '/google_callback') {
// Extract the authorization code from the query parameters
const code = url.searchParams.get('code')
if (!code) {
return new Response('Missing authorization code.', {
status: 400
});
}
if (!user_found) {
const jsonResponse = {
ok: false,
}
let response = new Response(JSON.stringify(jsonResponse), {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Access-Control-Allow-Origin': '*', // Required for CORS support to
work
'Access-Control-Allow-Credentials': true, // Required for cookies,
authorization headers with HTTPS
}
});
return response;
}
if (user_found) {
const current_time = Date.now(); // this results in a timestamp of the
number of milliseconds since epoch.
const session_time = current_time + 86400000 * authConfig.login_days;
const encryptedSession = `${await encryptString(username)}|${await
encryptString(password)}|${await encryptString(session_time.toString())}`;
if (authConfig.single_session) {
await ENV.put(username + '_session', encryptedSession);
}
if (authConfig.ip_changed_action && user_ip) {
await ENV.put(username + '_ip', user_ip);
}
const jsonResponse = {
ok: true,
}
let response = new Response(JSON.stringify(jsonResponse), {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Set-Cookie': `session=${encryptedSession}; path=/; HttpOnly; Secure;
SameSite=Lax`,
'Access-Control-Allow-Origin': '*', // Required for CORS support to
work
'Access-Control-Allow-Credentials': true, // Required for cookies,
authorization headers with HTTPS
}
});
return response;
} else {
const jsonResponse = {
ok: false,
}
let response = new Response(JSON.stringify(jsonResponse), {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Access-Control-Allow-Origin': '*', // Required for CORS support to
work
'Access-Control-Allow-Credentials': true, // Required for cookies,
authorization headers with HTTPS
}
});
return response;
}
} else if (path == '/signup' && authConfig.enable_signup) {
return new Response(signup_html, {
status: 200,
headers: {
'Content-Type': 'text/html; charset=utf-8',
'Access-Control-Allow-Origin': '*', // Required for CORS support to work
'Access-Control-Allow-Credentials': true, // Required for cookies,
authorization headers with HTTPS
}
});
} else if (authConfig.enable_signup && request.method === 'POST' && path ===
'/signup_api') {
if (login_database == 'kv') {
const formdata = await request.formData();
const username = formdata.get('username');
const password = formdata.get('password');
if (username == null || password == null) {
const jsonResponse = {
ok: true,
error: "Username or Password is null"
}
let response = new Response(JSON.stringify(jsonResponse), {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Set-Cookie': `session=; path=/; HttpOnly; Secure; SameSite=Lax`,
'Access-Control-Allow-Origin': '*', // Required for CORS support to
work
'Access-Control-Allow-Credentials': true, // Required for cookies,
authorization headers with HTTPS
}
});
return response;
} else if (username.length > 8 && password.length > 8) {
const checkKey = await ENV.get(username);
let jsonResponse;
if (checkKey != null) {
jsonResponse = {
ok: false,
error: "User Already Exists"
}
} else {
await ENV.put(username, password);
jsonResponse = {
ok: true,
error: "User Created"
}
}
let response = new Response(JSON.stringify(jsonResponse), {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Set-Cookie': `session=; path=/; HttpOnly; Secure; SameSite=Lax`,
'Access-Control-Allow-Origin': '*', // Required for CORS support to
work
'Access-Control-Allow-Credentials': true, // Required for cookies,
authorization headers with HTTPS
}
});
return response;
} else {
const jsonResponse = {
ok: false,
error: "Username or Password length is less than 8 characters"
}
let response = new Response(JSON.stringify(jsonResponse), {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Set-Cookie': `session=; path=/; HttpOnly; Secure; SameSite=Lax`,
'Access-Control-Allow-Origin': '*', // Required for CORS support to
work
'Access-Control-Allow-Credentials': true, // Required for cookies,
authorization headers with HTTPS
}
});
return response;
}
} else if (login_database == 'mongodb') {
// to be implemented later
} else {
return new Response("Signup is not supported with local database", {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Access-Control-Allow-Origin': '*', // Required for CORS support to
work
'Access-Control-Allow-Credentials': true, // Required for cookies,
authorization headers with HTTPS
}
});
}
} else if (request.method === 'GET') {
//console.log("GET Request")
const cookie = request.headers.get('cookie');
if (cookie && cookie.includes('session=')) {
const session = cookie.split('session=').pop().split(';').shift().trim();
if (session == 'null' || session == '' || session == null) {
return login()
}
const username = await decryptString(session.split('|')[0]);
let kv_session
if (authConfig.single_session) {
kv_session = await ENV.get(username + '_session');
if (kv_session != session) {
let response = new Response('User Logged in Someplace Else!', {
headers: {
'Set-Cookie': `session=; HttpOnly; Secure; SameSite=Lax;`,
}
});
response.headers.set("Refresh", "1; url=/?error=User Logged in
Someplace Else!");
return response;
}
}
if (authConfig.ip_changed_action && user_ip) {
const kv_ip = await ENV.get(username + '_ip');
if (kv_ip != user_ip) {
let response = new Response('IP Changed! Login Required', {
headers: {
'Set-Cookie': `session=; HttpOnly; Secure; SameSite=Lax;`,
}
});
response.headers.set("Refresh", "1; url=/?error=IP Changed! Login
Required");
return response;
}
}
const session_time = await decryptString(session.split('|')[2]);
console.log("User: " + username + " | Session Time: " + session_time)
const current_time = Date.now(); // this results in a timestamp of the
number of milliseconds since epoch.
if (Number(session_time) < current_time) {
let response = new Response('Session Expired!', {
headers: {
'Set-Cookie': `session=; HttpOnly; Secure; SameSite=Lax;`,
}
});
response.headers.set("Refresh", "1; url=/?error=Session Expired!");
return response;
}
if (login_database == 'kv') {
const kv_key = await ENV.get(username);
if (kv_key == null) {
var user_found = false;
} else {
if (kv_key) {
var user_found = true;
} else {
var user_found = false;
}
}
} else if (login_database == 'mongodb') {
// to be implemented later
} else { // local database
for (i = 0; i < authConfig.users_list.length; i++) {
if (authConfig.users_list[i].username == username) {
var user_found = true;
break;
}
}
}
if (user_found) {
console.log("User Found")
} else {
let response = new Response('Invalid User! Something Wrong', {});
response.headers.set('Set-Cookie', `session=; HttpOnly; Secure;
SameSite=Lax;`);
response.headers.set("Refresh", "1; url=/?error=Invalid User");
return response;
}
} else {
return login()
}
}
}
if (gds.length === 0) {
for (let i = 0; i < authConfig.roots.length; i++) {
const gd = new googleDrive(authConfig, i);
await gd.init();
gds.push(gd)
}
let tasks = [];
gds.forEach(gd => {
tasks.push(gd.initRootType());
});
for (let task of tasks) {
await task;
}
}
let gd;
function redirectToIndexPage() {
return new Response('', {
status: 307,
headers: {
'Location': `${url.origin}/0:/`
}
});
}
if (authConfig['direct_link_protection']) {
if (referer == null) {
return new Response(directlink, {
headers: {
'content-type': 'text/html;charset=UTF-8'
},
status: 401
});
} else if (referer.includes(hostname)) {
console.log("Refer Detected");
} else {
return new Response(directlink, {
headers: {
'content-type': 'text/html;charset=UTF-8'
},
status: 401
});
}
}
}
// end handlerequest
function enQuery(data) {
const ret = [];
for (let d in data) {
ret.push(encodeURIComponent(d) + '=' + encodeURIComponent(data[d]));
}
return ret.join('&');
}
let requestOption = {
'method': 'POST',
'headers': headers,
'body': enQuery(post_data)
};
let response;
for (let i = 0; i < 3; i++) {
response = await fetch(url, requestOption);
if (response.ok) {
break;
}
await sleep(800 * (i + 1));
}
return await response.json();
}
if (path.slice(-1) == '/') {
let requestData = await request.json();
let list_result = await gd.request_list_of_files(
path,
requestData.page_token || null,
Number(requestData.page_index) || 0
);
if (authConfig['enable_password_file_verify']) {
let password = await gd.password(path);
// console.log("dir password", password);
if (password && password.replace("\n", "") !== form.get('password')) {
let html =
`Y29kZWlzcHJvdGVjdGVk=0Xfi4icvJnclBCZy92dzNXYwJCI6ISZnF2czVWbiwSMwQDI6ISZk92YisHI6I
icvJnclJyeYmFzZTY0aXNleGNsdWRlZA==`;
return new Response(html, option);
}
}
return {
...fileWithoutId,
id: encryptedId,
driveId: encryptedDriveId,
mimeType: mimeType,
link: link,
};
}));
}
});
} else {
let file_json = await gd.get_single_file(path);
const {
driveId,
id,
...fileWithoutId
} = file_json;
}
});
}
}
async get_single_file(path) {
if (typeof this.files[path] == 'undefined') {
this.files[path] = await this.get_single_file_api(path);
}
return this.files[path];
}
async get_single_file_api(path) {
let arr = path.split('/');
let name = arr.pop();
name = decodeURIComponent(name).replace(/\'/g, "\\'");
let dir = arr.join('/') + '/';
console.log("try " + name, dir);
let parent = await this.findPathId(dir);
console.log("try " + parent)
let url = 'https://www.googleapis.com/drive/v3/files';
let params = {
'includeItemsFromAllDrives': true,
'supportsAllDrives': true
};
params.q = `'${parent}' in parents and name = '${name}' and trashed = false and
mimeType != 'application/vnd.google-apps.shortcut'`;
params.fields = "files(id, name, mimeType, size ,createdTime, modifiedTime,
iconLink, thumbnailLink, driveId, fileExtension)";
url += '?' + enQuery(params);
let requestOption = await this.requestOptions();
let response;
for (let i = 0; i < 3; i++) {
response = await fetch(url, requestOption);
if (response.ok) {
break;
}
await sleep(800 * (i + 1));
}
let obj = await response.json();
// console.log(obj);
return obj.files[0];
}
if (this.path_children_cache[path] &&
this.path_children_cache[path][page_index] &&
this.path_children_cache[path][page_index].data
) {
let child_obj = this.path_children_cache[path][page_index];
return {
nextPageToken: child_obj.nextPageToken || null,
curPageIndex: page_index,
data: child_obj.data
};
}
return result
}
if (parent == undefined) {
return null;
}
let obj;
let params = {
'includeItemsFromAllDrives': true,
'supportsAllDrives': true
};
params.q = `'${parent}' in parents and trashed = false AND name !='.password'
and mimeType != 'application/vnd.google-apps.shortcut' and mimeType !=
'application/vnd.google-apps.document' and mimeType != 'application/vnd.google-
apps.spreadsheet' and mimeType != 'application/vnd.google-apps.form' and mimeType !
= 'application/vnd.google-apps.site'`;
params.orderBy = 'folder, name, modifiedTime desc';
params.fields = "nextPageToken, files(id, name, mimeType, size, modifiedTime,
driveId, kind, fileExtension)";
params.pageSize = this.authConfig.files_list_page_size;
if (page_token) {
params.pageToken = page_token;
}
let url = 'https://www.googleapis.com/drive/v3/files';
url += '?' + enQuery(params);
let requestOption = await this.requestOptions();
let response;
for (let i = 0; i < 3; i++) {
response = await fetch(url, requestOption);
if (response.ok) {
break;
}
await sleep(800 * (i + 1));
}
obj = await response.json();
return {
nextPageToken: obj.nextPageToken || null,
curPageIndex: page_index,
data: obj
};
}
async password(path) {
if (this.passwords[path] !== undefined) {
return this.passwords[path];
}
return this.passwords[path];
}
return {
nextPageToken: res_obj.nextPageToken || null,
curPageIndex: page_index,
data: res_obj
};
}
async findPathById(child_id) {
let p_files
let drive_index_no = 0;
try {
[p_files, drive_index_no] = await this.findParentFilesRecursion(child_id);
} catch (error) {
return null;
}
async findItemById(id) {
const is_user_drive = this.root_type ===
DriveFixedTerms.gd_root_type.user_drive;
let url = `https://www.googleapis.com/drive/v3/files/${id}?fields=$
{DriveFixedTerms.default_file_fields}${is_user_drive ? '' :
'&supportsAllDrives=true'}`;
let requestOption = await this.requestOptions();
let res = await fetch(url, requestOption);
return await res.json()
}
async findPathId(path) {
let c_path = '/';
let c_id = this.paths[c_path];
c_id = this.paths[c_path];
if (c_id == undefined || c_id == null) {
break;
}
}
console.log('findPathId: ', path, c_id)
return this.paths[path];
}
/*async getAccessToken() {
console.log("accessToken");
if (this.authConfig.expires == undefined || this.authConfig.expires <
Date.now()) {
const obj = await fetchAccessToken();
if (obj.access_token != undefined) {
this.authConfig.accessToken = obj.access_token;
this.authConfig.expires = Date.now() + 3500 * 1000;
}
}
return this.authConfig.accessToken;
}*/
/*async fetchAccessToken() {
console.log("fetchAccessToken");
const url = "https://www.googleapis.com/oauth2/v4/token";
const headers = {
'Content-Type': 'application/x-www-form-urlencoded'
};
var post_data;
if (this.authConfig.service_account && typeof
this.authConfig.service_account_json != "undefined") {
const jwttoken = await
JSONWebToken.generateGCPToken(this.authConfig.service_account_json);
post_data = {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: jwttoken,
};
} else {
post_data = {
client_id: this.authConfig.client_id,
client_secret: this.authConfig.client_secret,
refresh_token: this.authConfig.refresh_token,
grant_type: "refresh_token",
};
}
let requestOption = {
'method': 'POST',
'headers': headers,
'body': enQuery(post_data)
};
let response;
for (let i = 0; i < 3; i++) {
response = await fetch(url, requestOption);
if (response.ok) {
break;
}
await sleep(800 * (i + 1));
}
return await response.json();
}*/
/*sleep(ms) {
return new Promise(function(resolve, reject) {
let i = 0;
setTimeout(function() {
console.log('sleep' + ms);
i++;
if (i >= 2) reject(new Error('i>=2'));
else resolve(i);
}, ms);
})
}*/
}
// end of class googleDrive
const drive = new googleDrive(authConfig, 0);
async function download(id, range = '', inline) {
let url = `https://www.googleapis.com/drive/v3/files/${id}?alt=media`;
const requestOption = await drive.requestOptions();
requestOption.headers['Range'] = range;
let file = await drive.findItemById(id);
if (!file.name) {
return new Response(`{"error":"Unable to Find this File, Try Again."}`, {
status: 500,
headers: {
"content-type": "application/json",
"Access-Control-Allow-Origin": authConfig.cors_domain,
"Cache-Control": "max-age=3600",
}
});
}
let res;
for (let i = 0; i < 3; i++) {
res = await fetch(url, requestOption);
if (res.ok) {
break;
}
sleep(800 * (i + 1));
console.log(res);
}
const second_domain_for_dl = `${uiConfig.second_domain_for_dl}`
if (second_domain_for_dl == 'true') {
const res = await
fetch(`${uiConfig.jsdelivr_cdn_src}@${uiConfig.version}/assets/disable_download.htm
l`);
return new Response(await res.text(), {
headers: {
"content-type": "text/html;charset=UTF-8",
},
})
} else if (res.ok) {
const {
headers
} = res = new Response(res.body, res)
headers.set("Content-Disposition", `attachment; filename="${file.name}"`);
headers.set("Content-Length", file.size);
authConfig.enable_cors_file_down && headers.append('Access-Control-Allow-
Origin', '*');
inline === true && headers.set('Content-Disposition', 'inline');
return res;
} else if (res.status == 404) {
return new Response(not_found, {
status: 404,
headers: {
"content-type": "text/html;charset=UTF-8",
},
})
} else if (res.status == 403) {
const details = await res.text()
return new Response(details, {
status: 403,
headers: {
"content-type": "text/html;charset=UTF-8",
},
})
} else {
const details = await res.text()
/*const res = await
fetch(`${uiConfig.jsdelivr_cdn_src}@${uiConfig.version}/assets/download_error.html`
);
return new Response(await res.text(), {
headers: {
"content-type": "text/html;charset=UTF-8",
},
})*/
return new Response(details, {})
}
}
String.prototype.trim = function(char) {
if (char) {
return this.replace(new RegExp('^\\' + char + '+|\\' + char + '+$', 'g'), '');
}
return this.replace(/^\s+|\s+$/g, '');
};
function decodeJwtToken(token) {
const base64Url = token.split('.')[1];
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
const jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
return JSON.parse(jsonPayload);
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request, event).catch(
(err) => new Response("Report this page when asked at the time of support...
==> " + err.stack, { status: 500 })
)
);
});