💬: add some more comments

License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
This commit is contained in:
Henrique Dias 2019-01-04 09:03:57 +00:00
parent b94e134cf7
commit 38bcc3b992
3 changed files with 39 additions and 21 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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)
}