nice progressbar and correct authentication header

This commit is contained in:
lukas 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
}

View File

@ -2,6 +2,7 @@ import React, {BaseSyntheticEvent, useState} from 'react';
import style from './MovieSettings.module.css';
import {APINode, callAPI} from '../../utils/Api';
import {GeneralSuccess} from '../../types/GeneralTypes';
import {cookie} from '../../utils/context/Cookie';
interface state {
text: string[];
@ -166,6 +167,7 @@ export default MovieSettings;
const FileHandler = (): JSX.Element => {
const [file, setfile] = useState<File | null>(null);
const [percent, setpercent] = useState(0.0);
const uploadFile = (): void => {
let xhr = new XMLHttpRequest(); // create XMLHttpRequest
@ -181,11 +183,17 @@ const FileHandler = (): JSX.Element => {
xhr.upload.onprogress = function (e): void {
console.log(e.loaded / e.total);
setpercent((e.loaded * 100.0) / e.total);
};
xhr.open('post', '/api/video/fileupload'); // open connection
xhr.setRequestHeader('Accept', 'multipart/form-data');
const tkn = cookie.Load();
if (tkn) {
xhr.setRequestHeader('Token', tkn.Token);
}
xhr.send(data); // send data
};
@ -204,6 +212,9 @@ const FileHandler = (): JSX.Element => {
name='Select file to upload'
/>
<button onClick={(): void => uploadFile()}>upload</button>
<div style={{width: '100%', height: 5, marginTop: 3}}>
<div style={{width: percent + '%', backgroundColor: 'green', height: 5}} />
</div>
</div>
);
};