From d3bd810a1a174c860ee4a7656f33c7095b0cd252 Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 21 Sep 2021 23:39:21 +0200 Subject: [PATCH] nice progressbar and correct authentication header --- apiGo/api/FileUpload.go | 5 ++++- apiGo/api/api/ApiBase.go | 2 +- apiGo/api/api/Auth.go | 16 +++++++++++----- apiGo/api/api/Context.go | 7 ++++++- src/pages/SettingsPage/MovieSettings.tsx | 11 +++++++++++ 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/apiGo/api/FileUpload.go b/apiGo/api/FileUpload.go index c71b043..c140642 100644 --- a/apiGo/api/FileUpload.go +++ b/apiGo/api/FileUpload.go @@ -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") diff --git a/apiGo/api/api/ApiBase.go b/apiGo/api/api/ApiBase.go index 375a4f0..a5ee382 100644 --- a/apiGo/api/api/ApiBase.go +++ b/apiGo/api/api/ApiBase.go @@ -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 { diff --git a/apiGo/api/api/Auth.go b/apiGo/api/api/Auth.go index 9e50ddd..982fde0 100644 --- a/apiGo/api/api/Auth.go +++ b/apiGo/api/api/Auth.go @@ -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() { diff --git a/apiGo/api/api/Context.go b/apiGo/api/api/Context.go index edde0ef..3904fe5 100644 --- a/apiGo/api/api/Context.go +++ b/apiGo/api/api/Context.go @@ -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 +} diff --git a/src/pages/SettingsPage/MovieSettings.tsx b/src/pages/SettingsPage/MovieSettings.tsx index 47a218f..a4aa820 100644 --- a/src/pages/SettingsPage/MovieSettings.tsx +++ b/src/pages/SettingsPage/MovieSettings.tsx @@ -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(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' /> +
+
+
); };