Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implemented hideDotfiles feature in the backend #1148

Merged
merged 5 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions frontend/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"toggleSidebar": "Toggle sidebar",
"update": "Update",
"upload": "Upload",
"permalink": "Get Permanent Link"
"permalink": "Get Permanent Link",
"hideDotfiles": "Hide dotfiles"
},
"success": {
"linkCopied": "Link copied!"
Expand Down Expand Up @@ -188,7 +189,8 @@
"execute": "Execute commands",
"rename": "Rename or move files and directories",
"share": "Share files"
}
},
"hideDotfiles": "Hide dotfiles"
},
"sidebar": {
"help": "Help",
Expand Down Expand Up @@ -244,4 +246,4 @@
"downloadFile": "Download File",
"downloadFolder": "Download Folder"
}
}
}
6 changes: 4 additions & 2 deletions frontend/src/views/settings/Profile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</div>

<div class="card-content">
<p><input type="checkbox" v-model="hideDotfiles"> {{ $t('settings.hideDotfiles') }}</p>
<h3>{{ $t('settings.language') }}</h3>
<languages class="input input--block" :locale.sync="locale"></languages>
</div>
Expand Down Expand Up @@ -67,6 +68,7 @@ export default {
},
created () {
this.locale = this.user.locale
this.hideDotfiles = this.user.hideDotfiles
},
methods: {
...mapMutations([ 'updateUser' ]),
Expand All @@ -90,8 +92,8 @@ export default {
event.preventDefault()

try {
const data = { id: this.user.id, locale: this.locale }
await api.update(data, ['locale'])
const data = { id: this.user.id, locale: this.locale, hideDotfiles: this.hideDotfiles }
await api.update(data, ['locale', 'hideDotfiles'])
this.updateUser(data)
this.$showSuccess(this.$t('settings.settingsUpdated'))
} catch (e) {
Expand Down
2 changes: 2 additions & 0 deletions http/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type userInfo struct {
Perm users.Permissions `json:"perm"`
Commands []string `json:"commands"`
LockPassword bool `json:"lockPassword"`
HideDotfiles bool `json:"hideDotfiles"`
}

type authToken struct {
Expand Down Expand Up @@ -175,6 +176,7 @@ func printToken(w http.ResponseWriter, _ *http.Request, d *data, user *users.Use
Perm: user.Perm,
LockPassword: user.LockPassword,
Commands: user.Commands,
HideDotfiles: user.HideDotfiles,
},
StandardClaims: jwt.StandardClaims{
IssuedAt: time.Now().Unix(),
Expand Down
5 changes: 5 additions & 0 deletions http/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/tomasen/realip"

"github.com/filebrowser/filebrowser/v2/rules"
"github.com/filebrowser/filebrowser/v2/runner"
"github.com/filebrowser/filebrowser/v2/settings"
"github.com/filebrowser/filebrowser/v2/storage"
Expand All @@ -26,6 +27,10 @@ type data struct {

// Check implements rules.Checker.
func (d *data) Check(path string) bool {
if d.user.HideDotfiles && rules.MatchHidden(path) {
return false
}

allow := true
for _, rule := range d.settings.Rules {
if rule.Matches(path) {
Expand Down
7 changes: 7 additions & 0 deletions rules/rules.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rules

import (
"path/filepath"
"regexp"
"strings"
)
Expand All @@ -18,6 +19,12 @@ type Rule struct {
Regexp *Regexp `json:"regexp"`
}

// MatchHidden matches paths with a basename
// that begins with a dot.
func MatchHidden(path string) bool {
return strings.HasPrefix(filepath.Base(path), ".")
}

// Matches matches a path against a rule.
func (r *Rule) Matches(path string) bool {
if r.Regex {
Expand Down
23 changes: 23 additions & 0 deletions rules/rules_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package rules

import "testing"

func TestMatchHidden(t *testing.T) {
cases := map[string]bool{
"/": false,
"/src": false,
"/src/": false,
"/.circleci": true,
"/a/b/c/.docker.json": true,
".docker.json": true,
"Dockerfile": false,
"/Dockerfile": false,
}

for path, want := range cases {
got := MatchHidden(path)
if got != want {
t.Errorf("MatchHidden(%s)=%v; want %v", path, got, want)
}
}
}
14 changes: 8 additions & 6 deletions settings/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
// UserDefaults is a type that holds the default values
// for some fields on User.
type UserDefaults struct {
Scope string `json:"scope"`
Locale string `json:"locale"`
ViewMode users.ViewMode `json:"viewMode"`
Sorting files.Sorting `json:"sorting"`
Perm users.Permissions `json:"perm"`
Commands []string `json:"commands"`
Scope string `json:"scope"`
Locale string `json:"locale"`
ViewMode users.ViewMode `json:"viewMode"`
Sorting files.Sorting `json:"sorting"`
Perm users.Permissions `json:"perm"`
Commands []string `json:"commands"`
HideDotfiles bool `json:"hideDotfiles"`
}

// Apply applies the default options to a user.
Expand All @@ -24,4 +25,5 @@ func (d *UserDefaults) Apply(u *users.User) {
u.Perm = d.Perm
u.Sorting = d.Sorting
u.Commands = d.Commands
u.HideDotfiles = d.HideDotfiles
}
1 change: 1 addition & 0 deletions users/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type User struct {
Sorting files.Sorting `json:"sorting"`
Fs afero.Fs `json:"-" yaml:"-"`
Rules []rules.Rule `json:"rules"`
HideDotfiles bool `json:"hideDotfiles"`
}

// GetRules implements rules.Provider.
Expand Down