db

package
v0.10.5 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2024 License: LGPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UserSchemaNs = "UserSchemaNs"
	FileSchemaNs = "FileSchemaNs"
	UserIDsNs    = "UserIDsNs"
	UsersNs      = "UsersNs"
	RolesNs      = "RolesNs"
	FileInfoNs   = "FileInfoNs"
	ShareIDNs    = "ShareIDNs"

	KeyInitTime = "keyInitTime"

	AdminRole   = "admin"
	UserRole    = "user"
	VisitorRole = "visitor"

	VisitorID   = uint64(1)
	VisitorName = "visitor"
)

Variables

View Source
var (
	// users related errors
	ErrReachedLimit       = errors.New("reached space limit")
	ErrUserNotFound       = errors.New("user not found")
	ErrNegtiveUsedSpace   = errors.New("used space can not be negative")
	ErrInvalidFileInfo    = errors.New("invalid fileInfo")
	ErrInvalidUser        = errors.New("invalid user")
	ErrInvalidQuota       = errors.New("invalid quota")
	ErrInvalidPreferences = errors.New("invalid preferences")
	// files related errors
	ErrEmpty            = errors.New("can not hash empty string")
	ErrFileInfoNotFound = errors.New("file info not found")
	ErrSharingNotFound  = errors.New("sharing id not found")
	ErrConflicted       = errors.New("conflict found in hashing")
	ErrVerNotFound      = errors.New("file info schema version not found")
	// uploadings
	ErrGreaterThanSize = errors.New("uploaded is greater than file size")
	ErrUploadNotFound  = errors.New("upload info not found")

	// site
	ErrConfigNotFound = errors.New("site config not found")

	ErrBucketNotFound = errors.New("bucket not found")
	ErrKeyNotFound    = errors.New("key not found")
	ErrKeyExisting    = errors.New("key is existing")
	ErrCreateExisting = errors.New("create upload info which already exists")
	ErrQuota          = errors.New("quota limit reached")

	DefaultSiteName = "Quickshare"
	DefaultSiteDesc = "Quickshare"
	DefaultBgConfig = &BgConfig{
		Url:      "",
		Repeat:   "repeat",
		Position: "top",
		Align:    "fixed",
		BgColor:  "",
	}
	DefaultAllowSetBg = false
	DefaultAutoTheme  = true
	BgRepeatValues    = map[string]bool{
		"repeat-x":  true,
		"repeat-y":  true,
		"repeat":    true,
		"space":     true,
		"round":     true,
		"no-repeat": true,
	}
	BgAlignValues = map[string]bool{
		"scroll": true,
		"fixed":  true,
		"local":  true,
	}
	BgPositionValues = map[string]bool{
		"top":    true,
		"bottom": true,
		"left":   true,
		"right":  true,
		"center": true,
	}

	DefaultCSSURL     = ""
	DefaultLanPackURL = ""
	DefaultLan        = "en_US"
	DefaultTheme      = "light"
	DefaultAvatar     = ""
	DefaultEmail      = ""

	DefaultSpaceLimit         = int64(1024 * 1024 * 1024) // 1GB
	DefaultUploadSpeedLimit   = 50 * 1024 * 1024          // 50MB
	DefaultDownloadSpeedLimit = 50 * 1024 * 1024          // 50MB
	VisitorUploadSpeedLimit   = 10 * 1024 * 1024          // 10MB
	VisitorDownloadSpeedLimit = 10 * 1024 * 1024          // 10MB

	DefaultPreferences = Preferences{
		Bg:         DefaultBgConfig,
		CSSURL:     DefaultCSSURL,
		LanPackURL: DefaultLanPackURL,
		Lan:        DefaultLan,
		Theme:      DefaultTheme,
		Avatar:     DefaultAvatar,
		Email:      DefaultEmail,
	}
)

Functions

func CheckBgConfig added in v0.8.1

func CheckBgConfig(cfg *BgConfig, fillDefault bool) error

func CheckFileInfo added in v0.8.1

func CheckFileInfo(info *FileInfo, fillDefault bool) error

TODO: auto trigger hash generating

func CheckPreferences added in v0.8.1

func CheckPreferences(prefers *Preferences, fillDefault bool) error

func CheckQuota added in v0.8.1

func CheckQuota(quota *Quota) error

TODO: check upper and lower limit

func CheckSiteCfg added in v0.8.1

func CheckSiteCfg(cfg *SiteConfig, fillDefault bool) error

func CheckUser added in v0.8.1

func CheckUser(user *User, fillDefault bool) error

func ComparePreferences added in v0.7.1

func ComparePreferences(p1, p2 *Preferences) bool

func UploadNS

func UploadNS(user string) string

Types

type BgConfig added in v0.8.1

