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

add sub folder prefix support #34

Open
wants to merge 5 commits into
base: master
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
4 changes: 2 additions & 2 deletions web/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
</v-app>
</template>
<script>
import api from './api'
import api, { getSubDirPath } from './api'
import LoginDialog from './components/LoginDialog.vue'

export default {
Expand Down Expand Up @@ -84,7 +84,7 @@ export default {
}))
if (!ok) return

const { drives } = await api.get('/~_~_gdindex/drives').json()
const { drives } = await api.get(getSubDirPath('/~_~_gdindex/drives')).json()
this.drives = [{ text: this.$t('mainDrive'), value: 'root' }].concat(
drives.map(d => ({
value: d.id,
Expand Down
19 changes: 19 additions & 0 deletions web/src/api.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
import xf from './xfetch'

const subdir = window.props.subdir

export function getSubDirPath(path) {
if (
typeof path === 'string' &&
!path.startsWith(`${subdir}/`) &&
!path.startsWith(`/${subdir}/`)
) {
if (path.startsWith('/')) {
return `/${subdir}${path}`
} else {
return `/${subdir}/${path}`
}
}
return path
}

const headers = {}

if (localStorage.token) {
headers.Authorization = 'Basic ' + localStorage.token
}

export default xf.extend({
baseURI: window.props.api,
headers
Expand Down
4 changes: 2 additions & 2 deletions web/src/components/FileViewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ import { format } from 'date-fns'
import prettyBytes from 'pretty-bytes'
import nodeUrl from 'url'
import nodePath from 'path'
import api from '../api'
import api, { getSubDirPath } from '../api'
import ImageViewer from 'viewerjs'
import 'viewerjs/dist/viewer.css'
import FileUploadDialog from './FileUploadDialog'
Expand Down Expand Up @@ -160,7 +160,7 @@ export default {
},
computed: {
path() {
return '/' + this.$route.params.path
return getSubDirPath(this.$route.params.path)
},
pathSegments() {
const list = this.path
Expand Down
1 change: 1 addition & 0 deletions worker/bili.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
banner: `
self.props = {
title: 'GDIndex',
subdir: '',
default_root_id: 'root',
client_id: '202264815644.apps.googleusercontent.com',
client_secret: 'X4Z3ca8xfWDb1Voo-F9a7ZxJ',
Expand Down
15 changes: 14 additions & 1 deletion worker/dist/worker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
self.props = {
title: 'GDIndex',
subdir: '',
default_root_id: 'root',
client_id: '202264815644.apps.googleusercontent.com',
client_secret: 'X4Z3ca8xfWDb1Voo-F9a7ZxJ',
Expand Down Expand Up @@ -441,14 +442,18 @@ self.props = {
}

const gd = new GoogleDrive(self.props);
const HTML = `<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1"><title>${self.props.title}</title><link href="/~_~_gdindex/resources/css/app.css" rel=stylesheet></head><body><script>window.props = { title: '${self.props.title}', default_root_id: '${self.props.default_root_id}', api: location.protocol + '//' + location.host, upload: ${self.props.upload} }<\/script><div id=app></div><script src="/~_~_gdindex/resources/js/app.js"><\/script></body></html>`;
const HTML = `<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1"><title>${self.props.title}</title><link href="/${self.props.subdir}/~_~_gdindex/resources/css/app.css" rel=stylesheet></head><body><script>window.props = { title: '${self.props.title}', default_root_id: '${self.props.default_root_id}', api: location.protocol + '//' + location.host, subdir: "${self.props.subdir}", upload: ${self.props.upload} }<\/script><div id=app></div><script src="/${self.props.subdir}/~_~_gdindex/resources/js/app.js"><\/script></body></html>`;

async function onGet(request) {
let {
pathname: path
} = request;
const rootId = request.searchParams.get('rootId') || self.props.default_root_id;

if (path.startsWith(`/${self.props.subdir}/`)) {
path = path.substr(self.props.subdir.length + 1);
}

if (path.startsWith('/~_~_gdindex/resources/')) {
const remain = path.replace('/~_~_gdindex/resources/', '');
const r = await fetch(`https://raw.githubusercontent.com/maple3142/GDIndex/master/web/dist/${remain}`);
Expand Down Expand Up @@ -504,6 +509,10 @@ self.props = {
} = request;
const rootId = request.searchParams.get('rootId') || self.props.default_root_id;

if (path.startsWith(`/${self.props.subdir}/`)) {
path = path.substr(self.props.subdir.length + 1);
}

if (path.substr(-1) === '/') {
return new Response(JSON.stringify((await gd.listFolderByPath(path, rootId))), {
headers: {
Expand Down Expand Up @@ -543,6 +552,10 @@ self.props = {
pathname: path
} = request;

if (path.startsWith(`/${self.props.subdir}/`)) {
path = path.substr(self.props.subdir.length + 1);
}

if (path.substr(-1) === '/') {
return new Response(null, {
headers: {
Expand Down
11 changes: 10 additions & 1 deletion worker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import GoogleDrive from './googleDrive'

const gd = new GoogleDrive(self.props)

const HTML = `<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1"><title>${self.props.title}</title><link href="/~_~_gdindex/resources/css/app.css" rel=stylesheet></head><body><script>window.props = { title: '${self.props.title}', default_root_id: '${self.props.default_root_id}', api: location.protocol + '//' + location.host, upload: ${self.props.upload} }<\/script><div id=app></div><script src="/~_~_gdindex/resources/js/app.js"><\/script></body></html>`
const HTML = `<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1"><title>${self.props.title}</title><link href="/${self.props.subdir}/~_~_gdindex/resources/css/app.css" rel=stylesheet></head><body><script>window.props = { title: '${self.props.title}', default_root_id: '${self.props.default_root_id}', api: location.protocol + '//' + location.host, subdir: "${self.props.subdir}", upload: ${self.props.upload} }<\/script><div id=app></div><script src="/${self.props.subdir}/~_~_gdindex/resources/js/app.js"><\/script></body></html>`

async function onGet(request) {
let { pathname: path } = request
const rootId = request.searchParams.get('rootId') || self.props.default_root_id
if (path.startsWith(`/${self.props.subdir}/`)) {
path = path.substr(self.props.subdir.length + 1)
}
if (path.startsWith('/~_~_gdindex/resources/')) {
const remain = path.replace('/~_~_gdindex/resources/', '')
const r = await fetch(`https://raw.githubusercontent.com/maple3142/GDIndex/master/web/dist/${remain}`)
Expand Down Expand Up @@ -56,6 +59,9 @@ async function onGet(request) {
async function onPost(request) {
let { pathname: path } = request
const rootId = request.searchParams.get('rootId') || self.props.default_root_id
if (path.startsWith(`/${self.props.subdir}/`)) {
path = path.substr(self.props.subdir.length + 1)
}
if (path.substr(-1) === '/') {
return new Response(JSON.stringify(await gd.listFolderByPath(path, rootId)), {
headers: {
Expand Down Expand Up @@ -88,6 +94,9 @@ async function onPost(request) {
}
async function onPut(request) {
let { pathname: path } = request
if (path.startsWith(`/${self.props.subdir}/`)) {
path = path.substr(self.props.subdir.length + 1)
}
if (path.substr(-1) === '/') {
return new Response(null, {
headers: {
Expand Down