Skip to content

Commit

Permalink
+ support local file system
Browse files Browse the repository at this point in the history
  • Loading branch information
reruin committed Sep 4, 2018
1 parent c6bdd94 commit 91e560f
Show file tree
Hide file tree
Showing 14 changed files with 316 additions and 83 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
在线挂载 GoogleDrive、OneDrive 的简易工具 , 只需提供分享文件夹ID。

## 特性
1. 文件夹ID挂载网盘
2. 支持虚拟目录
在网盘内以如下格式重命名文件夹
1. 支持使用GD,OD文件夹ID挂载网盘
2. 支持列出本地目录
统一使用linux的路径,例如 windows D盘 为 ```/d/```
3. 自定义文件目录
可使用yaml构建目录内容,保存为```目录名.xd```。参考 ```example/download.xd```
4. 支持虚拟目录嵌套
以如下格式重命名文件夹
格式:```显示名称.文件夹id.类型```
类型支持 od(OneDrive) gd(GoogleDrive)
3. 加密目录
类型支持 od(OneDrive) gd(GoogleDrive)
5. 加密目录
在文件夹内新建 ```.密码.passwd``` 命名的文件即可,例如
```.123456.passwd```
不要省略最前方的```.```
4. xdrive文件系统,可使用yaml构建,并保存为```xd```后缀。参考 ```example/download.xd```


## 已知BUG
1. GoogleDrive目录内文件过多时无法完全显示,也无法分页。


## 安装
### Shell
Expand Down
5 changes: 3 additions & 2 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const port = process.env.PORT || 33001
const providers = [
{name:'GoogleDrive',code:'gd'},
{name:'OneDrive',code:'od'},
{name:'Custom',code:'cu'},
{name:'虚拟目录',code:'xd'},
{name:'本地目录',code:'ld'},
]

//onedrive 链接有效期 10 分钟
Expand All @@ -23,7 +24,7 @@ var data = {
}

try{
var cfg =fs.readFileSync(config_path,'utf-8');
var cfg = fs.readFileSync(config_path,'utf-8');
if(cfg){
cfg = JSON.parse(cfg)
for(var i in cfg){
Expand Down
99 changes: 60 additions & 39 deletions controllers/sharelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,71 @@ const base = require('../utils/base')
const http = require('../utils/http')
const {encode , decode} = require('../utils/format')
const request = require('request')
const config =require('../config')
const config = require('../config')
const sendFile = require('../utils/sendfile')
const cache = {}
const parse_path = require('../utils/base').parse_path
const fs = require('fs')

const proxy_header_supports = ['video' , 'audio']
const auth = (data , ctx)=>{

}


const output = async (ctx , data)=>{

let preview = ctx.request.querystring.indexOf('preview') >= 0
let download_url = data.url

let enabled_proxy = config.data.enabled_proxy
if(preview){
if(enabled_proxy){
download_url = ctx.path
}

if(data.fs){
download_url = ctx.path
}

await ctx.render('detail',{
data , download_url
})
}
// download
else{
if(data.fs){
await sendFile(ctx, data.url)
}else{
if(enabled_proxy){

let proxy_header_support = proxy_header_supports.includes(data.type)

if( (data.proxy_header || config.data.enabled_proxy_header ) && proxy_header_support){

try{
let headers = await http.header2(download_url,{headers:{'Range': 'bytes=0-'}})
// console.log(headers)
if(headers){
for(let i in headers){
ctx.response.set(i, headers[i])
}
}
}catch(e){
console.log(e)
}
}

ctx.body = ctx.req.pipe(request({url :download_url}))

}else{
ctx.redirect( download_url )
}
}

}
}

module.exports = {
async index(ctx){
let data = await service.path(ctx.paths , ctx.query , ctx.paths_raw)
Expand Down Expand Up @@ -40,7 +97,7 @@ module.exports = {
if(['audio','video','image'].indexOf(i.type) >= 0){
href += (href.indexOf('?')>=0 ? '&' : '?') + 'preview'
}
resp.push( { href , type : i.type , name:i.name})
resp.push( { href , type : i.type , size: i.size , updated_at:i.updated_at , name:i.name})
}
})

Expand All @@ -50,44 +107,8 @@ module.exports = {
}

}else{
let preview = ctx.request.querystring.indexOf('preview') >= 0
let download_url = data.url

let proxy_header_support = (data.type == 'video' || data.type == 'audio')
console.log( proxy_header_support )
if(preview){
if(config.data.enabled_proxy){
download_url = ctx.path
}
await ctx.render('detail',{
data , download_url
})
}
else{
if(config.data.enabled_proxy){
console.log('proxy:',download_url)
if( (data.proxy_header || config.data.enabled_proxy_header ) && proxy_header_support){

try{
let headers = await http.header2(download_url,{headers:{'Range': 'bytes=0-'}})
// console.log(headers)
if(headers){
for(let i in headers){
ctx.response.set(i, headers[i])
}
}
}catch(e){
console.log(e)
}
}

ctx.body = ctx.req.pipe(request({url :download_url}))

}else{
ctx.redirect( download_url )
}
}

await output(ctx , data)
}

},
Expand Down
12 changes: 8 additions & 4 deletions example/download.xd
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@
name: mssql
children:
-
name: SQL SERVER 2008R2 EXPRESS
name: SQL SERVER 2008R2 EXPRESS.exe
url: https://download.microsoft.com/download/9/4/8/948966AB-52CA-40F1-8051-0216481065E6/SQLEXPRWT_x64_CHS.exe
-
name: SQL SERVER 2012 EXPRESS
name: SQL SERVER 2012 EXPRESS.exe
url: https://download.microsoft.com/download/A/4/3/A43F9D8D-5346-441A-ABAE-86C3AFE17B4D/SQLEXPRWT_x64_CHS.exe
-
name: SQL SERVER 2017 EXPRESS
name: SQL SERVER 2017 EXPRESS.exe
url: https://download.microsoft.com/download/5/E/9/5E9B18CC-8FD5-467E-B5BF-BADE39C51F73/SQLServer2017-SSEI-Expr.exe
-
name: 嵌套GoodleDrive虚拟目录.0B0vQvfdCBUFjdnRpVnZsWmlFanM.gd
name: 嵌套GoodleDrive虚拟目录.0B0vQvfdCBUFjdnRpVnZsWmlFanM.gd
-
name: 嵌套windows本地D盘./d/.ld
-
name: 嵌套linux root目录./root/.ld
2 changes: 0 additions & 2 deletions models/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ const folder = (d) => {

d.provider = provider
d.type = 'folder'

console.log('========>',d)
}
return d
}
Expand Down
87 changes: 85 additions & 2 deletions models/drive/localdrive.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,97 @@ const format = require('../../utils/format')