type BgConfig struct {
	Url      string `json:"url" yaml:"url"`
	Repeat   string `json:"repeat" yaml:"repeat"`
	Position string `json:"position" yaml:"position"`
	Align    string `json:"align" yaml:"align"`
	BgColor  string `json:"bgColor" yaml:"bgColor"`
}

type ClientConfig added in v0.8.1

type ClientConfig struct {
	SiteName   string    `json:"siteName" yaml:"siteName"`
	SiteDesc   string    `json:"siteDesc" yaml:"siteDesc"`
	Bg         *BgConfig `json:"bg" yaml:"bg"`
	AllowSetBg bool      `json:"allowSetBg" yaml:"allowSetBg"`
	AutoTheme  bool      `json:"autoTheme" yaml:"autoTheme"`
}

type FileInfo

type FileInfo struct {
	Id      uint64 `json:"id" yaml:"id"`
	IsDir   bool   `json:"isDir" yaml:"isDir"`
	Shared  bool   `json:"shared" yaml:"shared"`
	ShareID string `json:"shareID" yaml:"shareID"`
	Sha1    string `json:"sha1" yaml:"sha1"`
	Size    int64  `json:"size" yaml:"size"`
}

type IConfigDB added in v0.10.1

type IConfigDB interface {
	SetClientCfg(ctx context.Context, cfg *ClientConfig) error
	GetCfg(ctx context.Context) (*SiteConfig, error)
}

type IDB added in v0.10.1

