diff --git a/internal/api/utils.go b/internal/api/utils.go index bb994e7..fa41d26 100644 --- a/internal/api/utils.go +++ b/internal/api/utils.go @@ -22,9 +22,14 @@ func saveFormFile(name string, c echo.Context) (string, error) { } defer src.Close() + tmpDir, err := ensureDir("tmp") + if err != nil { + return "", err + } + ext := filepath.Ext(file.Filename) filename := time.Now().Format(time.RFC3339) - filename = "./tmp/" + sanitizeFilename(filename) + ext + filename = tmpDir + "/" + sanitizeFilename(filename) + ext dst, err := os.Create(filename) if err != nil { @@ -46,3 +51,14 @@ func sanitizeFilename(filename string) string { } return filename } + +func ensureDir(dirPath string) (string, error) { + if _, err := os.Stat(dirPath); os.IsNotExist(err) { + err := os.MkdirAll(dirPath, 0700) + if err != nil { + return "", err + } + } + + return dirPath, nil +}