const path = require('path')
const fs = require('fs')
const adapter = require('../adapter')
const os = require('os')

const isWinOS = os.platform() == 'win32'

const exists = (path) => fs.existsSync( isWinOS ? l2w(path) : path)

const readdir = (path) => fs.readdirSync( isWinOS ? l2w(path) : path )

const fsstat = (path) => fs.statSync( isWinOS ? l2w(path) : path )

const w2l = (p)=>{
return p.replace(/^([a-z]):\\/,'$1/').replace(/\\/g,'/')
}

const l2w = (p)=>{
p = p.replace(/^\/([^\/]+?)/,'$1:\\').replace(/\//g,'\\').replace(/\\$/,'').replace(/\\{2,}/g,'\\')
return p
}

const realpath = (p)=> (isWinOS ? l2w(p) : p)

const normalize = (p) => (p+'/').replace(/\/{2,}/g,'/')

const extname = (p) => path.extname(p).substring(1)

const folder = async(id) => {



let dir = normalize(id) , resp = { id : dir , type:'folder', provider:'ld'}

if( exists(dir) ){
let children = []
readdir(dir).forEach(function(filename){
let path = normalize(dir + '/' + filename)
let stat
try{
stat = fsstat(path)
}catch(e){

}

let obj = {
id:path ,
name:filename,
provider:'ld',
type:'other'
}

if(stat){
obj.size = format.byte(stat.size)
obj.created_at = format.datetime(stat.ctime)
obj.updated_at = format.datetime(stat.mtime)
if(stat.isDirectory()){
obj.type = 'folder'
}
if(stat.isFile()){
let ext = extname(filename)
obj.ext = ext

obj.type = base.mime_type(ext)
adapter.folder(obj)
}
}

children.push(obj)
})

resp.children = children
return resp
}else{

return false
}

// return resp
}

const file = async(id)=>{

let name = path.basename(id)
let ext = extname(id)

console.log(fs.existsSync(realpath(id)))

return {
name , id, fs:true ,
url: realpath(id),
type: base.mime_type(ext),
provider:'ld'
}
}


module.exports = { folder , file }
26 changes: 14 additions & 12 deletions models/drive/xdrive.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@


const yaml = require('yaml').default
const http = require('../../utils/http')

const base = require('../../utils/base')
const cache = require('../../utils/cache')
const config = require('../../config')
const host = 'https://drive.google.com'

const format = require('../../utils/format')
const source = require('../source')
const adapter = require('../adapter')
Expand All @@ -29,7 +29,6 @@ const createId = (d , rootId)=>{
i.type = base.mime_type(i.ext)
}


adapter.folder(i)

}else if(base.isArray(i)){
Expand All @@ -55,24 +54,28 @@ const mount = async(id)=>{

let data = await source(...id.split('@'))

data = await http.get(data.url)
console.log('yarml' , data)
if(data){
let json = yaml.parse( data )

let json = yaml.parse( data.body )
json = createId(json , id + ':')

json = createId(json , id + ':')
resp.children = json
resp.updated_at = Date.now()

resp.children = json
resp.updated_at = Date.now()
cache(resid,resp)
return resp
}else{
return undefined
}

cache(resid,resp)

return resp
}

const findById = (id)=>{
let rootId = id.split(':')[0]
let disk = cache(`xd_${rootId}`)
let path = id.replace(/^[^\/]+\//,'').split('/')
let path = id.split(':/')[1].split('/')

for(let i=0; i<path.length && disk; i++){
disk = disk.children
Expand All @@ -83,7 +86,6 @@ const findById = (id)=>{
}

const folder = async(id) => {
console.log('id=====>',id)
let resp
//xdrive目录内
if(/@[a-wA-W]+:\//.test(id)){
Expand Down
Loading

0 comments on commit 91e560f

Please sign in to comment.