type IDB interface {
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
	Close() error
	PingContext(ctx context.Context) error
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

TODO: expose more APIs if needed

type IDBLockable added in v0.10.1

type IDBLockable interface {
	Lock()
	Unlock()
	RLock()
	RUnlock()
}

type IDBQuickshare added in v0.10.1

type IDBQuickshare interface {
	Init(ctx context.Context, adminName, adminPwd string, config *SiteConfig) error
	InitUserTable(ctx context.Context, tx *sql.Tx, rootName, rootPwd string) error
	InitFileTables(ctx context.Context, tx *sql.Tx) error
	InitConfigTable(ctx context.Context, tx *sql.Tx, cfg *SiteConfig) error
	Close() error
	IDBLockable
	IUserDB
	IFileDB
	IUploadDB
	ISharingDB
	IConfigDB
}

type IFileDB added in v0.10.1

type IFileDB interface {
	AddFileInfo(ctx context.Context, infoId, userId uint64, itemPath string, info *FileInfo) error
	DelFileInfo(ctx context.Context, userId uint64, itemPath string) error
	GetFileInfo(ctx context.Context, itemPath string) (*FileInfo, error)
	SetSha1(ctx context.Context, itemPath, sign string) error
	MoveFileInfo(ctx context.Context, userId uint64, oldPath, newPath string, isDir bool) error
	ListFileInfos(ctx context.Context, itemPaths []string) (map[string]*FileInfo, error)
}

type IFileInfoStore added in v0.10.1

type IFileInfoStore interface {
	AddSharing(ctx context.Context, dirPath string) error
	DelSharing(ctx context.Context, dirPath string) error
	GetSharing(ctx context.Context, dirPath string) (bool, bool)
	ListSharings(ctx context.Context, prefix string) (map[string]string, error)
	GetFileInfo(ctx context.Context, itemPath string) (*FileInfo, error)
	SetFileInfo(ctx context.Context, itemPath string, info *FileInfo) error
	DelFileInfo(ctx context.Context, itemPath string) error
	ListFileInfos(ctx context.Context, itemPaths []string) (map[string]*FileInfo, error)
	SetSha1(ctx context.Context, itemPath, sign string) error
	GetSharingDir(ctx context.Context, hashID string) (string, error)
	// upload info
	AddUploadInfo(ctx context.Context, user, filePath, tmpPath string, fileSize int64) error
	SetUploadInfo(ctx context.Context, user, filePath string, newUploaded int64) error
	GetUploadInfo(ctx context.Context, user, filePath string) (string, int64, int64, error)
	DelUploadInfo(ctx context.Context, user, filePath string) error
	ListUploadInfo(ctx context.Context, user string) ([]*UploadInfo, error)
}

type IFilesFunctions added in v0.10.1

type IFilesFunctions interface {
	IFileDB
	IUploadDB
	ISharingDB
}

type ISharingDB added in v0.10.1

type ISharingDB interface {
	IsSharing(ctx context.Context, dirPath string) (bool, error)
	GetSharingDir(ctx context.Context, hashID string) (string, error)
	AddSharing(ctx context.Context, infoId, userId uint64, dirPath string) error
	DelSharing(ctx context.Context, userId uint64, dirPath string) error
	ListSharingsByLocation(ctx context.Context, location string) (map[string]string, error)
}

type ISiteStore added in v0.10.1

type ISiteStore interface {
	SetClientCfg(ctx context.Context, cfg *ClientConfig) error
	GetCfg(ctx context.Context) (*SiteConfig, error)
}

type IUploadDB added in v0.10.1

type IUploadDB interface {
	AddUploadInfos(ctx context.Context, uploadId, userId uint64, tmpPath, filePath string, info *FileInfo) error
	DelUploadingInfos(ctx context.Context, userId uint64, realPath string) error
	MoveUploadingInfos(ctx context.Context, uploadId, userId uint64, uploadPath, itemPath string) error
	SetUploadInfo(ctx context.Context, user uint64, filePath string, newUploaded int64) error
	GetUploadInfo(ctx context.Context, userId uint64, filePath string) (string, int64, int64, error)
	ListUploadInfos(ctx context.Context, user uint64) ([]*UploadInfo, error)
}

type IUserDB added in v0.10.1

type IUserDB interface {
	AddUser(ctx context.Context, user *User) error
	DelUser(ctx context.Context, id uint64) error
	GetUser(ctx context.Context, id uint64) (*User, error)
	GetUserByName(ctx context.Context, name string) (*User, error)
	SetPwd(ctx context.Context, id uint64, pwd string) error
	SetInfo(ctx context.Context, id uint64, user *User) error
	SetPreferences(ctx context.Context, id uint64, prefers *Preferences) error
	SetUsed(ctx context.Context, id uint64, incr bool, capacity int64) error
	ResetUsed(ctx context.Context, id uint64, used int64) error
	ListUsers(ctx context.Context) ([]*User, error)
	ListUserIDs(ctx context.Context) (map[string]string, error)
	AddRole(role string) error
	DelRole(role string) error
	ListRoles() (map[string]bool, error)
}

type IUserStore added in v0.10.1

type IUserStore interface {
	Init(ctx context.Context, rootName, rootPwd string) error
	IsInited() bool
	AddUser(ctx context.Context, user *User) error
	DelUser(ctx context.Context, id uint64) error
	GetUser(ctx context.Context, id uint64) (*User, error)
	GetUserByName(ctx context.Context, name string) (*User, error)
	SetInfo(ctx context.Context, id uint64, user *User) error
	SetUsed(ctx context.Context, id uint64, incr bool, capacity int64) error
	ResetUsed(ctx context.Context, id uint64, used int64) error
	SetPwd(ctx context.Context, id uint64, pwd string) error
	SetPreferences(ctx context.Context, id uint64, settings *Preferences) error
	ListUsers(context.Context) ([]*User, error)
	ListUserIDs(context.Context) (map[string]string, error)
	AddRole(role string) error
	DelRole(role string) error
	ListRoles() (map[string]bool, error)
}

type Preferences

type Preferences struct {
	Bg         *BgConfig `json:"bg" yaml:"bg"`
	CSSURL     string    `json:"cssURL" yaml:"cssURL"`
	LanPackURL string    `json:"lanPackURL" yaml:"lanPackURL"`
	Lan        string    `json:"lan" yaml:"lan"`
	Theme      string    `json:"theme" yaml:"theme"`
	Avatar     string    `json:"avatar" yaml:"avatar"`
	Email      string    `json:"email" yaml:"email"`
}

type Quota

type Quota struct {
	SpaceLimit         int64 `json:"spaceLimit,string" yaml:"spaceLimit,string"`
	UploadSpeedLimit   int   `json:"uploadSpeedLimit" yaml:"uploadSpeedLimit"`
	DownloadSpeedLimit int   `json:"downloadSpeedLimit" yaml:"downloadSpeedLimit"`
}

type SiteConfig added in v0.8.1

type SiteConfig struct {
	ClientCfg *ClientConfig `json:"clientCfg" yaml:"clientCfg"`
}

type UploadInfo

type UploadInfo struct {
	RealFilePath string `json:"realFilePath" yaml:"realFilePath"`
	Size         int64  `json:"size" yaml:"size"`
	Uploaded     int64  `json:"uploaded" yaml:"uploaded"`
}

type User

type User struct {
	ID          uint64       `json:"id,string" yaml:"id,string"`
	Name        string       `json:"name" yaml:"name"`
	Pwd         string       `json:"pwd" yaml:"pwd"`
	Role        string       `json:"role" yaml:"role"`
	UsedSpace   int64        `json:"usedSpace,string" yaml:"usedSpace,string"`
	Quota       *Quota       `json:"quota" yaml:"quota"`
	Preferences *Preferences `json:"preferences" yaml:"preferences"`
}

type UserCfg added in v0.8.1

type UserCfg struct {
	Name string `json:"name" yaml:"name"`
	Role string `json:"role" yaml:"role"`
	Pwd  string `json:"pwd" yaml:"pwd"`
}

Directories

Path Synopsis
rdb

Jump to

Keyboard shortcuts

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