Skip to content

Commit

Permalink
[add] Add file path customization for file uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
Firdavs9512 committed Mar 29, 2024
1 parent 7456b4b commit a79c8b8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 18 deletions.
14 changes: 10 additions & 4 deletions core/configInit.go → app/http/configInit.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -108,5 +114,5 @@ func ConfigInit() {
config.App = appConfig

// Restart http server
http.RestartServer()
RestartServer()
}
12 changes: 11 additions & 1 deletion app/http/controllers/fileUploadController.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand Down
13 changes: 12 additions & 1 deletion app/http/controllers/filesUploadController.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions app/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 0 additions & 6 deletions core/FileDirectory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 0 additions & 6 deletions core/installation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ func StartInitiation() {
// Migrate database models
Migrate()

// Init config
ConfigInit()

// Init file directory
InitDirectory()

// Console texts
startText()
authorText()
Expand Down

0 comments on commit a79c8b8

Please sign in to comment.