-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
56 lines (44 loc) · 1.16 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
package filesystem
import (
"io"
"io/fs"
"os"
"path"
"strings"
"github.com/ViBiOh/absto/pkg/model"
)
func (a Service) getRelativePath(name string) string {
return strings.TrimPrefix(name, a.rootDirectory)
}
func (a Service) getFile(filename string, flags int) (*os.File, error) {
file, err := os.OpenFile(a.Path(filename), flags, getMode(filename))
return file, a.ConvertError(err)
}
func (a Service) getReadableFile(filename string) (model.ReadAtSeekCloser, error) {
return a.getFile(filename, model.ReadFlag)
}
func (a Service) getWritableFile(filename string) (io.WriteCloser, error) {
return a.getFile(filename, model.WriteFlag)
}
func getMode(name string) os.FileMode {
if strings.HasSuffix(name, "/") {
return model.DirectoryPerm
}
return model.RegularFilePerm
}
func convertToItem(pathname string, info fs.FileInfo) model.Item {
name := info.Name()
item := model.Item{
ID: model.ID(pathname),
NameValue: name,
Pathname: pathname,
IsDirValue: info.IsDir(),
Date: info.ModTime(),
FileMode: info.Mode(),
}
if !item.IsDir() {
item.Extension = strings.ToLower(path.Ext(name))
item.SizeValue = info.Size()
}
return item
}