filebox

package module
v0.0.0-...-e1210ba Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2017 License: MIT Imports: 11 Imported by: 8

README

Filebox

Filebox provides access permission control for files and directories.

You could utilize Filebox to satisfy the following scenarios:

Scenario 1:

  • You would like to make a file, let's say ~/documents/files/users.csv, downloadable via the URL http://127.0.0.1/downloads/users.csv.
  • restrict downloads of that file to Admin users.

Scenario 2:

  • Restricting access to files within a folder, let's say ~/exchanges, to Admin users.

GoDoc

Usage

import (
  "github.com/qor/filebox"
  "github.com/qor/roles"
  "net/http"
  "string"
)

func main() {
  mux := http.NewServeMux()

  // Mount filebox into `/downloads`
  Filebox := filebox.New("/home/qor/project/downloads")
  Filebox.MountTo("/downloads", mux)

  // Assert folder downloads has file users.csv
  // then you could download this file by http://127.0.0.1:7000/downloads/users.csv

  // Add permission for users.csv, limit to only admin user able to access
  permission := roles.Allow(roles.Read, "admin")
  userFile := Filebox.AccessFile("users.csv")
  userFile.SetPermission(permission)
  // read content from file `users.csv`
  fileContentReader, err := userFile.Read()
  // write content for file `users.csv`
  userFile.Write(fileContentReader)

  // Add permission for a specific directory
  exchangesDir := Dir.AccessDir("/exchanges")
  exchangesDir.SetPermission(permission)
  // Create a new file and it will use directory's permission if it hasn't define its own
  exchangesDir.WriteFile("products.csv", strings.NewReader("Content"))
}

License

Released under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dir

type Dir struct {
	DirPath string
	Roles   []string
	Filebox *Filebox
}

Dir is a object to access a specific directory

func (*Dir) HasPermission

func (dir *Dir) HasPermission(mode roles.PermissionMode) bool

HasPermission used to check current user whether have permission to access directory

func (*Dir) SetPermission

func (dir *Dir) SetPermission(permission *roles.Permission) (err error)

SetPermission used to set a Permission to directory

func (*Dir) WriteFile

func (dir *Dir) WriteFile(fileName string, reader io.Reader) (file *File, err error)

WriteFile writes data to a file named by filename. If the file does not exist, WriteFile will create a new file

type File

type File struct {
	FilePath string
	Roles    []string
	Dir      *Dir
	Filebox  *Filebox
}

File is a object to access a specific file

func (*File) HasPermission

func (f *File) HasPermission(mode roles.PermissionMode) bool

HasPermission used to check current user whether have permission to access file

func (*File) Read

func (f *File) Read() (io.ReadSeeker, error)

Read will get a io reader for a specific file

func (*File) SetPermission

func (f *File) SetPermission(permission *roles.Permission) (err error)

SetPermission used to set a Permission to file

func (*File) Write

func (f *File) Write(reader io.Reader) (err error)

Write used to store reader's content to a file

type Filebox

type Filebox struct {
	BaseDir string
	Auth    admin.Auth
	// contains filtered or unexported fields
}

Filebox is a based object contains download folder path and admin.Auth used to get current user

func New

func New(dir string) *Filebox

New a filebox struct

func (*Filebox) AccessDir

func (filebox *Filebox) AccessDir(dirPath string, roles ...string) *Dir

AccessDir will return a specific Dir object

func (*Filebox) AccessFile

func (filebox *Filebox) AccessFile(filePath string, roles ...string) *File

AccessFile will return a specific File object

func (*Filebox) Download

func (filebox *Filebox) Download(w http.ResponseWriter, req *http.Request)

Download is a handler will return a specific file

func (*Filebox) MountTo

func (filebox *Filebox) MountTo(mountTo string, mux *http.ServeMux)

MountTo will mount to mux to route `mountto`

func (*Filebox) ServeHTTP

func (filebox *Filebox) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP is a implement for http server interface

func (*Filebox) SetAuth

func (filebox *Filebox) SetAuth(auth admin.Auth)

SetAuth will set a admin.Auth struct to Filebox, used to get current user's role

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL