-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
executable file
·59 lines (53 loc) · 1.04 KB
/
util.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
package main
import (
"os"
"path/filepath"
"log"
"strings"
"io"
"time"
"strconv"
)
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func GetDirectory(file string) string {
dir, err := filepath.Abs(filepath.Dir(file))
if err != nil {
log.Fatal(err)
}
return strings.Replace(dir, "\\", "/", -1)
}
func ChangePathFormat(file string) string{
return strings.Replace(file, "\\", "/", -1)
}
func DeleteFile(file string) error {
err := os.Remove(file)
return err
}
func CopyFile(src string) (string, error) {
srcFile, err := os.Open(src)
if err != nil {
return "", err
}
defer srcFile.Close()
dst := filepath.Join(GetDirectory("."), strconv.FormatInt(time.Now().Unix(), 10)+".ass")
dstFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return "", err
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
DeleteFile(dst)
return "", err
}
return dst, nil
}