Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
reruin committed Jan 2, 2019
1 parent ab528fb commit 36e26e9
Show file tree
Hide file tree
Showing 14 changed files with 621 additions and 144 deletions.
12 changes: 11 additions & 1 deletion app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const os = require('os')
const config_path = process.cwd() +'/cache/config.json'
const port = process.env.PORT || 33001

var location = {}

var data = {
port ,

Expand Down Expand Up @@ -48,6 +50,14 @@ const getPort = () => data.port

const getPath = () => [].concat( data.path || [] )

const setLocation = (d) => { location = d }

const getLocation = () => location

const setConfig = (d) => {

}

try{
let cfg = fs.readFileSync(config_path,'utf-8');
if(cfg){
Expand All @@ -61,4 +71,4 @@ try{

}

module.exports = { get:getAllConfig, getConfig , getToken , getPath , getPort , getTitle , save , installed }
module.exports = { get:getAllConfig, getConfig , getToken , getPath , getPort , getTitle , save , installed , getLocation , setLocation}
192 changes: 98 additions & 94 deletions app/controllers/manage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,112 +6,116 @@ const { getVendors } = require('../services/plugin')

module.exports = {

async home(ctx , next){
let token = ctx.params.token
async home(ctx, next) {

let token = ctx.request.body.token
let act = ctx.query.a
let message , access = false
let message, access = !!ctx.session.admin

if( token ){
if( token == config.getToken() ){
access = true
}else{
message = 'Invalid Password'
}

if (access) {
await ctx.render('manage', { access, message, config: config.get(), vendors: getVendors() })
} else {
await ctx.render('manage', { access })
}

if(act == 'export'){
ctx.body = config.get()
}else{
await ctx.render('manage',{access , message , config:config.get() , vendors:getVendors()})
}
// if(act == 'export'){
// ctx.body = config.get()
// }else{
// await ctx.render('manage',{access , message , config:config.get() , vendors:getVendors()})
// }
},

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

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

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

if(Array.isArray(name)){
path = name.map((i ,index)=>{
return { name:i , path:vendor[index]+':'+path[index]}
})
}else{
path = [{name , path:vendor+':'+path}]
}
let body = ctx.request.body
let a = body.a
let result = { status: 0, message: 'Success', data: '' , a}

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

if( path ){
await config.save( { path } )
message = 'Success'
}else{
message = 'Invalid Arguments'
}
}
else if( act == 'token'){
let newtoken = ctx.request.body.token
if(newtoken){
await config.save( { token:newtoken } )
message = 'Success'
ctx.redirect('/manage')
return
}else{
message = 'Invalid password'
}
}
else if( act == 'title'){
let title = ctx.request.body.title
if(title){
await config.save( { title:title } )
message = 'Success'
}else{
message = ''
if (a == 'signin') {
if (body.token == config.getToken()) {
ctx.session.admin = true
result.a = a
result.message = 'Success'
} else {
result.status = -1
result.message = 'Invalid Password'
}
}
else if(act == 'clear_cache'){
cache.clear()
message = 'Success'
}
else if(act == 'cfg'){
let {proxy_enable , max_age_dir , max_age_file} = ctx.request.body
let opts = {}
if(max_age_dir !== undefined){
max_age_dir = parseInt(max_age_dir)
if(!isNaN(max_age_dir)){
opts.max_age_dir = max_age_dir * 1000
}
}

if(max_age_file){
max_age_file = parseInt(max_age_file)
if(!isNaN(max_age_file)){
opts.max_age_file = max_age_file * 1000
} else {
if (!ctx.session.admin) {
result.status = 403
result.message = 'Require Auth'
} else {

if (a == 'path') {
let { name, path, vendor } = body

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

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

if (path) {
await config.save({ path })
result.message = 'Success'
} else {
result.message = 'Invalid Arguments'
}
} else if (a == 'token') {
let newtoken = body.token
if (newtoken) {
await config.save({ token: newtoken })
ctx.session.admin = false
result.message = 'Success'
} else {
result.status = -1
result.message = 'Invalid password'
}
} else if (a == 'title') {
let title = body.title
if (title) {
await config.save({ title: title })
result.message = 'Success'
} else {
result.status = -1
result.message = 'Invalid Title'
}
} else if (a == 'clear_cache') {
cache.clear()
result.message = 'Success'
} else if (a == 'cfg') {
let { proxy_enable, max_age_dir, max_age_file } = body
let opts = {}
if (max_age_dir !== undefined) {
max_age_dir = parseInt(max_age_dir)
if (!isNaN(max_age_dir)) {
opts.max_age_dir = max_age_dir * 1000
}
}

if (max_age_file) {
max_age_file = parseInt(max_age_file)
if (!isNaN(max_age_file)) {
opts.max_age_file = max_age_file * 1000
}
}

if (proxy_enable) {
proxy_enable = proxy_enable == '1' ? 1 : 0
opts.proxy_enable = proxy_enable
}

await config.save(opts)
result.message = 'Success'
}
}

if(proxy_enable){
proxy_enable = proxy_enable == '1' ? 1 : 0
opts.proxy_enable = proxy_enable
}

await config.save( opts )
message = 'Success'

}

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

ctx.body = result
}


}
}
12 changes: 10 additions & 2 deletions app/controllers/sharelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const output = async (ctx , data)=>{
module.exports = {
async index(ctx){

let data = await service.path(ctx.paths , ctx.query , ctx.paths)
let data = await service.path(ctx.paths , ctx.query , ctx.paths , ctx.method)
let base_url = ctx.path == '/' ? '' : ctx.path
let parent = ctx.paths.length ? ('/' + ctx.paths.slice(0,-1).join('/')) : ''
//data is readonly
Expand All @@ -57,7 +57,15 @@ module.exports = {
else if(data === 401){
ctx.status = 401
}

else if(data.body){
await ctx.render('custom',{
body : data.body
})
}
else if(data.redirect){
ctx.redirect(data.redirect)
return
}
else if(data.type == 'folder'){

let ra = requireAuth(data)
Expand Down
12 changes: 11 additions & 1 deletion app/middleware/koa-paths.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const parseXML = require('xml2js').parseString
const parsePath = require('../utils/base').parsePath
const { setLocation } = require('../config')

const parser = (req, options) => {
return new Promise((resolve, reject) => {
Expand All @@ -19,7 +20,7 @@ const xml2js = ( xml , options = {}) => {
})
}

const webdavMethods = ['pptions','head','trace','get','put','post','delete','mkcol','propfind','proppatch','copy','move','lock','unlock']
const webdavMethods = ['options','head','trace','get','put','post','delete','mkcol','propfind','proppatch','copy','move','lock','unlock']

module.exports = async(ctx, next) => {
if (!ctx.session.access) {
Expand All @@ -33,7 +34,16 @@ module.exports = async(ctx, next) => {
let [paths, paths_raw] = parsePath(url)
ctx.paths = paths
ctx.paths_raw = paths_raw
setLocation({
href:ctx.href,
path:ctx.path,
query:ctx.query,
host:ctx.host,
origin:ctx.origin,
protocol:ctx.protocol
})
console.log('webdav:',isWebDAV )
console.log(ctx.href, ctx.request.body)
if(
( isWebDAV || ctx.is('xml') )
&&
Expand Down
6 changes: 4 additions & 2 deletions app/routers/manage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ const manage = require('../controllers/manage')

const routers = router
.get('/', manage.home)
.get('/:token', manage.home)
.post('/:token', manage.update)
.post('/', manage.home)
.post('/api' , manage.api)
// .get('/:token', manage.home)
// .post('/:token', manage.update)


module.exports = routers
6 changes: 6 additions & 0 deletions app/services/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ const helper = {
getSource: getSource,
getConfig : config.getConfig,
getRandomIP:getRandomIP,
getLocation : config.getLocation
}

const setPrivateConfig = (name) => ( path ) => {

}

const load = (options) => {

const dir = options.dir
Expand Down Expand Up @@ -142,6 +147,7 @@ const updateFile = async (file) => {
// 用于更新目录数据
const updateFolder = (folder) => {
let parentType = folder.protocol
if(!folder.children) return folder
folder.children.forEach( (d , index) => {
let name = d.name

Expand Down
6 changes: 3 additions & 3 deletions app/services/sharelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ShareList {
//{gd , od , xd , ld , remote}
}

async path(paths, query, full_paths) {
async path(paths, query, full_paths , method) {
let pl = paths.join('/'),
hit, resp = false,
miss
Expand All @@ -50,7 +50,7 @@ class ShareList {
if (pl == '') {
hit = this.mount()
} else {
let parent = await this.path(paths.slice(0, -1), query, full_paths)
let parent = await this.path(paths.slice(0, -1), query, full_paths , method)
let curname = decodeURIComponent(paths[paths.length - 1])
//父目录必然是 folder
if (parent) {
Expand Down Expand Up @@ -100,7 +100,7 @@ class ShareList {
// folder /a/b/c
if (hit.type == 'folder') {

resp = await vendor.folder(hit.id, { query, paths: diff(paths, full_paths), content: hit.content })
resp = await vendor.folder(hit.id, { query, req : config.getLocation() ,paths: diff(paths, full_paths), content: hit.content })
if (resp) updateFolder(resp)
//let passwd = base.checkPasswd(resp)
//resp.auth = passwd !== false
Expand Down
Loading

0 comments on commit 36e26e9

Please sign in to comment.