Skip to content

Commit

Permalink
PDF file Preview; Cors file download configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
yanzai committed Apr 22, 2020
1 parent 3839530 commit 1b56e8d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
25 changes: 17 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
var authConfig = {
"siteName": "GoIndex", // 网站名称
"version": "_3.9", // 程序版本。用户不要手动修改
"version": "_4.23", // 程序版本。用户不要手动修改
// 此版本只支持 material
"theme": "material", // material classic
"client_id": "202264815644.apps.googleusercontent.com",
"client_secret": "X4Z3ca8xfWDb1Voo-F9a7ZxJ",
/*"client_id": "202264815644.apps.googleusercontent.com",
"client_secret": "X4Z3ca8xfWDb1Voo-F9a7ZxJ",*/
// 强烈推荐使用自己的 client_id 和 client_secret
"client_id": "",
"client_secret": "",
"refresh_token": "", // 授权 token
/**
* 设置要显示的多个云端硬盘;按格式添加多个
Expand Down Expand Up @@ -43,9 +46,10 @@ var authConfig = {
* 如果设置的值过小,会导致搜索结果页面滚动条增量加载(分页加载)失效;
* 此值的大小影响搜索操作的响应速度。
*/
"search_result_list_page_size": 50
"search_result_list_page_size": 50,
// 确认有 cors 用途的可以开启
"enable_cors_file_down": false
// user_drive_real_root_id

};


Expand Down Expand Up @@ -222,7 +226,8 @@ async function handleRequest(request) {
}
let file = await gd.file(path);
let range = request.headers.get('Range');
return gd.down(file.id, range);
const inline_down = 'true' === url.searchParams.get('inline');
return gd.down(file.id, range, inline_down);
}
}

Expand Down Expand Up @@ -342,11 +347,15 @@ class googleDrive {
}
}

async down(id, range = '') {
async down(id, range = '', inline = false) {
let url = `https://www.googleapis.com/drive/v3/files/${id}?alt=media`;
let requestOption = await this.requestOption();
requestOption.headers['Range'] = range;
return await fetch(url, requestOption);
let res = await fetch(url, requestOption);
const {headers} = res = new Response(res.body, res)
this.authConfig.enable_cors_file_down && headers.append('Access-Control-Allow-Origin', '*');
inline === true && headers.set('Content-Disposition', 'inline');
return res;
}

async file(path) {
Expand Down
15 changes: 14 additions & 1 deletion themes/material/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ function append_files_to_list(path, files) {
});
}
var ext = p.split('.').pop().toLowerCase();
if ("|html|php|css|go|java|js|json|txt|sh|md|mp4|webm|avi|bmp|jpg|jpeg|png|gif|m4a|mp3|flac|wav|ogg|mpg|mpeg|mkv|rm|rmvb|mov|wmv|asf|ts|flv|".indexOf(`|${ext}|`) >= 0) {
if ("|html|php|css|go|java|js|json|txt|sh|md|mp4|webm|avi|bmp|jpg|jpeg|png|gif|m4a|mp3|flac|wav|ogg|mpg|mpeg|mkv|rm|rmvb|mov|wmv|asf|ts|flv|pdf|".indexOf(`|${ext}|`) >= 0) {
targetFiles.push(filepath);
p += "?a=view";
c += " view";
Expand Down Expand Up @@ -681,6 +681,8 @@ function file(path) {
if ("|bmp|jpg|jpeg|png|gif|".indexOf(`|${ext}|`) >= 0) {
return file_image(path);
}

if ('pdf' === ext) return file_pdf(path);
}

// 文件展示 |html|php|css|go|java|js|json|txt|sh|md|
Expand Down Expand Up @@ -799,6 +801,17 @@ function file_audio(path) {
$('#content').html(content);
}

// 文件展示 pdf pdf
function file_pdf(path) {
const url = window.location.origin + path;
const inline_url = `${url}?inline=true`
const file_name = decodeURI(path.slice(path.lastIndexOf('/') + 1, path.length))
var content = `
<object data="${inline_url}" type="application/pdf" name="${file_name}" style="width:100%;height:94vh;"><embed src="${inline_url}" type="application/pdf"/></object>
<a href="${url}" class="mdui-fab mdui-fab-fixed mdui-ripple mdui-color-theme-accent"><i class="mdui-icon material-icons">file_download</i></a>
`;
$('#content').removeClass('mdui-container').addClass('mdui-container-fluid').css({padding: 0}).html(content);
}

// 图片展示
function file_image(path) {
Expand Down

0 comments on commit 1b56e8d

Please sign in to comment.