Documentation ¶
Overview ¶
Package filebrowser provides a web interface to access your files wherever you are. To use this package as a middleware for your app, you'll need to import both File Browser and File Browser HTTP packages.
import ( fm "github.com/filebrowser/filebrowser" h "github.com/filebrowser/filebrowser/web" )
Then, you should create a new FileBrowser object with your options. In this case, I'm using BoltDB (via Storm package) as a Store. So, you'll also need to import "github.com/filebrowser/filebrowser/bolt".
db, _ := storm.Open("bolt.db") m := &fm.FileBrowser{ NoAuth: false, Auth: { Method: "default", LoginHeader: "X-Fowarded-User" }, DefaultUser: &fm.User{ AllowCommands: true, AllowEdit: true, AllowNew: true, AllowPublish: true, Commands: []string{"git"}, Rules: []*fm.Rule{}, Locale: "en", CSS: "", Scope: ".", FileSystem: fileutils.Dir("."), }, Store: &fm.Store{ Config: bolt.ConfigStore{DB: db}, Users: bolt.UsersStore{DB: db}, Share: bolt.ShareStore{DB: db}, }, NewFS: func(scope string) fm.FileSystem { return fileutils.Dir(scope) }, }
The credentials for the first user are always 'admin' for both the user and the password, and they can be changed later through the settings. The first user is always an Admin and has all of the permissions set to 'true'.
Then, you should set the Prefix URL and the Base URL, using the following functions:
m.SetBaseURL("/") m.SetPrefixURL("/")
The Prefix URL is a part of the path that is already stripped from the r.URL.Path variable before the request arrives to File Browser's handler. This is a function that will rarely be used. You can see one example on Caddy filemanager plugin.
The Base URL is the URL path where you want File Browser to be available in. If you want to be available at the root path, you should call:
m.SetBaseURL("/")
But if you want to access it at '/admin', you would call:
m.SetBaseURL("/admin")
Now, that you already have a File Browser instance created, you just need to add it to your handlers using m.ServeHTTP which is compatible to web.Handler. We also have a m.ServeWithErrorsHTTP that returns the status code and an error.
One simple implementation for this, at port 80, in the root of the domain, would be:
web.ListenAndServe(":80", h.Handler(m))
Index ¶
Constants ¶
const ( // Version is the current File Browser version. Version = "(untracked)" MosaicViewMode = "mosaic" )
Variables ¶
var ( ErrExist = errors.New("the resource already exists") ErrNotExist = errors.New("the resource does not exist") ErrEmptyRequest = errors.New("request body is empty") ErrEmptyPassword = errors.New("password is empty") ErrEmptyUsername = errors.New("username is empty") ErrEmptyScope = errors.New("scope is empty") ErrWrongDataType = errors.New("wrong data type") ErrInvalidUpdateField = errors.New("invalid field to update") ErrInvalidOption = errors.New("invalid option") )
var DefaultUser = UserModel{ UserConfig: &config.UserConfig{ AllowEdit: true, AllowNew: true, LockPassword: false, Admin: true, Locale: "", Scope: "./scope", ViewMode: "mosaic", PreviewScope: "./preview", }, FileSystem: fileutils.Dir("."), FileSystemPreview: fileutils.Dir("."), }
DefaultUser is used on New, when no 'base' user is provided.
Functions ¶
func CheckPasswordHash ¶
CheckPasswordHash compares a password with an hash to check if they match.
func GenerateRandomBytes ¶
GenerateRandomBytes returns securely generated random bytes. It will return an fm.Error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.
func HashPassword ¶
HashPassword generates an hash from a password using bcrypt.
Types ¶
type Context ¶
type Context struct { *FileBrowser User *UserModel File *File // On API handlers, Router is the APi handler we want. Router string //indicate that requested preview PreviewType string //return files list by recursion IsRecursive bool ShareUser string }
Context contains the needed information to make handlers work.
func (*Context) GenPreview ¶
type File ¶
type File struct { // Indicates the Kind of view on the front-end (Listing, editor or preview). Kind string `json:"kind"` // The name of the file. Name string `json:"name"` // The Size of the file. Size int64 `json:"size"` // The absolute URL. URL string `json:"url"` // The extension of the file. Extension string `json:"extension"` // The last modified time. ModTime time.Time `json:"modified"` // The File Mode. Mode os.FileMode `json:"mode"` // Indicates if this file is a directory. IsDir bool `json:"isDir"` // Absolute path. Path string `json:"-"` // Relative path to user's virtual File System. VirtualPath string `json:"-"` // Indicates the file content type: video, text, image, music or blob. Type string `json:"type"` // Stores the content of a text file. Content string `json:"content,omitempty"` Checksums map[string]string `json:"checksums,omitempty"` *Listing `json:",omitempty"` Language string `json:"language,omitempty"` }
File contains the information about a particular file or directory.
func GetInfo ¶
GetInfo gets the file information and, in case of error, returns the respective HTTP error code
func (File) CanBeEdited ¶
CanBeEdited checks if the extension of a file is supported by the editor
func (*File) GetListing ¶
GetListing gets the information about a specific directory and its files.
func (*File) SetFileType ¶
SetFileType obtains the mimetype and converts it to a simple type nomenclature.
type FileBrowser ¶
type FileBrowser struct { // The static assets. Assets *rice.Box // PrefixURL is a part of the URL that is already trimmed from the request URL before it // arrives to our handlers. It may be useful when using File Browser as a middleware // such as in caddy-filemanager plugin. It is only useful in certain situations. PrefixURL string // BaseURL is the path where the GUI will be accessible. It musn't end with // a trailing slash and mustn't contain PrefixURL, if set. It shouldn't be // edited directly. Use SetBaseURL. BaseURL string // ReCaptcha host, key and secret. ReCaptcha *ReCaptcha // Global stylesheet. CSS string // NewFS should build a new file system for a given path. NewFS FSBuilder //generates preview Pgen *preview.PreviewGen Config *config.GlobalConfig }
FileBrowser is a file manager instance. It should be creating using the 'New' function and not directly.
func (FileBrowser) RootURL ¶
func (m FileBrowser) RootURL() string
RootURL returns the actual URL where File Browser interface can be accessed.
func (*FileBrowser) SetBaseURL ¶
func (m *FileBrowser) SetBaseURL(url string)
SetBaseURL updates the baseURL of a File Browser object.
func (*FileBrowser) SetPrefixURL ¶
func (m *FileBrowser) SetPrefixURL(url string)
SetPrefixURL updates the prefixURL of a File Manager object.
func (*FileBrowser) Setup ¶
func (fb *FileBrowser) Setup() (bool, error)
Setup loads the configuration from the database and configures the Assets and the Cron job. It must always be run after creating a File Browser object.
type FileSystem ¶
type FileSystem interface { Mkdir(name string, perm os.FileMode, uid, gid int) error OpenFile(name string, flag int, perm os.FileMode, uid, gid int) (*os.File, error) RemoveAll(name string) error Rename(oldName, newName string) error Stat(name string) (os.FileInfo, error) Copy(src, dst string, uid, gid int) error }
FileSystem is the interface to work with the file system.
type Listing ¶
type Listing struct { // The items (files and folders) in the path. Items []*File `json:"items"` // The number of directories in the Listing. NumDirs int `json:"numDirs"` // The number of files (items that aren't directories) in the Listing. NumFiles int `json:"numFiles"` // Which sorting order is used. Sort string `json:"sort"` // And which order. Order string `json:"order"` //indicator to the frontend, to prevent request previews AllowGeneratePreview bool `json:"allowGeneratePreview"` }
A Listing is the context used to fill out a template.
type UserModel ¶
type UserModel struct { *config.UserConfig ID string `json:"ID"` // FileSystem is the virtual file system the user has access. FileSystem FileSystem `json:"-"` // FileSystem is the virtual file system the user has access, uses to store previews. FileSystemPreview FileSystem `json:"-"` }