added missing commit

This commit is contained in:
Weidi Deng 2020-12-29 10:56:32 +08:00
parent 8362a2ee2d
commit 0d2b9bcabf
8 changed files with 70 additions and 138 deletions

View File

@ -66,6 +66,7 @@ func addServerFlags(flags *pflag.FlagSet) {
flags.Bool("disable-thumbnails", false, "disable image thumbnails") flags.Bool("disable-thumbnails", false, "disable image thumbnails")
flags.Bool("disable-preview-resize", false, "disable resize of image previews") flags.Bool("disable-preview-resize", false, "disable resize of image previews")
flags.Bool("disable-exec", false, "disables Command Runner feature") flags.Bool("disable-exec", false, "disables Command Runner feature")
flags.Bool("disable-type-detection-by-header", false, "disables type detection by reading file headers")
} }
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
@ -243,6 +244,9 @@ func getRunParams(flags *pflag.FlagSet, st *storage.Storage) *settings.Server {
_, disablePreviewResize := getParamB(flags, "disable-preview-resize") _, disablePreviewResize := getParamB(flags, "disable-preview-resize")
server.ResizePreview = !disablePreviewResize server.ResizePreview = !disablePreviewResize
_, disableTypeDetectionByHeader := getParamB(flags, "disable-type-detection-by-header")
server.TypeDetectionByHeader = !disableTypeDetectionByHeader
_, disableExec := getParamB(flags, "disable-exec") _, disableExec := getParamB(flags, "disable-exec")
server.EnableExec = !disableExec server.EnableExec = !disableExec

View File

@ -81,7 +81,7 @@ func NewFileInfo(opts FileOptions) (*FileInfo, error) {
return file, nil return file, nil
} }
err = file.detectType(opts.Modify, true) err = file.detectType(opts.Modify, true, true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -134,7 +134,7 @@ func (i *FileInfo) Checksum(algo string) error {
//nolint:goconst //nolint:goconst
//TODO: use constants //TODO: use constants
func (i *FileInfo) detectType(modify, saveContent bool) error { func (i *FileInfo) detectType(modify, saveContent, readHeader bool) error {
if IsNamedPipe(i.Mode) { if IsNamedPipe(i.Mode) {
i.Type = "blob" i.Type = "blob"
return nil return nil
@ -143,6 +143,51 @@ func (i *FileInfo) detectType(modify, saveContent bool) error {
// imagine the situation where a file in a dir with thousands // imagine the situation where a file in a dir with thousands
// of files couldn't be opened: we'd have immediately // of files couldn't be opened: we'd have immediately
// a 500 even though it doesn't matter. So we just log it. // a 500 even though it doesn't matter. So we just log it.
var buffer []byte
mimetype := mime.TypeByExtension(i.Extension)
if mimetype == "" && readHeader {
buffer = i.readFirstBytes()
mimetype = http.DetectContentType(buffer)
}
switch {
case strings.HasPrefix(mimetype, "video"):
i.Type = "video"
i.detectSubtitles()
return nil
case strings.HasPrefix(mimetype, "audio"):
i.Type = "audio"
return nil
case strings.HasPrefix(mimetype, "image"):
i.Type = "image"
return nil
case (strings.HasPrefix(mimetype, "text") || (len(buffer) > 0 && !isBinary(buffer))) && i.Size <= 10*1024*1024: // 10 MB
i.Type = "text"
if !modify {
i.Type = "textImmutable"
}
if saveContent {
afs := &afero.Afero{Fs: i.Fs}
content, err := afs.ReadFile(i.Path)
if err != nil {
return err
}
i.Content = string(content)
}
return nil
default:
i.Type = "blob"
}
return nil
}
func (i *FileInfo) readFirstBytes() []byte {
reader, err := i.Fs.Open(i.Path) reader, err := i.Fs.Open(i.Path)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
@ -159,44 +204,7 @@ func (i *FileInfo) detectType(modify, saveContent bool) error {
return nil return nil
} }
mimetype := mime.TypeByExtension(i.Extension) return buffer[:n]
if mimetype == "" {
mimetype = http.DetectContentType(buffer[:n])
}
switch {
case strings.HasPrefix(mimetype, "video"):
i.Type = "video"
i.detectSubtitles()
return nil
case strings.HasPrefix(mimetype, "audio"):
i.Type = "audio"
return nil
case strings.HasPrefix(mimetype, "image"):
i.Type = "image"
return nil
case isBinary(buffer[:n], n) || i.Size > 10*1024*1024: // 10 MB
i.Type = "blob"
return nil
default:
i.Type = "text"
if !modify {
i.Type = "textImmutable"
}
if saveContent {
afs := &afero.Afero{Fs: i.Fs}
content, err := afs.ReadFile(i.Path)
if err != nil {
return err
}
i.Content = string(content)
}
}
return nil
} }
func (i *FileInfo) detectSubtitles() { func (i *FileInfo) detectSubtitles() {
@ -228,7 +236,6 @@ func (i *FileInfo) readListing(checker rules.Checker) error {
NumFiles: 0, NumFiles: 0,
} }
detectDisabled := checker.IsTypeDetectDisabled(i.Path)
for _, f := range dir { for _, f := range dir {
name := f.Name() name := f.Name()
fPath := path.Join(i.Path, name) fPath := path.Join(i.Path, name)
@ -262,15 +269,11 @@ func (i *FileInfo) readListing(checker rules.Checker) error {
} else { } else {
listing.NumFiles++ listing.NumFiles++
if detectDisabled { err := file.detectType(true, false, checker.ReadHeader())
file.Type = "blob"
} else {
err := file.detectType(true, false)
if err != nil { if err != nil {
return err return err
} }
} }
}
listing.Items = append(listing.Items, file) listing.Items = append(listing.Items, file)
} }

View File

@ -1,56 +0,0 @@
<template>
<form class="rules small">
<div v-for="(disableTypeDetection, index) in disableTypeDetections" :key="index">
<input type="checkbox" v-model="disableTypeDetection.regex"><label>Regex</label>
<input
@keypress.enter.prevent
type="text"
v-if="disableTypeDetection.regex"
v-model="disableTypeDetection.regexp.raw"
:placeholder="$t('settings.insertRegex')" />
<input
@keypress.enter.prevent
type="text"
v-else
v-model="disableTypeDetection.path"
:placeholder="$t('settings.insertPath')" />
<button class="button button--red" @click="remove($event, index)">-</button>
</div>
<div>
<button class="button" @click="create" default="false">{{ $t('buttons.new') }}</button>
</div>
</form>
</template>
<script>
export default {
name: 'disableTypeDetections-textarea',
props: ['disableTypeDetections'],
methods: {
remove (event, index) {
event.preventDefault()
let disableTypeDetections = [ ...this.disableTypeDetections ]
disableTypeDetections.splice(index, 1)
this.$emit('update:disableTypeDetections', [ ...disableTypeDetections ])
},
create (event) {
event.preventDefault()
this.$emit('update:disableTypeDetections', [
...this.disableTypeDetections,
{
allow: true,
path: '',
regex: false,
regexp: {
raw: ''
}
}
])
}
}
}
</script>

View File

@ -36,19 +36,12 @@
<p class="small">{{ $t('settings.rulesHelp') }}</p> <p class="small">{{ $t('settings.rulesHelp') }}</p>
<rules :rules.sync="user.rules" /> <rules :rules.sync="user.rules" />
</div> </div>
<div v-if="!isDefault">
<h3>{{ $t('settings.disableTypeDetections') }}</h3>
<p class="small">{{ $t('settings.disableTypeDetectionsHelp') }}</p>
<disableTypeDetections :disableTypeDetections.sync="user.disableTypeDetections" />
</div>
</div> </div>
</template> </template>
<script> <script>
import Languages from './Languages' import Languages from './Languages'
import Rules from './Rules' import Rules from './Rules'
import DisableTypeDetections from './DisableTypeDetections'
import Permissions from './Permissions' import Permissions from './Permissions'
import Commands from './Commands' import Commands from './Commands'
import { enableExec } from '@/utils/constants' import { enableExec } from '@/utils/constants'
@ -59,7 +52,6 @@ export default {
Permissions, Permissions,
Languages, Languages,
Rules, Rules,
DisableTypeDetections,
Commands Commands
}, },
props: [ 'user', 'isNew', 'isDefault' ], props: [ 'user', 'isNew', 'isDefault' ],

View File

@ -161,8 +161,6 @@
"ruleExample2": "blocks the access to the file named Caddyfile on the root of the scope.", "ruleExample2": "blocks the access to the file named Caddyfile on the root of the scope.",
"rules": "Rules", "rules": "Rules",
"rulesHelp": "Here you can define a set of allow and disallow rules for this specific user. The blocked files won't show up in the listings and they wont be accessible to the user. We support regex and paths relative to the users scope.\n", "rulesHelp": "Here you can define a set of allow and disallow rules for this specific user. The blocked files won't show up in the listings and they wont be accessible to the user. We support regex and paths relative to the users scope.\n",
"disableTypeDetections": "Disable File Type Detections",
"disableTypeDetectionsHelp": "Here you can define a set of paths to disable file type detections for this specific user. Useful when the path is a network drive or has a lot of files. We support regex and paths relative to the users scope.\n",
"scope": "Scope", "scope": "Scope",
"settingsUpdated": "Settings updated!", "settingsUpdated": "Settings updated!",
"user": "User", "user": "User",

View File

@ -160,8 +160,6 @@
"ruleExample2": "阻止用户访问其目录范围的根目录下名为 Caddyfile 的文件。", "ruleExample2": "阻止用户访问其目录范围的根目录下名为 Caddyfile 的文件。",
"rules": "规则", "rules": "规则",
"rulesHelp": "您可以为该用户制定一组黑名单或白名单式的规则,被屏蔽的文件将不会显示在列表中,用户也无权限访问,支持相对于目录范围的路径。", "rulesHelp": "您可以为该用户制定一组黑名单或白名单式的规则,被屏蔽的文件将不会显示在列表中,用户也无权限访问,支持相对于目录范围的路径。",
"disableTypeDetections": "关闭文件类型检测",
"disableTypeDetectionsHelp": "您可以为该用户制定一组不会检测文件类型的路径,当路径是挂载的网络磁盘或者文件较多时能加快反应速度,支持相对于目录范围的路径。",
"scope": "目录范围", "scope": "目录范围",
"settingsUpdated": "设置已更新!", "settingsUpdated": "设置已更新!",
"user": "用户", "user": "用户",

View File

@ -91,7 +91,6 @@ export default {
username: '', username: '',
passsword: '', passsword: '',
rules: [], rules: [],
disableTypeDetections: [],
lockPassword: false, lockPassword: false,
id: 0 id: 0
} }

View File

@ -35,7 +35,6 @@ type User struct {
Fs afero.Fs `json:"-" yaml:"-"` Fs afero.Fs `json:"-" yaml:"-"`
Rules []rules.Rule `json:"rules"` Rules []rules.Rule `json:"rules"`
HideDotfiles bool `json:"hideDotfiles"` HideDotfiles bool `json:"hideDotfiles"`
DisableTypeDetections []rules.Rule `json:"disableTypeDetections"`
} }
// GetRules implements rules.Provider. // GetRules implements rules.Provider.
@ -51,7 +50,6 @@ var checkableFields = []string{
"Commands", "Commands",
"Sorting", "Sorting",
"Rules", "Rules",
"DisableTypeDetections",
} }
// Clean cleans up a user and verifies if all its fields // Clean cleans up a user and verifies if all its fields
@ -88,10 +86,6 @@ func (u *User) Clean(baseScope string, fields ...string) error {
if u.Rules == nil { if u.Rules == nil {
u.Rules = []rules.Rule{} u.Rules = []rules.Rule{}
} }
case "DisableTypeDetections":
if u.DisableTypeDetections == nil {
u.DisableTypeDetections = []rules.Rule{}
}
} }
} }