From 38bcc3b99242c619ca529d7dd6042fe3a2aae967 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 4 Jan 2019 09:03:57 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=AC:=20add=20some=20more=20comments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Henrique Dias --- bolt/bolt.go | 3 ++- lib/filebrowser.go | 41 ++++++++++++++++++++++++++++------------- lib/storage.go | 16 +++++++++------- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/bolt/bolt.go b/bolt/bolt.go index 2bd3d08e..70c8e369 100644 --- a/bolt/bolt.go +++ b/bolt/bolt.go @@ -2,7 +2,8 @@ package bolt import "github.com/asdine/storm" -// Backend implements lib.StorageBackend. +// Backend implements lib.StorageBackend +// using Bolt DB. type Backend struct { DB *storm.DB } diff --git a/lib/filebrowser.go b/lib/filebrowser.go index 736fed58..38fff549 100644 --- a/lib/filebrowser.go +++ b/lib/filebrowser.go @@ -31,22 +31,21 @@ var defaultEvents = []string{ "delete", } +// FileBrowser represents a File Browser instance which must +// be created through NewFileBrowser. type FileBrowser struct { settings *Settings storage StorageBackend mux sync.RWMutex } -func (f *FileBrowser) RLockSettings() { - f.mux.RLock() -} - -func (f *FileBrowser) RUnlockSettings() { - f.mux.RUnlock() -} - +// NewFileBrowser creates a new File Browser instance from a +// storage backend. If that backend doesn't contain settings +// on it (returns ErrNotExist), then we generate a new key +// and base settings. func NewFileBrowser(backend StorageBackend) (*FileBrowser, error) { settings, err := backend.GetSettings() + if err == ErrNotExist { var key []byte key, err = generateRandomBytes(64) @@ -56,7 +55,6 @@ func NewFileBrowser(backend StorageBackend) (*FileBrowser, error) { } settings = &Settings{Key: key} - err = backend.SaveSettings(settings) } @@ -70,6 +68,16 @@ func NewFileBrowser(backend StorageBackend) (*FileBrowser, error) { }, nil } +// RLockSettings locks the settings for reading. +func (f *FileBrowser) RLockSettings() { + f.mux.RLock() +} + +// RUnlockSettings unlocks the settings for reading. +func (f *FileBrowser) RUnlockSettings() { + f.mux.RUnlock() +} + // RulesCheck matches a path against the user rules and the // global rules. Returns true if allowed, false if not. func (f *FileBrowser) RulesCheck(u *User, path string) bool { @@ -122,7 +130,9 @@ func (f *FileBrowser) RunHook(fn func() error, evt, path, dst string, user *User return nil } -// ParseCommand parses the command taking in account +// ParseCommand parses the command taking in account if the current +// instance uses a shell to run the commands or just calls the binary +// directyly. func (f *FileBrowser) ParseCommand(raw string) ([]string, error) { f.RLockSettings() defer f.RUnlockSettings() @@ -149,18 +159,21 @@ func (f *FileBrowser) ParseCommand(raw string) ([]string, error) { return command, nil } -// ApplyDefaults applies defaults to a user. +// ApplyDefaults applies the default options to a user. func (f *FileBrowser) ApplyDefaults(u *User) { - f.mux.RLock() + f.RLockSettings() u.Scope = f.settings.Defaults.Scope u.Locale = f.settings.Defaults.Locale u.ViewMode = f.settings.Defaults.ViewMode u.Perm = f.settings.Defaults.Perm u.Sorting = f.settings.Defaults.Sorting u.Commands = f.settings.Defaults.Commands - f.mux.RUnlock() + f.RUnlockSettings() } +// NewFile creates a File object from a path and a given user. This File +// object will be automatically filled depending on if it is a directory +// or a file. If it's a video file, it will also detect any subtitles. func (f *FileBrowser) NewFile(path string, user *User) (*File, error) { if !f.RulesCheck(user, path) { return nil, os.ErrPermission @@ -197,6 +210,8 @@ func (f *FileBrowser) NewFile(path string, user *User) (*File, error) { return file, err } +// Checksum checksums a given File for a given User, using a specific +// algorithm. The checksums data is saved on File object. func (f *FileBrowser) Checksum(file *File, user *User, algo string) error { if file.IsDir { return ErrIsDirectory diff --git a/lib/storage.go b/lib/storage.go index 54d3670b..b3d563d6 100644 --- a/lib/storage.go +++ b/lib/storage.go @@ -99,10 +99,12 @@ func (f *FileBrowser) DeleteUser(id interface{}) (err error) { return } +// GetSettings returns the settings for the current instance. func (f *FileBrowser) GetSettings() *Settings { return f.settings } +// SaveSettings saves the settings for the current instance. func (f *FileBrowser) SaveSettings(s *Settings) error { s.BaseURL = strings.TrimSuffix(s.BaseURL, "/") @@ -155,7 +157,7 @@ func (f *FileBrowser) SaveSettings(s *Settings) error { return nil } -// GetAuther wraps a Storage.GetAuther +// GetAuther wraps a StorageBackend.GetAuther and calls SetInstance on the auther. func (f *FileBrowser) GetAuther(t AuthMethod) (Auther, error) { auther, err := f.storage.GetAuther(t) if err != nil { @@ -166,32 +168,32 @@ func (f *FileBrowser) GetAuther(t AuthMethod) (Auther, error) { return auther, nil } -// SaveAuther wraps a Storage.SaveAuther +// SaveAuther wraps a StorageBackend.SaveAuther. func (f *FileBrowser) SaveAuther(a Auther) error { return f.storage.SaveAuther(a) } -// GetLinkByHash wraps a Storage.GetLinkByHash. +// GetLinkByHash wraps a StorageBackend.GetLinkByHash. func (f *FileBrowser) GetLinkByHash(hash string) (*ShareLink, error) { return f.storage.GetLinkByHash(hash) } -// GetLinkPermanent wraps a Storage.GetLinkPermanent +// GetLinkPermanent wraps a StorageBackend.GetLinkPermanent func (f *FileBrowser) GetLinkPermanent(path string) (*ShareLink, error) { return f.storage.GetLinkPermanent(path) } -// GetLinksByPath wraps a Storage.GetLinksByPath +// GetLinksByPath wraps a StorageBackend.GetLinksByPath func (f *FileBrowser) GetLinksByPath(path string) ([]*ShareLink, error) { return f.storage.GetLinksByPath(path) } -// SaveLink wraps a Storage.SaveLink +// SaveLink wraps a StorageBackend.SaveLink func (f *FileBrowser) SaveLink(s *ShareLink) error { return f.storage.SaveLink(s) } -// DeleteLink wraps a Storage.DeleteLink +// DeleteLink wraps a StorageBackend.DeleteLink func (f *FileBrowser) DeleteLink(hash string) error { return f.storage.DeleteLink(hash) }