Almost working!
parent
a04ff87bf9
commit
5b619337df
|
@ -2,6 +2,7 @@ package bolt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/asdine/storm"
|
"github.com/asdine/storm"
|
||||||
|
fm "github.com/hacdias/filemanager"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ConfigStore struct {
|
type ConfigStore struct {
|
||||||
|
@ -9,7 +10,12 @@ type ConfigStore struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c ConfigStore) Get(name string, to interface{}) error {
|
func (c ConfigStore) Get(name string, to interface{}) error {
|
||||||
return c.DB.Get("config", name, to)
|
err := c.DB.Get("config", name, to)
|
||||||
|
if err == storm.ErrNotFound {
|
||||||
|
return fm.ErrNotExist
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c ConfigStore) Save(name string, from interface{}) error {
|
func (c ConfigStore) Save(name string, from interface{}) error {
|
||||||
|
|
|
@ -12,16 +12,24 @@ type ShareStore struct {
|
||||||
func (s ShareStore) Get(hash string) (*fm.ShareLink, error) {
|
func (s ShareStore) Get(hash string) (*fm.ShareLink, error) {
|
||||||
var v *fm.ShareLink
|
var v *fm.ShareLink
|
||||||
err := s.DB.One("Hash", hash, &v)
|
err := s.DB.One("Hash", hash, &v)
|
||||||
|
if err == storm.ErrNotFound {
|
||||||
|
return v, fm.ErrNotExist
|
||||||
|
}
|
||||||
|
|
||||||
return v, err
|
return v, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s ShareStore) GetByPath(hash string) ([]*fm.ShareLink, error) {
|
func (s ShareStore) GetByPath(hash string) ([]*fm.ShareLink, error) {
|
||||||
var v []*fm.ShareLink
|
var v []*fm.ShareLink
|
||||||
err := s.DB.Find("Path", hash, &v)
|
err := s.DB.Find("Path", hash, &v)
|
||||||
|
if err == storm.ErrNotFound {
|
||||||
|
return v, fm.ErrNotExist
|
||||||
|
}
|
||||||
|
|
||||||
return v, err
|
return v, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s ShareStore) Gets(hash string) ([]*fm.ShareLink, error) {
|
func (s ShareStore) Gets() ([]*fm.ShareLink, error) {
|
||||||
var v []*fm.ShareLink
|
var v []*fm.ShareLink
|
||||||
err := s.DB.All(&v)
|
err := s.DB.All(&v)
|
||||||
return v, err
|
return v, err
|
||||||
|
|
|
@ -15,7 +15,7 @@ func (u UsersStore) Get(id int) (*fm.User, error) {
|
||||||
var us *fm.User
|
var us *fm.User
|
||||||
err := u.DB.One("ID", id, us)
|
err := u.DB.One("ID", id, us)
|
||||||
if err == storm.ErrNotFound {
|
if err == storm.ErrNotFound {
|
||||||
return nil, fm.ErrUserNotExist
|
return nil, fm.ErrNotExist
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -10,9 +10,14 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/asdine/storm"
|
||||||
|
|
||||||
lumberjack "gopkg.in/natefinch/lumberjack.v2"
|
lumberjack "gopkg.in/natefinch/lumberjack.v2"
|
||||||
|
|
||||||
"github.com/hacdias/filemanager"
|
"github.com/hacdias/filemanager"
|
||||||
|
"github.com/hacdias/filemanager/bolt"
|
||||||
|
h "github.com/hacdias/filemanager/http"
|
||||||
|
"github.com/hacdias/filemanager/staticgen"
|
||||||
"github.com/hacdias/fileutils"
|
"github.com/hacdias/fileutils"
|
||||||
flag "github.com/spf13/pflag"
|
flag "github.com/spf13/pflag"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
@ -25,7 +30,7 @@ var (
|
||||||
scope string
|
scope string
|
||||||
commands string
|
commands string
|
||||||
logfile string
|
logfile string
|
||||||
staticgen string
|
staticg string
|
||||||
locale string
|
locale string
|
||||||
port int
|
port int
|
||||||
noAuth bool
|
noAuth bool
|
||||||
|
@ -51,7 +56,7 @@ func init() {
|
||||||
flag.BoolVar(&allowNew, "allow-new", true, "Default allow new option for new users")
|
flag.BoolVar(&allowNew, "allow-new", true, "Default allow new option for new users")
|
||||||
flag.BoolVar(&noAuth, "no-auth", false, "Disables authentication")
|
flag.BoolVar(&noAuth, "no-auth", false, "Disables authentication")
|
||||||
flag.StringVar(&locale, "locale", "en", "Default locale for new users")
|
flag.StringVar(&locale, "locale", "en", "Default locale for new users")
|
||||||
flag.StringVar(&staticgen, "staticgen", "", "Static Generator you want to enable")
|
flag.StringVar(&staticg, "staticgen", "", "Static Generator you want to enable")
|
||||||
flag.BoolVarP(&showVer, "version", "v", false, "Show version")
|
flag.BoolVarP(&showVer, "version", "v", false, "Show version")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,52 +153,6 @@ func main() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a File Manager instance.
|
|
||||||
fm, err := filemanager.New(viper.GetString("Database"), filemanager.User{
|
|
||||||
AllowCommands: viper.GetBool("AllowCommands"),
|
|
||||||
AllowEdit: viper.GetBool("AllowEdit"),
|
|
||||||
AllowNew: viper.GetBool("AllowNew"),
|
|
||||||
AllowPublish: viper.GetBool("AllowPublish"),
|
|
||||||
Commands: viper.GetStringSlice("Commands"),
|
|
||||||
Rules: []*filemanager.Rule{},
|
|
||||||
Locale: viper.GetString("Locale"),
|
|
||||||
CSS: "",
|
|
||||||
FileSystem: fileutils.Dir(viper.GetString("Scope")),
|
|
||||||
})
|
|
||||||
|
|
||||||
if viper.GetBool("NoAuth") {
|
|
||||||
fm.NoAuth = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch viper.GetString("StaticGen") {
|
|
||||||
case "hugo":
|
|
||||||
hugo := &filemanager.Hugo{
|
|
||||||
Root: viper.GetString("Scope"),
|
|
||||||
Public: filepath.Join(viper.GetString("Scope"), "public"),
|
|
||||||
Args: []string{},
|
|
||||||
CleanPublic: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = fm.EnableStaticGen(hugo); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
case "jekyll":
|
|
||||||
jekyll := &filemanager.Jekyll{
|
|
||||||
Root: viper.GetString("Scope"),
|
|
||||||
Public: filepath.Join(viper.GetString("Scope"), "_site"),
|
|
||||||
Args: []string{"build"},
|
|
||||||
CleanPublic: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = fm.EnableStaticGen(jekyll); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Builds the address and a listener.
|
// Builds the address and a listener.
|
||||||
laddr := viper.GetString("Address") + ":" + viper.GetString("Port")
|
laddr := viper.GetString("Address") + ":" + viper.GetString("Port")
|
||||||
listener, err := net.Listen("tcp", laddr)
|
listener, err := net.Listen("tcp", laddr)
|
||||||
|
@ -205,7 +164,68 @@ func main() {
|
||||||
fmt.Println("Listening on", listener.Addr().String())
|
fmt.Println("Listening on", listener.Addr().String())
|
||||||
|
|
||||||
// Starts the server.
|
// Starts the server.
|
||||||
if err := http.Serve(listener, fm); err != nil {
|
if err := http.Serve(listener, handler()); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handler() http.Handler {
|
||||||
|
db, err := storm.Open(viper.GetString("Database"))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fm := &filemanager.FileManager{
|
||||||
|
NoAuth: viper.GetBool("NoAuth"),
|
||||||
|
BaseURL: "",
|
||||||
|
PrefixURL: "",
|
||||||
|
DefaultUser: &filemanager.User{
|
||||||
|
AllowCommands: viper.GetBool("AllowCommands"),
|
||||||
|
AllowEdit: viper.GetBool("AllowEdit"),
|
||||||
|
AllowNew: viper.GetBool("AllowNew"),
|
||||||
|
AllowPublish: viper.GetBool("AllowPublish"),
|
||||||
|
Commands: viper.GetStringSlice("Commands"),
|
||||||
|
Rules: []*filemanager.Rule{},
|
||||||
|
Locale: viper.GetString("Locale"),
|
||||||
|
CSS: "",
|
||||||
|
FileSystem: fileutils.Dir(viper.GetString("Scope")),
|
||||||
|
},
|
||||||
|
Store: &filemanager.Store{
|
||||||
|
Config: bolt.ConfigStore{DB: db},
|
||||||
|
Users: bolt.UsersStore{DB: db},
|
||||||
|
Share: bolt.ShareStore{DB: db},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err = fm.Load()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch viper.GetString("StaticGen") {
|
||||||
|
case "hugo":
|
||||||
|
hugo := &staticgen.Hugo{
|
||||||
|
Root: viper.GetString("Scope"),
|
||||||
|
Public: filepath.Join(viper.GetString("Scope"), "public"),
|
||||||
|
Args: []string{},
|
||||||
|
CleanPublic: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = fm.Attach(hugo); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
case "jekyll":
|
||||||
|
jekyll := &staticgen.Jekyll{
|
||||||
|
Root: viper.GetString("Scope"),
|
||||||
|
Public: filepath.Join(viper.GetString("Scope"), "_site"),
|
||||||
|
Args: []string{"build"},
|
||||||
|
CleanPublic: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = fm.Attach(jekyll); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return h.ServeHTTP(fm)
|
||||||
|
}
|
||||||
|
|
5
file.go
5
file.go
|
@ -7,7 +7,6 @@ import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
|
||||||
"hash"
|
"hash"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -23,10 +22,6 @@ import (
|
||||||
"github.com/gohugoio/hugo/parser"
|
"github.com/gohugoio/hugo/parser"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
ErrInvalidOption = errors.New("Invalid option")
|
|
||||||
)
|
|
||||||
|
|
||||||
// File contains the information about a particular file or directory.
|
// File contains the information about a particular file or directory.
|
||||||
type File struct {
|
type File struct {
|
||||||
// Indicates the Kind of view on the front-end (Listing, editor or preview).
|
// Indicates the Kind of view on the front-end (Listing, editor or preview).
|
||||||
|
|
232
filemanager.go
232
filemanager.go
|
@ -54,21 +54,38 @@
|
||||||
package filemanager
|
package filemanager
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
|
||||||
rice "github.com/GeertJohan/go.rice"
|
rice "github.com/GeertJohan/go.rice"
|
||||||
"github.com/asdine/storm"
|
"github.com/asdine/storm"
|
||||||
|
"github.com/hacdias/fileutils"
|
||||||
"github.com/mholt/caddy"
|
"github.com/mholt/caddy"
|
||||||
"github.com/robfig/cron"
|
"github.com/robfig/cron"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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")
|
||||||
|
)
|
||||||
|
|
||||||
// FileManager is a file manager instance. It should be creating using the
|
// FileManager is a file manager instance. It should be creating using the
|
||||||
// 'New' function and not directly.
|
// 'New' function and not directly.
|
||||||
type FileManager struct {
|
type FileManager struct {
|
||||||
|
@ -110,6 +127,7 @@ type FileManager struct {
|
||||||
// Command is a command function.
|
// Command is a command function.
|
||||||
type Command func(r *http.Request, m *FileManager, u *User) error
|
type Command func(r *http.Request, m *FileManager, u *User) error
|
||||||
|
|
||||||
|
// Load loads the configuration from the database.
|
||||||
func (m *FileManager) Load() error {
|
func (m *FileManager) Load() error {
|
||||||
// Creates a new File Manager instance with the Users
|
// Creates a new File Manager instance with the Users
|
||||||
// map and Assets box.
|
// map and Assets box.
|
||||||
|
@ -215,30 +233,6 @@ func (m *FileManager) SetBaseURL(url string) {
|
||||||
m.BaseURL = strings.TrimSuffix(url, "/")
|
m.BaseURL = strings.TrimSuffix(url, "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeHTTP handles the request.
|
|
||||||
func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
||||||
/* code, err := serveHTTP(&RequestContext{
|
|
||||||
FileManager: m,
|
|
||||||
User: nil,
|
|
||||||
File: nil,
|
|
||||||
}, w, r)
|
|
||||||
|
|
||||||
if code >= 400 {
|
|
||||||
w.WriteHeader(code)
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
txt := http.StatusText(code)
|
|
||||||
log.Printf("%v: %v %v\n", r.URL.Path, code, txt)
|
|
||||||
w.Write([]byte(txt))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Print(err)
|
|
||||||
w.Write([]byte(err.Error()))
|
|
||||||
} */
|
|
||||||
}
|
|
||||||
|
|
||||||
// Attach attaches a static generator to the current File Manager.
|
// Attach attaches a static generator to the current File Manager.
|
||||||
func (m *FileManager) Attach(s StaticGen) error {
|
func (m *FileManager) Attach(s StaticGen) error {
|
||||||
if reflect.TypeOf(s).Kind() != reflect.Ptr {
|
if reflect.TypeOf(s).Kind() != reflect.Ptr {
|
||||||
|
@ -329,3 +323,193 @@ func (m FileManager) Runner(event string, path string) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DefaultUser is used on New, when no 'base' user is provided.
|
||||||
|
var DefaultUser = User{
|
||||||
|
AllowCommands: true,
|
||||||
|
AllowEdit: true,
|
||||||
|
AllowNew: true,
|
||||||
|
AllowPublish: true,
|
||||||
|
Commands: []string{},
|
||||||
|
Rules: []*Rule{},
|
||||||
|
CSS: "",
|
||||||
|
Admin: true,
|
||||||
|
Locale: "en",
|
||||||
|
FileSystem: fileutils.Dir("."),
|
||||||
|
}
|
||||||
|
|
||||||
|
// User contains the configuration for each user.
|
||||||
|
type User struct {
|
||||||
|
// ID is the required primary key with auto increment0
|
||||||
|
ID int `storm:"id,increment"`
|
||||||
|
|
||||||
|
// Username is the user username used to login.
|
||||||
|
Username string `json:"username" storm:"index,unique"`
|
||||||
|
|
||||||
|
// The hashed password. This never reaches the front-end because it's temporarily
|
||||||
|
// emptied during JSON marshall.
|
||||||
|
Password string `json:"password"`
|
||||||
|
|
||||||
|
// Tells if this user is an admin.
|
||||||
|
Admin bool `json:"admin"`
|
||||||
|
|
||||||
|
// FileSystem is the virtual file system the user has access.
|
||||||
|
FileSystem fileutils.Dir `json:"filesystem"`
|
||||||
|
|
||||||
|
// Rules is an array of access and deny rules.
|
||||||
|
Rules []*Rule `json:"rules"`
|
||||||
|
|
||||||
|
// Custom styles for this user.
|
||||||
|
CSS string `json:"css"`
|
||||||
|
|
||||||
|
// Locale is the language of the user.
|
||||||
|
Locale string `json:"locale"`
|
||||||
|
|
||||||
|
// These indicate if the user can perform certain actions.
|
||||||
|
AllowNew bool `json:"allowNew"` // Create files and folders
|
||||||
|
AllowEdit bool `json:"allowEdit"` // Edit/rename files
|
||||||
|
AllowCommands bool `json:"allowCommands"` // Execute commands
|
||||||
|
AllowPublish bool `json:"allowPublish"` // Publish content (to use with static gen)
|
||||||
|
|
||||||
|
// Commands is the list of commands the user can execute.
|
||||||
|
Commands []string `json:"commands"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allowed checks if the user has permission to access a directory/file.
|
||||||
|
func (u User) Allowed(url string) bool {
|
||||||
|
var rule *Rule
|
||||||
|
i := len(u.Rules) - 1
|
||||||
|
|
||||||
|
for i >= 0 {
|
||||||
|
rule = u.Rules[i]
|
||||||
|
|
||||||
|
if rule.Regex {
|
||||||
|
if rule.Regexp.MatchString(url) {
|
||||||
|
return rule.Allow
|
||||||
|
}
|
||||||
|
} else if strings.HasPrefix(url, rule.Path) {
|
||||||
|
return rule.Allow
|
||||||
|
}
|
||||||
|
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rule is a dissalow/allow rule.
|
||||||
|
type Rule struct {
|
||||||
|
// Regex indicates if this rule uses Regular Expressions or not.
|
||||||
|
Regex bool `json:"regex"`
|
||||||
|
|
||||||
|
// Allow indicates if this is an allow rule. Set 'false' to be a disallow rule.
|
||||||
|
Allow bool `json:"allow"`
|
||||||
|
|
||||||
|
// Path is the corresponding URL path for this rule.
|
||||||
|
Path string `json:"path"`
|
||||||
|
|
||||||
|
// Regexp is the regular expression. Only use this when 'Regex' was set to true.
|
||||||
|
Regexp *Regexp `json:"regexp"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Regexp is a regular expression wrapper around native regexp.
|
||||||
|
type Regexp struct {
|
||||||
|
Raw string `json:"raw"`
|
||||||
|
regexp *regexp.Regexp
|
||||||
|
}
|
||||||
|
|
||||||
|
// MatchString checks if this string matches the regular expression.
|
||||||
|
func (r *Regexp) MatchString(s string) bool {
|
||||||
|
if r.regexp == nil {
|
||||||
|
r.regexp = regexp.MustCompile(r.Raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.regexp.MatchString(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ShareLink is the information needed to build a shareable link.
|
||||||
|
type ShareLink struct {
|
||||||
|
Hash string `json:"hash" storm:"id,index"`
|
||||||
|
Path string `json:"path" storm:"index"`
|
||||||
|
Expires bool `json:"expires"`
|
||||||
|
ExpireDate time.Time `json:"expireDate"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store is a collection of the stores needed to get
|
||||||
|
// and save information.
|
||||||
|
type Store struct {
|
||||||
|
Users UsersStore
|
||||||
|
Config ConfigStore
|
||||||
|
Share ShareStore
|
||||||
|
}
|
||||||
|
|
||||||
|
// UsersStore is the interface to manage users.
|
||||||
|
type UsersStore interface {
|
||||||
|
Get(id int) (*User, error)
|
||||||
|
Gets() ([]*User, error)
|
||||||
|
Save(u *User) error
|
||||||
|
Update(u *User, fields ...string) error
|
||||||
|
Delete(id int) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConfigStore is the interface to manage configuration.
|
||||||
|
type ConfigStore interface {
|
||||||
|
Get(name string, to interface{}) error
|
||||||
|
Save(name string, from interface{}) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// ShareStore is the interface to manage share links.
|
||||||
|
type ShareStore interface {
|
||||||
|
Get(hash string) (*ShareLink, error)
|
||||||
|
GetByPath(path string) ([]*ShareLink, error)
|
||||||
|
Gets() ([]*ShareLink, error)
|
||||||
|
Save(s *ShareLink) error
|
||||||
|
Delete(hash string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// StaticGen is a static website generator.
|
||||||
|
type StaticGen interface {
|
||||||
|
SettingsPath() string
|
||||||
|
Name() string
|
||||||
|
Setup() error
|
||||||
|
|
||||||
|
Hook(c *Context, w http.ResponseWriter, r *http.Request) (int, error)
|
||||||
|
Preview(c *Context, w http.ResponseWriter, r *http.Request) (int, error)
|
||||||
|
Publish(c *Context, w http.ResponseWriter, r *http.Request) (int, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Context contains the needed information to make handlers work.
|
||||||
|
type Context struct {
|
||||||
|
*FileManager
|
||||||
|
User *User
|
||||||
|
File *File
|
||||||
|
// On API handlers, Router is the APi handler we want.
|
||||||
|
Router string
|
||||||
|
}
|
||||||
|
|
||||||
|
// HashPassword generates an hash from a password using bcrypt.
|
||||||
|
func HashPassword(password string) (string, error) {
|
||||||
|
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||||
|
return string(bytes), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// CheckPasswordHash compares a password with an hash to check if they match.
|
||||||
|
func CheckPasswordHash(password, hash string) bool {
|
||||||
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 GenerateRandomBytes(n int) ([]byte, error) {
|
||||||
|
b := make([]byte, n)
|
||||||
|
_, err := rand.Read(b)
|
||||||
|
// Note that err == nil only if we read len(b) bytes.
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
|
|
23
http.go
23
http.go
|
@ -1,23 +0,0 @@
|
||||||
package filemanager
|
|
||||||
|
|
||||||
import "net/http"
|
|
||||||
|
|
||||||
// StaticGen is a static website generator.
|
|
||||||
type StaticGen interface {
|
|
||||||
SettingsPath() string
|
|
||||||
Name() string
|
|
||||||
Setup() error
|
|
||||||
|
|
||||||
Hook(c *Context, w http.ResponseWriter, r *http.Request) (int, error)
|
|
||||||
Preview(c *Context, w http.ResponseWriter, r *http.Request) (int, error)
|
|
||||||
Publish(c *Context, w http.ResponseWriter, r *http.Request) (int, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Context contains the needed information to make handlers work.
|
|
||||||
type Context struct {
|
|
||||||
*FileManager
|
|
||||||
User *User
|
|
||||||
File *File
|
|
||||||
// On API handlers, Router is the APi handler we want.
|
|
||||||
Router string
|
|
||||||
}
|
|
31
http/http.go
31
http/http.go
|
@ -3,6 +3,7 @@ package http
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -12,8 +13,34 @@ import (
|
||||||
fm "github.com/hacdias/filemanager"
|
fm "github.com/hacdias/filemanager"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ServeHTTP is the main entry point of this HTML application.
|
// ServeHTTP returns a function compatible with http.HandleFunc.
|
||||||
func ServeHTTP(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
func ServeHTTP(m *fm.FileManager) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
code, err := serve(&fm.Context{
|
||||||
|
FileManager: m,
|
||||||
|
User: nil,
|
||||||
|
File: nil,
|
||||||
|
}, w, r)
|
||||||
|
|
||||||
|
if code >= 400 {
|
||||||
|
w.WriteHeader(code)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
txt := http.StatusText(code)
|
||||||
|
log.Printf("%v: %v %v\n", r.URL.Path, code, txt)
|
||||||
|
w.Write([]byte(txt))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
w.Write([]byte(err.Error()))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// serve is the main entry point of this HTML application.
|
||||||
|
func serve(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
// Checks if the URL contains the baseURL and strips it. Otherwise, it just
|
// Checks if the URL contains the baseURL and strips it. Otherwise, it just
|
||||||
// returns a 404 fm.Error because we're not supposed to be here!
|
// returns a 404 fm.Error because we're not supposed to be here!
|
||||||
p := strings.TrimPrefix(r.URL.Path, c.BaseURL)
|
p := strings.TrimPrefix(r.URL.Path, c.BaseURL)
|
||||||
|
|
|
@ -28,6 +28,11 @@ type Jekyll struct {
|
||||||
previewPath string
|
previewPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Name is the plugin's name.
|
||||||
|
func (j Jekyll) Name() string {
|
||||||
|
return "jekyll"
|
||||||
|
}
|
||||||
|
|
||||||
// SettingsPath retrieves the correct settings path.
|
// SettingsPath retrieves the correct settings path.
|
||||||
func (j Jekyll) SettingsPath() string {
|
func (j Jekyll) SettingsPath() string {
|
||||||
return "/_config.yml"
|
return "/_config.yml"
|
||||||
|
|
188
user.go
188
user.go
|
@ -1,188 +0,0 @@
|
||||||
package filemanager
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/rand"
|
|
||||||
"errors"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
|
||||||
|
|
||||||
"github.com/hacdias/fileutils"
|
|
||||||
)
|
|
||||||
|
|
||||||
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")
|
|
||||||
)
|
|
||||||
|
|
||||||
// DefaultUser is used on New, when no 'base' user is provided.
|
|
||||||
var DefaultUser = User{
|
|
||||||
AllowCommands: true,
|
|
||||||
AllowEdit: true,
|
|
||||||
AllowNew: true,
|
|
||||||
AllowPublish: true,
|
|
||||||
Commands: []string{},
|
|
||||||
Rules: []*Rule{},
|
|
||||||
CSS: "",
|
|
||||||
Admin: true,
|
|
||||||
Locale: "en",
|
|
||||||
FileSystem: fileutils.Dir("."),
|
|
||||||
}
|
|
||||||
|
|
||||||
// User contains the configuration for each user.
|
|
||||||
type User struct {
|
|
||||||
// ID is the required primary key with auto increment0
|
|
||||||
ID int `storm:"id,increment"`
|
|
||||||
|
|
||||||
// Username is the user username used to login.
|
|
||||||
Username string `json:"username" storm:"index,unique"`
|
|
||||||
|
|
||||||
// The hashed password. This never reaches the front-end because it's temporarily
|
|
||||||
// emptied during JSON marshall.
|
|
||||||
Password string `json:"password"`
|
|
||||||
|
|
||||||
// Tells if this user is an admin.
|
|
||||||
Admin bool `json:"admin"`
|
|
||||||
|
|
||||||
// FileSystem is the virtual file system the user has access.
|
|
||||||
FileSystem fileutils.Dir `json:"filesystem"`
|
|
||||||
|
|
||||||
// Rules is an array of access and deny rules.
|
|
||||||
Rules []*Rule `json:"rules"`
|
|
||||||
|
|
||||||
// Custom styles for this user.
|
|
||||||
CSS string `json:"css"`
|
|
||||||
|
|
||||||
// Locale is the language of the user.
|
|
||||||
Locale string `json:"locale"`
|
|
||||||
|
|
||||||
// These indicate if the user can perform certain actions.
|
|
||||||
AllowNew bool `json:"allowNew"` // Create files and folders
|
|
||||||
AllowEdit bool `json:"allowEdit"` // Edit/rename files
|
|
||||||
AllowCommands bool `json:"allowCommands"` // Execute commands
|
|
||||||
AllowPublish bool `json:"allowPublish"` // Publish content (to use with static gen)
|
|
||||||
|
|
||||||
// Commands is the list of commands the user can execute.
|
|
||||||
Commands []string `json:"commands"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Rule is a dissalow/allow rule.
|
|
||||||
type Rule struct {
|
|
||||||
// Regex indicates if this rule uses Regular Expressions or not.
|
|
||||||
Regex bool `json:"regex"`
|
|
||||||
|
|
||||||
// Allow indicates if this is an allow rule. Set 'false' to be a disallow rule.
|
|
||||||
Allow bool `json:"allow"`
|
|
||||||
|
|
||||||
// Path is the corresponding URL path for this rule.
|
|
||||||
Path string `json:"path"`
|
|
||||||
|
|
||||||
// Regexp is the regular expression. Only use this when 'Regex' was set to true.
|
|
||||||
Regexp *Regexp `json:"regexp"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Regexp is a regular expression wrapper around native regexp.
|
|
||||||
type Regexp struct {
|
|
||||||
Raw string `json:"raw"`
|
|
||||||
regexp *regexp.Regexp
|
|
||||||
}
|
|
||||||
|
|
||||||
// Allowed checks if the user has permission to access a directory/file.
|
|
||||||
func (u User) Allowed(url string) bool {
|
|
||||||
var rule *Rule
|
|
||||||
i := len(u.Rules) - 1
|
|
||||||
|
|
||||||
for i >= 0 {
|
|
||||||
rule = u.Rules[i]
|
|
||||||
|
|
||||||
if rule.Regex {
|
|
||||||
if rule.Regexp.MatchString(url) {
|
|
||||||
return rule.Allow
|
|
||||||
}
|
|
||||||
} else if strings.HasPrefix(url, rule.Path) {
|
|
||||||
return rule.Allow
|
|
||||||
}
|
|
||||||
|
|
||||||
i--
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// MatchString checks if this string matches the regular expression.
|
|
||||||
func (r *Regexp) MatchString(s string) bool {
|
|
||||||
if r.regexp == nil {
|
|
||||||
r.regexp = regexp.MustCompile(r.Raw)
|
|
||||||
}
|
|
||||||
|
|
||||||
return r.regexp.MatchString(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
type ShareLink struct {
|
|
||||||
Hash string `json:"hash" storm:"id,index"`
|
|
||||||
Path string `json:"path" storm:"index"`
|
|
||||||
Expires bool `json:"expires"`
|
|
||||||
ExpireDate time.Time `json:"expireDate"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Store struct {
|
|
||||||
Users UsersStore
|
|
||||||
Config ConfigStore
|
|
||||||
Share ShareStore
|
|
||||||
}
|
|
||||||
|
|
||||||
type UsersStore interface {
|
|
||||||
Get(id int) (*User, error)
|
|
||||||
Gets() ([]*User, error)
|
|
||||||
Save(u *User) error
|
|
||||||
Update(u *User, fields ...string) error
|
|
||||||
Delete(id int) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type ConfigStore interface {
|
|
||||||
Get(name string, to interface{}) error
|
|
||||||
Save(name string, from interface{}) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type ShareStore interface {
|
|
||||||
Get(hash string) (*ShareLink, error)
|
|
||||||
GetByPath(path string) ([]*ShareLink, error)
|
|
||||||
Gets() ([]*ShareLink, error)
|
|
||||||
Save(s *ShareLink) error
|
|
||||||
Delete(hash string) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// HashPassword generates an hash from a password using bcrypt.
|
|
||||||
func HashPassword(password string) (string, error) {
|
|
||||||
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
||||||
return string(bytes), err
|
|
||||||
}
|
|
||||||
|
|
||||||
// CheckPasswordHash compares a password with an hash to check if they match.
|
|
||||||
func CheckPasswordHash(password, hash string) bool {
|
|
||||||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
|
||||||
return err == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 GenerateRandomBytes(n int) ([]byte, error) {
|
|
||||||
b := make([]byte, n)
|
|
||||||
_, err := rand.Read(b)
|
|
||||||
// Note that err == nil only if we read len(b) bytes.
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return b, nil
|
|
||||||
}
|
|
Loading…
Reference in New Issue