global

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 9, 2023 License: AGPL-3.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DataFolder stores all backend data in
	DataFolder = "./data/"
	// FileFolder stores all uploaded blob files
	FileFolder = DataFolder + "file/"
	// PaperFolder stores all protected files
	PaperFolder = DataFolder + "paper/"
)
View Source
const (
	FileTableList         = "lst"
	FileTableFile         = "file"
	FileTableTempFile     = "tmpfile"
	FileTableQuestion     = "question"
	FileTableTempQuestion = "tmpqstn"
)
View Source
const (
	UserTableUser            = "user"
	UserTableMessage         = "msg"
	UserTableMonthlyAPIVisit = "visit"
	UserTableRegex           = "re"
)

Variables

View Source
var (
	UserDB = UserDatabase{/* contains filtered or unexported fields */}
	FileDB = FileDatabase{/* contains filtered or unexported fields */}
)
View Source
var (
	ErrMajorSplitsTooShort       = errors.New("major splits too short")
	ErrEmptyClass                = errors.New("empty class")
	ErrHasntAnalyzed             = errors.New("hasn't analyzed")
	ErrNoGetFileStatusPermission = errors.New("no get file status permission")
)
View Source
var (
	ErrInvalidGenerateConfig          = errors.New("invalid generate config")
	ErrMajorTooLarge                  = errors.New("major too large")
	ErrNoSuchMajor                    = errors.New("no such major")
	ErrNoEnoughQuestionToMatchRequire = errors.New("no enough question to match require")
	ErrRateLimitExceeded              = errors.New("rate limit exceeded")
)
View Source
var (
	ErrInvalidRole       = errors.New("invalid role")
	ErrEmptyPassword     = errors.New("empty password")
	ErrEmptyName         = errors.New("empty name")
	ErrInvalidUsersCount = errors.New("invalid users count")
	ErrInvalidUserID     = errors.New("invalid user ID")
	ErrInvalidAvatar     = errors.New("invalid avatar")
	ErrEmptyUserID       = errors.New("empty user ID")
	ErrEmptyContact      = errors.New("empty contact")
	ErrUsernameExists    = errors.New("username exists")
	ErrInvalidName       = errors.New("invalid name")
	ErrInvalidContact    = errors.New("invalid contact")
	ErrNoSuchFieldName   = errors.New("no such field name")
	ErrEmptyRegex        = errors.New("empty regex")
)
View Source
var (
	ErrInvalidSaveName = errors.New("invalid save name")
)
View Source
var (
	ErrPaperFileExist = errors.New("paper file exist")
)

Functions

This section is empty.

Types

type File

type File struct {
	ID        int64 // ID is the first 8 bytes of the original file's md5
	ListID    int   // ListID is the foreign key to List(ID)
	Year      StudyYear
	Type      PaperType
	Date      uint32        // Date is the yyyymmdd of 考试日期
	Time      time.Duration // Time is 考试时长
	Class     string        // Class is 考试科目
	Rate      string        // Rate is 成绩构成比例
	Questions []byte        // Questions is for []QuestionJSON
}

File stores to paper/Class/2022-2023学年/第一学期/期末/A/xxx.docx

func (*File) GetList added in v0.1.0

func (file *File) GetList(f *FileDatabase) (List, error)

type FileDatabase

type FileDatabase struct {
	// contains filtered or unexported fields
}

func (*FileDatabase) AddFile

func (f *FileDatabase) AddFile(lstid int, reg *Regex, istemp bool, progress func(uint)) error

AddFile from lst and copy it to analyzed path. The para reg must belong to a valid user

func (*FileDatabase) DelFile

func (f *FileDatabase) DelFile(lstid, uid int, istemp bool) error

DelFile by listid

func (*FileDatabase) DelQuestion

func (f *FileDatabase) DelQuestion(id int64, istemp bool) error

DelQuestion 删除问题, 与其它问题的 dup

func (*FileDatabase) GenerateFile

func (f *FileDatabase) GenerateFile(config *GenerateConfig) (docf *docx.Docx, err error)

