From 4e944e31c60907a7d4a3684e9701ca57ce36c51a Mon Sep 17 00:00:00 2001 From: kirari04 Date: Thu, 18 Jan 2024 21:57:48 +0100 Subject: [PATCH] idk cleanup and stuff --- controllers/AuthCheckController.go | 7 +- controllers/DownloadVideoController.go | 13 ++- controllers/ViewIndexController.go | 5 +- go.mod | 17 +-- go.sum | 35 ++----- inits/Server.go | 140 ++++++++++--------------- logic/CreateFile.go | 37 +++---- logic/CreateFolder.go | 9 +- logic/CreateUploadChunck.go | 26 +++-- logic/CreateUploadFile.go | 31 +++--- logic/CreateUploadSession.go | 23 ++-- logic/CreateWebhook.go | 7 +- logic/DeleteFiles.go | 15 +-- logic/DeleteFolders.go | 17 ++- logic/DeleteWebhook.go | 11 +- logic/GetAccount.go | 11 +- logic/GetAudioData.go | 11 +- logic/GetFile.go | 8 +- logic/GetFileExample.go | 8 +- logic/GetM3u8Data.go | 7 +- logic/GetSubtitleData.go | 11 +- logic/GetThumbnailData.go | 9 +- logic/GetVideoData.go | 11 +- logic/HashCloneFile.go | 10 +- logic/ListFiles.go | 9 +- logic/Thumbnail.go | 12 +-- logic/UpdateWebhook.go | 11 +- middlewares/IsAdmin.go | 3 +- middlewares/JwtStream.go | 47 +++++---- routes/api.go | 99 ++++++++--------- routes/web.go | 16 +-- 31 files changed, 318 insertions(+), 358 deletions(-) diff --git a/controllers/AuthCheckController.go b/controllers/AuthCheckController.go index d99dc8a..014f9d6 100755 --- a/controllers/AuthCheckController.go +++ b/controllers/AuthCheckController.go @@ -5,23 +5,22 @@ import ( "net/http" "strings" - "github.com/gofiber/fiber/v2" "github.com/labstack/echo/v4" ) func AuthCheck(c echo.Context) error { bearer := c.Request().Header.Get("Authorization") if bearer == "" { - return c.NoContent(fiber.StatusForbidden) + return c.NoContent(http.StatusForbidden) } bearerHeader := strings.Split(bearer, " ") tokenString := bearerHeader[len(bearerHeader)-1] token, claims, err := auth.VerifyJWT(tokenString) if err != nil { - return c.NoContent(fiber.StatusForbidden) + return c.NoContent(http.StatusForbidden) } if !token.Valid { - return c.NoContent(fiber.StatusForbidden) + return c.NoContent(http.StatusForbidden) } return c.JSON(http.StatusOK, echo.Map{ "username": claims.Username, diff --git a/controllers/DownloadVideoController.go b/controllers/DownloadVideoController.go index 441c311..958087a 100644 --- a/controllers/DownloadVideoController.go +++ b/controllers/DownloadVideoController.go @@ -12,7 +12,6 @@ import ( "sync" "time" - "github.com/gofiber/fiber/v2" "github.com/labstack/echo/v4" ) @@ -91,11 +90,11 @@ func DownloadVideoController(c echo.Context) error { return nil } - c.Response().Header().Add(fiber.HeaderContentType, "video/x-matroska") - c.Response().Header().Add(fiber.HeaderTransferEncoding, "chunked") - c.Response().Header().Add(fiber.HeaderTrailer, "AtEnd") - c.Response().Header().Add(fiber.HeaderCacheControl, "no-cache") - c.Response().Header().Add(fiber.HeaderContentDisposition, `attachment; filename="video.mkv"`) + c.Response().Header().Add("Content-Type", "video/x-matroska") + c.Response().Header().Add("Transfer-Encoding", "chunked") + c.Response().Header().Add("Trailer", "AtEnd") + c.Response().Header().Add("Cache-Control", "no-cache") + c.Response().Header().Add("Content-Disposition", `attachment; filename="video.mkv"`) c.Response().Status = http.StatusOK var wg sync.WaitGroup @@ -134,6 +133,6 @@ func DownloadVideoController(c echo.Context) error { wg.Wait() cmd.Wait() - c.Response().Header().Set(fiber.HeaderContentLength, fmt.Sprintf("%d", c.Response().Size)) + c.Response().Header().Set("Content-Length", fmt.Sprintf("%d", c.Response().Size)) return nil } diff --git a/controllers/ViewIndexController.go b/controllers/ViewIndexController.go index e755cc7..82724a9 100755 --- a/controllers/ViewIndexController.go +++ b/controllers/ViewIndexController.go @@ -7,21 +7,20 @@ import ( "fmt" "net/http" - "github.com/gofiber/fiber/v2" "github.com/labstack/echo/v4" ) func ViewIndex(c echo.Context) error { var link models.Link if res := inits.DB.First(&link); res.Error != nil { - return c.Render(http.StatusOK, "index", fiber.Map{ + return c.Render(http.StatusOK, "index", echo.Map{ "ExampleVideo": fmt.Sprintf("/%v", "notfound"), "AppName": config.ENV.AppName, "ProjectDocumentation": config.ENV.ProjectDocumentation, "ProjectDownload": config.ENV.ProjectDownload, }) } - return c.Render(http.StatusOK, "index", fiber.Map{ + return c.Render(http.StatusOK, "index", echo.Map{ "ExampleVideo": fmt.Sprintf("/%v", link.UUID), "AppName": config.ENV.AppName, "ProjectDocumentation": config.ENV.ProjectDocumentation, diff --git a/go.mod b/go.mod index 398ed81..06e4b8d 100755 --- a/go.mod +++ b/go.mod @@ -5,12 +5,13 @@ go 1.20 require ( github.com/dpapathanasiou/go-recaptcha v0.0.0-20190121160230-be5090b17804 github.com/go-playground/validator/v10 v10.16.0 - github.com/gofiber/template/html/v2 v2.0.5 github.com/golang-jwt/jwt/v5 v5.2.0 + github.com/labstack/echo/v4 v4.11.4 github.com/patrickmn/go-cache v2.1.0+incompatible github.com/thanhpk/randstr v1.0.6 github.com/thatisuday/commando v1.0.4 golang.org/x/term v0.16.0 + golang.org/x/time v0.5.0 gopkg.in/vansante/go-ffprobe.v2 v2.1.1 gorm.io/driver/sqlite v1.5.4 gorm.io/gorm v1.25.5 @@ -20,8 +21,8 @@ require ( github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/go-ole/go-ole v1.2.6 // indirect - github.com/gofiber/template v1.8.2 // indirect - github.com/gofiber/utils v1.1.0 // indirect + github.com/golang-jwt/jwt v3.2.2+incompatible // indirect + github.com/labstack/gommon v0.4.2 // indirect github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect @@ -29,32 +30,24 @@ require ( github.com/thatisuday/clapper v1.0.10 // indirect github.com/tklauser/go-sysconf v0.3.12 // indirect github.com/tklauser/numcpus v0.6.1 // indirect + github.com/valyala/fasttemplate v1.2.2 // indirect github.com/yusufpapurcu/wmi v1.2.3 // indirect golang.org/x/net v0.20.0 // indirect ) require ( - github.com/andybalholm/brotli v1.0.6 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/gofiber/fiber/v2 v2.51.0 github.com/google/uuid v1.5.0 github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/kirari04/go-hcaptcha v0.0.0-20230322135436-9fe4847aa674 - github.com/klauspost/compress v1.17.4 // indirect github.com/leodido/go-urn v1.2.4 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect - github.com/mattn/go-runewidth v0.0.15 // indirect github.com/mattn/go-sqlite3 v1.14.19 // indirect - github.com/philhofer/fwd v1.1.2 // indirect - github.com/rivo/uniseg v0.4.4 // indirect github.com/shirou/gopsutil/v3 v3.23.12 - github.com/tinylib/msgp v1.1.9 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect - github.com/valyala/fasthttp v1.51.0 // indirect - github.com/valyala/tcplisten v1.0.0 // indirect golang.org/x/crypto v0.18.0 golang.org/x/sys v0.16.0 // indirect golang.org/x/text v0.14.0 // indirect diff --git a/go.sum b/go.sum index e6d39ea..2cf008e 100755 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI= -github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= @@ -17,14 +15,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE= github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= -github.com/gofiber/fiber/v2 v2.51.0 h1:JNACcZy5e2tGApWB2QrRpenTWn0fq0hkFm6k0C86gKQ= -github.com/gofiber/fiber/v2 v2.51.0/go.mod h1:xaQRZQJGqnKOQnbQw+ltvku3/h8QxvNi8o6JiJ7Ll0U= -github.com/gofiber/template v1.8.2 h1:PIv9s/7Uq6m+Fm2MDNd20pAFFKt5wWs7ZBd8iV9pWwk= -github.com/gofiber/template v1.8.2/go.mod h1:bs/2n0pSNPOkRa5VJ8zTIvedcI/lEYxzV3+YPXdBvq8= -github.com/gofiber/template/html/v2 v2.0.5 h1:BKLJ6Qr940NjntbGmpO3zVa4nFNGDCi/IfUiDB9OC20= -github.com/gofiber/template/html/v2 v2.0.5/go.mod h1:RCF14eLeQDCSUPp0IGc2wbSSDv6yt+V54XB/+Unz+LM= -github.com/gofiber/utils v1.1.0 h1:vdEBpn7AzIUJRhe+CiTOJdUcTg4Q9RK+pEa0KPbLdrM= -github.com/gofiber/utils v1.1.0/go.mod h1:poZpsnhBykfnY1Mc0KeEa6mSHrS3dV0+oBWyeQmb2e0= +github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= +github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw= github.com/golang-jwt/jwt/v5 v5.2.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= @@ -39,8 +31,10 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/kirari04/go-hcaptcha v0.0.0-20230322135436-9fe4847aa674 h1:z3AP4Q8W3AXlQ2M7OkWY8//hEYRjmGmImFn8wyhOwsM= github.com/kirari04/go-hcaptcha v0.0.0-20230322135436-9fe4847aa674/go.mod h1:BNgLe/A5uGw3u7OF7+Wcpxsf4VksmpmdPPIfGjRS/1I= -github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4= -github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= +github.com/labstack/echo/v4 v4.11.4 h1:vDZmA+qNeh1pd/cCkEicDMrjtrnMGQ1QFI9gWN1zGq8= +github.com/labstack/echo/v4 v4.11.4/go.mod h1:noh7EvLwqDsmh/X/HWKPUl1AjzJrhyptRyEbQJfxen8= +github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= +github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU= github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4= @@ -50,22 +44,15 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= -github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI= github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= -github.com/philhofer/fwd v1.1.2 h1:bnDivRJ1EWPjUIRXV5KfORO897HTbpFAQddBdE8t7Gw= -github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= -github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= -github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/shirou/gopsutil/v3 v3.23.12 h1:z90NtUkp3bMtmICZKpC4+WaknU1eXtp5vtbQ11DgpE4= github.com/shirou/gopsutil/v3 v3.23.12/go.mod h1:1FrWgea594Jp7qmjHUUPlJDTPgcsb9mGnXDxavtikzM= github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM= @@ -86,18 +73,14 @@ github.com/thatisuday/clapper v1.0.10 h1:1EkqE/nb4npp8DuTKnpvVzO/Mcac9lOPND34uUK github.com/thatisuday/clapper v1.0.10/go.mod h1:FQGIg8q2uzeI+3SUS82YKF4E3KexkHStbiK4qTfDknM= github.com/thatisuday/commando v1.0.4 h1:aNdH9tvmx2EPG6rT3NTQOV/qFYPf4Ap4Spo+q+n9Ois= github.com/thatisuday/commando v1.0.4/go.mod h1:ODGz6jwJs4QqhLJtCjRRs8xIrmLLMdatYYddP+v1b4E= -github.com/tinylib/msgp v1.1.9 h1:SHf3yoO2sGA0veCJeCBYLHuttAVFHGm2RHgNodW7wQU= -github.com/tinylib/msgp v1.1.9/go.mod h1:BCXGB54lDD8qUEPmiG0cQQUANC4IUQyB2ItS2UDlO/k= github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU= github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI= github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk= github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= -github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= -github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g= -github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= -github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= +github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= +github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw= github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= @@ -117,6 +100,8 @@ golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE= golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= +golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/vansante/go-ffprobe.v2 v2.1.1 h1:DIh5fMn+tlBvG7pXyUZdemVmLdERnf2xX6XOFF+0BBU= diff --git a/inits/Server.go b/inits/Server.go index 92caf45..c4328ea 100755 --- a/inits/Server.go +++ b/inits/Server.go @@ -2,32 +2,36 @@ package inits import ( "ch/kirari04/videocms/config" - "errors" + "io" "log" + "net" + "net/http" "os" "strings" - "time" + "text/template" - "github.com/gofiber/fiber/v2" - "github.com/gofiber/fiber/v2/middleware/compress" - "github.com/gofiber/fiber/v2/middleware/cors" - "github.com/gofiber/fiber/v2/middleware/etag" - "github.com/gofiber/fiber/v2/middleware/logger" - "github.com/gofiber/fiber/v2/middleware/recover" - "github.com/gofiber/template/html/v2" + "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" ) -var App *fiber.App -var Api fiber.Router +var App *echo.Echo +var Api echo.Group var logFile *os.File +type Template struct { + templates *template.Template +} + +func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error { + return t.templates.ExecuteTemplate(w, name, data) +} + func Server() { - engine := html.New("./views", ".html") - engine.Reload(*config.ENV.ReloadHtml) + htmlTemplate := &Template{ + templates: template.Must(template.ParseGlob("./views/*.html")), + } trustedProxies := []string{} - trustedProxiesEnabled := false if *config.ENV.CloudflareEnabled { - trustedProxiesEnabled = true trustedProxies = append(trustedProxies, []string{ "173.245.48.0/20", "103.21.244.0/22", @@ -53,96 +57,68 @@ func Server() { "2c0f:f248::/32", }...) } + app := echo.New() + app.Renderer = htmlTemplate + trustOptions := []echo.TrustOption{ + echo.TrustLoopback(false), // e.g. ipv4 start with 127. + echo.TrustLinkLocal(false), // e.g. ipv4 start with 169.254 + echo.TrustPrivateNet(false), // e.g. ipv4 start with 10. or 192.168 + } + for _, trustedIpRanges := range trustedProxies { + _, ipNet, err := net.ParseCIDR(trustedIpRanges) + if err != nil { + app.Logger.Error("Failed to parse ip range", err) + continue + } + trustOptions = append(trustOptions, echo.TrustIPRange(ipNet)) + } - app := fiber.New(fiber.Config{ - - Prefork: false, - CaseSensitive: true, - StrictRouting: true, - ServerHeader: "Videocms", - AppName: config.ENV.AppName, - IdleTimeout: time.Minute, - ReadTimeout: time.Minute * 10, - WriteTimeout: time.Minute * 10, - BodyLimit: int(config.ENV.MaxPostSize), //5gb - Views: engine, - ErrorHandler: func(c *fiber.Ctx, err error) error { - // Status code defaults to 500 - code := fiber.StatusInternalServerError - - // Retrieve the custom status code if it's a *fiber.Error - var e *fiber.Error - if errors.As(err, &e) { - code = e.Code - } + app.IPExtractor = echo.ExtractIPFromXFFHeader(trustOptions...) + app.HTTPErrorHandler = func(err error, c echo.Context) { + // Status code defaults to 500 + code := http.StatusInternalServerError - if code == fiber.StatusInternalServerError { - body := "\nBody hidden because too big" - if len(c.Request().Body()) <= 5000 { - body = string(c.Request().Body()) - } else { - body = string(c.Request().Body()[0:5000]) - } - log.Printf("InternalServerError: %v \n{%v}{%v}\n%v\n\n", err, c.IP(), c.Request().URI(), body) - } + // Retrieve the custom status code if it's a *echo.HTTPError + if he, ok := err.(*echo.HTTPError); ok { + code = he.Code + } - return c.SendStatus(code) - }, - TrustedProxies: trustedProxies, - EnableTrustedProxyCheck: trustedProxiesEnabled, - }) + if code == http.StatusInternalServerError { + c.Logger().Error(err) + } + } // recovering from panics - app.Use(recover.New(recover.Config{})) + app.Use(middleware.Recover()) // Compression middleware - app.Use(compress.New(compress.Config{ - Next: func(c *fiber.Ctx) bool { + app.Use(middleware.GzipWithConfig(middleware.GzipConfig{ + Skipper: func(c echo.Context) bool { res := strings.HasPrefix(c.Path(), config.ENV.FolderVideoQualitysPub) if res { - c.Append("Compress", "LevelDisabled") + c.Response().Header().Add("Compress", "LevelDisabled") } else { - c.Append("Compress", "LevelBestCompression") + c.Response().Header().Add("Compress", "LevelBestCompression") } return res }, - Level: compress.LevelBestCompression, // 1 })) // cors configuration - app.Use(cors.New(cors.Config{ - AllowOrigins: config.ENV.CorsAllowOrigins, - AllowHeaders: config.ENV.CorsAllowHeaders, + app.Use(middleware.CORSWithConfig(middleware.CORSConfig{ + AllowOrigins: []string{config.ENV.CorsAllowOrigins}, + AllowHeaders: []string{config.ENV.CorsAllowHeaders}, AllowCredentials: *config.ENV.CorsAllowCredentials, })) - // caches response to be more efficient and save bandwidth - app.Use(etag.New()) - - // Loggin into file - file, err := os.OpenFile("./logs/access.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) - if err != nil { - log.Panicf("error opening file: %v", err) - } - logFile = file - // using nginxs logformat: https://docs.nginx.com/nginx-amplify/metrics-metadata/nginx-metrics/#additional-nginx-metrics - app.Use(logger.New(logger.Config{ - Format: "${ip} - - [${time}] \"${method} ${url} ${protocol}\" " + - "${status} ${bytesSent} \"${referer}\" " + - "\"${ua}\" \"${ips}\" " + - "\"${host}\" sn=\"${port}\" " + - "rt=${latency} " + - "ua=\"${route}\" us=\"${status}\" " + - "ut=\"0\" ul=\"${bytesReceived}\" " + - "cs=${error}\n", - Output: file, - })) + // Logging + app.Use(middleware.Logger()) App = app - Api = app.Group("/api") + Api = *app.Group("/api") } func ServerStart() { defer logFile.Close() - log.Fatal(App.Listen(config.ENV.Host)) + log.Fatal(App.Start(config.ENV.Host)) } diff --git a/logic/CreateFile.go b/logic/CreateFile.go index d4c60ec..14ef812 100755 --- a/logic/CreateFile.go +++ b/logic/CreateFile.go @@ -10,14 +10,15 @@ import ( "fmt" "log" "math" + "net/http" "os" "slices" "strconv" "strings" "time" - "github.com/gofiber/fiber/v2" "github.com/google/uuid" + "github.com/labstack/echo/v4" "gopkg.in/vansante/go-ffprobe.v2" "gorm.io/gorm" ) @@ -27,7 +28,7 @@ func CreateFile(fromFile *string, toFolder uint, fileName string, fileId string, if toFolder > 0 { res := inits.DB.First(&models.Folder{}, toFolder) if res.Error != nil { - return fiber.StatusBadRequest, nil, false, errors.New("parent folder doesn't exist") + return http.StatusBadRequest, nil, false, errors.New("parent folder doesn't exist") } } @@ -35,7 +36,7 @@ func CreateFile(fromFile *string, toFolder uint, fileName string, fileId string, FileHash, err := helpers.HashFile(*fromFile) if err != nil { log.Printf("Failed to create hash from file: %v", err) - return fiber.StatusInternalServerError, nil, false, fiber.ErrInternalServerError + return http.StatusInternalServerError, nil, false, echo.ErrInternalServerError } // check file hash with database @@ -53,7 +54,7 @@ func CreateFile(fromFile *string, toFolder uint, fileName string, fileId string, // check if file extension is supported if !slices.Contains(config.EXTENSIONS, strings.ToLower(fileExt)) { - return fiber.StatusBadRequest, nil, false, errors.New("Video extension is not supported") + return http.StatusBadRequest, nil, false, errors.New("Video extension is not supported") } if err := os.Rename(oldOutPath, newOutPath); err != nil { @@ -68,7 +69,7 @@ func CreateFile(fromFile *string, toFolder uint, fileName string, fileId string, data, err := ffprobe.ProbeURL(ctx, *fromFile) if err != nil { log.Printf("Error getting data using ffprobe: %v", err) - return fiber.StatusInternalServerError, nil, false, fiber.ErrInternalServerError + return http.StatusInternalServerError, nil, false, echo.ErrInternalServerError } // proobe type dataStreams := data.StreamType(ffprobe.StreamAny) @@ -82,7 +83,7 @@ func CreateFile(fromFile *string, toFolder uint, fileName string, fileId string, hasVideoStream := false if videoDuration == 0 || videoDuration > 60*60*10 { - return fiber.StatusBadRequest, nil, false, errors.New("invalid video duration") + return http.StatusBadRequest, nil, false, errors.New("invalid video duration") } // loop over streams in file @@ -106,7 +107,7 @@ func CreateFile(fromFile *string, toFolder uint, fileName string, fileId string, //check if video stream exists if !hasVideoStream { - return fiber.StatusBadRequest, nil, false, errors.New("uploaded file doesn't contain any video stream") + return http.StatusBadRequest, nil, false, errors.New("uploaded file doesn't contain any video stream") } // set average framerate @@ -120,7 +121,7 @@ func CreateFile(fromFile *string, toFolder uint, fileName string, fileId string, // check average framerate if avgFramerate > 120 || avgFramerate < 0 { - return fiber.StatusBadRequest, nil, false, errors.New("invalid video framerate") + return http.StatusBadRequest, nil, false, errors.New("invalid video framerate") } // check video stream data (resolution) @@ -131,15 +132,15 @@ func CreateFile(fromFile *string, toFolder uint, fileName string, fileId string, videoStream.Width, videoStream.Height, ) - return fiber.StatusInternalServerError, nil, false, errors.New(fiber.ErrInternalServerError.Message) + return http.StatusInternalServerError, nil, false, echo.ErrInternalServerError } // check if resolution is in scope of supported sizes if videoStream.Height > 8000 || videoStream.Width > 8000 { - return fiber.StatusBadRequest, nil, false, errors.New("video resolution is too high") + return http.StatusBadRequest, nil, false, errors.New("video resolution is too high") } if videoStream.Height < 50 || videoStream.Width < 50 { - return fiber.StatusBadRequest, nil, false, errors.New("video resolution is too low") + return http.StatusBadRequest, nil, false, errors.New("video resolution is too low") } // declare required variables for database insert @@ -148,7 +149,7 @@ func CreateFile(fromFile *string, toFolder uint, fileName string, fileId string, if videoDuration == 0 { log.Printf("Error getting videoDuration: %v %v", err, dataStreams) - return fiber.StatusInternalServerError, nil, false, errors.New(fiber.ErrInternalServerError.Message) + return http.StatusInternalServerError, nil, false, echo.ErrInternalServerError } thumbnailFileName := "4x4.webp" @@ -201,7 +202,7 @@ func CreateFile(fromFile *string, toFolder uint, fileName string, fileId string, return nil }); err != nil { log.Printf("Error saving file & link in database: %v", err) - return fiber.StatusInternalServerError, nil, false, errors.New(fiber.ErrInternalServerError.Message) + return http.StatusInternalServerError, nil, false, echo.ErrInternalServerError } // save subtitle data to database so they can be converted later @@ -243,7 +244,7 @@ func CreateFile(fromFile *string, toFolder uint, fileName string, fileId string, } if res := inits.DB.Create(&dbSubtitle); res.Error != nil { log.Printf("Error saving Subtitle in database: %v", res.Error) - return fiber.StatusInternalServerError, nil, false, errors.New(fiber.ErrInternalServerError.Message) + return http.StatusInternalServerError, nil, false, echo.ErrInternalServerError } } } @@ -285,7 +286,7 @@ func CreateFile(fromFile *string, toFolder uint, fileName string, fileId string, Error: "", }); res.Error != nil { log.Printf("Error saving Audio in database: %v", res.Error) - return fiber.StatusInternalServerError, nil, false, errors.New(fiber.ErrInternalServerError.Message) + return http.StatusInternalServerError, nil, false, echo.ErrInternalServerError } } } @@ -323,7 +324,7 @@ func CreateFile(fromFile *string, toFolder uint, fileName string, fileId string, Error: "", }); res.Error != nil { log.Printf("Error saving quality in database: %v\n", res.Error) - return fiber.StatusInternalServerError, nil, false, errors.New(fiber.ErrInternalServerError.Message) + return http.StatusInternalServerError, nil, false, echo.ErrInternalServerError } } } else { @@ -347,12 +348,12 @@ func CreateFile(fromFile *string, toFolder uint, fileName string, fileId string, Error: "", }); res.Error != nil { log.Printf("Error saving quality in database: %v\n", res.Error) - return fiber.StatusInternalServerError, nil, false, errors.New(fiber.ErrInternalServerError.Message) + return http.StatusInternalServerError, nil, false, echo.ErrInternalServerError } } } } // return link to file - return fiber.StatusOK, &dbLink, false, nil + return http.StatusOK, &dbLink, false, nil } diff --git a/logic/CreateFolder.go b/logic/CreateFolder.go index 704182d..a14e8c2 100755 --- a/logic/CreateFolder.go +++ b/logic/CreateFolder.go @@ -5,8 +5,9 @@ import ( "ch/kirari04/videocms/models" "errors" "log" + "net/http" - "github.com/gofiber/fiber/v2" + "github.com/labstack/echo/v4" ) func CreateFolder(folderName string, toFolder uint, userId uint) (status int, newFolder *models.Folder, err error) { @@ -14,7 +15,7 @@ func CreateFolder(folderName string, toFolder uint, userId uint) (status int, ne if toFolder > 0 { res := inits.DB.First(&models.Folder{}, toFolder) if res.Error != nil { - return fiber.StatusBadRequest, nil, errors.New("parent folder doesn't exist") + return http.StatusBadRequest, nil, errors.New("parent folder doesn't exist") } } @@ -27,8 +28,8 @@ func CreateFolder(folderName string, toFolder uint, userId uint) (status int, ne if res := inits.DB.Model(&models.Folder{}).Create(&folder); res.Error != nil { log.Printf("Error creating new folder: %v", res.Error) - return fiber.StatusInternalServerError, nil, errors.New(fiber.ErrInternalServerError.Message) + return http.StatusInternalServerError, nil, echo.ErrInternalServerError } - return fiber.StatusOK, &folder, nil + return http.StatusOK, &folder, nil } diff --git a/logic/CreateUploadChunck.go b/logic/CreateUploadChunck.go index 106548f..41842ac 100755 --- a/logic/CreateUploadChunck.go +++ b/logic/CreateUploadChunck.go @@ -9,9 +9,10 @@ import ( "fmt" "log" "math" + "net/http" "os" - "github.com/gofiber/fiber/v2" + "github.com/labstack/echo/v4" ) func CreateUploadChunck(index uint, sessionToken string, fromFile string) (status int, response string, err error) { @@ -19,14 +20,11 @@ func CreateUploadChunck(index uint, sessionToken string, fromFile string) (statu token, claims, err := helpers.VerifyDynamicJWT(sessionToken, &models.UploadSessionClaims{}, []byte(config.ENV.JwtUploadSecretKey)) if err != nil || claims == nil { log.Printf("err: %v", err) - return fiber.StatusBadRequest, "", errors.New("broken upload session token") + return http.StatusBadRequest, "", errors.New("broken upload session token") } if !token.Valid { - return fiber.StatusBadRequest, "", errors.New("invalid upload session token") + return http.StatusBadRequest, "", errors.New("invalid upload session token") } - // if (*claims).UserID != userId { - // return fiber.StatusForbidden, "", fiber.ErrForbidden - // } //check if session still active uploadSession := models.UploadSession{} @@ -34,28 +32,28 @@ func CreateUploadChunck(index uint, sessionToken string, fromFile string) (statu Where(&models.UploadSession{ UUID: (*claims).UUID, }).First(&uploadSession); res.Error != nil { - return fiber.StatusNotFound, "", errors.New("upload session not found") + return http.StatusNotFound, "", errors.New("upload session not found") } // check chunck size chunckFile, err := os.Open(fromFile) if err != nil { log.Printf("Failed to open uploaded chunck: %v", err) - return fiber.StatusInternalServerError, "", fiber.ErrInternalServerError + return http.StatusInternalServerError, "", echo.ErrInternalServerError } chunckFileStat, err := chunckFile.Stat() if err != nil { log.Printf("Failed to read stat from uploaded chunck: %v", err) - return fiber.StatusInternalServerError, "", fiber.ErrInternalServerError + return http.StatusInternalServerError, "", echo.ErrInternalServerError } maxchunckFileSize := int64(math.Ceil(float64(uploadSession.Size) / float64(uploadSession.ChunckCount))) if chunckFileStat.Size() > maxchunckFileSize+100 { - return fiber.StatusRequestEntityTooLarge, "", fiber.ErrRequestEntityTooLarge + return http.StatusRequestEntityTooLarge, "", echo.ErrStatusRequestEntityTooLarge } // check chunck count if int(index) >= uploadSession.ChunckCount { - return fiber.StatusBadRequest, "", fmt.Errorf("chunck index is too high: chunck index: %d vs max index: %d", index, uploadSession.ChunckCount) + return http.StatusBadRequest, "", fmt.Errorf("chunck index is too high: chunck index: %d vs max index: %d", index, uploadSession.ChunckCount) } /* @@ -65,7 +63,7 @@ func CreateUploadChunck(index uint, sessionToken string, fromFile string) (statu chunckPath := fmt.Sprintf("%s/%v.chunck", uploadSession.SessionFolder, index) if err := os.Rename(fromFile, chunckPath); err != nil { log.Printf("Failed to move uploaded chunck into upload session folder: %v", err) - return fiber.StatusInternalServerError, "", fiber.ErrInternalServerError + return http.StatusInternalServerError, "", echo.ErrInternalServerError } existingUploadedChunck := models.UploadChunck{} @@ -76,8 +74,8 @@ func CreateUploadChunck(index uint, sessionToken string, fromFile string) (statu }).FirstOrCreate(&existingUploadedChunck); res.Error != nil { log.Printf("Failed to add uploaded chunck into db: %v", res.Error) log.Printf("Removing Chunck: %v", os.Remove(chunckPath)) - return fiber.StatusInternalServerError, "", fiber.ErrInternalServerError + return http.StatusInternalServerError, "", echo.ErrInternalServerError } - return fiber.StatusOK, "ok", nil + return http.StatusOK, "ok", nil } diff --git a/logic/CreateUploadFile.go b/logic/CreateUploadFile.go index 58e74e6..6a8171b 100755 --- a/logic/CreateUploadFile.go +++ b/logic/CreateUploadFile.go @@ -9,10 +9,11 @@ import ( "fmt" "io" "log" + "net/http" "os" - "github.com/gofiber/fiber/v2" "github.com/google/uuid" + "github.com/labstack/echo/v4" ) /* @@ -22,13 +23,13 @@ func CreateUploadFile(sessionToken string, userId uint) (status int, response *m token, claims, err := helpers.VerifyDynamicJWT(sessionToken, &models.UploadSessionClaims{}, []byte(config.ENV.JwtUploadSecretKey)) if err != nil && claims != nil { log.Printf("err: %v", err) - return fiber.StatusBadRequest, nil, errors.New("broken upload session token") + return http.StatusBadRequest, nil, errors.New("broken upload session token") } if !token.Valid { - return fiber.StatusBadRequest, nil, errors.New("invalid upload session token") + return http.StatusBadRequest, nil, errors.New("invalid upload session token") } if (*claims).UserID != userId { - return fiber.StatusForbidden, nil, fiber.ErrForbidden + return http.StatusForbidden, nil, echo.ErrForbidden } //check if session still active @@ -38,7 +39,7 @@ func CreateUploadFile(sessionToken string, userId uint) (status int, response *m UUID: (*claims).UUID, UserID: userId, }).First(&uploadSession); res.Error != nil { - return fiber.StatusNotFound, nil, errors.New("upload session not found") + return http.StatusNotFound, nil, errors.New("upload session not found") } //list all chuncks @@ -50,10 +51,10 @@ func CreateUploadFile(sessionToken string, userId uint) (status int, response *m Order("`index` ASC"). Find(&uploadChuncks); res.Error != nil { log.Printf("Failed to create find upload chuncks: %v", res.Error) - return fiber.StatusNotFound, nil, errors.New("upload chuncks not found") + return http.StatusNotFound, nil, errors.New("upload chuncks not found") } if len(uploadChuncks) != uploadSession.ChunckCount { - return fiber.StatusBadRequest, nil, fmt.Errorf("missing Chuncks: uploaded %v, required %v", len(uploadChuncks), uploadSession.ChunckCount) + return http.StatusBadRequest, nil, fmt.Errorf("missing Chuncks: uploaded %v, required %v", len(uploadChuncks), uploadSession.ChunckCount) } // delete any missing files or sessions inside database if anything failes or it successfully finishes @@ -64,7 +65,7 @@ func CreateUploadFile(sessionToken string, userId uint) (status int, response *m finalFile, err := os.OpenFile(finalFilePath, os.O_CREATE|os.O_WRONLY, 0766) if err != nil { log.Printf("Failed to create final file: %v", err) - return fiber.StatusInternalServerError, nil, fiber.ErrInternalServerError + return http.StatusInternalServerError, nil, echo.ErrInternalServerError } // copy uploaded chuncks into final file @@ -73,33 +74,33 @@ func CreateUploadFile(sessionToken string, userId uint) (status int, response *m openedChunck, err := os.Open(uploadChunck.Path) if err != nil { log.Printf("Failed to read chunck %v: %v", uploadChunck.Path, err) - return fiber.StatusInternalServerError, nil, fiber.ErrInternalServerError + return http.StatusInternalServerError, nil, echo.ErrInternalServerError } n, err := io.Copy(finalFile, openedChunck) if err != nil { log.Printf("Failed to copy chunck %v: %v", uploadChunck.Path, err) - return fiber.StatusInternalServerError, nil, fiber.ErrInternalServerError + return http.StatusInternalServerError, nil, echo.ErrInternalServerError } written += n if err := openedChunck.Close(); err != nil { log.Printf("Failed to close chunck %v: %v", uploadChunck.Path, err) - return fiber.StatusInternalServerError, nil, fiber.ErrInternalServerError + return http.StatusInternalServerError, nil, echo.ErrInternalServerError } } if err := finalFile.Close(); err != nil { log.Printf("Failed to close final file: %v", err) - return fiber.StatusInternalServerError, nil, fiber.ErrInternalServerError + return http.StatusInternalServerError, nil, echo.ErrInternalServerError } // check file size finalFilePathInfo, err := os.Stat(finalFilePath) if err != nil { log.Printf("Failed to read filestat of final file: %v", err) - return fiber.StatusInternalServerError, nil, fiber.ErrInternalServerError + return http.StatusInternalServerError, nil, echo.ErrInternalServerError } if finalFilePathInfo.Size() != uploadSession.Size { - return fiber.StatusConflict, nil, fmt.Errorf("the uploaded file size doesnt match with the uploaded file: server %v, client %v", finalFilePathInfo.Size(), uploadSession.Size) + return http.StatusConflict, nil, fmt.Errorf("the uploaded file size doesnt match with the uploaded file: server %v, client %v", finalFilePathInfo.Size(), uploadSession.Size) } // create file @@ -107,7 +108,7 @@ func CreateUploadFile(sessionToken string, userId uint) (status int, response *m filePath := fmt.Sprintf("%s/%s.%s", config.ENV.FolderVideoUploadsPriv, fileId, "tmp") if err := os.Rename(finalFilePath, filePath); err != nil { log.Printf("Failed to copy final file to destination: %v", err) - return fiber.StatusInternalServerError, nil, fiber.ErrInternalServerError + return http.StatusInternalServerError, nil, echo.ErrInternalServerError } status, dbLink, cloned, err := CreateFile(&filePath, uploadSession.ParentFolderID, uploadSession.Name, fileId, uploadSession.Size, userId) if err != nil { diff --git a/logic/CreateUploadSession.go b/logic/CreateUploadSession.go index 67598ae..e78b2a3 100755 --- a/logic/CreateUploadSession.go +++ b/logic/CreateUploadSession.go @@ -9,10 +9,11 @@ import ( "fmt" "log" "math" + "net/http" "os" "time" - "github.com/gofiber/fiber/v2" + "github.com/labstack/echo/v4" ) type CreateUploadSessionResponse struct { @@ -30,7 +31,7 @@ use the split delay in the db lookup to have more concurrent upload sessions the func CreateUploadSession(toFolder uint, fileName string, uploadSessionUUID string, fileSize int64, userId uint) (status int, response *CreateUploadSessionResponse, err error) { if helpers.UserRequestAsyncObj.Blocked(userId) { - return fiber.StatusTooManyRequests, nil, errors.New("wait until the previous delete request finished") + return http.StatusTooManyRequests, nil, errors.New("wait until the previous delete request finished") } helpers.UserRequestAsyncObj.Start(userId) defer helpers.UserRequestAsyncObj.End(userId) @@ -39,20 +40,20 @@ func CreateUploadSession(toFolder uint, fileName string, uploadSessionUUID strin if toFolder > 0 { res := inits.DB.First(&models.Folder{}, toFolder) if res.Error != nil { - return fiber.StatusBadRequest, nil, errors.New("parent folder doesn't exist") + return http.StatusBadRequest, nil, errors.New("parent folder doesn't exist") } } //check requested filesize size if fileSize > config.ENV.MaxUploadFilesize { - return fiber.StatusRequestEntityTooLarge, nil, fmt.Errorf("exceeded max upload filesize: %v", config.ENV.MaxUploadFilesize) + return http.StatusRequestEntityTooLarge, nil, fmt.Errorf("exceeded max upload filesize: %v", config.ENV.MaxUploadFilesize) } // get user settings User, err := helpers.GetUser(userId) if err != nil { log.Printf("Failed to fetch user %v: %v", userId, err) - return fiber.StatusInternalServerError, nil, fiber.ErrInternalServerError + return http.StatusInternalServerError, nil, echo.ErrInternalServerError } //check for active upload sessions @@ -63,11 +64,11 @@ func CreateUploadSession(toFolder uint, fileName string, uploadSessionUUID strin UserID: userId, }).Count(&activeUploadSessions); res.Error != nil { log.Printf("Failed to calc activeUploadSessions: %v : %v", activeUploadSessions, res.Error) - return fiber.StatusInternalServerError, nil, fiber.ErrInternalServerError + return http.StatusInternalServerError, nil, echo.ErrInternalServerError } if activeUploadSessions >= config.ENV.MaxUploadSessions { if activeUploadSessions >= User.Settings.UploadSessionsMax { - return fiber.StatusBadRequest, nil, fmt.Errorf("exceeded max upload sessions: %v", config.ENV.MaxUploadSessions) + return http.StatusBadRequest, nil, fmt.Errorf("exceeded max upload sessions: %v", config.ENV.MaxUploadSessions) } } @@ -75,7 +76,7 @@ func CreateUploadSession(toFolder uint, fileName string, uploadSessionUUID strin sessionFolder := fmt.Sprintf("%s/%s", config.ENV.FolderVideoUploadsPriv, uploadSessionUUID) if err := os.MkdirAll(sessionFolder, 0766); err != nil { log.Printf("Failed to create upload session folder: %v : %v", sessionFolder, err) - return fiber.StatusInternalServerError, nil, fiber.ErrInternalServerError + return http.StatusInternalServerError, nil, echo.ErrInternalServerError } // create session @@ -91,7 +92,7 @@ func CreateUploadSession(toFolder uint, fileName string, uploadSessionUUID strin } if res := inits.DB.Create(&newSession); res.Error != nil { log.Printf("Failed to create new upload session: %v", res.Error) - return fiber.StatusInternalServerError, nil, fiber.ErrInternalServerError + return http.StatusInternalServerError, nil, echo.ErrInternalServerError } claims := models.UploadSessionClaims{ @@ -103,10 +104,10 @@ func CreateUploadSession(toFolder uint, fileName string, uploadSessionUUID strin token, expirationTime, err := helpers.GenerateDynamicJWT[models.UploadSessionClaims](&claims, maxUploadDuration, []byte(config.ENV.JwtUploadSecretKey)) if err != nil { log.Printf("Failed to generate jwt token for upload session: %v", err) - return fiber.StatusInternalServerError, nil, fiber.ErrInternalServerError + return http.StatusInternalServerError, nil, echo.ErrInternalServerError } - return fiber.StatusOK, &CreateUploadSessionResponse{ + return http.StatusOK, &CreateUploadSessionResponse{ Token: token, Expires: expirationTime, UUID: uploadSessionUUID, diff --git a/logic/CreateWebhook.go b/logic/CreateWebhook.go index 7c67b63..df73915 100755 --- a/logic/CreateWebhook.go +++ b/logic/CreateWebhook.go @@ -4,8 +4,9 @@ import ( "ch/kirari04/videocms/inits" "ch/kirari04/videocms/models" "log" + "net/http" - "github.com/gofiber/fiber/v2" + "github.com/labstack/echo/v4" ) func CreateWebhook(webhookValidation *models.WebhookCreateValidation, userID uint) (status int, response string, err error) { @@ -18,7 +19,7 @@ func CreateWebhook(webhookValidation *models.WebhookCreateValidation, userID uin UserID: userID, }); res.Error != nil { log.Printf("Failed to create webhook: %v", res.Error) - return fiber.StatusInternalServerError, "", fiber.ErrInternalServerError + return http.StatusInternalServerError, "", echo.ErrInternalServerError } - return fiber.StatusOK, "ok", nil + return http.StatusOK, "ok", nil } diff --git a/logic/DeleteFiles.go b/logic/DeleteFiles.go index 5ff73b8..f398426 100755 --- a/logic/DeleteFiles.go +++ b/logic/DeleteFiles.go @@ -7,16 +7,17 @@ import ( "errors" "fmt" "log" + "net/http" - "github.com/gofiber/fiber/v2" + "github.com/labstack/echo/v4" ) func DeleteFiles(fileValidation *models.LinksDeleteValidation, userID uint) (status int, err error) { if len(fileValidation.LinkIDs) == 0 { - return fiber.StatusBadRequest, errors.New("array LinkIDs is empty") + return http.StatusBadRequest, errors.New("array LinkIDs is empty") } if int64(len(fileValidation.LinkIDs)) > config.ENV.MaxItemsMultiDelete { - return fiber.StatusBadRequest, errors.New("max requested items exceeded") + return http.StatusBadRequest, errors.New("max requested items exceeded") } //check if requested files exists @@ -26,10 +27,10 @@ func DeleteFiles(fileValidation *models.LinksDeleteValidation, userID uint) (sta if res := inits.DB.First(&models.Link{ UserID: userID, }, LinkValidation.LinkID); res.Error != nil { - return fiber.StatusBadRequest, fmt.Errorf("linkID (%d) doesn't exist", LinkValidation.LinkID) + return http.StatusBadRequest, fmt.Errorf("linkID (%d) doesn't exist", LinkValidation.LinkID) } if linkIdDeleteMap[LinkValidation.LinkID] { - return fiber.StatusBadRequest, fmt.Errorf("the files have to be distinct. File %d is dublicate", LinkValidation.LinkID) + return http.StatusBadRequest, fmt.Errorf("the files have to be distinct. File %d is dublicate", LinkValidation.LinkID) } linkIdDeleteList = append(linkIdDeleteList, LinkValidation.LinkID) linkIdDeleteMap[LinkValidation.LinkID] = true @@ -38,8 +39,8 @@ func DeleteFiles(fileValidation *models.LinksDeleteValidation, userID uint) (sta // delete links if res := inits.DB.Delete(&models.Link{}, linkIdDeleteList); res.Error != nil { log.Printf("Failed to delete links: %v", res.Error) - return fiber.StatusInternalServerError, errors.New(fiber.ErrInternalServerError.Message) + return http.StatusInternalServerError, echo.ErrInternalServerError } - return fiber.StatusOK, nil + return http.StatusOK, nil } diff --git a/logic/DeleteFolders.go b/logic/DeleteFolders.go index 32e0890..33ef536 100755 --- a/logic/DeleteFolders.go +++ b/logic/DeleteFolders.go @@ -7,8 +7,7 @@ import ( "ch/kirari04/videocms/models" "errors" "fmt" - - "github.com/gofiber/fiber/v2" + "net/http" ) /* @@ -21,17 +20,17 @@ that should prevent the user from calling this method multiple times concurrentl func DeleteFolders(folderValidation *models.FoldersDeleteValidation, userID uint) (status int, err error) { if helpers.UserRequestAsyncObj.Blocked(userID) { - return fiber.StatusTooManyRequests, errors.New("wait until the previous delete request finished") + return http.StatusTooManyRequests, errors.New("wait until the previous delete request finished") } helpers.UserRequestAsyncObj.Start(userID) defer helpers.UserRequestAsyncObj.End(userID) if len(folderValidation.FolderIDs) == 0 { - return fiber.StatusBadRequest, errors.New("array FolderIDs is empty") + return http.StatusBadRequest, errors.New("array FolderIDs is empty") } if int64(len(folderValidation.FolderIDs)) > config.ENV.MaxItemsMultiDelete { - return fiber.StatusBadRequest, errors.New("max requested items exceeded") + return http.StatusBadRequest, errors.New("max requested items exceeded") } //check if requested folders exists @@ -43,7 +42,7 @@ func DeleteFolders(folderValidation *models.FoldersDeleteValidation, userID uint UserID: userID, } if res := inits.DB.First(&dbFolder, FolderValidation.FolderID); res.Error != nil { - return fiber.StatusBadRequest, fmt.Errorf("FolderID (%d) doesn't exist", FolderValidation.FolderID) + return http.StatusBadRequest, fmt.Errorf("FolderID (%d) doesn't exist", FolderValidation.FolderID) } // check if has same parent folder if i == 0 { @@ -51,7 +50,7 @@ func DeleteFolders(folderValidation *models.FoldersDeleteValidation, userID uint } if i > 0 { if parentFolderID != dbFolder.ParentFolderID { - return fiber.StatusBadRequest, fmt.Errorf( + return http.StatusBadRequest, fmt.Errorf( "all folders have to share the same parent folder. Folder %d doesnt: %d (required) vs %d (actual)", FolderValidation.FolderID, parentFolderID, @@ -61,7 +60,7 @@ func DeleteFolders(folderValidation *models.FoldersDeleteValidation, userID uint } if reqFolderIdDeleteMap[FolderValidation.FolderID] { - return fiber.StatusBadRequest, fmt.Errorf( + return http.StatusBadRequest, fmt.Errorf( "the folders have to be distinct. Folder %d is dublicate", FolderValidation.FolderID, ) @@ -93,7 +92,7 @@ func DeleteFolders(folderValidation *models.FoldersDeleteValidation, userID uint return status, fmt.Errorf("failed to delete all folders: %v", err) } - return fiber.StatusOK, nil + return http.StatusOK, nil } func listFolders(folderId uint, folders *[]uint, files *[]models.LinkDeleteValidation) { diff --git a/logic/DeleteWebhook.go b/logic/DeleteWebhook.go index e07eff6..0733f3e 100755 --- a/logic/DeleteWebhook.go +++ b/logic/DeleteWebhook.go @@ -5,8 +5,9 @@ import ( "ch/kirari04/videocms/models" "errors" "log" + "net/http" - "github.com/gofiber/fiber/v2" + "github.com/labstack/echo/v4" "gorm.io/gorm" ) @@ -17,15 +18,15 @@ func DeleteWebhook(validated *models.WebhookDeleteValidation, userID uint) (stat UserID: userID, }).First(&webhook, validated.WebhookID); res.Error != nil { if errors.Is(res.Error, gorm.ErrRecordNotFound) { - return fiber.StatusNotFound, "", fiber.ErrNotFound + return http.StatusNotFound, "", echo.ErrNotFound } log.Printf("Failed to query webhook %v: %v", validated.WebhookID, res.Error) - return fiber.StatusInternalServerError, "", fiber.ErrInternalServerError + return http.StatusInternalServerError, "", echo.ErrInternalServerError } if res := inits.DB.Delete(&webhook); res.Error != nil { log.Printf("Failed to delete webhook %v: %v", validated.WebhookID, res.Error) - return fiber.StatusInternalServerError, "", fiber.ErrInternalServerError + return http.StatusInternalServerError, "", echo.ErrInternalServerError } - return fiber.StatusOK, "ok", nil + return http.StatusOK, "ok", nil } diff --git a/logic/GetAccount.go b/logic/GetAccount.go index 7d06540..1e8be9b 100755 --- a/logic/GetAccount.go +++ b/logic/GetAccount.go @@ -6,9 +6,10 @@ import ( "errors" "fmt" "log" + "net/http" "time" - "github.com/gofiber/fiber/v2" + "github.com/labstack/echo/v4" "gorm.io/gorm" ) @@ -26,13 +27,13 @@ type GetAccountResponse struct { func GetAccount(userID uint) (status int, response *GetAccountResponse, err error) { if data, found := inits.Cache.Get(fmt.Sprintf("account-%d", userID)); found { res := data.(GetAccountResponse) - return fiber.StatusOK, &res, nil + return http.StatusOK, &res, nil } var dbUser models.User if res := inits.DB.Find(&dbUser, userID); res.Error != nil { log.Printf("Failed to query user: %v", res.Error) - return fiber.StatusInternalServerError, nil, errors.New(fiber.ErrInternalServerError.Message) + return http.StatusInternalServerError, nil, echo.ErrInternalServerError } type DBResponse struct { UploadedFiles int64 @@ -49,7 +50,7 @@ func GetAccount(userID uint) (status int, response *GetAccountResponse, err erro First(&dbUsed); res.Error != nil { if !errors.Is(res.Error, gorm.ErrRecordNotFound) { log.Printf("Failed to query UploadedFiles & StorageUsed: %v", res.Error) - return fiber.StatusInternalServerError, nil, errors.New(fiber.ErrInternalServerError.Message) + return http.StatusInternalServerError, nil, echo.ErrInternalServerError } } newResponse := GetAccountResponse{ @@ -65,5 +66,5 @@ func GetAccount(userID uint) (status int, response *GetAccountResponse, err erro // save in cache inits.Cache.Set(fmt.Sprintf("account-%d", userID), newResponse, time.Minute) - return fiber.StatusOK, &newResponse, nil + return http.StatusOK, &newResponse, nil } diff --git a/logic/GetAudioData.go b/logic/GetAudioData.go index 7b0570f..2091a4c 100755 --- a/logic/GetAudioData.go +++ b/logic/GetAudioData.go @@ -6,16 +6,15 @@ import ( "ch/kirari04/videocms/models" "errors" "fmt" + "net/http" "regexp" - - "github.com/gofiber/fiber/v2" ) func GetAudioData(requestValidation *models.AudioGetValidation) (status int, filePath *string, err error) { reFILE := regexp.MustCompile(`^audio[0-9]{0,4}\.(m3u8|ts|wav|mp3|ogg)$`) if !reFILE.MatchString(requestValidation.FILE) { - return fiber.StatusBadRequest, nil, errors.New("bad file format") + return http.StatusBadRequest, nil, errors.New("bad file format") } //translate link id to file id @@ -29,7 +28,7 @@ func GetAudioData(requestValidation *models.AudioGetValidation) (status int, fil UUID: requestValidation.UUID, }). First(&dbLink); dbRes.Error != nil { - return fiber.StatusNotFound, nil, errors.New("audio doesn't exist") + return http.StatusNotFound, nil, errors.New("audio doesn't exist") } //check if audio uuid exists @@ -41,9 +40,9 @@ func GetAudioData(requestValidation *models.AudioGetValidation) (status int, fil } } if !audioExists { - return fiber.StatusNotFound, nil, errors.New("audio doesn't exist") + return http.StatusNotFound, nil, errors.New("audio doesn't exist") } resPath := fmt.Sprintf("%s/%s/%s/%s", config.ENV.FolderVideoQualitysPriv, dbLink.File.UUID, requestValidation.AUDIOUUID, requestValidation.FILE) - return fiber.StatusOK, &resPath, nil + return http.StatusOK, &resPath, nil } diff --git a/logic/GetFile.go b/logic/GetFile.go index baf647d..a1de027 100755 --- a/logic/GetFile.go +++ b/logic/GetFile.go @@ -4,11 +4,11 @@ import ( "ch/kirari04/videocms/config" "ch/kirari04/videocms/inits" "ch/kirari04/videocms/models" - "errors" "fmt" + "net/http" "time" - "github.com/gofiber/fiber/v2" + "github.com/labstack/echo/v4" ) type GetFileRespQuali struct { @@ -64,7 +64,7 @@ func GetFile(LinkID uint, userID uint) (status int, fileData *GetFileResp, err e UserID: userID, }). First(&link, LinkID); res.Error != nil { - return fiber.StatusNotFound, nil, errors.New(fiber.ErrNotFound.Message) + return http.StatusNotFound, nil, echo.ErrNotFound } var Qualitys []GetFileRespQuali @@ -121,5 +121,5 @@ func GetFile(LinkID uint, userID uint) (status int, fileData *GetFileResp, err e Audios: Audios, } - return fiber.StatusOK, &response, nil + return http.StatusOK, &response, nil } diff --git a/logic/GetFileExample.go b/logic/GetFileExample.go index 379cad7..e50f595 100755 --- a/logic/GetFileExample.go +++ b/logic/GetFileExample.go @@ -3,15 +3,15 @@ package logic import ( "ch/kirari04/videocms/inits" "ch/kirari04/videocms/models" - "errors" + "net/http" - "github.com/gofiber/fiber/v2" + "github.com/labstack/echo/v4" ) func GetFileExample() (status int, response string, err error) { var link models.Link if res := inits.DB.First(&link); res.Error != nil { - return fiber.StatusNotFound, "", errors.New(fiber.ErrNotFound.Message) + return http.StatusNotFound, "", echo.ErrNotFound } - return fiber.StatusOK, link.UUID, nil + return http.StatusOK, link.UUID, nil } diff --git a/logic/GetM3u8Data.go b/logic/GetM3u8Data.go index ab5fa79..8be9f1f 100755 --- a/logic/GetM3u8Data.go +++ b/logic/GetM3u8Data.go @@ -5,8 +5,7 @@ import ( "ch/kirari04/videocms/inits" "ch/kirari04/videocms/models" "errors" - - "github.com/gofiber/fiber/v2" + "net/http" ) type GetM3u8DataRequest struct { @@ -32,7 +31,7 @@ func GetM3u8Data(UUID string, AUDIOUUID string, JWT string) (status int, m3u8Str UUID: UUID, }). First(&dbLink); dbRes.Error != nil { - return fiber.StatusNotFound, nil, errors.New("link doesn't exist") + return http.StatusNotFound, nil, errors.New("link doesn't exist") } //check if contains audio @@ -48,5 +47,5 @@ func GetM3u8Data(UUID string, AUDIOUUID string, JWT string) (status int, m3u8Str } } m3u8Response := helpers.GenM3u8Stream(&dbLink, &dbLink.File.Qualitys, dbAudioPtr, JWT) - return fiber.StatusOK, &m3u8Response, nil + return http.StatusOK, &m3u8Response, nil } diff --git a/logic/GetSubtitleData.go b/logic/GetSubtitleData.go index 3bf202a..f34dfcc 100755 --- a/logic/GetSubtitleData.go +++ b/logic/GetSubtitleData.go @@ -6,16 +6,15 @@ import ( "ch/kirari04/videocms/models" "errors" "fmt" + "net/http" "regexp" - - "github.com/gofiber/fiber/v2" ) func GetSubtitleData(fileName string, UUID string, SUBUUID string) (status int, filePath *string, err error) { reFILE := regexp.MustCompile(`^out\.(ass)$`) if !reFILE.MatchString(fileName) { - return fiber.StatusBadRequest, nil, errors.New("bad file format") + return http.StatusBadRequest, nil, errors.New("bad file format") } //translate link id to file id @@ -29,7 +28,7 @@ func GetSubtitleData(fileName string, UUID string, SUBUUID string) (status int, UUID: UUID, }). First(&dbLink); dbRes.Error != nil { - return fiber.StatusNotFound, nil, errors.New("subtitle doesn't exist") + return http.StatusNotFound, nil, errors.New("subtitle doesn't exist") } //check if subtitle uuid exists @@ -41,10 +40,10 @@ func GetSubtitleData(fileName string, UUID string, SUBUUID string) (status int, } } if !subExists { - return fiber.StatusNotFound, nil, errors.New("subtitle doesn't exist") + return http.StatusNotFound, nil, errors.New("subtitle doesn't exist") } fileRes := fmt.Sprintf("%s/%s/%s/%s", config.ENV.FolderVideoQualitysPriv, dbLink.File.UUID, SUBUUID, fileName) - return fiber.StatusOK, &fileRes, nil + return http.StatusOK, &fileRes, nil } diff --git a/logic/GetThumbnailData.go b/logic/GetThumbnailData.go index 94b4f65..925d293 100755 --- a/logic/GetThumbnailData.go +++ b/logic/GetThumbnailData.go @@ -6,16 +6,15 @@ import ( "ch/kirari04/videocms/models" "errors" "fmt" + "net/http" "regexp" - - "github.com/gofiber/fiber/v2" ) func GetThumbnailData(fileName string, UUID string) (status int, filePath *string, err error) { reFILE := regexp.MustCompile(`^[1-4]x[1-4]\.(webp)$`) if !reFILE.MatchString(fileName) { - return fiber.StatusBadRequest, nil, errors.New("bad file format") + return http.StatusBadRequest, nil, errors.New("bad file format") } //translate link id to file id @@ -27,10 +26,10 @@ func GetThumbnailData(fileName string, UUID string) (status int, filePath *strin UUID: UUID, }). First(&dbLink); dbRes.Error != nil { - return fiber.StatusNotFound, nil, errors.New("thumbnail doesn't exist") + return http.StatusNotFound, nil, errors.New("thumbnail doesn't exist") } fileRes := fmt.Sprintf("%s/%s/%s", config.ENV.FolderVideoQualitysPriv, dbLink.File.UUID, fileName) - return fiber.StatusOK, &fileRes, nil + return http.StatusOK, &fileRes, nil } diff --git a/logic/GetVideoData.go b/logic/GetVideoData.go index 46a569d..6c5dc2a 100755 --- a/logic/GetVideoData.go +++ b/logic/GetVideoData.go @@ -6,9 +6,8 @@ import ( "ch/kirari04/videocms/models" "errors" "fmt" + "net/http" "regexp" - - "github.com/gofiber/fiber/v2" ) func GetVideoData(fileName string, qualityName string, UUID string) (status int, filePath *string, err error) { @@ -16,11 +15,11 @@ func GetVideoData(fileName string, qualityName string, UUID string) (status int, reFILE := regexp.MustCompile(`^out[0-9]{0,4}\.(m3u8|ts|webm|mp4)$`) if !reQUALITY.MatchString(qualityName) { - return fiber.StatusBadRequest, nil, errors.New("bad quality format") + return http.StatusBadRequest, nil, errors.New("bad quality format") } if !reFILE.MatchString(fileName) { - return fiber.StatusBadRequest, nil, errors.New("bad file format") + return http.StatusBadRequest, nil, errors.New("bad file format") } //translate link id to file id @@ -32,9 +31,9 @@ func GetVideoData(fileName string, qualityName string, UUID string) (status int, UUID: UUID, }). First(&dbLink); dbRes.Error != nil { - return fiber.StatusNotFound, nil, errors.New("video doesn't exist") + return http.StatusNotFound, nil, errors.New("video doesn't exist") } fileRes := fmt.Sprintf("%s/%s/%s/%s", config.ENV.FolderVideoQualitysPriv, dbLink.File.UUID, qualityName, fileName) - return fiber.StatusOK, &fileRes, nil + return http.StatusOK, &fileRes, nil } diff --git a/logic/HashCloneFile.go b/logic/HashCloneFile.go index 768c1fa..3191e71 100755 --- a/logic/HashCloneFile.go +++ b/logic/HashCloneFile.go @@ -5,8 +5,8 @@ import ( "ch/kirari04/videocms/models" "errors" "log" + "net/http" - "github.com/gofiber/fiber/v2" "github.com/google/uuid" ) @@ -15,7 +15,7 @@ func CloneFileByHash(fromHash string, toFolder uint, fileName string, userId uin if toFolder > 0 { res := inits.DB.First(&models.Folder{}, toFolder) if res.Error != nil { - return fiber.StatusBadRequest, nil, errors.New("parent folder doesn't exist") + return http.StatusBadRequest, nil, errors.New("parent folder doesn't exist") } } @@ -25,7 +25,7 @@ func CloneFileByHash(fromHash string, toFolder uint, fileName string, userId uin Where(&models.File{ Hash: fromHash, }).First(&existingFile); res.Error != nil { - return fiber.StatusNotFound, nil, errors.New("requested hash doesnt match any file") + return http.StatusNotFound, nil, errors.New("requested hash doesnt match any file") } // file is dublicate and can be linked @@ -39,7 +39,7 @@ func CloneFileByHash(fromHash string, toFolder uint, fileName string, userId uin } if res := inits.DB.Create(&dbLink); res.Error != nil { log.Printf("Error saving link in database: %v", res.Error) - return fiber.StatusInternalServerError, nil, res.Error + return http.StatusInternalServerError, nil, res.Error } - return fiber.StatusOK, &dbLink, nil + return http.StatusOK, &dbLink, nil } diff --git a/logic/ListFiles.go b/logic/ListFiles.go index 688ef82..cdc5ef8 100755 --- a/logic/ListFiles.go +++ b/logic/ListFiles.go @@ -5,8 +5,9 @@ import ( "ch/kirari04/videocms/models" "errors" "log" + "net/http" - "github.com/gofiber/fiber/v2" + "github.com/labstack/echo/v4" ) func ListFiles(fromFolder uint, userId uint) (status int, response *[]models.Link, err error) { @@ -14,7 +15,7 @@ func ListFiles(fromFolder uint, userId uint) (status int, response *[]models.Lin if fromFolder > 0 { res := inits.DB.First(&models.Folder{}, fromFolder) if res.Error != nil { - return fiber.StatusBadRequest, nil, errors.New("parent folder doesn't exist") + return http.StatusBadRequest, nil, errors.New("parent folder doesn't exist") } } @@ -32,8 +33,8 @@ func ListFiles(fromFolder uint, userId uint) (status int, response *[]models.Lin Find(&links) if res.Error != nil { log.Printf("Failed to query file list: %v", res.Error) - return fiber.StatusInternalServerError, nil, errors.New(fiber.ErrInternalServerError.Message) + return http.StatusInternalServerError, nil, echo.ErrInternalServerError } - return fiber.StatusOK, &links, nil + return http.StatusOK, &links, nil } diff --git a/logic/Thumbnail.go b/logic/Thumbnail.go index d7f0b33..d2cd470 100755 --- a/logic/Thumbnail.go +++ b/logic/Thumbnail.go @@ -1,28 +1,28 @@ package logic import ( - "errors" "fmt" "log" "math" + "net/http" "os" "os/exec" "path/filepath" - "github.com/gofiber/fiber/v2" + "github.com/labstack/echo/v4" ) func CreateThumbnail(imageCountAxis int, inputFile string, height int, outputFile string, outputFolder string, videoDuration float64, fps float64) (status int, err error) { // read file & folder absOutputFolder, err := filepath.Abs(outputFolder) if err != nil { - return fiber.StatusBadRequest, err + return http.StatusBadRequest, err } os.MkdirAll(absOutputFolder, 0777) absInputFile, err := filepath.Abs(inputFile) if err != nil { - return fiber.StatusBadRequest, err + return http.StatusBadRequest, err } // build ffmpeg command @@ -99,10 +99,10 @@ func CreateThumbnail(imageCountAxis int, inputFile string, height int, outputFil ) if err := cmd.Run(); err != nil { log.Printf("Failed during simple thumbnail conversion: %v : %s", err, ffmpegCommandSimpleImage) - return fiber.StatusInternalServerError, errors.New(fiber.ErrInternalServerError.Message) + return http.StatusInternalServerError, echo.ErrInternalServerError } } - return fiber.StatusOK, nil + return http.StatusOK, nil } diff --git a/logic/UpdateWebhook.go b/logic/UpdateWebhook.go index 299904a..88ff5c5 100755 --- a/logic/UpdateWebhook.go +++ b/logic/UpdateWebhook.go @@ -5,8 +5,9 @@ import ( "ch/kirari04/videocms/models" "errors" "log" + "net/http" - "github.com/gofiber/fiber/v2" + "github.com/labstack/echo/v4" "gorm.io/gorm" ) @@ -17,10 +18,10 @@ func UpdateWebhook(validated *models.WebhookUpdateValidation, userID uint) (stat UserID: userID, }).First(&webhook, validated.WebhookID); res.Error != nil { if errors.Is(res.Error, gorm.ErrRecordNotFound) { - return fiber.StatusNotFound, "", fiber.ErrNotFound + return http.StatusNotFound, "", echo.ErrNotFound } log.Printf("Failed to query webhook %v: %v", validated.WebhookID, res.Error) - return fiber.StatusInternalServerError, "", fiber.ErrInternalServerError + return http.StatusInternalServerError, "", echo.ErrInternalServerError } webhook.Name = validated.Name @@ -31,8 +32,8 @@ func UpdateWebhook(validated *models.WebhookUpdateValidation, userID uint) (stat if res := inits.DB.Save(&webhook); res.Error != nil { log.Printf("Failed to update webhook %v: %v", validated.WebhookID, res.Error) - return fiber.StatusInternalServerError, "", fiber.ErrInternalServerError + return http.StatusInternalServerError, "", echo.ErrInternalServerError } - return fiber.StatusOK, "ok", nil + return http.StatusOK, "ok", nil } diff --git a/middlewares/IsAdmin.go b/middlewares/IsAdmin.go index d4bd867..b0f5bcc 100755 --- a/middlewares/IsAdmin.go +++ b/middlewares/IsAdmin.go @@ -3,7 +3,6 @@ package middlewares import ( "net/http" - "github.com/gofiber/fiber/v2" "github.com/labstack/echo/v4" ) @@ -13,7 +12,7 @@ func IsAdmin() echo.MiddlewareFunc { isAdmin, ok := c.Get("Admin").(bool) if !ok { c.Logger().Error("Failed to catch Admin") - return c.NoContent(fiber.StatusInternalServerError) + return c.NoContent(http.StatusInternalServerError) } if !isAdmin { return c.String(http.StatusForbidden, "Not Permitted") diff --git a/middlewares/JwtStream.go b/middlewares/JwtStream.go index 0e529f7..73255fc 100755 --- a/middlewares/JwtStream.go +++ b/middlewares/JwtStream.go @@ -2,28 +2,33 @@ package middlewares import ( "ch/kirari04/videocms/auth" + "net/http" - "github.com/gofiber/fiber/v2" + "github.com/labstack/echo/v4" ) -func JwtStream(c *fiber.Ctx) error { - uuid := c.Params("UUID", "") - if uuid == "" { - return c.Status(fiber.StatusBadRequest).SendString("Missing UUID parameter") - } - tknStr := c.Query("jwt", "") - if tknStr == "" { - return c.Status(fiber.StatusBadRequest).SendString("UUID parameter match issue") - } - token, claims, err := auth.VerifyJWTStream(tknStr) - if err != nil { - return c.Status(fiber.StatusForbidden).SendString("Broken JWT") - } - if !token.Valid { - return c.Status(fiber.StatusForbidden).SendString("Invalid JWT") - } - if claims.UUID != uuid { - return c.Status(fiber.StatusForbidden).SendString("Mismacht UUID") - } - return c.Next() +func JwtStream() echo.MiddlewareFunc { + return echo.MiddlewareFunc(func(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + uuid := c.Param("UUID") + if uuid == "" { + return c.String(http.StatusBadRequest, "Missing UUID parameter") + } + tknStr := c.QueryParam("jwt") + if tknStr == "" { + return c.String(http.StatusBadRequest, "UUID parameter match issue") + } + token, claims, err := auth.VerifyJWTStream(tknStr) + if err != nil { + return c.String(http.StatusForbidden, "Broken JWT") + } + if !token.Valid { + return c.String(http.StatusForbidden, "Invalid JWT") + } + if claims.UUID != uuid { + return c.String(http.StatusForbidden, "Mismacht UUID") + } + return next(c) + } + }) } diff --git a/routes/api.go b/routes/api.go index 2838d71..0975484 100755 --- a/routes/api.go +++ b/routes/api.go @@ -7,72 +7,75 @@ import ( "ch/kirari04/videocms/middlewares" "time" - "github.com/gofiber/fiber/v2/middleware/limiter" + "github.com/labstack/echo/v4/middleware" ) func Api() { - inits.Api.Use(limiter.New(*helpers.LimiterConfig(10, time.Second))) + inits.Api.Use(middleware.RateLimiterWithConfig(*helpers.LimiterConfig(10, 500, time.Minute*5))) auth := inits.Api.Group("/auth") - auth.Use(limiter.New(*helpers.LimiterConfig(10, time.Hour))). - Post("/login", controllers.AuthLogin) - auth.Use(limiter.New(*helpers.LimiterConfig(1, time.Second*10))). - Get("/check", controllers.AuthCheck) - auth.Use(limiter.New(*helpers.LimiterConfig(1, time.Minute))). - Get("/refresh", controllers.AuthRefresh) + auth.POST("/login", + controllers.AuthLogin, + middleware.RateLimiterWithConfig(*helpers.LimiterConfig(1, 2, time.Minute*5))) + auth.GET("/check", + controllers.AuthCheck, + middleware.RateLimiterWithConfig(*helpers.LimiterConfig(1, 2, time.Minute*5))) + auth.GET("/refresh", + controllers.AuthRefresh, + middleware.RateLimiterWithConfig(*helpers.LimiterConfig(1, 2, time.Minute*5))) // Routes that dont require authentication - inits.Api.Get("/config", controllers.GetConfig) - inits.Api.Get("/file/example", controllers.GetFileExample) - inits.Api.Get("/p/pages", controllers.ListPublicWebPage) - inits.Api.Get("/p/page", controllers.GetPublicWebPage) + inits.Api.GET("/config", controllers.GetConfig) + inits.Api.GET("/file/example", controllers.GetFileExample) + inits.Api.GET("/p/pages", controllers.ListPublicWebPage) + inits.Api.GET("/p/page", controllers.GetPublicWebPage) // requires uploadsession jwt inside body - inits.Api.Post("/pcu/chunck", controllers.CreateUploadChunck) + inits.Api.POST("/pcu/chunck", controllers.CreateUploadChunck) // Routes that require to be authenticated - protectedApi := inits.Api.Group("", middlewares.Auth) - protectedApi.Post("/folder", controllers.CreateFolder) - protectedApi.Put("/folder", controllers.UpdateFolder) - protectedApi.Delete("/folder", controllers.DeleteFolder) - protectedApi.Get("/folders", controllers.ListFolders) - protectedApi.Delete("/folders", controllers.DeleteFolders) + protectedApi := inits.Api.Group("", middlewares.Auth()) + protectedApi.POST("/folder", controllers.CreateFolder) + protectedApi.PUT("/folder", controllers.UpdateFolder) + protectedApi.DELETE("/folder", controllers.DeleteFolder) + protectedApi.GET("/folders", controllers.ListFolders) + protectedApi.DELETE("/folders", controllers.DeleteFolders) - protectedApi.Post("/file", controllers.CreateFile) - protectedApi.Post("/file/clone", controllers.CloneFile) - protectedApi.Get("/file", controllers.GetFile) - protectedApi.Put("/file", controllers.UpdateFile) - protectedApi.Delete("/file", controllers.DeleteFileController) - protectedApi.Get("/files", controllers.ListFiles) - protectedApi.Delete("/files", controllers.DeleteFilesController) + protectedApi.POST("/file", controllers.CreateFile) + protectedApi.POST("/file/clone", controllers.CloneFile) + protectedApi.GET("/file", controllers.GetFile) + protectedApi.PUT("/file", controllers.UpdateFile) + protectedApi.DELETE("/file", controllers.DeleteFileController) + protectedApi.GET("/files", controllers.ListFiles) + protectedApi.DELETE("/files", controllers.DeleteFilesController) - protectedApi.Get("/account", controllers.GetAccount) - protectedApi.Get("/account/settings", controllers.GetUserSettingsController) - protectedApi.Put("/account/settings", controllers.UpdateUserSettingsController) + protectedApi.GET("/account", controllers.GetAccount) + protectedApi.GET("/account/settings", controllers.GetUserSettingsController) + protectedApi.PUT("/account/settings", controllers.UpdateUserSettingsController) - protectedApi.Post("/server", middlewares.IsAdmin, controllers.CreateServer) - protectedApi.Delete("/server", middlewares.IsAdmin, controllers.DeleteServer) - protectedApi.Get("/servers", middlewares.IsAdmin, controllers.ListServers) + protectedApi.POST("/server", controllers.CreateServer, middlewares.IsAdmin()) + protectedApi.DELETE("/server", controllers.DeleteServer, middlewares.IsAdmin()) + protectedApi.GET("/servers", controllers.ListServers, middlewares.IsAdmin()) - protectedApi.Get("/pages", middlewares.IsAdmin, controllers.ListWebPage) - protectedApi.Post("/page", middlewares.IsAdmin, controllers.CreateWebPage) - protectedApi.Put("/page", middlewares.IsAdmin, controllers.UpdateWebPage) - protectedApi.Delete("/page", middlewares.IsAdmin, controllers.DeleteWebPage) + protectedApi.GET("/pages", controllers.ListWebPage, middlewares.IsAdmin()) + protectedApi.GET("/page", controllers.CreateWebPage, middlewares.IsAdmin()) + protectedApi.PUT("/page", controllers.UpdateWebPage, middlewares.IsAdmin()) + protectedApi.DELETE("/page", controllers.DeleteWebPage, middlewares.IsAdmin()) - protectedApi.Get("/stats", middlewares.IsAdmin, controllers.GetSystemStats) - protectedApi.Get("/settings", middlewares.IsAdmin, controllers.GetSettings) - protectedApi.Put("/settings", middlewares.IsAdmin, controllers.UpdateSettings) + protectedApi.GET("/stats", controllers.GetSystemStats, middlewares.IsAdmin()) + protectedApi.GET("/settings", controllers.GetSettings, middlewares.IsAdmin()) + protectedApi.PUT("/settings", controllers.UpdateSettings, middlewares.IsAdmin()) - protectedApi.Post("/webhook", controllers.CreateWebhook) - protectedApi.Put("/webhook", controllers.UpdateWebhook) - protectedApi.Delete("/webhook", controllers.DeleteWebhook) - protectedApi.Get("/webhooks", controllers.ListWebhooks) + protectedApi.POST("/webhook", controllers.CreateWebhook) + protectedApi.PUT("/webhook", controllers.UpdateWebhook) + protectedApi.DELETE("/webhook", controllers.DeleteWebhook) + protectedApi.GET("/webhooks", controllers.ListWebhooks) - protectedApi.Get("/encodings", controllers.GetEncodingFiles) + protectedApi.GET("/encodings", controllers.GetEncodingFiles) - protectedApi.Get("/pcu/sessions", controllers.GetUploadSessions) - protectedApi.Post("/pcu/session", controllers.CreateUploadSession) - protectedApi.Delete("/pcu/session", controllers.DeleteUploadSession) + protectedApi.GET("/pcu/sessions", controllers.GetUploadSessions) + protectedApi.POST("/pcu/session", controllers.CreateUploadSession) + protectedApi.DELETE("/pcu/session", controllers.DeleteUploadSession) // protectedApi.Post("/pcu/chunck", controllers.CreateUploadChunck) - protectedApi.Post("/pcu/file", controllers.CreateUploadFile) + protectedApi.POST("/pcu/file", controllers.CreateUploadFile) } diff --git a/routes/web.go b/routes/web.go index 356329d..03c5378 100755 --- a/routes/web.go +++ b/routes/web.go @@ -10,15 +10,15 @@ import ( func Web() { inits.App.Static("/", "./public") - inits.App.Get("/:UUID", controllers.PlayerController) + inits.App.GET("/:UUID", controllers.PlayerController) videoData := inits.App.Group(config.ENV.FolderVideoQualitysPub) - videoData.Get("/:UUID/stream/muted/master.m3u8", middlewares.JwtStream, controllers.GetM3u8Data) - videoData.Get("/:UUID/image/thumb/:FILE", controllers.GetThumbnailData) - videoData.Get("/:UUID/:SUBUUID/subtitle/:FILE", middlewares.JwtStream, controllers.GetSubtitleData) - videoData.Get("/:UUID/:AUDIOUUID/stream/master.m3u8", middlewares.JwtStream, controllers.GetM3u8Data) - videoData.Get("/:UUID/:QUALITY/download/video.mkv", middlewares.JwtStream, controllers.DownloadVideoController) + videoData.GET("/:UUID/stream/muted/master.m3u8", controllers.GetM3u8Data, middlewares.JwtStream()) + videoData.GET("/:UUID/image/thumb/:FILE", controllers.GetThumbnailData, middlewares.JwtStream()) + videoData.GET("/:UUID/:SUBUUID/subtitle/:FILE", controllers.GetSubtitleData, middlewares.JwtStream()) + videoData.GET("/:UUID/:AUDIOUUID/stream/master.m3u8", controllers.GetM3u8Data, middlewares.JwtStream()) + videoData.GET("/:UUID/:QUALITY/download/video.mkv", controllers.DownloadVideoController, middlewares.JwtStream()) // no jwt stream - videoData.Get("/:UUID/:QUALITY/:FILE", controllers.GetVideoData) - videoData.Get("/:UUID/:AUDIOUUID/audio/:FILE", controllers.GetAudioData) + videoData.GET("/:UUID/:QUALITY/:FILE", controllers.GetVideoData) + videoData.GET("/:UUID/:AUDIOUUID/audio/:FILE", controllers.GetAudioData) }