64 lines
1.2 KiB
Go
64 lines
1.2 KiB
Go
package sugar
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
)
|
|
|
|
func EnsureDir(dirPath ...string) (string, error) {
|
|
outPath := filepath.Join(dirPath...)
|
|
if len(outPath) < 3 {
|
|
return "", fmt.Errorf("the path is short to be a folder: %s", outPath)
|
|
}
|
|
|
|
if _, err := os.Stat(outPath); os.IsNotExist(err) {
|
|
var dirMod uint64
|
|
if dirMod, err = strconv.ParseUint("0775", 8, 32); err == nil {
|
|
err = os.Mkdir(outPath, os.FileMode(dirMod))
|
|
}
|
|
|
|
if err != nil && !os.IsExist(err) {
|
|
slog.Error("error creating tmp dir", "err", err)
|
|
}
|
|
}
|
|
|
|
return outPath, nil
|
|
}
|
|
|
|
func WriteDataToTmpFile(content, fp string) (int, string, error) {
|
|
dir, err := EnsureDir(os.TempDir(), "mdb")
|
|
if err != nil {
|
|
slog.Error("EnsureDir", "err", err.Error())
|
|
return 0, dir, err
|
|
}
|
|
|
|
fp = filepath.Join(dir, fp)
|
|
|
|
f, err := os.Create(fp)
|
|
if err != nil {
|
|
slog.Error("CreateFile", "e", err)
|
|
return 0, fp, err
|
|
}
|
|
|
|
filesize, err := f.WriteString(content)
|
|
if err != nil {
|
|
slog.Error("WriteString", "e", err)
|
|
return 0, fp, err
|
|
}
|
|
|
|
err = f.Close()
|
|
if err != nil {
|
|
slog.Error("CloseFile", "e", err)
|
|
return 0, fp, err
|
|
}
|
|
|
|
return filesize, fp, nil
|
|
}
|
|
|
|
/*
|
|
REMOVED_FROM_MMT_DUE_TO_REDOING_OF_THE_EVENT_LOG
|
|
*/
|