nice progressbar and correct authentication header

This commit is contained in:
2021-09-21 23:39:21 +02:00
parent 284f78de49
commit d3bd810a1a
5 changed files with 33 additions and 8 deletions

View File

@ -10,6 +10,7 @@ import (
func addUploadHandler() {
api.AddHandler("fileupload", api.VideoNode, api.PermUser, func(ctx api.Context) {
fmt.Println("we are in file upload handler")
fmt.Printf("permission: %s\n", ctx.PermID().String())
req := ctx.GetRequest()
@ -30,6 +31,7 @@ func addUploadHandler() {
var read int64
var p float32
// todo check where we want to place this file
dst, err := os.OpenFile(part.FileName(), os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return
@ -41,7 +43,6 @@ func addUploadHandler() {
cBytes, err := part.Read(buffer)
if cBytes > 0 {
read = read + int64(cBytes)
//fmt.Printf("read: %v \n",read )
p = float32(read) / float32(length) * 100
fmt.Printf("progress: %v \n", p)
dst.Write(buffer[0:cBytes])
@ -53,6 +54,8 @@ func addUploadHandler() {
break
}
}
_ = dst.Close()
}
ctx.Text("finished")

View File

@ -15,7 +15,7 @@ const (
LoginNode = "login"
)
func AddHandler(action string, apiNode string, perm uint8, handler func(ctx Context)) {
func AddHandler(action string, apiNode string, perm Perm, handler func(ctx Context)) {
http.Handle(fmt.Sprintf("/api/%s/%s", apiNode, action), http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
srvPwd := settings.GetPassword()
if srvPwd == nil {

View File

@ -13,12 +13,18 @@ import (
var srv *server.Server
type Perm uint8
const (
PermAdmin uint8 = iota
PermUser uint8 = iota
PermUnauthorized uint8 = iota
PermAdmin Perm = iota
PermUser
PermUnauthorized
)
func (p Perm) String() string {
return [...]string{"PermAdmin", "PermUser", "PermUnauthorized"}[p]
}
const SignKey = "89013f1753a6890c6090b09e3c23ff43"
const TokenExpireHours = 24
@ -27,7 +33,7 @@ type Token struct {
ExpiresAt int64
}
func TokenValid(token string) (int, uint8) {
func TokenValid(token string) (int, Perm) {
t, err := jwt.ParseWithClaims(token, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(SignKey), nil
})
@ -42,7 +48,7 @@ func TokenValid(token string) (int, uint8) {
if err != nil {
return -1, PermUnauthorized
}
return id, uint8(permid)
return id, Perm(permid)
}
func InitOAuth() {

View File

@ -13,6 +13,7 @@ type Context interface {
GetRequest() *http.Request
GetWriter() http.ResponseWriter
UserID() int
PermID() Perm
}
type apicontext struct {
@ -20,7 +21,7 @@ type apicontext struct {
request *http.Request
responseWritten bool
userid int
permid uint8
permid Perm
}
func (r *apicontext) GetRequest() *http.Request {
@ -58,3 +59,7 @@ func (r *apicontext) Error(msg string) {
func (r *apicontext) Errorf(msg string, args ...interface{}) {
r.Error(fmt.Sprintf(msg, &args))
}
func (r *apicontext) PermID() Perm {
return r.permid
}