/
local.go
executable file
·80 lines (62 loc) · 1.57 KB
/
local.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
75
76
77
78
79
80
package fileserver
import (
"context"
"io"
"io/fs"
"os"
"path/filepath"
"sync"
"github.com/gofrs/flock"
)
type LocalFileServer struct {
sync.Mutex
config Config
}
func NewLocalFileServer(cfg Config) (*LocalFileServer, error) {
return &LocalFileServer{
config: cfg,
}, nil
}
func (fs *LocalFileServer) Glob(ctx context.Context, pattern string) ([]string, error) {
files := []string{}
matchs, err := filepath.Glob(pattern)
if err != nil {
return nil, err
}
for _, match := range matchs {
if f, _ := os.Stat(match); !f.IsDir() {
absFilePath, err := filepath.Abs(match)
if err != nil {
return nil, err
}
files = append(files, absFilePath)
}
}
return files, nil
}
func (fs *LocalFileServer) Open(ctx context.Context, filePath string) (io.ReadSeekCloser, error) {
return os.Open(filePath)
}
func (fs *LocalFileServer) Remove(ctx context.Context, filePath string) error {
return os.Remove(filePath)
}
func (fs *LocalFileServer) Move(ctx context.Context, oldname, newname string) error {
dirName, _ := filepath.Split(newname)
if err := os.MkdirAll(dirName, os.ModePerm); err != nil {
return err
}
return os.Rename(oldname, newname)
}
func (fs *LocalFileServer) AcquireLock(ctx context.Context, filePath string) (Locker, error) {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return nil, err
}
locker := flock.New(filePath)
if _, err := locker.TryLock(); err != nil {
return nil, err
}
return locker, nil
}
func (fs *LocalFileServer) Stat(ctx context.Context, filepath string) (fs.FileInfo, error) {
return os.Stat(filepath)
}