From 482616fb4c825fc4f77f950049d7e38e484d951b Mon Sep 17 00:00:00 2001 From: xzeldon Date: Thu, 5 Oct 2023 23:42:04 +0300 Subject: [PATCH] Ensure the existence of the temp directory in the binary root --- internal/api/utils.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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 +}