GenerateFile 用一些限定条件生成新试卷, 云端不保存

func (*FileDatabase) GetFile

func (f *FileDatabase) GetFile(lstid, uid int) (file *File, lst List, err error)

GetFile get analyzed file's structure from List(ID)

func (*FileDatabase) GetFilesByYearRange added in v0.1.0

func (f *FileDatabase) GetFilesByYearRange(yearstart, yearend StudyYear) ([]*File, error)

GetFilesByYearRange ...

func (*FileDatabase) GetMajors

func (f *FileDatabase) GetMajors() (majors []string)

GetMajors ...

func (*FileDatabase) GetQuestion

func (f *FileDatabase) GetQuestion(id int64, istemp bool) (Question, error)

GetQuestion by id

func (*FileDatabase) GetQuestionByHex added in v0.1.0

func (f *FileDatabase) GetQuestionByHex(hexid string, istemp bool) (q Question, err error)

GetQuestionHex by hexid

func (*FileDatabase) ListFileByID

func (f *FileDatabase) ListFileByID(id int) (lst List, err error)

func (*FileDatabase) ListUploadedFile

func (f *FileDatabase) ListUploadedFile(istemp *bool, uid int) (lst []*List, err error)

ListUploadedFile will select all file that HasntAnalyzed && IsTemp or !HasntAnalyzed && !IsTemp

func (*FileDatabase) SaveFileToTemp

func (f *FileDatabase) SaveFileToTemp(uploader int, file io.Reader, name string) (id int, err error)

SaveFileToTemp copy file to PaperFolder/tmp/uploader/name and add record into list.

type GenerateConfig

type GenerateConfig struct {
	Distribution map[string]uint // Distribution is map[majorname]subcount
	RateLimit    float64         // RateLimit 重复率上限
	YearStart    StudyYear       // YearStart 起始年份(空则直到最旧)
	YearEnd      StudyYear       // YearEnd 截止年份(空则直到最新)
	TypeMask     PaperType       // TypeMask & File.Type != 0 则匹配
}

GenerateConfig 试卷生成配置

type List

type List struct {
	ID            *int   // ID is self-inc
	Uploader      int    // Uploader is uid
	UpName        string // UpName is uploader's name
	UpTime        int64  // UpTime is upload time (unix timestamp)
	Size          int64  // Size of the original file
	QuesC         int    // QuesC 总小题数
	HasntAnalyzed bool   // HasntAnalyzed whether file has been analyzed
	IsTemp        bool   // IsTemp whether file is temp
	Path          string // Path of file, normally unique
	Desc          string // Desc is file's description
}

List of file path

type Message

type Message struct {
	ID   *int
	ToID int // ToID user's ID
	Date int64
	Text string // Text is the message content
	Name string // Name is the user's name to add in register message
	Cont string // Cont is the user's phone number to add in register message or an operator's name in add user message
	Pswd string // Pswd is the user's password to add in register message
}

Message is shown in the workbench

func (*Message) Type

func (m *Message) Type() MessageType

Type decide message type by fields Name, Cont and Pswd.

type MessageType

type MessageType uint8
const (
	MessageNormal MessageType = iota
	MessageRegister
	MessageUserAdded
	MessageContactChange
	MessagePasswordChange
	MessageResetPassword
	MessageOperator
)

type MonthlyAPIVisit

type MonthlyAPIVisit struct {
	YM    uint32 // YM is yyyymm
	Count uint32 // visit count this mounth
}

MonthlyAPIVisit counts the api visit history

type PaperType

type PaperType uint16

PaperType [4 开 一页纸 闭] [4 上下] [4 中末] [4 AB]

func (PaperType) AB

func (pt PaperType) AB() byte

AB default A

func (PaperType) FirstSecond

func (pt PaperType) FirstSecond() string

FirstSecond default is 年度

func (PaperType) MiddleFinal

func (pt PaperType) MiddleFinal() string

MiddleFinal default 平时

func (PaperType) OpenClose

func (pt PaperType) OpenClose() string

