From a79c8b8e307b81477e15d362558ebf74b3039fe8 Mon Sep 17 00:00:00 2001 From: Firdavs Date: Fri, 29 Mar 2024 21:36:37 +0300 Subject: [PATCH] [add] Add file path customization for file uploads --- {core => app/http}/configInit.go | 14 ++++++++++---- app/http/controllers/fileUploadController.go | 12 +++++++++++- app/http/controllers/filesUploadController.go | 13 ++++++++++++- app/http/server.go | 3 +++ core/FileDirectory.go | 6 ------ core/installation.go | 6 ------ 6 files changed, 36 insertions(+), 18 deletions(-) rename {core => app/http}/configInit.go (94%) diff --git a/core/configInit.go b/app/http/configInit.go similarity index 94% rename from core/configInit.go rename to app/http/configInit.go index 72512dd..a2abcd1 100644 --- a/core/configInit.go +++ b/app/http/configInit.go @@ -1,19 +1,25 @@ -package core +package http import ( "fmt" "strconv" - "github.com/Firdavs9512/qk-server/app/http" "github.com/Firdavs9512/qk-server/app/models" "github.com/Firdavs9512/qk-server/config" + "github.com/Firdavs9512/qk-server/core" "github.com/Firdavs9512/qk-server/utils" "github.com/fatih/color" "gorm.io/gorm" ) +// Start initiation +func Init() { + core.StartInitiation() + configInit() +} + // Local config files initialization in database -func ConfigInit() { +func configInit() { var appConfig config.AppType appConfig.Name = config.App.Name appConfig.Author = config.App.Author @@ -108,5 +114,5 @@ func ConfigInit() { config.App = appConfig // Restart http server - http.RestartServer() + RestartServer() } diff --git a/app/http/controllers/fileUploadController.go b/app/http/controllers/fileUploadController.go index 3fb5470..b808c14 100644 --- a/app/http/controllers/fileUploadController.go +++ b/app/http/controllers/fileUploadController.go @@ -5,6 +5,7 @@ import ( "github.com/Firdavs9512/qk-server/app/models" "github.com/Firdavs9512/qk-server/config" + "github.com/Firdavs9512/qk-server/core" "github.com/google/uuid" "github.com/kataras/iris/v12" ) @@ -20,7 +21,16 @@ func (f *FileUploadController) Post(ctx iris.Context) int { } uuidModel := uuid.New().String() - filePath := filepath.Join(config.App.UploadUrl, uuidModel) + path := ctx.PostValueDefault("path", config.App.UploadUrl) + + if path == config.App.UploadUrl { + path = filepath.Join(config.App.UploadUrl, "files") + } else { + path = filepath.Join(config.App.UploadUrl, path) + } + + core.CreateDirectory(path) + filePath := filepath.Join(path, uuidModel) // Save file to the server _, err = ctx.SaveFormFile(fileHeader, filePath) diff --git a/app/http/controllers/filesUploadController.go b/app/http/controllers/filesUploadController.go index d12c734..4680d2e 100644 --- a/app/http/controllers/filesUploadController.go +++ b/app/http/controllers/filesUploadController.go @@ -5,6 +5,7 @@ import ( "github.com/Firdavs9512/qk-server/app/models" "github.com/Firdavs9512/qk-server/config" + "github.com/Firdavs9512/qk-server/core" "github.com/google/uuid" "github.com/kataras/iris/v12" ) @@ -18,13 +19,23 @@ func (f *FilesUploadController) Post(ctx iris.Context) int { return iris.StatusInternalServerError } + path := ctx.PostValueDefault("path", config.App.UploadUrl) + + if path == config.App.UploadUrl { + path = filepath.Join(config.App.UploadUrl, "files") + } else { + path = filepath.Join(config.App.UploadUrl, path) + } + + core.CreateDirectory(path) + form := ctx.Request().MultipartForm files := form.File["files[]"] var fileNames []string for _, file := range files { uuidModel := uuid.New().String() - filePath := filepath.Join(config.App.UploadUrl, uuidModel) + filePath := filepath.Join(path, uuidModel) _, err := ctx.SaveFormFile(file, filePath) if err != nil { diff --git a/app/http/server.go b/app/http/server.go index da8ea99..878e596 100644 --- a/app/http/server.go +++ b/app/http/server.go @@ -14,6 +14,9 @@ type Server struct{} var Application *iris.Application func (s *Server) Start() { + // Initialize the application + Init() + // Create a new Iris application Application = iris.Default() diff --git a/core/FileDirectory.go b/core/FileDirectory.go index b3d28cb..c10bee8 100644 --- a/core/FileDirectory.go +++ b/core/FileDirectory.go @@ -3,14 +3,8 @@ package core import ( "fmt" "os" - - "github.com/Firdavs9512/qk-server/config" ) -func InitDirectory() { - CreateDirectory(config.App.UploadUrl) -} - // Create new directory func CreateDirectory(path string) { if _, err := os.Stat(path); os.IsNotExist(err) { diff --git a/core/installation.go b/core/installation.go index a22d64f..e3fa00b 100644 --- a/core/installation.go +++ b/core/installation.go @@ -13,12 +13,6 @@ func StartInitiation() { // Migrate database models Migrate() - // Init config - ConfigInit() - - // Init file directory - InitDirectory() - // Console texts startText() authorText()