/
fs.go
74 lines (56 loc) · 1.8 KB
/
fs.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
72
73
74
package service
import (
"context"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strconv"
types "github.com/gogo/protobuf/types"
"github.com/golang/protobuf/ptypes/empty"
"github.com/michaelmass/hellomicro/api"
"github.com/pkg/errors"
)
func (service *Service) ListFiles(ctx context.Context, request *api.ListFilesReq) (*api.ListFilesRes, error) {
fileInfos := &api.ListFilesRes{
FileInfos: []*api.FileInfo{},
}
files, err := filepath.Glob(request.Path + "/*")
if err != nil {
return nil, errors.Wrap(err, "unable to list files")
}
for _, file := range files {
fileInfo, err := os.Stat(file)
if err != nil {
return nil, errors.Wrapf(err, "unable get info of file %s", file)
}
modTime, err := types.TimestampProto(fileInfo.ModTime())
if err != nil {
return nil, errors.Wrapf(err, "unable to convert mod time of file %s", file)
}
fileInfos.FileInfos = append(fileInfos.FileInfos, &api.FileInfo{
Path: file,
Size_: fileInfo.Size(),
ModTime: modTime,
Mode: fileInfo.Mode().String(),
})
}
return fileInfos, nil
}
func (service *Service) WriteFile(ctx context.Context, request *api.WriteFileReq) (*empty.Empty, error) {
mode, err := strconv.ParseUint(request.Mode, 8, 32)
if err != nil {
return nil, errors.Wrapf(err, "unable to convert file mode from string to uint32 %s", request.Mode)
}
err = ioutil.WriteFile(request.Path, request.Content, fs.FileMode(mode))
if err != nil {
return nil, errors.Wrapf(err, "unable to write file %s", request.Path)
}
return e, nil
}
func (service *Service) ReadFile(ctx context.Context, request *api.ReadFileReq) (*api.ReadFileRes, error) {
return &api.ReadFileRes{}, nil
}
func (service *Service) DeleteFile(ctx context.Context, request *api.DeleteFileReq) (*api.DeleteFileRes, error) {
return &api.DeleteFileRes{}, nil
}