Skip to content

Commit 75033b3

Browse files
committed
feature: Added file downloads from extension folder
1 parent 0ce7e3c commit 75033b3

File tree

5 files changed

+156
-21
lines changed

5 files changed

+156
-21
lines changed

app/handlers/file.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package handlers
22

33
import (
44
"path/filepath"
5+
"strings"
56

67
"github.com/gofiber/fiber/v2"
78
"github.com/limanmys/render-engine/app/models"
89
"github.com/limanmys/render-engine/internal/bridge"
910
"github.com/limanmys/render-engine/internal/liman"
11+
"github.com/limanmys/render-engine/internal/sandbox"
12+
"github.com/limanmys/render-engine/pkg/helpers"
1013
"github.com/limanmys/render-engine/pkg/logger"
1114
)
1215

@@ -109,3 +112,78 @@ func GetFile(c *fiber.Ctx) error {
109112
"message": "file transfer completed successfully"
110113
}`)
111114
}
115+
116+
func DownloadFile(c *fiber.Ctx) error {
117+
params := []string{"server_id", "path", "extension_id"}
118+
119+
for _, param := range params {
120+
if len(c.FormValue(param)) < 1 {
121+
return logger.FiberError(fiber.StatusBadRequest, param+" parameter is missing")
122+
}
123+
}
124+
if len(c.FormValue("extension_id")) < 1 {
125+
return logger.FiberError(fiber.StatusBadRequest, "extension not found")
126+
}
127+
128+
extension, err := liman.GetExtension(&models.Extension{
129+
ID: c.FormValue("extension_id"),
130+
})
131+
132+
if err != nil {
133+
return err
134+
}
135+
136+
if extension.Status == "0" {
137+
return logger.FiberError(fiber.StatusServiceUnavailable, "extension is unavailable")
138+
}
139+
140+
credentials := &models.Credentials{}
141+
if extension.RequireKey == "true" {
142+
credentials, err = liman.GetCredentials(
143+
&models.User{
144+
ID: c.Locals("user_id").(string),
145+
},
146+
&models.Server{
147+
ID: c.FormValue("server_id"),
148+
},
149+
)
150+
151+
if err != nil || len(credentials.Username) < 1 {
152+
return logger.FiberError(fiber.StatusForbidden, "you need a key to use this extension")
153+
}
154+
}
155+
156+
formValues := helpers.GetFormData(c)
157+
158+
token := c.FormValue("token")
159+
if len(c.FormValue("liman-token")) > 0 {
160+
token = c.FormValue("liman-token")
161+
}
162+
163+
_, err = sandbox.GenerateCommand(
164+
extension,
165+
credentials,
166+
&models.CommandParams{
167+
TargetFunction: "",
168+
User: c.Locals("user_id").(string),
169+
Extension: c.FormValue("extension_id"),
170+
Server: c.FormValue("server_id"),
171+
RequestData: formValues,
172+
Token: token,
173+
BaseURL: c.FormValue("lmnbaseurl", c.Get("origin")),
174+
Locale: c.FormValue("locale", helpers.Env("APP_LANG", "tr")),
175+
LogID: c.Locals("log_id").(string),
176+
},
177+
)
178+
if err != nil {
179+
return err
180+
}
181+
182+
path := filepath.Clean(c.FormValue("path"))
183+
path = "/liman/extensions/" + strings.ToLower(extension.Name) + path
184+
path = strings.ReplaceAll(path, "../", "/")
185+
path = strings.ReplaceAll(path, "./", "/")
186+
path = strings.ReplaceAll(path, "..", "")
187+
188+
return c.Download(path)
189+
}

app/routes/index.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func Install(app *fiber.App) {
2020
// file
2121
app.Post("/getFile", handlers.GetFile)
2222
app.Post("/putFile", handlers.PutFile)
23+
app.Get("/download", handlers.DownloadFile)
2324

2425
// script
2526
app.Post("/script", handlers.ScriptRunner)

go.mod

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ module github.com/limanmys/render-engine
33
go 1.19
44

55
require (
6-
github.com/gofiber/fiber/v2 v2.37.1
7-
github.com/gofiber/helmet/v2 v2.2.16
6+
github.com/gofiber/fiber/v2 v2.40.1
7+
github.com/gofiber/helmet/v2 v2.2.22
88
github.com/google/uuid v1.3.0
99
github.com/joho/godotenv v1.4.0
10-
go.uber.org/zap v1.23.0
11-
gorm.io/driver/mysql v1.3.6
12-
gorm.io/driver/postgres v1.3.9
13-
gorm.io/gorm v1.23.9
10+
go.uber.org/zap v1.24.0
11+
gorm.io/driver/mysql v1.4.4
12+
gorm.io/driver/postgres v1.4.5
13+
gorm.io/gorm v1.24.2
1414
)
1515

1616
require (
17-
github.com/Azure/go-ntlmssp v0.0.0-20220621081337-cb9428e4ac1e // indirect
17+
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
1818
github.com/ChrisTrenkamp/goxpath v0.0.0-20210404020558-97928f7e12b6 // indirect
1919
github.com/benbjohnson/clock v1.3.0 // indirect
2020
github.com/geoffgarside/ber v1.1.0 // indirect
21-
github.com/gofrs/uuid v4.3.0+incompatible // indirect
21+
github.com/gofrs/uuid v4.3.1+incompatible // indirect
2222
github.com/hashicorp/go-uuid v1.0.3 // indirect
2323
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
2424
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
@@ -29,41 +29,45 @@ require (
2929
github.com/kr/fs v0.1.0 // indirect
3030
github.com/kr/pretty v0.3.0 // indirect
3131
github.com/masterzen/simplexml v0.0.0-20190410153822-31eea3082786 // indirect
32+
github.com/mattn/go-colorable v0.1.13 // indirect
33+
github.com/mattn/go-isatty v0.0.16 // indirect
34+
github.com/mattn/go-runewidth v0.0.14 // indirect
3235
github.com/pkg/errors v0.9.1 // indirect
36+
github.com/rivo/uniseg v0.4.3 // indirect
3337
github.com/rogpeppe/go-internal v1.8.1 // indirect
3438
go.uber.org/atomic v1.10.0 // indirect
3539
go.uber.org/goleak v1.1.12 // indirect
36-
go.uber.org/multierr v1.8.0 // indirect
37-
golang.org/x/net v0.0.0-20220909164309-bea034e7d591 // indirect
38-
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
40+
go.uber.org/multierr v1.9.0 // indirect
41+
golang.org/x/net v0.4.0 // indirect
42+
golang.org/x/term v0.3.0 // indirect
3943
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
4044
)
4145

4246
require (
4347
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
4448
github.com/alessio/shellescape v1.4.1
4549
github.com/andybalholm/brotli v1.0.4 // indirect
46-
github.com/go-sql-driver/mysql v1.6.0 // indirect
50+
github.com/go-sql-driver/mysql v1.7.0 // indirect
4751
github.com/hirochachacha/go-smb2 v1.1.0
4852
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
4953
github.com/jackc/pgconn v1.13.0 // indirect
5054
github.com/jackc/pgio v1.0.0 // indirect
5155
github.com/jackc/pgpassfile v1.0.0 // indirect
5256
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
5357
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
54-
github.com/jackc/pgtype v1.12.0 // indirect
58+
github.com/jackc/pgtype v1.13.0 // indirect
5559
github.com/jackc/pgx/v4 v4.17.2 // indirect
5660
github.com/jinzhu/inflection v1.0.0 // indirect
5761
github.com/jinzhu/now v1.1.5 // indirect
58-
github.com/klauspost/compress v1.15.10 // indirect
59-
github.com/masterzen/winrm v0.0.0-20220513085036-69f69afcd9e9
62+
github.com/klauspost/compress v1.15.13 // indirect
63+
github.com/masterzen/winrm v0.0.0-20220917170901-b07f6cb0598d
6064
github.com/mervick/aes-everywhere/go/aes256 v0.0.0-20220903070135-f13ed3789ae1
6165
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5
6266
github.com/pkg/sftp v1.13.5
6367
github.com/valyala/bytebufferpool v1.0.0 // indirect
64-
github.com/valyala/fasthttp v1.40.0 // indirect
68+
github.com/valyala/fasthttp v1.43.0 // indirect
6569
github.com/valyala/tcplisten v1.0.0 // indirect
66-
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90
67-
golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41 // indirect
68-
golang.org/x/text v0.3.7
70+
golang.org/x/crypto v0.4.0
71+
golang.org/x/sys v0.3.0 // indirect
72+
golang.org/x/text v0.5.0
6973
)

0 commit comments

Comments
 (0)