Ensure the existence of the temp directory in the binary root

This commit is contained in:
Timofey Gelazoniya 2023-10-05 23:42:04 +03:00
parent b36fe91b5c
commit 482616fb4c
Signed by: zeldon
GPG Key ID: 047886915281DD2A

View File

@ -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
}