-
Notifications
You must be signed in to change notification settings - Fork 3
/
tempDir.go
48 lines (39 loc) · 1.11 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
package pathKit
import (
"github.com/richelieu-yang/chimera/v2/src/consts"
"github.com/richelieu-yang/chimera/v2/src/core/strKit"
"github.com/richelieu-yang/chimera/v2/src/file/fileKit"
"os"
)
var tempDir string
// GetTempDir 获取系统的临时目录.
/*
Deprecated: 不建议向系统临时目录中放东西(服务器不一定会给权限).
e.g.
Windows: "C:\Users\Lenovo\AppData\Local\Temp"
Mac: "/var/folders/4_/33p_vn057msfh2nvgx6hwv_40000gn/T/"
*/
var GetTempDir func() string = os.TempDir
// GetExclusiveTempDir 获取 本依赖 的专属临时目录.
/*
PS: 不建议向系统临时目录中放东西(服务器不一定会给权限).
e.g. Mac
() => "/var/folders/4_/33p_vn057msfh2nvgx6hwv_40000gn/T/$$chimera", nil
*/
func GetExclusiveTempDir() (string, error) {
if strKit.IsNotEmpty(tempDir) {
return tempDir, nil
}
dir := Join(GetTempDir(), "$$"+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
}
tempDir = path
return nil
}