💬: add some more comments
License: MIT Signed-off-by: Henrique Dias <hacdias@gmail.com>
This commit is contained in:
parent
b94e134cf7
commit
38bcc3b992
@ -2,7 +2,8 @@ package bolt
|
|||||||
|
|
||||||
import "github.com/asdine/storm"
|
import "github.com/asdine/storm"
|
||||||
|
|
||||||
// Backend implements lib.StorageBackend.
|
// Backend implements lib.StorageBackend
|
||||||
|
// using Bolt DB.
|
||||||
type Backend struct {
|
type Backend struct {
|
||||||
DB *storm.DB
|
DB *storm.DB
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,22 +31,21 @@ var defaultEvents = []string{
|
|||||||
"delete",
|
"delete",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FileBrowser represents a File Browser instance which must
|
||||||
|
// be created through NewFileBrowser.
|
||||||
type FileBrowser struct {
|
type FileBrowser struct {
|
||||||
settings *Settings
|
settings *Settings
|
||||||
storage StorageBackend
|
storage StorageBackend
|
||||||
mux sync.RWMutex
|
mux sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FileBrowser) RLockSettings() {
|
// NewFileBrowser creates a new File Browser instance from a
|
||||||
f.mux.RLock()
|
// storage backend. If that backend doesn't contain settings
|
||||||
}
|
// on it (returns ErrNotExist), then we generate a new key
|
||||||
|
// and base settings.
|
||||||
func (f *FileBrowser) RUnlockSettings() {
|
|
||||||
f.mux.RUnlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewFileBrowser(backend StorageBackend) (*FileBrowser, error) {
|
func NewFileBrowser(backend StorageBackend) (*FileBrowser, error) {
|
||||||
settings, err := backend.GetSettings()
|
settings, err := backend.GetSettings()
|
||||||
|
|
||||||
if err == ErrNotExist {
|
if err == ErrNotExist {
|
||||||
var key []byte
|
var key []byte
|
||||||
key, err = generateRandomBytes(64)
|
key, err = generateRandomBytes(64)
|
||||||
@ -56,7 +55,6 @@ func NewFileBrowser(backend StorageBackend) (*FileBrowser, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
settings = &Settings{Key: key}
|
settings = &Settings{Key: key}
|
||||||
|
|
||||||
err = backend.SaveSettings(settings)
|
err = backend.SaveSettings(settings)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,6 +68,16 @@ func NewFileBrowser(backend StorageBackend) (*FileBrowser, error) {
|
|||||||
}, nil
|
}, 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
|
// RulesCheck matches a path against the user rules and the
|
||||||
// global rules. Returns true if allowed, false if not.
|
// global rules. Returns true if allowed, false if not.
|
||||||
func (f *FileBrowser) RulesCheck(u *User, path string) bool {
|
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
|
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) {
|
func (f *FileBrowser) ParseCommand(raw string) ([]string, error) {
|
||||||
f.RLockSettings()
|
f.RLockSettings()
|
||||||
defer f.RUnlockSettings()
|
defer f.RUnlockSettings()
|
||||||
@ -149,18 +159,21 @@ func (f *FileBrowser) ParseCommand(raw string) ([]string, error) {
|
|||||||
return command, nil
|
return command, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApplyDefaults applies defaults to a user.
|
// ApplyDefaults applies the default options to a user.
|
||||||
func (f *FileBrowser) ApplyDefaults(u *User) {
|
func (f *FileBrowser) ApplyDefaults(u *User) {
|
||||||
f.mux.RLock()
|
f.RLockSettings()
|
||||||
u.Scope = f.settings.Defaults.Scope
|
u.Scope = f.settings.Defaults.Scope
|
||||||
u.Locale = f.settings.Defaults.Locale
|
u.Locale = f.settings.Defaults.Locale
|
||||||
u.ViewMode = f.settings.Defaults.ViewMode
|
u.ViewMode = f.settings.Defaults.ViewMode
|
||||||
u.Perm = f.settings.Defaults.Perm
|
u.Perm = f.settings.Defaults.Perm
|
||||||
u.Sorting = f.settings.Defaults.Sorting
|
u.Sorting = f.settings.Defaults.Sorting
|
||||||
u.Commands = f.settings.Defaults.Commands
|
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) {
|
func (f *FileBrowser) NewFile(path string, user *User) (*File, error) {
|
||||||
if !f.RulesCheck(user, path) {
|
if !f.RulesCheck(user, path) {
|
||||||
return nil, os.ErrPermission
|
return nil, os.ErrPermission
|
||||||
@ -197,6 +210,8 @@ func (f *FileBrowser) NewFile(path string, user *User) (*File, error) {
|
|||||||
return file, err
|
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 {
|
func (f *FileBrowser) Checksum(file *File, user *User, algo string) error {
|
||||||
if file.IsDir {
|
if file.IsDir {
|
||||||
return ErrIsDirectory
|
return ErrIsDirectory
|
||||||
|
|||||||
@ -99,10 +99,12 @@ func (f *FileBrowser) DeleteUser(id interface{}) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetSettings returns the settings for the current instance.
|
||||||
func (f *FileBrowser) GetSettings() *Settings {
|
func (f *FileBrowser) GetSettings() *Settings {
|
||||||
return f.settings
|
return f.settings
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SaveSettings saves the settings for the current instance.
|
||||||
func (f *FileBrowser) SaveSettings(s *Settings) error {
|
func (f *FileBrowser) SaveSettings(s *Settings) error {
|
||||||
s.BaseURL = strings.TrimSuffix(s.BaseURL, "/")
|
s.BaseURL = strings.TrimSuffix(s.BaseURL, "/")
|
||||||
|
|
||||||
@ -155,7 +157,7 @@ func (f *FileBrowser) SaveSettings(s *Settings) error {
|
|||||||
return nil
|
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) {
|
func (f *FileBrowser) GetAuther(t AuthMethod) (Auther, error) {
|
||||||
auther, err := f.storage.GetAuther(t)
|
auther, err := f.storage.GetAuther(t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -166,32 +168,32 @@ func (f *FileBrowser) GetAuther(t AuthMethod) (Auther, error) {
|
|||||||
return auther, nil
|
return auther, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveAuther wraps a Storage.SaveAuther
|
// SaveAuther wraps a StorageBackend.SaveAuther.
|
||||||
func (f *FileBrowser) SaveAuther(a Auther) error {
|
func (f *FileBrowser) SaveAuther(a Auther) error {
|
||||||
return f.storage.SaveAuther(a)
|
return f.storage.SaveAuther(a)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLinkByHash wraps a Storage.GetLinkByHash.
|
// GetLinkByHash wraps a StorageBackend.GetLinkByHash.
|
||||||
func (f *FileBrowser) GetLinkByHash(hash string) (*ShareLink, error) {
|
func (f *FileBrowser) GetLinkByHash(hash string) (*ShareLink, error) {
|
||||||
return f.storage.GetLinkByHash(hash)
|
return f.storage.GetLinkByHash(hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLinkPermanent wraps a Storage.GetLinkPermanent
|
// GetLinkPermanent wraps a StorageBackend.GetLinkPermanent
|
||||||
func (f *FileBrowser) GetLinkPermanent(path string) (*ShareLink, error) {
|
func (f *FileBrowser) GetLinkPermanent(path string) (*ShareLink, error) {
|
||||||
return f.storage.GetLinkPermanent(path)
|
return f.storage.GetLinkPermanent(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLinksByPath wraps a Storage.GetLinksByPath
|
// GetLinksByPath wraps a StorageBackend.GetLinksByPath
|
||||||
func (f *FileBrowser) GetLinksByPath(path string) ([]*ShareLink, error) {
|
func (f *FileBrowser) GetLinksByPath(path string) ([]*ShareLink, error) {
|
||||||
return f.storage.GetLinksByPath(path)
|
return f.storage.GetLinksByPath(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveLink wraps a Storage.SaveLink
|
// SaveLink wraps a StorageBackend.SaveLink
|
||||||
func (f *FileBrowser) SaveLink(s *ShareLink) error {
|
func (f *FileBrowser) SaveLink(s *ShareLink) error {
|
||||||
return f.storage.SaveLink(s)
|
return f.storage.SaveLink(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteLink wraps a Storage.DeleteLink
|
// DeleteLink wraps a StorageBackend.DeleteLink
|
||||||
func (f *FileBrowser) DeleteLink(hash string) error {
|
func (f *FileBrowser) DeleteLink(hash string) error {
|
||||||
return f.storage.DeleteLink(hash)
|
return f.storage.DeleteLink(hash)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user