OpenClose default 闭卷

func (PaperType) SetAB

func (pt PaperType) SetAB(x byte) PaperType

func (PaperType) SetFirstSecond

func (pt PaperType) SetFirstSecond(x byte) PaperType

func (PaperType) SetMiddleFinal

func (pt PaperType) SetMiddleFinal(x string) PaperType

func (PaperType) SetOpenClose

func (pt PaperType) SetOpenClose(x string) PaperType

type Question

type Question struct {
	ID     int64  // ID is the first 8 bytes of the Plain's md5
	ListID int    // ListID is fk to List(ID)
	Major  string // Major is sub's major name
	Path   string // Path is the question's docx position
	Plain  string // Plain is the plain text of the question (like markdown format)
	Images []byte // Images is json of the image dhash in XML, ex. ['rId1': '1234567890abcdef', ...]
	Vector []byte // Vector is json of {word: freq, ...}
	Dup    []byte // Dup is json of {queid: rate, ...}
}

func (*Question) GetDuplicateRate

func (q *Question) GetDuplicateRate(que *Question) (float64, error)

GetDuplicateRate calc q & que's dup rate

func (*Question) MaxDuplicateRate

func (q *Question) MaxDuplicateRate() float64

MaxDuplicateRate parse q.Dup and get the max rate

type QuestionJSON

type QuestionJSON struct {
	Name   string         `json:"name"`             // Name is name or Question ID
	Points int            `json:"points,omitempty"` // Points is sum of subs' points or self
	Sub    []QuestionJSON `json:"sub,omitempty"`
}

QuestionJSON is the struct representation of File.Questions

func (*QuestionJSON) Delete

func (q *QuestionJSON) Delete(f *FileDatabase, istemp bool)

Delete me and all subs, ignore errors

type Regex

type Regex struct {
	ID     int    // ID is User(ID)
	Title  string // Title default `.*(\d{4})\s*-.*学年.*(\d).*([中末]).*([AB])\s*卷`
	Class  string // Class default `(考试科目|课程名称):\s*(\S+)\s*`
	OpenCl string // OpenCl default `考试形式:\s*(\S+)\s*`
	Date   string // Date default `考试日期:\s*(\d+)\s*年\s*(\d+)\s*月\s*(\d+)\s*日`
	Time   string // Time default `考试时长:\s*(\d+)\s*分钟`
	Rate   string // Rate default `(成绩构成比例|课程成绩构成):\s*(.*%)\s*`
	Major  string // Major default `([一二三四五六七八九十]+)、\s*(.*)\s*(.*([空题]?)\s*(\d*).*共\s*(\d+)\s*分.*)`
	Sub    string // Sub default `(\d+)、`
}

Regex stores user's config of splitting docx file

func GetDefaultRegex

func GetDefaultRegex() (reg Regex)

type StudyYear

type StudyYear uint16

StudyYear 学年

func (StudyYear) String

func (sy StudyYear) String() string

String ex. 2022-2023学年

type User

type User struct {
	ID   *int
	Role UserRole
	Date int64 // Date is the creating date's unix timestamp
	Pswd string
	Last int64  // Last is the last password reseting unix timestamp
	Name string `db:"Name,UNIQUE"`
	Nick string
	Avtr string // Avtr is the user's avatar, typically a image url
	Cont string // Cont is the user's contact, ex. phone number
	Desc string
}

User stores a user in table named UserTableUser

func (*User) IsFileManager

func (user *User) IsFileManager() bool

IsFileManager checks if token is valid for a filemgr

func (*User) IsSuper

func (user *User) IsSuper() bool

IsSuper checks if token is valid for a super

func (*User) IsUser

func (user *User) IsUser() bool

IsUser checks if token is valid for a user

type UserDatabase

type UserDatabase struct {
	// contains filtered or unexported fields
}

func (*UserDatabase) AddUser

func (u *UserDatabase) AddUser(user *User, opname string) error

AddUser but cannot customize the ID field for it is self-increasing

func (*UserDatabase) DelMessageByID

