Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
reruin committed Sep 17, 2019
1 parent 166ec01 commit ef487d2
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 81 deletions.
5 changes: 4 additions & 1 deletion app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ const db = createFiledb(configPath , {raw:true} , {
//外链 10分钟
max_age_file: 5 * 60 * 1000,

skin:'default'
skin:'default',

//忽略文件(扩展名)
ignore_file_extensions:'passwd'
});

if(process.env.PORT){
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/manage.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const handlers = async (a , body) => {
cache.clear()
result.message = 'Success'
} else if (a == 'cfg') {
let { proxy_enable, preview_enable, max_age_dir, max_age_file, webdav_path } = body
let { proxy_enable, preview_enable, max_age_dir, max_age_file, webdav_path , ignore_file_extensions } = body
let opts = {}
if (max_age_dir !== undefined) {
max_age_dir = parseInt(max_age_dir)
Expand Down Expand Up @@ -79,6 +79,8 @@ const handlers = async (a , body) => {
opts.webdav_path = webdav_path
}

opts.ignore_file_extensions = ignore_file_extensions

await config.save(opts)
result.message = 'Success'
}
Expand Down
15 changes: 12 additions & 3 deletions app/controllers/sharelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ module.exports = {
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('/')) : ''
let ignore = (config.getConfig('ignore_file_extensions') || '').split(',').concat('passwd')

//data is readonly
if( data === false){
ctx.status = 404
Expand Down Expand Up @@ -81,7 +83,7 @@ module.exports = {
let resp = []
let preview_enable = config.getConfig('preview_enable')
for(let i of data.children){
if(i.ext != 'passwd'){
if(!ignore.includes(i.ext)){
let href = ''
if( i.url && isRelativePath(i.url) ){
href = pathNormalize(base_url + '/' + i.url)
Expand All @@ -107,7 +109,11 @@ module.exports = {
}

}else{
await output(ctx , data)
if( data.ext && ignore.includes(data.ext)){
ctx.status = 404
}else{
await output(ctx , data)
}
}

},
Expand All @@ -116,6 +122,7 @@ module.exports = {
const { paths , query } = ctx
let data = await service.path(paths , query , paths)
let parent = paths.length ? ('/' + paths.slice(0,-1).join('/')) : ''
let ignore = (config.getConfig('ignore_file_extensions') || '').split(',').concat('passwd')

//data is readonly
if( data === false){
Expand All @@ -128,7 +135,9 @@ module.exports = {
else if(data.type == 'folder'){
let ret = { ...data }
ret.auth = requireAuth(data)
ret.children = data.children.map(i => {
ret.children = data.children
.filter(i => (i.ext && !ignore.includes(i.ext)))
.map(i => {
let obj = { ...i }
if( i.url && isRelativePath(i.url) ){
obj.href = pathNormalize(base_path + '/' + i.url)
Expand Down
4 changes: 1 addition & 3 deletions app/plugins/drive.fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ const realpath = (p) => (isWinOS ? l2w(p) : p)

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

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

module.exports = ({datetime}) => {
module.exports = ({datetime , extname}) => {

const folder = async(id) => {
let dir = normalize(id) , resp = { id : dir , type:'folder', protocol:defaultProtocol}
Expand Down
3 changes: 2 additions & 1 deletion app/services/plugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs')
const path = require('path')
const querystring = require('querystring')
const {getFileType , getMIME , isArray , isObject , params , base64 , getRandomIP , retrieveSize } = require('../utils/base')
const {getFileType , getMIME , isArray , isObject , params , base64 , getRandomIP , retrieveSize , extname } = require('../utils/base')
const format = require('../utils/format')
const cache = require('../utils/cache')
const http = require('../utils/http')
Expand Down Expand Up @@ -121,6 +121,7 @@ const getHelpers = (id) => {
retrieveSize : format.retrieveByte,
getDrive : config.getDrive,
getRuntime:config.getRuntime,
extname:extname,

saveDrive : (path , name) => {
let resource = resources[id]
Expand Down
121 changes: 69 additions & 52 deletions app/utils/base.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const mime = require('mime');
const mime = require('mime')

const rnd = (min , max) => Math.floor(min+Math.random()*(max-min))
const path = require('path')

const isType = (type) => (obj) => ( Object.prototype.toString.call(obj) === `[object ${type}]`)
const rnd = (min, max) => Math.floor(min + Math.random() * (max - min))

const isType = (type) => (obj) => (Object.prototype.toString.call(obj) === `[object ${type}]`)

const isArray = isType('Array')

Expand All @@ -14,44 +16,40 @@ const isDate = isType('Date')

const isEmail = (v) => /^[A-Za-zd]+([-_.][A-Za-zd]+)*@([A-Za-zd]+[-.])+[A-Za-zd]{2,5}$/.test(v)

const parsePath = (url)=>{
if(url){
let raw = url.replace(/^\/*/,'').split('/')
const parsePath = (url) => {
if (url) {
let raw = url.replace(/^\/*/, '').split('/')
let paths = []
for(let i = 0 ; i<raw.length ; i++){
if( i == 0 || /[^!]$/.test(raw[i-1]) ){
for (let i = 0; i < raw.length; i++) {
if (i == 0 || /[^!]$/.test(raw[i - 1])) {
paths.push(decodeURIComponent(raw[i]))
}
}
return [paths , raw]
}else{
return [[] , []]
return [paths, raw]
} else {
return [
[],
[]
]
}
}

const getFileType = (v) => {
if(['mp4' , 'mpeg' , 'wmv' , 'webm' , 'avi' , 'rmvb' , 'mov' , 'mkv','f4v','flv'].includes(v)){
if (['mp4', 'mpeg', 'wmv', 'webm', 'avi', 'rmvb', 'mov', 'mkv', 'f4v', 'flv'].includes(v)) {
return 'video'
}
else if(['mp3' , 'm4a' ,'wav' ,'wma', 'ape' , 'flac' , 'ogg'].includes(v)){
} else if (['mp3', 'm4a', 'wav', 'wma', 'ape', 'flac', 'ogg'].includes(v)) {
return 'audio'
}
else if(['doc','docx','wps'].includes(v)){
} else if (['doc', 'docx', 'wps'].includes(v)) {
return 'word'
}
else if(['pdf'].includes(v)){
} else if (['pdf'].includes(v)) {
return 'pdf'
}
else if(['doc', 'docx','ppt','pptx','xls','xlsx','pdf','txt','yaml','ini','cfg'].includes(v)){
} else if (['doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', 'pdf', 'txt', 'yaml', 'ini', 'cfg'].includes(v)) {
return 'doc'
}
else if(['jpg','jpeg','png','gif','bmp','tiff','wmf','tif'].includes(v)){
} else if (['jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff', 'wmf', 'tif'].includes(v)) {
return 'image'
}
else if(['zip','rar','7z','tar','gz','gz2'].includes(v)){
} else if (['zip', 'rar', '7z', 'tar', 'gz', 'gz2'].includes(v)) {
return 'archive'
}
else{
} else {
return 'other'
}
}
Expand All @@ -60,27 +58,27 @@ const getMIME = (v) => {
return mime.getType(v)
}

const extend = (source , src) => {
for(var i in src){
const extend = (source, src) => {
for (var i in src) {
source[i] = src[i]
}
return source
}

const hash = (d , key) => {
const hash = (d, key) => {
let ret = {}
d.forEach((i)=>{
d.forEach((i) => {
ret[i[key]] = i
})
return ret
}


const getRandomIP = () => (rnd(50,250) + "." + rnd(50,250) + "." + rnd(50,250)+ "." + rnd(50,250))
const getRandomIP = () => (rnd(50, 250) + "." + rnd(50, 250) + "." + rnd(50, 250) + "." + rnd(50, 250))

const search = (ret , key , value) => {
for(let i in ret){
if(ret[i][key] == value){
const search = (ret, key, value) => {
for (let i in ret) {
if (ret[i][key] == value) {
return i
}
}
Expand All @@ -90,45 +88,64 @@ const search = (ret , key , value) => {
const pathNormalize = (path) => {
// see https://github.com/seajs/seajs/blob/master/src/util-path.js
let DOUBLE_DOT_RE = /\/[^\/]+\/\.\.\//,
basepath = ''
basepath = ''

path = path
.replace(/\/\.\//g, "/") // /./ => /
.replace(/([^:\/])\/+\//g,"$1/"); // a//b/c ==> a/b/c
.replace(/\/\.\//g, "/") // /./ => /
.replace(/([^:\/])\/+\//g, "$1/"); // a//b/c ==> a/b/c
// a/b/c/../../d ==> a/b/../d ==> a/d
while (path.match(DOUBLE_DOT_RE)) {
path = path.replace(DOUBLE_DOT_RE, "/");
path = path.replace(DOUBLE_DOT_RE, "/");
}
return path;
}

const base64 = {
encode : (v) => Buffer.from(v).toString('base64'),
decode : (v) => Buffer.from(v, 'base64').toString()
encode: (v) => Buffer.from(v).toString('base64'),
decode: (v) => Buffer.from(v, 'base64').toString()
}

const enablePreview = (v) => ['audio','video','image'].includes(v)
const enablePreview = (v) => ['audio', 'video', 'image'].includes(v)

const enableRange = (v) => ['audio','video'].includes(v)
const enableRange = (v) => ['audio', 'video'].includes(v)

const isRelativePath = (v) => !/^http/.test(v)

module.exports = {
parsePath , getFileType, getMIME,

isArray , isObject, isString, isDate, isEmail, isRelativePath , enablePreview, enableRange ,
// const extname = (p) => path.extname(p).substring(1)

hash, extend, getRandomIP, pathNormalize , search, base64,
const extname = (p) => p.split('.').pop()

params (url){
module.exports = {
parsePath,
getFileType,
getMIME,

isArray,
isObject,
isString,
isDate,
isEmail,
isRelativePath,
enablePreview,
enableRange,

hash,
extend,
getRandomIP,
pathNormalize,
search,
base64,
extname,

params(url) {
url = url.split('?')[1]
let reg = /(?:&)?([^=]+)=([^&]*)/ig,
obj = {},
m
obj = {},
m

while(m = reg.exec(url)) obj[m[1]] = m[2]
while (m = reg.exec(url)) obj[m[1]] = m[2]

return obj
}

}
}
2 changes: 1 addition & 1 deletion app/views/default/auth.pug
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ block content
div
.form-group
input.sl-input(id="j_user",type='text', name='user', placeholder=__('auth_user_placeholder'))
input.sl-input(id="j_passwd",type='password', name='passwd', placeholder=__('auth_user_placeholder'))
input.sl-input(id="j_passwd",type='password', name='passwd', placeholder=__('auth_passwd_placeholder'))
button.sl-button.btn-primary(type="button")= __('ok')
script(src='https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js')

Expand Down
5 changes: 5 additions & 0 deletions app/views/default/manage.pug
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ block content
label= __('webdav_path')
.col-sm-3
input.form-control(type='text', name='webdav_path' , value=config.webdav_path,required)
.form-group
.col-sm-6
label= __('ignore_file_extensions')
.col-sm-3
input.form-control(type='text', name='ignore_file_extensions' , value=config.ignore_file_extensions,placeholder=__('ignore_file_extensions_placeholder'))
.form-group.text-center
button.btn.btn-default(type='submit')= __('save')
form.form-horizontal(method='post',action="/manage/api")
Expand Down
4 changes: 3 additions & 1 deletion locales/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ module.exports = {
"max_age_file":"File max age(second)",
"virtual_path":"Virtual path",
"general":"General",
"webdav_path":"WebDAV Path"
"webdav_path":"WebDAV Path",
"ignore_file_extensions":"Ignore File Extensions",
"ignore_file_extensions_placeholder":"e.g. docx,pptx"
}
4 changes: 3 additions & 1 deletion locales/zh-CN.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ module.exports = {
"max_age_file":"链接缓存时长(秒)",
"virtual_path":"虚拟路径",
"general":"常规",
"webdav_path":"WebDAV路径"
"webdav_path":"WebDAV路径",
"ignore_file_extensions":"忽略文件类型",
"ignore_file_extensions_placeholder":"文件扩展名,多个用逗号分隔。如 docx,pptx",
}
6 changes: 4 additions & 2 deletions locales/zh-TW.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ module.exports = {
"max_age_file":"檔案快取周期(秒)",
"virtual_path":"虛擬路徑",
"general":"通用設定",
"webdav_path":"WebDAV路徑"
}
"webdav_path":"WebDAV路徑",
"ignore_file_extensions":"忽略文件類型",
"ignore_file_extensions_placeholder":"文件擴展名,多個用逗號分隔",
}
4 changes: 1 addition & 3 deletions plugins/drive.ftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ const { PassThrough } = require('stream')

const clientMap = {}

module.exports = ({ getConfig, cache }) => {

const extname = (p) => path.extname(p).substring(1)
module.exports = ({ getConfig, cache, extname }) => {

const getClient = async (url, cd = false) => {
let key = (url.match(/ftp\:\/\/[\w\W]+?\//) || [''])[0];
Expand Down
4 changes: 1 addition & 3 deletions plugins/drive.github.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ const { Writable } = require('stream')

const clientMap = {}

module.exports = ({ request , getConfig, base64 , getRuntime}) => {

const extname = (p) => path.extname(p).substring(1)
module.exports = ({ request , getConfig, base64 , extname }) => {

const getContent = async (id) => {
let p = id.split('/')
Expand Down
4 changes: 1 addition & 3 deletions plugins/drive.h5ai.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ const { Writable } = require('stream')

const clientMap = {}

module.exports = ({ request , getConfig, cache, base64 , retrieveSize }) => {

const extname = (p) => path.extname(p).substring(1)
module.exports = ({ request , getConfig, cache, base64 , retrieveSize, extname }) => {

const getContent = async (p) => {
let resp = await request.get(p)
Expand Down
Loading

0 comments on commit ef487d2

Please sign in to comment.