Skip to content

Commit

Permalink
Ensure the existence of the temp directory in the binary root
Browse files Browse the repository at this point in the history
  • Loading branch information
xzeldon committed Oct 5, 2023
1 parent b36fe91 commit 482616f
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion internal/api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit 482616f

Please sign in to comment.