Skip to content

Commit

Permalink
create virtual path
Browse files Browse the repository at this point in the history
  • Loading branch information
reruin committed Apr 18, 2018
1 parent ffcaaa4 commit 8a6d1f5
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 72 deletions.
1 change: 0 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const koaStatic = require('koa-static')
const less = require('./middleware/koa-less')
const addr = require('./middleware/koa-addr')
const paths = require('./middleware/koa-paths')
const install = require('./middleware/koa-gdlist-install')

const routers = require('./routers/index')
const cors = require('@koa/cors');
Expand Down
39 changes: 14 additions & 25 deletions controllers/gdlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,32 @@ const cache = {}

module.exports = {

async list(ctx) {
let result = {
status: 0,
data: null,
}

let { page = 1 , cat = ''} = ctx.query


if (page < 1) page = 1

let data = await service.list(page , cat)

result.data = data

result.count = data.length

ctx.body = result

},

async listPage(ctx){
async index(ctx){
let data = await service.path(ctx.paths)
let base_url = ctx.url == '/' ? '' : ctx.url
let parent = ctx.paths.length ? ('/' + ctx.paths.slice(0,-1).join('/')) : ''
if( data === false){
ctx.status = 404
}

else if(Array.isArray(data)){
await ctx.render('index',{
data , url : base_url , parent
})
}else{
let act = ctx.request.querystring
if(act == 'preview'){
await ctx.render('detail',{
data , url : base_url , parent
})
}
else if(act == 'proxy'){
ctx.body = ctx.req.pipe(request(url))
}
else{
ctx.redirect( data.url )
}

await ctx.render('detail',{
data , url : base_url , parent
})
}

},
Expand Down
12 changes: 10 additions & 2 deletions controllers/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ module.exports = {
}
,
async save(ctx){
let { token , path } = ctx.request.body
let { token , name , path } = ctx.request.body
let cfg = {token}
if(Array.isArray(name)){
cfg.path = name.map((i ,index)=>{
return { name:i , path:path[index]}
})
}else{
cfg.path = [{name , path}]
}
let result = { status : 0 , message : ''}
if( token && path ){
await config.save({token , path})
await config.save( cfg )
ctx.redirect('/')
}else{
result.status = -1
Expand Down
66 changes: 52 additions & 14 deletions controllers/manage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const service = require('./../models/gdlist')
const base = require('../utils/base')
const request = require('request')
const config = require('../config')
const config = require('../utils/config')
const cache = require('../utils/cache')

module.exports = {
Expand All @@ -11,27 +11,65 @@ module.exports = {
let token = ctx.params.token
let act = ctx.query.a
let message , access = false

if( token ){
if( token == config.token ){
if( token == config.data.token ){
access = true

if(act == 'clear_cache'){
cache.clear()
message = '成功清除缓存'

}
else if(act == 'signout'){
access = false
ctx.redirect('/manage')
return
}
}else{
message = '密码错误'
}

}
await ctx.render('manage',{access , message})
await ctx.render('manage',{access , message , config:config.data})
},

async update(ctx){
let token = ctx.params.token
let act = ctx.request.body.a
let message = ''

if(token !== config.data.token){
ctx.redirect('/manage')
return
}

if(act == 'path'){
let { name , path } = ctx.request.body

if(Array.isArray(name)){
path = name.map((i ,index)=>{
return { name:i , path:path[index]}
})
}else{
path = [{name , path}]
}

let result = { status : 0 , message : ''}

if( path ){
await config.save( { path } )
message = '保存成功'
}else{
message = '请填写完整'
}
}
else if( act == 'token'){
let newtoken = ctx.request.body.token
if(newtoken){
await config.save( { token:newtoken } )
message = '口令修改成功'
ctx.redirect('/manage')
return
}else{
message = '请填写新口令'
}
}else if(act == 'clear_cache'){
cache.clear()
message = '成功清除缓存'
}

await ctx.render('manage',{ message , access : true , config:config.data})

}


Expand Down
2 changes: 1 addition & 1 deletion middleware/koa-paths.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = async (ctx , next)=>{
let url = ctx.req.url.substring(1)
let url = ctx.request.path.substring(1)
if(url){
ctx.paths = url.split('/')
}else{
Expand Down
30 changes: 27 additions & 3 deletions models/gdlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const http = require('../utils/http')
const base = require('../utils/base')
const cache = require('../utils/cache')
const config = require('../config')
const config = require('../utils/config')
const host = 'https://drive.google.com'
const format = require('../utils/format')

Expand Down Expand Up @@ -86,13 +86,12 @@ const file = async(id) =>{
// path => gd folder => files
const path = async(p) => {
let pl = p.join('.') , hit , resp

if(cache(pl)){
hit = cache(pl)
}
else{
if(pl == ''){
hit = {id:config.path , type:'folder'}
hit = mount()
}

else{
Expand Down Expand Up @@ -128,5 +127,30 @@ const path = async(p) => {

}

const mount = () =>{
let data = config.data , key
if(Array.isArray( data.path )){
if(data.path.length == 1){
key = data.path[0].path
}else{
//根路径不判断缓存,防止添加路径路径时丢失
let disk = data.path.map((i,index)=>({
id : i.path,
name : i.name,
size : '-',
updated_at : '-',
type : 'folder'
}))
cache('root' , disk)
key = 'root'
}

}else{
key = data.path
}
return {id:key , type:'folder'}

}


module.exports = { path }
3 changes: 2 additions & 1 deletion routers/manage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const manage = require('../controllers/manage')
const routers = router
.get('/', manage.home)
.get('/:token', manage.home)
.post('/:token', manage.update)


module.exports = routers
module.exports = routers
2 changes: 1 addition & 1 deletion routers/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ const router = require('koa-router')()
const gdlist = require('../controllers/gdlist')

const routers = router
.get('/:path(.*)', gdlist.listPage)
.get('/:path(.*)', gdlist.index)

module.exports = routers
5 changes: 4 additions & 1 deletion utils/config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
const fs = require('fs')
const os = require('os')

const cache = require('./cache')
const config_path = process.cwd() +'/config.json'

var data = require('../config.json')

var app , handler




async function save(d){
if(d.token) data.token = d.token
if(d.path) data.path = d.path
Expand Down
2 changes: 1 addition & 1 deletion views/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ block content
ul.list
each i in data
li
a.clearfix(href=url+'/'+i.name , target=i.type == 'folder' ? '' : '_blank')
a.clearfix(href=url+'/'+i.name+(i.type=='folder'?'':'?preview') , target=i.type == 'folder' ? '' : '_blank')
.row
span.file-name.col-md-7.col-sm-6.col-xs-9
i.ic(class=i.type)
Expand Down
53 changes: 38 additions & 15 deletions views/install.pug
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,44 @@ block content
section.wrap
.page-header
h4.text-center 初始化
form.form-horizontal(action='',method='post')
.form-group
label.control-label.col-sm-2(for='path') 根路径
.col-sm-10
input.form-control(type='text', name='path',placeholder='GoogleDrive 分享的文件夹ID')
.form-group
label.control-label.col-sm-2(for='token') 密码
.col-sm-10
input.form-control(type='text', name='token' , placeholder='用于管理登录')

.form-group
.col-sm-offset-2.col-sm-10
button.btn.btn-default(type='submit') 提交


form.form-horizontal(action='',method='post')
.panel.panel-default
.panel-heading
| 口令
.panel-body
.form-group
.col-sm-12
input.form-control(type='text', name='token' , placeholder='新口令',required)
.panel.panel-default
.panel-heading
| 虚拟路径
a.create-path 添加
.panel-body
.group-path
.form-group.item
.col-sm-2
input.form-control(type='text', name='name',placeholder='名称',required)
.col-sm-9
input.form-control(type='text', name='path',placeholder='GoogleDrive 分享的文件夹ID',required)
.col-sm-1
button.btn.btn-default.remove-path(type='button') -
.form-group
.col-sm-offset-2.col-sm-10
button.btn.btn-default(type='submit') 提交
script(scr='https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js')
script.
$(function(){
var item
if($('.item').length){
item = $('.item:first').clone()
item.find('input').val('')
}
$('body').on('click','.create-path' , function(){
$('.group-path').append( item.clone() )
}).on('click' ,'.remove-path' , function(){
$(this).parents('.item').remove()
})
})
include footer


Loading

0 comments on commit 8a6d1f5

Please sign in to comment.