func (u *UserDatabase) DelMessageByID(id int) (err error)

DelMessageByID ...

func (*UserDatabase) DelUserByID

func (u *UserDatabase) DelUserByID(id int) (err error)

DelUserByID ...

func (*UserDatabase) DisableUser

func (u *UserDatabase) DisableUser(id int, opname string) error

DisableUser ...

func (*UserDatabase) GetAnnualAPIVisitCount

func (u *UserDatabase) GetAnnualAPIVisitCount() (cnts [12]uint32)

GetAnnualAPIVisitCount get the latest 12 mounths' count

func (*UserDatabase) GetMessageByID

func (u *UserDatabase) GetMessageByID(id int) (m Message, err error)

GetMessageByID ...

func (*UserDatabase) GetMessagesOfUser

func (u *UserDatabase) GetMessagesOfUser(to int) (ms []Message, err error)

GetMessagesOfUser will change non-empty Pswd field to "-"

func (*UserDatabase) GetSuperIDs

func (u *UserDatabase) GetSuperIDs() (ids []int, err error)

func (*UserDatabase) GetUserByID

func (u *UserDatabase) GetUserByID(id int) (user User, err error)

GetUserByID ...

func (*UserDatabase) GetUserByName

func (u *UserDatabase) GetUserByName(username string) (user User, err error)

GetUserByName avoids sql injection by limiting username to 0-9A-Za-z

func (*UserDatabase) GetUserRegex

func (u *UserDatabase) GetUserRegex(oper *User, id int) (*Regex, error)

GetUserRegex default newRegex()

func (*UserDatabase) GetUsers

func (u *UserDatabase) GetUsers() (users []User, err error)

GetUsers will set Pswd field to empty

func (*UserDatabase) GetUsersCount

func (u *UserDatabase) GetUsersCount() (int, error)

GetUsersCount ...

func (*UserDatabase) IsIDExists

func (u *UserDatabase) IsIDExists(id int) bool

IsIDExists ...

func (*UserDatabase) IsNameExists

func (u *UserDatabase) IsNameExists(username string) bool

IsNameExists avoids sql injection by limiting username to 0-9A-Za-z

func (*UserDatabase) NotifyRegister

func (u *UserDatabase) NotifyRegister(ip, name, cont, pswd string) error

NotifyRegister will send register notification to all supers

func (*UserDatabase) NotifyResetPassword

func (u *UserDatabase) NotifyResetPassword(ip, name, cont string) error

NotifyResetPassword will send notification to all supers

func (*UserDatabase) SendMessage

func (u *UserDatabase) SendMessage(text, opname string, to int) error

SendMessage will send a normal message to id

func (*UserDatabase) SetUserRegex

func (u *UserDatabase) SetUserRegex(id int, reg *Regex) error

SetUserRegex set Regex.name = re

func (*UserDatabase) UpdateUserContact

func (u *UserDatabase) UpdateUserContact(id int, opname, ncont string) error

UpdateUserContact ...

func (*UserDatabase) UpdateUserInfo

func (u *UserDatabase) UpdateUserInfo(id int, opname, nick, avtr, desc string) error

UpdateUserInfo ...

func (*UserDatabase) UpdateUserPassword

func (u *UserDatabase) UpdateUserPassword(id int, opname, npwd string) error

UpdateUserPassword ...

func (*UserDatabase) UpdateUserRole

func (u *UserDatabase) UpdateUserRole(id int, nr UserRole, opname string) error

UpdateUserRole ...

func (*UserDatabase) VisitAPI

func (u *UserDatabase) VisitAPI()

VisitAPI increases count of this mounth by 1

type UserRole

type UserRole uint8
const (
	RoleNil UserRole = iota
	RoleSuper
	RoleFileManager
	RoleUser
	RoleTop
)

func (UserRole) IsVaild

func (r UserRole) IsVaild() bool

func (UserRole) Nick

func (r UserRole) Nick() string

func (UserRole) String

func (r UserRole) String() string

Jump to

Keyboard shortcuts

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