-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
71 lines (59 loc) · 1.46 KB
/
common.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package exturl
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/tliron/commonlog"
"github.com/tliron/kutil/util"
)
var log = commonlog.GetLogger("exturl")
func GetFormat(path string) string {
extension := filepath.Ext(path)
if extension == "" {
return ""
}
extension = strings.ToLower(extension[1:])
// Special handling for tarballs
if pre4start := len(path) - len(extension) - 5; pre4start > 0 {
pre4 := path[pre4start : pre4start+4]
if pre4 == ".tar" {
return "tar." + extension
}
}
// Special handling for alternative extensions
switch extension {
case "yml":
return "yaml"
case "tgz":
return "tar.gz"
}
return extension
}
func GetTemporaryPathPattern(key string) string {
return fmt.Sprintf("exturl-%s-*", util.SanitizeFilename(key))
}
func DeleteTemporaryFile(path string) error {
if err := os.Remove(path); err == nil {
log.Infof("deleted temporary file %q", path)
return nil
} else if os.IsNotExist(err) {
log.Infof("temporary file already deleted %q", path)
return nil
} else {
log.Errorf("could not delete temporary file %q: %s", path, err.Error())
return err
}
}
func DeleteTemporaryDir(path string) error {
if err := os.RemoveAll(path); err == nil {
log.Infof("deleted temporary dir %q", path)
return nil
} else if os.IsNotExist(err) {
log.Infof("temporary dir already deleted %q", path)
return nil
} else {
log.Errorf("could not delete temporary dir %q: %s", path, err.Error())
return err
}
}