-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
61 lines (47 loc) · 1.09 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
60
61
package filesystem
import (
"io"
"os"
"path"
"strings"
"github.com/ViBiOh/absto/pkg/model"
)
const (
writeFlags = os.O_RDWR | os.O_CREATE | os.O_TRUNC
)
func checkPathname(pathname string) error {
if strings.Contains(pathname, "..") {
return ErrRelativePath
}
return nil
}
func (a App) getRelativePath(pathname string) string {
return strings.TrimPrefix(pathname, a.rootDirectory)
}
func (a App) getFile(filename string, flags int) (*os.File, error) {
return os.OpenFile(a.Path(filename), flags, getMode(filename))
}
func (a App) getWritableFile(filename string) (io.WriteCloser, error) {
return a.getFile(filename, writeFlags)
}
func getMode(name string) os.FileMode {
if strings.HasSuffix(name, "/") {
return 0o700
}
return 0o600
}
func convertToItem(pathname string, info os.FileInfo) model.Item {
name := info.Name()
item := model.Item{
ID: model.ID(pathname),
Name: name,
Pathname: pathname,
IsDir: info.IsDir(),
Date: info.ModTime(),
}
if !item.IsDir {
item.Extension = strings.ToLower(path.Ext(name))
item.Size = info.Size()
}
return item
}