-
Notifications
You must be signed in to change notification settings - Fork 3
/
tempDir.go
50 lines (42 loc) · 1.16 KB
/
tempDir.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package pathKit
import (
"github.com/richelieu-yang/chimera/v2/src/consts"
"github.com/richelieu-yang/chimera/v2/src/core/fileKit"
"github.com/richelieu-yang/chimera/v2/src/core/strKit"
"os"
)
var tempDir string
// GetOsTempDir 获取系统的临时目录.
/*
Deprecated: 不建议向系统临时目录中放东西(服务器不一定会给权限).
e.g.
Windows: "C:\Users\Lenovo\AppData\Local\Temp"
Mac: "/var/folders/4_/33p_vn057msfh2nvgx6hwv_40000gn/T/"
*/
var GetOsTempDir func() string = os.TempDir
// GetTempDir 获取 本依赖 的专属临时目录.
/*
PS: 不建议向系统临时目录中放东西(服务器不一定会给权限).
e.g. Mac
() => "/var/folders/4_/33p_vn057msfh2nvgx6hwv_40000gn/T/$$chimera", nil
*/
func GetTempDir() (string, error) {
if strKit.IsNotEmpty(tempDir) {
return tempDir, nil
}
dir := Join(GetOsTempDir(), "$$"+consts.ProjectName)
if err := fileKit.MkDirs(dir); err != nil {
return "", err
}
return dir, nil
}
func SetTempDir(path string) error {
if err := fileKit.AssertNotExistOrIsDir(path); err != nil {
return err
}
if err := fileKit.MkDirs(path); err != nil {
return err
}
tempDir = path
return nil
}